|
| 1 | +import { |
| 2 | + afterEach, |
| 3 | + beforeEach, |
| 4 | + describe, |
| 5 | + expect, |
| 6 | + it, |
| 7 | + type MockInstance, |
| 8 | + vi, |
| 9 | +} from 'vitest'; |
| 10 | +import { rotateWebhookSigningSecretCommand } from '../../../src/commands/webhooks/rotate-signing-secret'; |
| 11 | +import { |
| 12 | + captureTestEnv, |
| 13 | + expectExit1, |
| 14 | + mockExitThrow, |
| 15 | + mockSdkError, |
| 16 | + setNonInteractive, |
| 17 | + setupOutputSpies, |
| 18 | +} from '../../helpers'; |
| 19 | + |
| 20 | +const WEBHOOK_ID = '4dd369bc-aa82-4ff3-97de-514ae3000ee0'; |
| 21 | + |
| 22 | +const mockRotateSigningSecret = vi.fn(async () => ({ |
| 23 | + data: { |
| 24 | + object: 'webhook' as const, |
| 25 | + id: WEBHOOK_ID, |
| 26 | + signing_secret: 'whsec_new_secret', |
| 27 | + }, |
| 28 | + error: null, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('resend', () => ({ |
| 32 | + Resend: class MockResend { |
| 33 | + constructor(public key: string) {} |
| 34 | + webhooks = { rotateSigningSecret: mockRotateSigningSecret }; |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +describe('webhooks rotate-signing-secret command', () => { |
| 39 | + const restoreEnv = captureTestEnv(); |
| 40 | + let spies: ReturnType<typeof setupOutputSpies> | undefined; |
| 41 | + let errorSpy: MockInstance | undefined; |
| 42 | + let stderrSpy: MockInstance | undefined; |
| 43 | + let exitSpy: MockInstance | undefined; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + process.env.RESEND_API_KEY = 're_test_key'; |
| 47 | + mockRotateSigningSecret.mockClear(); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + restoreEnv(); |
| 52 | + errorSpy?.mockRestore(); |
| 53 | + stderrSpy?.mockRestore(); |
| 54 | + exitSpy?.mockRestore(); |
| 55 | + spies = undefined; |
| 56 | + errorSpy = undefined; |
| 57 | + stderrSpy = undefined; |
| 58 | + exitSpy = undefined; |
| 59 | + }); |
| 60 | + |
| 61 | + it('rotates the secret for the given webhook and outputs it as JSON when non-interactive', async () => { |
| 62 | + spies = setupOutputSpies(); |
| 63 | + |
| 64 | + await rotateWebhookSigningSecretCommand.parseAsync([WEBHOOK_ID], { |
| 65 | + from: 'user', |
| 66 | + }); |
| 67 | + |
| 68 | + expect(mockRotateSigningSecret).toHaveBeenCalledWith(WEBHOOK_ID); |
| 69 | + const parsed = JSON.parse(spies.logSpy.mock.calls[0][0] as string); |
| 70 | + expect(parsed.object).toBe('webhook'); |
| 71 | + expect(parsed.id).toBe(WEBHOOK_ID); |
| 72 | + expect(parsed.signing_secret).toBe('whsec_new_secret'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('errors with create_error when the SDK returns an error', async () => { |
| 76 | + setNonInteractive(); |
| 77 | + mockRotateSigningSecret.mockResolvedValueOnce( |
| 78 | + mockSdkError('Webhook not found', 'not_found'), |
| 79 | + ); |
| 80 | + errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 81 | + stderrSpy = vi |
| 82 | + .spyOn(process.stderr, 'write') |
| 83 | + .mockImplementation(() => true); |
| 84 | + exitSpy = mockExitThrow(); |
| 85 | + |
| 86 | + await expectExit1(() => |
| 87 | + rotateWebhookSigningSecretCommand.parseAsync([WEBHOOK_ID], { |
| 88 | + from: 'user', |
| 89 | + }), |
| 90 | + ); |
| 91 | + |
| 92 | + const output = errorSpy.mock.calls.map((c) => c[0]).join(' '); |
| 93 | + expect(output).toContain('create_error'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments