Chore/integration tests modules utils (#6581)
new wrapper for medusa integration tests. for now it is only applied to the modules directory, but it could be used in the api integration tests or any other integrations that requires a db and a server up and running. It is not perfect, but I wanted to have something working and centralised before improving it, also avoiding too many conflicts with other prs
This commit is contained in:
committed by
GitHub
parent
a6736c7ee0
commit
51bb6f1e89
@@ -1,135 +1,117 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IAuthModuleService, ICustomerModuleService } from "@medusajs/types"
|
||||
import path from "path"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
|
||||
describe("POST /auth/emailpass", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let customerModuleService: ICustomerModuleService
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
describe("POST /auth/emailpass", () => {
|
||||
let appContainer
|
||||
let customerModuleService: ICustomerModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
customerModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
|
||||
it("should return a token on successful login", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
await authService.create({
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "admin",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: email,
|
||||
password: password,
|
||||
beforeAll(async () => {
|
||||
appContainer = getContainer()
|
||||
customerModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data).toEqual(
|
||||
expect.objectContaining({
|
||||
token: expect.any(String),
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error upon incorrect password", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
|
||||
await authService.create({
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "admin",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
})
|
||||
it("should return a token on successful login", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const api = useApi() as any
|
||||
const error = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: email,
|
||||
password: "incorrect-password",
|
||||
await authService.create({
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "admin",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: email,
|
||||
password: password,
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data).toEqual(
|
||||
expect.objectContaining({
|
||||
token: expect.any(String),
|
||||
})
|
||||
)
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(401)
|
||||
expect(error.response.data).toEqual({
|
||||
type: "unauthorized",
|
||||
message: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
it("should throw an error upon incorrect password", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
it.skip("should throw an error upon logging in with a non existing auth user", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
await authService.create({
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "admin",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const error = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: "should-not-exist",
|
||||
password: "should-not-exist",
|
||||
const error = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: email,
|
||||
password: "incorrect-password",
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(401)
|
||||
expect(error.response.data).toEqual({
|
||||
type: "unauthorized",
|
||||
message: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
// TODO: This is creating a user with a scope of admin. The client consuming the auth service
|
||||
// should reject this if its not being created by an admin user
|
||||
expect(error.response.status).toEqual(401)
|
||||
expect(error.response.data).toEqual({
|
||||
type: "unauthorized",
|
||||
message: "Invalid email or password",
|
||||
it.skip("should throw an error upon logging in with a non existing auth user", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
const error = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: "should-not-exist",
|
||||
password: "should-not-exist",
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
// TODO: This is creating a user with a scope of admin. The client consuming the auth service
|
||||
// should reject this if its not being created by an admin user
|
||||
expect(error.response.status).toEqual(401)
|
||||
expect(error.response.data).toEqual({
|
||||
type: "unauthorized",
|
||||
message: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user