-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-key): Add the endpoints and workflows for api key module
- Loading branch information
Showing
23 changed files
with
716 additions
and
38 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
integration-tests/plugins/__tests__/api-key/admin/api-key.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IApiKeyModuleService } from "@medusajs/types" | ||
import path from "path" | ||
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 { ApiKeyType } from "@medusajs/utils" | ||
|
||
jest.setTimeout(50000) | ||
|
||
const env = { MEDUSA_FF_MEDUSA_V2: true } | ||
const adminHeaders = { | ||
headers: { "x-medusa-access-token": "test_token" }, | ||
} | ||
|
||
describe("API Keys - Admin", () => { | ||
let dbConnection | ||
let appContainer | ||
let shutdownServer | ||
let service: IApiKeyModuleService | ||
|
||
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) | ||
}) | ||
|
||
afterAll(async () => { | ||
const db = useDb() | ||
await db.shutdown() | ||
await shutdownServer() | ||
}) | ||
|
||
beforeEach(async () => { | ||
await adminSeeder(dbConnection) | ||
}) | ||
|
||
afterEach(async () => { | ||
const db = useDb() | ||
await db.teardown() | ||
}) | ||
|
||
it("should create, update, revoke, and delete an api key", async () => { | ||
const api = useApi() as any | ||
const created = await api.post( | ||
`/admin/api-keys`, | ||
{ | ||
title: "Test Secret Key", | ||
type: ApiKeyType.SECRET, | ||
// TODO: This should be extracted from the request | ||
created_by: "test_user", | ||
}, | ||
adminHeaders | ||
) | ||
|
||
expect(created.status).toEqual(200) | ||
expect(created.data.apiKey).toEqual( | ||
expect.objectContaining({ | ||
id: created.data.apiKey.id, | ||
name: "Test Secret Key", | ||
}) | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./steps" | ||
export * from "./workflows" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { CreateApiKeyDTO, IApiKeyModuleService } from "@medusajs/types" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
|
||
type CreateApiKeysStepInput = { | ||
apiKeysData: CreateApiKeyDTO[] | ||
} | ||
|
||
export const createApiKeysStepId = "create-api-keys" | ||
export const createApiKeysStep = createStep( | ||
createApiKeysStepId, | ||
async (data: CreateApiKeysStepInput, { container }) => { | ||
const service = container.resolve<IApiKeyModuleService>( | ||
ModuleRegistrationName.API_KEY | ||
) | ||
const created = await service.create(data.apiKeysData) | ||
return new StepResponse( | ||
created, | ||
created.map((apiKey) => apiKey.id) | ||
) | ||
}, | ||
async (createdIds, { container }) => { | ||
if (!createdIds?.length) { | ||
return | ||
} | ||
|
||
const service = container.resolve<IApiKeyModuleService>( | ||
ModuleRegistrationName.API_KEY | ||
) | ||
|
||
await service.delete(createdIds) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ModuleRegistrationName } from "@medusajs/modules-sdk" | ||
import { IApiKeyModuleService } from "@medusajs/types" | ||
import { StepResponse, createStep } from "@medusajs/workflows-sdk" | ||
|
||
export const deleteApiKeysStepId = "delete-api-keys" | ||
export const deleteApiKeysStep = createStep( | ||
{ name: deleteApiKeysStepId, noCompensation: true }, | ||
async (ids: string[], { container }) => { | ||
const service = container.resolve<IApiKeyModuleService>( | ||
ModuleRegistrationName.API_KEY | ||
) | ||
|
||
await service.delete(ids) | ||
return new StepResponse(void 0) | ||
}, | ||
async () => {} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./create-api-keys" | ||
export * from "./delete-api-keys" | ||
export * from "./update-api-keys" | ||
export * from "./revoke-api-keys" |
Oops, something went wrong.