|
| 1 | +import { afterAll, expect, test, beforeEach, vi, describe } from "vitest"; |
| 2 | +import init from "../../src/api/index.js"; |
| 3 | +import { |
| 4 | + GetSecretValueCommand, |
| 5 | + SecretsManagerClient, |
| 6 | +} from "@aws-sdk/client-secrets-manager"; |
| 7 | +import { mockClient } from "aws-sdk-client-mock"; |
| 8 | +import { secretJson } from "./secret.testdata.js"; |
| 9 | +import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; |
| 10 | +import supertest from "supertest"; |
| 11 | +import { createJwt } from "./auth.test.js"; |
| 12 | + |
| 13 | +const smMock = mockClient(SecretsManagerClient); |
| 14 | +const ddbMock = mockClient(DynamoDBClient); |
| 15 | + |
| 16 | +vi.mock("stripe", () => { |
| 17 | + const productMock = { id: "prod_123" }; |
| 18 | + const priceMock = { id: "price_123" }; |
| 19 | + const paymentLinkMock = { |
| 20 | + id: "plink_123", |
| 21 | + url: "https://stripe.com/payment-link", |
| 22 | + }; |
| 23 | + |
| 24 | + return { |
| 25 | + default: vi.fn(() => ({ |
| 26 | + products: { |
| 27 | + create: vi.fn().mockResolvedValue(productMock), |
| 28 | + }, |
| 29 | + prices: { |
| 30 | + create: vi.fn().mockResolvedValue(priceMock), |
| 31 | + }, |
| 32 | + paymentLinks: { |
| 33 | + create: vi.fn().mockResolvedValue(paymentLinkMock), |
| 34 | + }, |
| 35 | + })), |
| 36 | + }; |
| 37 | +}); |
| 38 | + |
| 39 | +const app = await init(); |
| 40 | +describe("Test Stripe link creation", async () => { |
| 41 | + test("Test body validation 1", async () => { |
| 42 | + smMock.on(GetSecretValueCommand).resolves({ |
| 43 | + SecretString: secretJson, |
| 44 | + }); |
| 45 | + ddbMock.on(PutItemCommand).rejects(); |
| 46 | + const testJwt = createJwt(); |
| 47 | + await app.ready(); |
| 48 | + |
| 49 | + const response = await supertest(app.server) |
| 50 | + .post("/api/v1/stripe/paymentLinks") |
| 51 | + .set("authorization", `Bearer ${testJwt}`) |
| 52 | + .send({ |
| 53 | + invoiceId: "", |
| 54 | + invoiceAmountUsd: 49, |
| 55 | + contactName: "", |
| 56 | + }); |
| 57 | + expect(response.statusCode).toBe(400); |
| 58 | + expect(response.body).toStrictEqual({ |
| 59 | + error: true, |
| 60 | + name: "ValidationError", |
| 61 | + id: 104, |
| 62 | + message: |
| 63 | + 'String must contain at least 1 character(s) at "invoiceId"; Number must be greater than or equal to 50 at "invoiceAmountUsd"; String must contain at least 1 character(s) at "contactName"; Required at "contactEmail"', |
| 64 | + }); |
| 65 | + }); |
| 66 | + test("Test body validation 2", async () => { |
| 67 | + smMock.on(GetSecretValueCommand).resolves({ |
| 68 | + SecretString: secretJson, |
| 69 | + }); |
| 70 | + ddbMock.on(PutItemCommand).rejects(); |
| 71 | + const testJwt = createJwt(); |
| 72 | + await app.ready(); |
| 73 | + |
| 74 | + const response = await supertest(app.server) |
| 75 | + .post("/api/v1/stripe/paymentLinks") |
| 76 | + .set("authorization", `Bearer ${testJwt}`) |
| 77 | + .send({ |
| 78 | + invoiceId: "ACM102", |
| 79 | + invoiceAmountUsd: 51, |
| 80 | + contactName: "Dev", |
| 81 | + contactEmail: "invalidEmail", |
| 82 | + }); |
| 83 | + expect(response.statusCode).toBe(400); |
| 84 | + expect(response.body).toStrictEqual({ |
| 85 | + error: true, |
| 86 | + name: "ValidationError", |
| 87 | + id: 104, |
| 88 | + message: 'Invalid email at "contactEmail"', |
| 89 | + }); |
| 90 | + }); |
| 91 | + test("Happy Path", async () => { |
| 92 | + ddbMock.on(PutItemCommand).resolves({}); |
| 93 | + const testJwt = createJwt(); |
| 94 | + await app.ready(); |
| 95 | + |
| 96 | + const response = await supertest(app.server) |
| 97 | + .post("/api/v1/stripe/paymentLinks") |
| 98 | + .set("authorization", `Bearer ${testJwt}`) |
| 99 | + .send({ |
| 100 | + invoiceId: "ACM102", |
| 101 | + invoiceAmountUsd: 51, |
| 102 | + contactName: "Infra User", |
| 103 | + contactEmail: "[email protected]", |
| 104 | + }); |
| 105 | + expect(response.statusCode).toBe(201); |
| 106 | + console.log(response.body); |
| 107 | + }); |
| 108 | + afterAll(async () => { |
| 109 | + await app.close(); |
| 110 | + }); |
| 111 | + beforeEach(() => { |
| 112 | + (app as any).nodeCache.flushAll(); |
| 113 | + vi.clearAllMocks(); |
| 114 | + smMock.on(GetSecretValueCommand).resolves({ |
| 115 | + SecretString: secretJson, |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments