chore: Use module test runner in all modules (#6706)
I replaced the custom test setup with the test runner. This should make migrating to mikroorm 6 a lot easier once we do it. There are few more modules to be done, but I thought the PR is super big already so we can tackle them separately. Note that there are no or very little test changes, it is mostly the setup around the tests.
This commit is contained in:
@@ -1,257 +1,237 @@
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthModuleService - AuthUser", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
await createAuthUsers(testManager)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
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"],
|
||||
moduleIntegrationTestRunner({
|
||||
moduleName: Modules.AUTH,
|
||||
testSuite: ({
|
||||
MikroOrmWrapper,
|
||||
service,
|
||||
}: SuiteOptions<IAuthModuleService>) => {
|
||||
describe("AuthModuleService - AuthUser", () => {
|
||||
beforeEach(async () => {
|
||||
await createAuthUsers(MikroOrmWrapper.forkManager())
|
||||
})
|
||||
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
describe("listAuthUsers", () => {
|
||||
it("should list authUsers", async () => {
|
||||
const authUsers = await service.list()
|
||||
|
||||
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,
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
provider: "store",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "manual",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error when an authUser with the given id does not exist", async () => {
|
||||
let error
|
||||
it("should list authUsers by id", async () => {
|
||||
const authUsers = await service.list({
|
||||
id: ["test-id"],
|
||||
})
|
||||
|
||||
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(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
expect(authUser["password_hash"]).toEqual(undefined)
|
||||
})
|
||||
|
||||
it("should throw an error when a authUserId is not provided", async () => {
|
||||
let error
|
||||
it("should list authUsers by provider", async () => {
|
||||
const authUsers = await service.list({
|
||||
provider: "manual",
|
||||
})
|
||||
|
||||
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" },
|
||||
expect(authUsers).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "test-id",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "test-id-1",
|
||||
}),
|
||||
])
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
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",
|
||||
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",
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -1,68 +1,41 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
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
|
||||
)
|
||||
|
||||
beforeAll(async () => {
|
||||
const initModulesConfig = getInitModuleConfig()
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
"AuthenticationProvider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
|
||||
)
|
||||
})
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
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)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await MikroOrmWrapper.setupDatabase()
|
||||
testManager = MikroOrmWrapper.forkManager()
|
||||
|
||||
if (service.__hooks?.onApplicationStart) {
|
||||
await service.__hooks.onApplicationStart()
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await MikroOrmWrapper.clearDatabase()
|
||||
MedusaModule.clearInstances()
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await shutdownFunc()
|
||||
})
|
||||
|
||||
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?"
|
||||
)
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
`Scope "non-existing" is not valid for provider emailpass`
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
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`
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user