-
Notifications
You must be signed in to change notification settings - Fork 6
CEXT-6079: add unit tests for lib-app runtime actions #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import type { RuntimeActionParams } from "@adobe/aio-commerce-lib-core/params"; | ||
|
|
||
| type CreateRuntimeActionParamsArgs = RuntimeActionParams & { | ||
| method?: RuntimeActionParams["__ow_method"]; | ||
| path?: string; | ||
| body?: unknown; | ||
| query?: string; | ||
| headers?: Record<string, string | undefined>; | ||
| }; | ||
|
|
||
| /** Builds OpenWhisk-style runtime action params for handler tests. */ | ||
| export function createRuntimeActionParams( | ||
| args: CreateRuntimeActionParamsArgs = {}, | ||
| ): RuntimeActionParams { | ||
| const { method = "get", path = "/", body, query, headers, ...params } = args; | ||
|
|
||
| return { | ||
| ...params, | ||
| __ow_method: method, | ||
| __ow_path: path, | ||
| ...(body === undefined ? {} : { __ow_body: JSON.stringify(body) }), | ||
| ...(query ? { __ow_query: query } : {}), | ||
| ...(headers ? { __ow_headers: headers } : {}), | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { appConfigRuntimeAction } from "#actions/app-config"; | ||
| import { createRuntimeActionParams } from "#test/fixtures/actions"; | ||
| import { minimalValidConfig } from "#test/fixtures/config"; | ||
|
|
||
| import type { CommerceAppConfigOutputModel } from "#config/schema/app"; | ||
|
|
||
| describe("appConfigRuntimeAction", () => { | ||
| test("returns the validated app config for GET /", async () => { | ||
| const handler = appConfigRuntimeAction({ | ||
| appConfig: minimalValidConfig, | ||
| }); | ||
|
|
||
| const result = await handler(createRuntimeActionParams()); | ||
|
|
||
| expect(result).toEqual({ | ||
| type: "success", | ||
| statusCode: 200, | ||
| body: minimalValidConfig, | ||
| }); | ||
| }); | ||
|
|
||
| test("returns a 500 error when the app config is invalid", async () => { | ||
| const handler = appConfigRuntimeAction({ | ||
| appConfig: {} as unknown as CommerceAppConfigOutputModel, | ||
| }); | ||
|
|
||
| const result = await handler(createRuntimeActionParams()); | ||
|
|
||
| expect(result).toMatchObject({ | ||
| type: "error", | ||
| error: { | ||
| statusCode: 500, | ||
| body: { | ||
| message: "Internal server error", | ||
| error: "Invalid commerce app config", | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import { beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| const { | ||
| byScopeIdMock, | ||
| getConfigurationMock, | ||
| initializeMock, | ||
| setConfigurationMock, | ||
| } = vi.hoisted(() => ({ | ||
| byScopeIdMock: vi.fn((scopeId: string) => ({ scopeId })), | ||
| getConfigurationMock: vi.fn(), | ||
| initializeMock: vi.fn(), | ||
| setConfigurationMock: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@adobe/aio-commerce-lib-config", async () => { | ||
| const actual = await vi.importActual< | ||
| typeof import("@adobe/aio-commerce-lib-config") | ||
| >("@adobe/aio-commerce-lib-config"); | ||
|
|
||
| return { | ||
| ...actual, | ||
| byScopeId: byScopeIdMock, | ||
| getConfiguration: getConfigurationMock, | ||
| initialize: initializeMock, | ||
| setConfiguration: setConfigurationMock, | ||
| }; | ||
| }); | ||
|
|
||
| import { configRuntimeAction } from "#actions/config"; | ||
| import { createRuntimeActionParams } from "#test/fixtures/actions"; | ||
|
|
||
| import type { BusinessConfigSchema } from "@adobe/aio-commerce-lib-config"; | ||
|
|
||
| const configSchema = [ | ||
| { | ||
| name: "apiKey", | ||
| label: "API Key", | ||
| description: "Commerce API key", | ||
| type: "password", | ||
| default: "", | ||
| }, | ||
| { | ||
| name: "mode", | ||
| label: "Mode", | ||
| description: "App mode", | ||
| type: "text", | ||
| default: "sandbox", | ||
| }, | ||
| ] satisfies BusinessConfigSchema; | ||
|
|
||
| describe("configRuntimeAction", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| byScopeIdMock.mockImplementation((scopeId: string) => ({ scopeId })); | ||
| }); | ||
|
|
||
| test("masks password values when retrieving configuration", async () => { | ||
| getConfigurationMock.mockResolvedValue({ | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "super-secret", origin: "scope" }, | ||
| { name: "mode", value: "live", origin: "scope" }, | ||
| ], | ||
| }); | ||
|
|
||
| const handler = configRuntimeAction({ configSchema }); | ||
|
|
||
| const result = await handler( | ||
| createRuntimeActionParams({ | ||
| query: "scopeId=store-1", | ||
| AIO_COMMERCE_CONFIG_ENCRYPTION_KEY: "encryption-key", | ||
| }), | ||
| ); | ||
|
|
||
| expect(initializeMock).toHaveBeenCalledWith({ schema: configSchema }); | ||
| expect(byScopeIdMock).toHaveBeenCalledWith("store-1"); | ||
| expect(getConfigurationMock).toHaveBeenCalledWith( | ||
| { scopeId: "store-1" }, | ||
| { encryptionKey: "encryption-key" }, | ||
| ); | ||
| expect(result).toEqual({ | ||
| type: "success", | ||
| statusCode: 200, | ||
| body: { | ||
| schema: configSchema, | ||
| values: { | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "*****", origin: "scope" }, | ||
| { name: "mode", value: "live", origin: "scope" }, | ||
| ], | ||
| }, | ||
| }, | ||
| }); | ||
|
Comment on lines
+93
to
+106
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably only assert the |
||
| }); | ||
|
|
||
| test("returns a 400 error when the scope id query parameter is missing", async () => { | ||
| const handler = configRuntimeAction({ configSchema }); | ||
|
|
||
| const result = await handler(createRuntimeActionParams()); | ||
|
|
||
| expect(result).toMatchObject({ | ||
| type: "error", | ||
| error: { | ||
| statusCode: 400, | ||
| body: { | ||
| message: "Invalid query parameters", | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("filters masked password values before saving with PUT /", async () => { | ||
| setConfigurationMock.mockResolvedValue({ | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "updated-secret", origin: "scope" }, | ||
| { name: "mode", value: "live", origin: "scope" }, | ||
| ], | ||
| }); | ||
|
|
||
| const handler = configRuntimeAction({ configSchema }); | ||
|
|
||
| const result = await handler( | ||
| createRuntimeActionParams({ | ||
| method: "put", | ||
| body: { | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "*****" }, | ||
| { name: "mode", value: "live" }, | ||
| ], | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| expect(initializeMock).toHaveBeenCalledWith({ schema: configSchema }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idem on this initialize check. Don't think this test needs to care whether we call initialize or not. |
||
| expect(setConfigurationMock).toHaveBeenCalledWith( | ||
| { | ||
| config: [{ name: "mode", value: "live" }], | ||
| }, | ||
| { scopeId: "store-1" }, | ||
| { encryptionKey: undefined }, | ||
|
Comment on lines
+154
to
+155
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's assert only the parameters we want to actually check (in this case the first one). For others we can just |
||
| ); | ||
| expect(result).toEqual({ | ||
| type: "success", | ||
| statusCode: 200, | ||
| headers: { | ||
| "Cache-Control": "no-store", | ||
| Deprecation: "Wed, 15 Apr 2026 00:00:00 GMT", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a specific test only for this header, to ensure future updates don't delete it. |
||
| }, | ||
| body: { | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "*****", origin: "scope" }, | ||
| { name: "mode", value: "live", origin: "scope" }, | ||
| ], | ||
| }, | ||
| }); | ||
|
Comment on lines
+157
to
+171
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, I would not assert the result in this test. I would only do it in a test that is intended to do that. |
||
| }); | ||
|
|
||
| test("forwards partial updates and null unsets with PATCH /", async () => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test it's a bit confusing to me. I would refactor it to two separate tests that assert the following:
|
||
| setConfigurationMock.mockResolvedValue({ | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "persisted-secret", origin: "scope" }, | ||
| { name: "mode", value: "live", origin: "scope" }, | ||
| ], | ||
| }); | ||
|
|
||
| const handler = configRuntimeAction({ configSchema }); | ||
|
|
||
| const result = await handler( | ||
| createRuntimeActionParams({ | ||
| method: "patch", | ||
| body: { | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: null }, | ||
| { name: "mode", value: "live" }, | ||
| ], | ||
| }, | ||
| AIO_COMMERCE_CONFIG_ENCRYPTION_KEY: "encryption-key", | ||
| }), | ||
| ); | ||
|
|
||
| expect(setConfigurationMock).toHaveBeenCalledWith( | ||
| { | ||
| config: [ | ||
| { name: "apiKey", value: null }, | ||
| { name: "mode", value: "live" }, | ||
| ], | ||
| }, | ||
| { scopeId: "store-1" }, | ||
| { encryptionKey: "encryption-key" }, | ||
| ); | ||
| expect(result).toEqual({ | ||
| type: "success", | ||
| statusCode: 200, | ||
| headers: { | ||
| "Cache-Control": "no-store", | ||
| }, | ||
| body: { | ||
| scopeId: "store-1", | ||
| config: [ | ||
| { name: "apiKey", value: "*****", origin: "scope" }, | ||
| { name: "mode", value: "live", origin: "scope" }, | ||
| ], | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines seem to assert implementation details that this test should not care about. I'd remove them