|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +jest.mock('./email', () => ({ |
| 4 | + email_myself: jest.fn().mockResolvedValue(undefined), |
| 5 | +})); |
| 6 | + |
| 7 | +import { send } from './contact'; |
| 8 | +import { email_myself } from './email'; |
| 9 | + |
| 10 | +describe('contact send', () => { |
| 11 | + const validToken = 'fd0kAn1zns'; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + email_myself.mockClear(); |
| 15 | + }); |
| 16 | + |
| 17 | + test('sends contact email with sender email in subject', async () => { |
| 18 | + const event = { |
| 19 | + body: JSON.stringify({ |
| 20 | + token: validToken, |
| 21 | + email: 'visitor@example.com', |
| 22 | + message: 'Hello, I have a question.', |
| 23 | + }), |
| 24 | + }; |
| 25 | + |
| 26 | + const result = await send(event, {}); |
| 27 | + |
| 28 | + expect(result.statusCode).toBe(200); |
| 29 | + expect(JSON.parse(result.body)).toEqual({ success: true }); |
| 30 | + expect(email_myself).toHaveBeenCalledWith( |
| 31 | + 'CryFS Contact Form', |
| 32 | + 'CryFS Contact Form (from visitor@example.com)', |
| 33 | + 'Hello, I have a question.', |
| 34 | + 'visitor@example.com' |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + test('shows "(from unknown)" when email is empty string', async () => { |
| 39 | + const event = { |
| 40 | + body: JSON.stringify({ |
| 41 | + token: validToken, |
| 42 | + email: '', |
| 43 | + message: 'Anonymous message', |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + const result = await send(event, {}); |
| 48 | + |
| 49 | + expect(result.statusCode).toBe(200); |
| 50 | + expect(email_myself).toHaveBeenCalledWith( |
| 51 | + 'CryFS Contact Form', |
| 52 | + 'CryFS Contact Form (from unknown)', |
| 53 | + 'Anonymous message', |
| 54 | + '' |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + test('handles undefined email', async () => { |
| 59 | + const event = { |
| 60 | + body: JSON.stringify({ |
| 61 | + token: validToken, |
| 62 | + message: 'Message without email', |
| 63 | + }), |
| 64 | + }; |
| 65 | + |
| 66 | + const result = await send(event, {}); |
| 67 | + |
| 68 | + expect(result.statusCode).toBe(200); |
| 69 | + // When email is undefined, it's passed as undefined to do_send |
| 70 | + expect(email_myself).toHaveBeenCalledWith( |
| 71 | + 'CryFS Contact Form', |
| 72 | + expect.stringContaining('CryFS Contact Form'), |
| 73 | + 'Message without email', |
| 74 | + undefined |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + test('returns 200 on success', async () => { |
| 79 | + const event = { |
| 80 | + body: JSON.stringify({ |
| 81 | + token: validToken, |
| 82 | + email: 'test@test.com', |
| 83 | + message: 'Test message', |
| 84 | + }), |
| 85 | + }; |
| 86 | + |
| 87 | + const result = await send(event, {}); |
| 88 | + |
| 89 | + expect(result.statusCode).toBe(200); |
| 90 | + expect(JSON.parse(result.body)).toEqual({ success: true }); |
| 91 | + }); |
| 92 | + |
| 93 | + test('includes CORS headers in response', async () => { |
| 94 | + const event = { |
| 95 | + body: JSON.stringify({ |
| 96 | + token: validToken, |
| 97 | + email: 'test@test.com', |
| 98 | + message: 'Test message', |
| 99 | + }), |
| 100 | + }; |
| 101 | + |
| 102 | + const result = await send(event, {}); |
| 103 | + |
| 104 | + expect(result.headers).toEqual({ |
| 105 | + 'Access-Control-Allow-Origin': 'https://www.cryfs.org', |
| 106 | + 'Access-Control-Allow-Credentials': false, |
| 107 | + 'Vary': 'Origin', |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + test('rejects invalid token', async () => { |
| 112 | + const event = { |
| 113 | + body: JSON.stringify({ |
| 114 | + token: 'invalid-token', |
| 115 | + email: 'test@test.com', |
| 116 | + message: 'Test message', |
| 117 | + }), |
| 118 | + }; |
| 119 | + |
| 120 | + const result = await send(event, {}); |
| 121 | + |
| 122 | + expect(result.statusCode).toBe(400); |
| 123 | + expect(JSON.parse(result.body)).toEqual({ |
| 124 | + success: false, |
| 125 | + error: 'Wrong token', |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments