|
| 1 | +import * as pulumi from '@pulumi/pulumi' |
| 2 | + |
| 3 | +import { MyMocks, promiseOf } from './utils' |
| 4 | +import * as resources from '../stacks/main/resources' |
| 5 | + |
| 6 | +vi.mock('../stacks/main/resources') |
| 7 | + |
| 8 | +describe('stacks/main/index.ts', () => { |
| 9 | + let envOrig: string |
| 10 | + let mocks: MyMocks |
| 11 | + let infra: typeof import('../stacks/main') |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + vi.resetModules() |
| 15 | + envOrig = JSON.stringify(process.env) |
| 16 | + mocks = new MyMocks() |
| 17 | + pulumi.runtime.setMocks(mocks) |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + process.env = JSON.parse(envOrig) |
| 22 | + }) |
| 23 | + |
| 24 | + it('Without FQDN', async () => { |
| 25 | + ;(resources.buildRouter as any).mockImplementation(() => { |
| 26 | + return 'mock' |
| 27 | + }) |
| 28 | + ;(resources.buildCDN as any).mockImplementation(() => { |
| 29 | + return { |
| 30 | + domainName: 'example.com', |
| 31 | + } |
| 32 | + }) |
| 33 | + // @ts-ignore |
| 34 | + pulumi.Config = vi.fn(() => { |
| 35 | + return { |
| 36 | + get: vi.fn((x) => { |
| 37 | + if (x === 'serverHeaders') { |
| 38 | + return '{"mock": "mock"}' |
| 39 | + } |
| 40 | + return '' |
| 41 | + }) |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + infra = await import('../stacks/main') |
| 46 | + |
| 47 | + expect(resources.getLambdaRole).toHaveBeenCalledTimes(1) |
| 48 | + expect(resources.validateCertificate).toHaveBeenCalledTimes(0) |
| 49 | + expect(resources.buildStatic).toHaveBeenCalledTimes(1) |
| 50 | + expect(resources.buildCDN).toHaveBeenCalledTimes(1) |
| 51 | + expect(resources.createAliasRecord).toHaveBeenCalledTimes(0) |
| 52 | + expect(resources.buildInvalidator).toHaveBeenCalledTimes(1) |
| 53 | + |
| 54 | + const allowedOrigin = await promiseOf(infra.allowedOrigins[0] as pulumi.Output<string>) |
| 55 | + const appUrl = await promiseOf(infra.appUrl as pulumi.Output<string>) |
| 56 | + expect(allowedOrigin).toMatch('https://example.com') |
| 57 | + expect(appUrl).toMatch('https://example.com') |
| 58 | + }) |
| 59 | + |
| 60 | + it('With FQDN', async () => { |
| 61 | + const fqdn = 'mock.application.net' |
| 62 | + ;(resources.buildRouter as any).mockImplementation(() => { |
| 63 | + return 'mock' |
| 64 | + }) |
| 65 | + ;(resources.buildCDN as any).mockImplementation(() => { |
| 66 | + return { |
| 67 | + domainName: 'example.com', |
| 68 | + } |
| 69 | + }) |
| 70 | + // @ts-ignore |
| 71 | + pulumi.Config = vi.fn(() => { |
| 72 | + return { |
| 73 | + get: vi.fn((x) => { |
| 74 | + if (x === 'serverHeaders') { |
| 75 | + return '{"mock": "mock"}' |
| 76 | + } else if (x === 'FQDN') { |
| 77 | + return fqdn |
| 78 | + } |
| 79 | + return '' |
| 80 | + }) |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + infra = await import('../stacks/main') |
| 85 | + |
| 86 | + expect(resources.validateCertificate).toHaveBeenCalledTimes(1) |
| 87 | + expect(resources.createAliasRecord).toHaveBeenCalledTimes(1) |
| 88 | + |
| 89 | + const distOrigin = await promiseOf(infra.allowedOrigins[0] as pulumi.Output<string>) |
| 90 | + const fqdnOrigin = infra.allowedOrigins[1] |
| 91 | + |
| 92 | + expect(distOrigin).toMatch('https://example.com') |
| 93 | + expect(fqdnOrigin).toMatch(`https://${fqdn}`) |
| 94 | + expect(infra.appUrl).toMatch(`https://${fqdn}`) |
| 95 | + }) |
| 96 | +}) |
0 commit comments