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:
Adrien de Peretti authored and GitHub committed 2024-03-06 10:03:07 +00:00
1 parent a6736c7ee0
commit 51bb6f1e89
99 files changed
+10164 -10303

No files matched your search

@@ -1,13 +1,7 @@
import { IAuthModuleService, IUserModuleService } from "@medusajs/types"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -16,97 +10,82 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/invites/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let userModuleService: IUserModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("GET /admin/invites/:id", () => {
let appContainer
let userModuleService: IUserModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should fail to accept an invite with an invalid invite token", async () => {
const api = useApi()! as AxiosInstance
const authResponse = await api.post(`/auth/admin/emailpass`, {
email: "potential_member@test.com",
password: "supersecret",
})
expect(authResponse.status).toEqual(200)
const token = authResponse.data.token
const acceptResponse = await api
.post(
`/admin/invites/accept?token=${"non-existing-token"}`,
{
first_name: "John",
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.catch((e) => e)
expect(acceptResponse.response.status).toEqual(401)
expect(acceptResponse.response.data.message).toEqual("Unauthorized")
})
it("should accept an invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
})
const api = useApi()! as AxiosInstance
const authResponse = await api.post(`/auth/admin/emailpass`, {
email: "potential_member@test.com",
password: "supersecret",
})
expect(authResponse.status).toEqual(200)
const token = authResponse.data.token
const acceptResponse = await api.post(
`/admin/invites/accept?token=${invite.token}`,
{
first_name: "John",
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
expect(acceptResponse.status).toEqual(200)
expect(acceptResponse.data.user).toEqual(
expect.objectContaining({
email: "potential_member@test.com",
first_name: "John",
beforeAll(async () => {
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
it("should fail to accept an invite with an invalid invite token", async () => {
const authResponse = await api.post(`/auth/admin/emailpass`, {
email: "potential_member@test.com",
password: "supersecret",
})
expect(authResponse.status).toEqual(200)
const token = authResponse.data.token
const acceptResponse = await api
.post(
`/admin/invites/accept?token=${"non-existing-token"}`,
{
first_name: "John",
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.catch((e) => e)
expect(acceptResponse.response.status).toEqual(401)
expect(acceptResponse.response.data.message).toEqual("Unauthorized")
})
it("should accept an invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
})
const authResponse = await api.post(`/auth/admin/emailpass`, {
email: "potential_member@test.com",
password: "supersecret",
})
expect(authResponse.status).toEqual(200)
const token = authResponse.data.token
const acceptResponse = await api.post(
`/admin/invites/accept?token=${invite.token}`,
{
first_name: "John",
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
expect(acceptResponse.status).toEqual(200)
expect(acceptResponse.data.user).toEqual(
expect.objectContaining({
email: "potential_member@test.com",
first_name: "John",
})
)
})
})
},
})
@@ -1,11 +1,5 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
import { createAdminUser } from "../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -14,43 +8,26 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("POST /admin/invites", () => {
let dbConnection
let shutdownServer
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("POST /admin/invites", () => {
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, getContainer())
})
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
})
it("create an invite", async () => {
const body = {
email: "test_member@test.com",
}
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
const response = await api.post(`/admin/invites`, body, adminHeaders)
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("create an invite", async () => {
const api = useApi()! as AxiosInstance
const body = {
email: "test_member@test.com",
}
const response = await api.post(`/admin/invites`, body, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
invite: expect.objectContaining(body),
expect(response.status).toEqual(200)
expect(response.data).toEqual({
invite: expect.objectContaining(body),
})
})
})
})
},
})
@@ -1,14 +1,7 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
import { createAdminUser } from "../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -17,61 +10,48 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("DELETE /admin/invites/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let userModuleService: IUserModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("DELETE /admin/invites/:id", () => {
let appContainer
let userModuleService: IUserModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeAll(async () => {
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should delete a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
const response = await api.delete(
`/admin/invites/${invite.id}`,
adminHeaders
)
it("should delete a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
expect(response.status).toEqual(200)
expect(response.data).toEqual({
id: invite.id,
object: "invite",
deleted: true,
})
const { response: deletedResponse } = await api
.get(`/admin/invites/${invite.id}`, adminHeaders)
.catch((e) => e)
expect(deletedResponse.status).toEqual(404)
expect(deletedResponse.data.type).toEqual("not_found")
})
})
const api = useApi()! as AxiosInstance
const response = await api.delete(
`/admin/invites/${invite.id}`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
id: invite.id,
object: "invite",
deleted: true,
})
const { response: deletedResponse } = await api
.get(`/admin/invites/${invite.id}`, adminHeaders)
.catch((e) => e)
expect(deletedResponse.status).toEqual(404)
expect(deletedResponse.data.type).toEqual("not_found")
})
},
})
@@ -1,14 +1,7 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
import { createAdminUser } from "../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -17,54 +10,41 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/invites", () => {
let dbConnection
let appContainer
let shutdownServer
let userModuleService: IUserModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("GET /admin/invites", () => {
let appContainer
let userModuleService: IUserModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeAll(async () => {
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should list invites", async () => {
await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
const response = await api.get(`/admin/invites`, adminHeaders)
it("should list invites", async () => {
await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
expect(response.status).toEqual(200)
expect(response.data).toEqual({
invites: [
expect.objectContaining({ email: "potential_member@test.com" }),
],
count: 1,
offset: 0,
limit: 50,
})
})
})
const api = useApi()! as AxiosInstance
const response = await api.get(`/admin/invites`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
invites: [
expect.objectContaining({ email: "potential_member@test.com" }),
],
count: 1,
offset: 0,
limit: 50,
})
})
},
})
@@ -1,13 +1,7 @@
import { IAuthModuleService, IUserModuleService } from "@medusajs/types"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -16,52 +10,39 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("POST /admin/invites/:id/resend", () => {
let dbConnection
let appContainer
let shutdownServer
let userModuleService: IUserModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("POST /admin/invites/:id/resend", () => {
let appContainer
let userModuleService: IUserModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeAll(async () => {
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should resend a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
const response = await api.post(
`/admin/invites/${invite.id}/resend`,
{},
adminHeaders
)
it("should resend a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
expect(response.status).toEqual(200)
expect(response.data.invite.token).not.toEqual(invite.token)
expect(response.data.invite).toEqual(
expect.objectContaining({ email: "potential_member@test.com" })
)
})
})
const api = useApi()! as AxiosInstance
const response = await api.post(
`/admin/invites/${invite.id}/resend`,
{},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.invite.token).not.toEqual(invite.token)
expect(response.data.invite).toEqual(
expect.objectContaining({ email: "potential_member@test.com" })
)
})
},
})
@@ -1,13 +1,7 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { IAuthModuleService, IUserModuleService } from "@medusajs/types"
import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import { AxiosInstance } from "axios"
import { createAdminUser } from "../../helpers/create-admin-user"
import { medusaIntegrationTestRunner } from "medusa-test-utils"
jest.setTimeout(50000)
@@ -16,47 +10,37 @@ const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/invites/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let userModuleService: IUserModuleService
medusaIntegrationTestRunner({
env,
testSuite: ({ dbConnection, getContainer, api }) => {
describe("GET /admin/invites/:id", () => {
let appContainer
let userModuleService: IUserModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeAll(async () => {
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
it("should retrieve a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
const response = await api.get(
`/admin/invites/${invite.id}`,
adminHeaders
)
it("should retrieve a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
expect(response.status).toEqual(200)
expect(response.data.invite).toEqual(
expect.objectContaining({ email: "potential_member@test.com" })
)
})
})
const api = useApi()! as AxiosInstance
const response = await api.get(`/admin/invites/${invite.id}`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.invite).toEqual(
expect.objectContaining({ email: "potential_member@test.com" })
)
})
},
})