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,13 +1,8 @@
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
|
||||
import { ApiKeyType } from "@medusajs/utils"
|
||||
import { IApiKeyModuleService, IRegionModuleService } from "@medusajs/types"
|
||||
import { IRegionModuleService } 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,182 +11,171 @@ const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("API Keys - Admin", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let service: IApiKeyModuleService
|
||||
let regionService: IRegionModuleService
|
||||
medusaIntegrationTestRunner({
|
||||
env,
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
describe("API Keys - Admin", () => {
|
||||
let regionService: IRegionModuleService
|
||||
let container
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
service = appContainer.resolve(ModuleRegistrationName.API_KEY)
|
||||
regionService = appContainer.resolve(ModuleRegistrationName.REGION)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
// TODO: Once teardown doesn't skip constraint checks and cascades, we can remove this
|
||||
const existingRegions = await regionService.list({})
|
||||
await regionService.delete(existingRegions.map((r) => r.id))
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should correctly implement the entire lifecycle of an api key", async () => {
|
||||
const api = useApi() as any
|
||||
const created = await api.post(
|
||||
`/admin/api-keys`,
|
||||
{
|
||||
title: "Test Secret Key",
|
||||
type: ApiKeyType.SECRET,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(created.status).toEqual(200)
|
||||
expect(created.data.apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created.data.apiKey.id,
|
||||
title: "Test Secret Key",
|
||||
created_by: "admin_user",
|
||||
beforeAll(async () => {
|
||||
container = getContainer()
|
||||
regionService = container.resolve(
|
||||
ModuleRegistrationName.REGION
|
||||
) as IRegionModuleService
|
||||
})
|
||||
)
|
||||
// On create we get the token in raw form so we can store it.
|
||||
expect(created.data.apiKey.token).toContain("sk_")
|
||||
|
||||
const updated = await api.post(
|
||||
`/admin/api-keys/${created.data.apiKey.id}`,
|
||||
{
|
||||
title: "Updated Secret Key",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(updated.status).toEqual(200)
|
||||
expect(updated.data.apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created.data.apiKey.id,
|
||||
title: "Updated Secret Key",
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders, container)
|
||||
})
|
||||
)
|
||||
|
||||
const revoked = await api.post(
|
||||
`/admin/api-keys/${created.data.apiKey.id}/revoke`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(revoked.status).toEqual(200)
|
||||
expect(revoked.data.apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created.data.apiKey.id,
|
||||
revoked_by: "admin_user",
|
||||
afterEach(async () => {
|
||||
// TODO: Once teardown doesn't skip constraint checks and cascades, we can remove this
|
||||
const existingRegions = await regionService.list({})
|
||||
await regionService.delete(existingRegions.map((r) => r.id))
|
||||
})
|
||||
)
|
||||
expect(revoked.data.apiKey.revoked_at).toBeTruthy()
|
||||
|
||||
const deleted = await api.delete(
|
||||
`/admin/api-keys/${created.data.apiKey.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
const listedApiKeys = await api.get(`/admin/api-keys`, adminHeaders)
|
||||
|
||||
expect(deleted.status).toEqual(200)
|
||||
expect(listedApiKeys.data.apiKeys).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("can use a secret api key for authentication", async () => {
|
||||
const api = useApi() as any
|
||||
const created = await api.post(
|
||||
`/admin/api-keys`,
|
||||
{
|
||||
title: "Test Secret Key",
|
||||
type: ApiKeyType.SECRET,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const createdRegion = await api.post(
|
||||
`/admin/regions`,
|
||||
{
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
auth: {
|
||||
username: created.data.apiKey.token,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(createdRegion.status).toEqual(200)
|
||||
expect(createdRegion.data.region.name).toEqual("Test Region")
|
||||
})
|
||||
|
||||
it("falls back to other mode of authentication when an api key is not valid", async () => {
|
||||
const api = useApi() as any
|
||||
const created = await api.post(
|
||||
`/admin/api-keys`,
|
||||
{
|
||||
title: "Test Secret Key",
|
||||
type: ApiKeyType.SECRET,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/api-keys/${created.data.apiKey.id}/revoke`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const err = await api
|
||||
.post(
|
||||
`/admin/regions`,
|
||||
{
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
auth: {
|
||||
username: created.data.apiKey.token,
|
||||
it("should correctly implement the entire lifecycle of an api key", async () => {
|
||||
const created = await api.post(
|
||||
`/admin/api-keys`,
|
||||
{
|
||||
title: "Test Secret Key",
|
||||
type: ApiKeyType.SECRET,
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((e) => e.message)
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const createdRegion = await api.post(
|
||||
`/admin/regions`,
|
||||
{
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
auth: {
|
||||
username: created.data.apiKey.token,
|
||||
},
|
||||
...adminHeaders,
|
||||
}
|
||||
)
|
||||
expect(created.status).toEqual(200)
|
||||
expect(created.data.apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created.data.apiKey.id,
|
||||
title: "Test Secret Key",
|
||||
created_by: "admin_user",
|
||||
})
|
||||
)
|
||||
// On create we get the token in raw form so we can store it.
|
||||
expect(created.data.apiKey.token).toContain("sk_")
|
||||
|
||||
expect(err).toEqual("Request failed with status code 401")
|
||||
expect(createdRegion.status).toEqual(200)
|
||||
expect(createdRegion.data.region.name).toEqual("Test Region")
|
||||
})
|
||||
const updated = await api.post(
|
||||
`/admin/api-keys/${created.data.apiKey.id}`,
|
||||
{
|
||||
title: "Updated Secret Key",
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(updated.status).toEqual(200)
|
||||
expect(updated.data.apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created.data.apiKey.id,
|
||||
title: "Updated Secret Key",
|
||||
})
|
||||
)
|
||||
|
||||
const revoked = await api.post(
|
||||
`/admin/api-keys/${created.data.apiKey.id}/revoke`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(revoked.status).toEqual(200)
|
||||
expect(revoked.data.apiKey).toEqual(
|
||||
expect.objectContaining({
|
||||
id: created.data.apiKey.id,
|
||||
revoked_by: "admin_user",
|
||||
})
|
||||
)
|
||||
expect(revoked.data.apiKey.revoked_at).toBeTruthy()
|
||||
|
||||
const deleted = await api.delete(
|
||||
`/admin/api-keys/${created.data.apiKey.id}`,
|
||||
adminHeaders
|
||||
)
|
||||
const listedApiKeys = await api.get(`/admin/api-keys`, adminHeaders)
|
||||
|
||||
expect(deleted.status).toEqual(200)
|
||||
expect(listedApiKeys.data.apiKeys).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("can use a secret api key for authentication", async () => {
|
||||
const created = await api.post(
|
||||
`/admin/api-keys`,
|
||||
{
|
||||
title: "Test Secret Key",
|
||||
type: ApiKeyType.SECRET,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const createdRegion = await api.post(
|
||||
`/admin/regions`,
|
||||
{
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
auth: {
|
||||
username: created.data.apiKey.token,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(createdRegion.status).toEqual(200)
|
||||
expect(createdRegion.data.region.name).toEqual("Test Region")
|
||||
})
|
||||
|
||||
it("falls back to other mode of authentication when an api key is not valid", async () => {
|
||||
const created = await api.post(
|
||||
`/admin/api-keys`,
|
||||
{
|
||||
title: "Test Secret Key",
|
||||
type: ApiKeyType.SECRET,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/api-keys/${created.data.apiKey.id}/revoke`,
|
||||
{},
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const err = await api
|
||||
.post(
|
||||
`/admin/regions`,
|
||||
{
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
auth: {
|
||||
username: created.data.apiKey.token,
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((e) => e.message)
|
||||
|
||||
const createdRegion = await api.post(
|
||||
`/admin/regions`,
|
||||
{
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
countries: ["us", "ca"],
|
||||
},
|
||||
{
|
||||
auth: {
|
||||
username: created.data.apiKey.token,
|
||||
},
|
||||
...adminHeaders,
|
||||
}
|
||||
)
|
||||
|
||||
expect(err).toEqual("Request failed with status code 401")
|
||||
expect(createdRegion.status).toEqual(200)
|
||||
expect(createdRegion.data.region.name).toEqual("Test Region")
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user