|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Push transport configuration (APNs token auth, FCM HTTP v1). Its two |
| 4 | +// secrets — the APNs `.p8` signing key and the FCM service-account JSON — are |
| 5 | +// named by reference only (ADR-0143 D3), so an inline secret is refused two |
| 6 | +// ways: under the key an operator would reach for (refused by name, with the |
| 7 | +// reference key as the prescription), and as the CONTENT of a reference (a |
| 8 | +// PEM block or a JSON document is a secret pasted where its name belongs). |
| 9 | + |
| 10 | +import { describe, it, expect } from 'vitest'; |
| 11 | +import { |
| 12 | + ApnsTransportConfigSchema, |
| 13 | + FcmTransportConfigSchema, |
| 14 | + PushServiceConfigSchema, |
| 15 | + PushVendorSchema, |
| 16 | +} from './push-config.zod'; |
| 17 | + |
| 18 | +const APNS = { |
| 19 | + teamId: 'ABCDE12345', |
| 20 | + keyId: 'KEY1234567', |
| 21 | + topic: 'com.example.shell', |
| 22 | + environment: 'production', |
| 23 | + keyRef: 'OS_PUSH_APNS_KEY', |
| 24 | +} as const; |
| 25 | + |
| 26 | +const FCM = { projectId: 'example-project', serviceAccountRef: 'OS_PUSH_FCM_SERVICE_ACCOUNT' } as const; |
| 27 | + |
| 28 | +const PEM = '-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg\n-----END PRIVATE KEY-----'; |
| 29 | +const SERVICE_ACCOUNT_JSON = '{"type":"service_account","project_id":"example-project","private_key":"x"}'; |
| 30 | + |
| 31 | +describe('PushVendorSchema', () => { |
| 32 | + it('names exactly the two vendors core delivers through', () => { |
| 33 | + expect(PushVendorSchema.options).toEqual(['apns', 'fcm']); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('PushServiceConfigSchema', () => { |
| 38 | + it('accepts both transports configured by reference', () => { |
| 39 | + const config = { apns: APNS, fcm: FCM }; |
| 40 | + expect(PushServiceConfigSchema.parse(config)).toEqual(config); |
| 41 | + }); |
| 42 | + |
| 43 | + it('accepts either transport alone, and the sandbox environment', () => { |
| 44 | + expect(PushServiceConfigSchema.safeParse({ apns: { ...APNS, environment: 'sandbox' } }).success).toBe(true); |
| 45 | + expect(PushServiceConfigSchema.safeParse({ fcm: FCM }).success).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it.each([ |
| 49 | + 'OS_PUSH_APNS_KEY', |
| 50 | + 'push/apns-signing-key', |
| 51 | + 'vault:push.apns.p8', |
| 52 | + ])('accepts the reference form %j', (keyRef) => { |
| 53 | + expect(ApnsTransportConfigSchema.safeParse({ ...APNS, keyRef }).success).toBe(true); |
| 54 | + expect(FcmTransportConfigSchema.safeParse({ ...FCM, serviceAccountRef: keyRef }).success).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it.each(['key', 'privateKey', 'p8'])('refuses an inline APNs key under %j, by name', (key) => { |
| 58 | + const parsed = ApnsTransportConfigSchema.safeParse({ ...APNS, [key]: PEM }); |
| 59 | + expect(parsed.success).toBe(false); |
| 60 | + const issue = parsed.error?.issues[0]; |
| 61 | + expect(issue?.code).toBe('unrecognized_keys'); |
| 62 | + expect(issue?.message).toContain('keyRef'); |
| 63 | + }); |
| 64 | + |
| 65 | + it.each(['serviceAccount', 'serviceAccountJson', 'privateKey'])('refuses an inline FCM credential under %j, by name', (key) => { |
| 66 | + const parsed = FcmTransportConfigSchema.safeParse({ ...FCM, [key]: SERVICE_ACCOUNT_JSON }); |
| 67 | + expect(parsed.success).toBe(false); |
| 68 | + const issue = parsed.error?.issues[0]; |
| 69 | + expect(issue?.code).toBe('unrecognized_keys'); |
| 70 | + expect(issue?.message).toContain('serviceAccountRef'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('refuses a PEM block pasted into keyRef', () => { |
| 74 | + const parsed = ApnsTransportConfigSchema.safeParse({ ...APNS, keyRef: PEM }); |
| 75 | + expect(parsed.success).toBe(false); |
| 76 | + expect(parsed.error?.issues[0]?.path).toEqual(['keyRef']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('refuses a service-account JSON document pasted into serviceAccountRef', () => { |
| 80 | + const parsed = FcmTransportConfigSchema.safeParse({ ...FCM, serviceAccountRef: SERVICE_ACCOUNT_JSON }); |
| 81 | + expect(parsed.success).toBe(false); |
| 82 | + expect(parsed.error?.issues[0]?.path).toEqual(['serviceAccountRef']); |
| 83 | + }); |
| 84 | + |
| 85 | + it('refuses an environment outside sandbox / production', () => { |
| 86 | + const parsed = ApnsTransportConfigSchema.safeParse({ ...APNS, environment: 'development' }); |
| 87 | + expect(parsed.success).toBe(false); |
| 88 | + expect(parsed.error?.issues[0]?.path).toEqual(['environment']); |
| 89 | + }); |
| 90 | + |
| 91 | + it('refuses an unknown transport key on the block', () => { |
| 92 | + const parsed = PushServiceConfigSchema.safeParse({ fcm: FCM, hms: {} }); |
| 93 | + expect(parsed.success).toBe(false); |
| 94 | + expect(parsed.error?.issues[0]?.code).toBe('unrecognized_keys'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments