chore(): Reorganize modules (#7210)
**What** Move all modules to the modules directory
This commit is contained in:
committed by
GitHub
parent
7a351eef09
commit
4eae25e1ef
@@ -0,0 +1,37 @@
|
||||
import { AuthUser } from "@models"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export async function createAuthUsers(
|
||||
manager: SqlEntityManager,
|
||||
userData: any[] = [
|
||||
{
|
||||
id: "test-id",
|
||||
entity_id: "test-id",
|
||||
provider: "manual",
|
||||
scope: "store",
|
||||
},
|
||||
{
|
||||
id: "test-id-1",
|
||||
entity_id: "test-id-1",
|
||||
provider: "manual",
|
||||
scope: "store",
|
||||
},
|
||||
{
|
||||
entity_id: "test-id-2",
|
||||
provider: "store",
|
||||
scope: "store",
|
||||
},
|
||||
]
|
||||
): Promise<AuthUser[]> {
|
||||
const authUsers: AuthUser[] = []
|
||||
|
||||
for (const user of userData) {
|
||||
const authUser = manager.create(AuthUser, user)
|
||||
|
||||
authUsers.push(authUser)
|
||||
}
|
||||
|
||||
await manager.persistAndFlush(authUsers)
|
||||
|
||||
return authUsers
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthUser Service", () => {
|
||||
beforeEach(async () => {
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
describe("list", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by provider_id", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCount", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(serialized).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieve", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
const serialized = JSON.parse(JSON.stringify(authUser))
|
||||
|
||||
expect(serialized).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("authUser - id must be defined")
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
const id = "test-id"
|
||||
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider: "manual",
|
||||
entity_id: "test",
|
||||
scope: "store",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,237 @@
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthUser", () => {
|
||||
beforeEach(async () => {
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
describe("listAuthUsers", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should list authUsers by provider", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("listAndCountAuthUsers", () => {
|
||||
it("should list and count authUsers", async () => {
|
||||
const [authUsers, count] = await service.listAndCount()
|
||||
|
||||
expect(count).toEqual(3)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("should listAndCount authUsers by provider_id", async () => {
|
||||
const [authUsers, count] = await service.listAndCount({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
expect(count).toEqual(2)
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("retrieveAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should return an authUser for the given id", async () => {
|
||||
const authUser = await service.retrieve(id)
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id,
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve("does-not-exist")
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
"AuthUser with id: does-not-exist was not found"
|
||||
)
|
||||
})
|
||||
|
||||
it("should not return an authUser with password hash", async () => {
|
||||
const authUser = await service.retrieve("test-id-1")
|
||||
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
})
|
||||
)
|
||||
expect(authUser["password_hash"]).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.retrieve(undefined as unknown as string)
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual("authUser - id must be defined")
|
||||
})
|
||||
|
||||
it("should return authUser based on config select param", async () => {
|
||||
const authUser = await service.retrieve(id, {
|
||||
select: ["id"],
|
||||
})
|
||||
|
||||
expect(authUser).toEqual({
|
||||
id,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("deleteAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should delete the authUsers given an id successfully", async () => {
|
||||
await service.delete([id])
|
||||
|
||||
const authUsers = await service.list({
|
||||
id: [id],
|
||||
})
|
||||
|
||||
expect(authUsers).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("updateAuthUser", () => {
|
||||
const id = "test-id"
|
||||
|
||||
it("should throw an error when a id does not exist", async () => {
|
||||
let error
|
||||
|
||||
try {
|
||||
await service.update([
|
||||
{
|
||||
id: "does-not-exist",
|
||||
},
|
||||
])
|
||||
} catch (e) {
|
||||
error = e
|
||||
}
|
||||
|
||||
expect(error.message).toEqual(
|
||||
'AuthUser with id "does-not-exist" not found'
|
||||
)
|
||||
})
|
||||
|
||||
it("should update authUser", async () => {
|
||||
await service.update([
|
||||
{
|
||||
id,
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser] = await service.list({ id: [id] })
|
||||
expect(authUser).toEqual(
|
||||
expect.objectContaining({
|
||||
provider_metadata: { email: "test@email.com" },
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("createAuthUser", () => {
|
||||
it("should create a authUser successfully", async () => {
|
||||
await service.create([
|
||||
{
|
||||
id: "test",
|
||||
provider: "manual",
|
||||
entity_id: "test",
|
||||
scope: "store",
|
||||
},
|
||||
])
|
||||
|
||||
const [authUser, count] = await service.listAndCount({
|
||||
id: ["test"],
|
||||
})
|
||||
|
||||
expect(count).toEqual(1)
|
||||
expect(authUser[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
id: "test",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,41 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
describe("authenticate", () => {
|
||||
it("authenticate validates that a provider is registered in container", async () => {
|
||||
const { success, error } = await service.authenticate(
|
||||
"notRegistered",
|
||||
{} as any
|
||||
)
|
||||
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
"AuthenticationProvider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
|
||||
)
|
||||
})
|
||||
|
||||
it("fails to authenticate using a valid provider with an invalid scope", async () => {
|
||||
const { success, error } = await service.authenticate("emailpass", {
|
||||
authScope: "non-existing",
|
||||
} as any)
|
||||
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
`Scope "non-existing" is not valid for provider emailpass`
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,133 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
const seedDefaultData = async (manager) => {
|
||||
await createAuthUsers(manager)
|
||||
}
|
||||
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
moduleOptions: {
|
||||
providers: [
|
||||
{
|
||||
name: "emailpass",
|
||||
scopes: {
|
||||
admin: {},
|
||||
store: {},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
describe("authenticate", () => {
|
||||
it("authenticate validates that a provider is registered in container", async () => {
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager(), [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "store",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: password,
|
||||
},
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: true,
|
||||
authUser: expect.objectContaining({
|
||||
entity_id: email,
|
||||
provider_metadata: {},
|
||||
}),
|
||||
})
|
||||
})
|
||||
|
||||
it("fails when no password is given", async () => {
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { email: "test@test.com" },
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Password should be a string",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails when no email is given", async () => {
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { password: "supersecret" },
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Email should be a string",
|
||||
})
|
||||
})
|
||||
|
||||
it("fails with an invalid password", async () => {
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
await seedDefaultData(MikroOrmWrapper.forkManager())
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager(), [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "emailpass",
|
||||
scope: "store",
|
||||
entity_id: email,
|
||||
provider_metadata: {
|
||||
password_hash: passwordHash,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: "password",
|
||||
},
|
||||
authScope: "store",
|
||||
} as any)
|
||||
|
||||
expect(res).toEqual({
|
||||
success: false,
|
||||
error: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user