Skip to content

Commit 520a003

Browse files
committed
move sqs unit tests
1 parent b7062b3 commit 520a003

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"lint": "yarn workspaces run lint",
1818
"prepare": "node .husky/install.mjs || true",
1919
"typecheck": "yarn workspaces run typecheck",
20-
"test:unit": "vitest run tests/unit --config tests/unit/vitest.config.ts && yarn workspace infra-core-ui run test:unit",
20+
"test:unit": "cross-env RunEnvironment='dev' vitest run tests/unit --config tests/unit/vitest.config.ts && yarn workspace infra-core-ui run test:unit",
2121
"test:unit-ui": "yarn test:unit --ui",
2222
"test:unit-watch": "vitest tests/unit",
2323
"test:live": "vitest tests/live",

src/api/routes/mobileWallet.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import {
66
} from "../../common/errors/index.js";
77
import { z } from "zod";
88
import { checkPaidMembership } from "../functions/membership.js";
9-
import { AvailableSQSFunctions, SQSPayload } from "common/types/sqsMessage.js";
9+
import {
10+
AvailableSQSFunctions,
11+
SQSPayload,
12+
} from "../../common/types/sqsMessage.js";
1013
import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
11-
import { genericConfig } from "common/config.js";
14+
import { genericConfig } from "../../common/config.js";
1215
import { zodToJsonSchema } from "zod-to-json-schema";
1316

1417
const queuedResponseJsonSchema = zodToJsonSchema(

src/api/sqs/handlers.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import {
44
runEnvironment,
55
SQSHandlerFunction,
66
} from "./index.js";
7-
import { getEntraIdToken, getUserProfile } from "api/functions/entraId.js";
7+
import {
8+
getEntraIdToken,
9+
getUserProfile,
10+
} from "../../api/functions/entraId.js";
811
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
9-
import { environmentConfig, genericConfig } from "common/config.js";
12+
import { environmentConfig, genericConfig } from "../../common/config.js";
1013
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
11-
import { issueAppleWalletMembershipCard } from "api/functions/mobileWallet.js";
12-
import { generateMembershipEmailCommand } from "api/functions/ses.js";
14+
import { issueAppleWalletMembershipCard } from "../../api/functions/mobileWallet.js";
15+
import { generateMembershipEmailCommand } from "../../api/functions/ses.js";
1316
import { SESClient } from "@aws-sdk/client-ses";
1417

1518
export const emailMembershipPassHandler: SQSHandlerFunction<

src/api/sqs/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import { logger } from "./logger.js";
1313
import { z, ZodError } from "zod";
1414
import pino from "pino";
1515
import { emailMembershipPassHandler, pingHandler } from "./handlers.js";
16-
import { ValidationError } from "common/errors/index.js";
16+
import { ValidationError } from "../../common/errors/index.js";
1717
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
1818
import { SESClient } from "@aws-sdk/client-ses";
1919
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
20-
import { RunEnvironment } from "common/roles.js";
21-
import { environmentConfig } from "common/config.js";
20+
import { RunEnvironment } from "../../common/roles.js";
21+
import { environmentConfig } from "../../common/config.js";
2222

2323
export type SQSFunctionPayloadTypes = {
2424
[K in keyof typeof sqsPayloadSchemas]: SQSHandlerFunction<K>;

tests/unit/mobileWallet.test.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import { afterAll, expect, test, beforeEach, vi } from "vitest";
1+
import { afterAll, expect, test, beforeEach, vi, describe } from "vitest";
22
import init from "../../src/api/index.js";
3-
import { describe } from "node:test";
43
import { EntraFetchError } from "../../src/common/errors/index.js";
5-
import { mockClient } from "aws-sdk-client-mock";
6-
import { issueAppleWalletMembershipCard } from "../../src/api/functions/mobileWallet.js";
7-
import { SendRawEmailCommand, SESClient } from "@aws-sdk/client-ses";
8-
9-
const sesMock = mockClient(SESClient);
104

115
vi.mock("../../src/api/functions/membership.js", () => {
126
return {
@@ -70,13 +64,11 @@ describe("Mobile wallet pass issuance", async () => {
7064
await response.json();
7165
});
7266
test("Test that passes will be issued for members", async () => {
73-
sesMock.on(SendRawEmailCommand).resolvesOnce({}).rejects();
7467
const response = await app.inject({
7568
method: "POST",
7669
url: "/api/v1/mobileWallet/[email protected]",
7770
});
7871
expect(response.statusCode).toBe(202);
79-
expect(issueAppleWalletMembershipCard).toHaveBeenCalledOnce();
8072
});
8173
afterAll(async () => {
8274
await app.close();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect, test, beforeEach, vi } from "vitest";
2+
vi.mock("../../../src/api/functions/mobileWallet.js", () => ({
3+
issueAppleWalletMembershipCard: vi.fn().mockResolvedValue(new ArrayBuffer()),
4+
}))
5+
6+
vi.mock("../../../src/api/functions/entraId.js", () => ({
7+
getEntraIdToken: vi.fn().mockResolvedValue("atokenofalltime"),
8+
getUserProfile: vi.fn().mockImplementation(async (_token: string, email: string) => {
9+
console.log('getUserProfile called with:', email);
10+
if (email === "[email protected]") {
11+
return { displayName: "John Doe" };
12+
}
13+
throw new EntraFetchError({
14+
message: "User not found",
15+
email,
16+
});
17+
}),
18+
resolveEmailToOid: vi.fn().mockResolvedValue("12345"),
19+
}));
20+
21+
vi.mock("../../api/functions/mobileWallet.js", () => ({
22+
issueAppleWalletMembershipCard: vi.fn().mockResolvedValue(new ArrayBuffer(0)),
23+
}));
24+
25+
vi.stubEnv("RunEnvironment", 'dev');
26+
27+
import { describe } from "node:test";
28+
import { emailMembershipPassHandler } from "../../../src/api/sqs/handlers.js";
29+
import { EntraFetchError } from "../../../src/common/errors/index.js";
30+
import { getEntraIdToken, getUserProfile } from "../../../src/api/functions/entraId.js";
31+
import { issueAppleWalletMembershipCard } from "../../../src/api/functions/mobileWallet.js";
32+
33+
describe("Mobile wallet pass issuance (SQS function)", () => {
34+
beforeEach(() => {
35+
vi.clearAllMocks();
36+
});
37+
38+
test("Test that passes will be issued for members", async () => {
39+
const email = '[email protected]';
40+
const logger = { info: vi.fn() } as any;
41+
await emailMembershipPassHandler(
42+
{ email },
43+
{ reqId: '1', initiator: '1' },
44+
logger
45+
);
46+
expect(getEntraIdToken).toHaveBeenCalled();
47+
expect(issueAppleWalletMembershipCard).toHaveBeenCalledOnce();
48+
expect(getUserProfile).toHaveBeenCalledWith("atokenofalltime", email);
49+
});
50+
});

0 commit comments

Comments
 (0)