|
| 1 | +const { expect } = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const request = require('supertest'); |
| 4 | +const express = require('express'); |
| 5 | +const session = require('express-session'); |
| 6 | +const contactController = require('../controllers/contact'); |
| 7 | + |
| 8 | +let app; |
| 9 | +let sendMailStub; |
| 10 | +let fetchStub; |
| 11 | +const OLD_ENV = { ...process.env }; |
| 12 | + |
| 13 | +function setupApp(controller) { |
| 14 | + const app = express(); |
| 15 | + app.use(express.urlencoded({ extended: false })); |
| 16 | + app.use(session({ secret: 'test', resave: false, saveUninitialized: false })); |
| 17 | + |
| 18 | + // Set a dummy CSRF token for all requests |
| 19 | + app.use((req, res, next) => { |
| 20 | + req.flash = (type, msg) => { |
| 21 | + req.session[type] = msg; |
| 22 | + }; |
| 23 | + req.csrfToken = () => 'testcsrf'; |
| 24 | + res.render = () => res.status(200).send('Contact Form'); |
| 25 | + next(); |
| 26 | + }); |
| 27 | + |
| 28 | + app.get('/contact', controller.getContact); |
| 29 | + app.post('/contact', controller.postContact); |
| 30 | + return app; |
| 31 | +} |
| 32 | + |
| 33 | +describe('Contact Controller', () => { |
| 34 | + before(() => { |
| 35 | + process.env.SITE_CONTACT_EMAIL = '[email protected]'; |
| 36 | + process.env.RECAPTCHA_SITE_KEY = 'dummy'; |
| 37 | + process.env.RECAPTCHA_SECRET_KEY = 'dummy'; |
| 38 | + }); |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + // Stub nodemailerConfig.sendMail |
| 42 | + sendMailStub = sinon.stub().resolves(); |
| 43 | + // Patch require cache for nodemailerConfig |
| 44 | + const nodemailerConfig = require.cache[require.resolve('../config/nodemailer')]; |
| 45 | + if (nodemailerConfig) { |
| 46 | + nodemailerConfig.exports.sendMail = sendMailStub; |
| 47 | + } |
| 48 | + |
| 49 | + // Stub global fetch for reCAPTCHA |
| 50 | + fetchStub = sinon.stub().resolves({ |
| 51 | + json: () => Promise.resolve({ success: true }), |
| 52 | + }); |
| 53 | + global.fetch = fetchStub; |
| 54 | + |
| 55 | + app = setupApp(contactController); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + sinon.restore(); |
| 60 | + if (sendMailStub) sendMailStub.resetHistory(); |
| 61 | + delete global.fetch; |
| 62 | + }); |
| 63 | + |
| 64 | + after(() => { |
| 65 | + process.env = OLD_ENV; |
| 66 | + }); |
| 67 | + |
| 68 | + describe('GET /contact', () => { |
| 69 | + it('renders the contact form', (done) => { |
| 70 | + request(app) |
| 71 | + .get('/contact') |
| 72 | + .expect(200) |
| 73 | + .end((err) => { |
| 74 | + if (err) return done(err); |
| 75 | + expect(true).to.be.true; // keep assertion for lint, actual check is above |
| 76 | + done(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('POST /contact', () => { |
| 82 | + it('rejects missing name/email for unknown user', (done) => { |
| 83 | + request(app) |
| 84 | + .post('/contact') |
| 85 | + .type('form') |
| 86 | + .send({ _csrf: 'testcsrf', name: '', email: '', message: 'Hello', 'g-recaptcha-response': 'token' }) |
| 87 | + .expect(302) |
| 88 | + .expect('Location', '/contact') |
| 89 | + .end((err) => { |
| 90 | + if (err) return done(err); |
| 91 | + expect(sendMailStub.called).to.be.false; |
| 92 | + done(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('rejects missing message', (done) => { |
| 97 | + request(app) |
| 98 | + .post('/contact') |
| 99 | + .type('form') |
| 100 | + .send({ _csrf: 'testcsrf', name: 'Test', email: '[email protected]', message: '', 'g-recaptcha-response': 'token' }) |
| 101 | + .expect(302) |
| 102 | + .expect('Location', '/contact') |
| 103 | + .end((err) => { |
| 104 | + if (err) return done(err); |
| 105 | + expect(sendMailStub.called).to.be.false; |
| 106 | + done(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('rejects missing reCAPTCHA', (done) => { |
| 111 | + request(app) |
| 112 | + .post('/contact') |
| 113 | + .type('form') |
| 114 | + .send({ _csrf: 'testcsrf', name: 'Test', email: '[email protected]', message: 'Hello', 'g-recaptcha-response': '' }) |
| 115 | + .expect(302) |
| 116 | + .expect('Location', '/contact') |
| 117 | + .end((err) => { |
| 118 | + if (err) return done(err); |
| 119 | + expect(sendMailStub.called).to.be.false; |
| 120 | + done(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('sends email if all fields are valid', (done) => { |
| 125 | + request(app) |
| 126 | + .post('/contact') |
| 127 | + .type('form') |
| 128 | + .send({ _csrf: 'testcsrf', name: 'Test', email: '[email protected]', message: 'Hello', 'g-recaptcha-response': 'token' }) |
| 129 | + .expect(302) |
| 130 | + .expect('Location', '/contact') |
| 131 | + .end((err) => { |
| 132 | + if (err) return done(err); |
| 133 | + expect(sendMailStub.calledOnce).to.be.true; |
| 134 | + done(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it('handles reCAPTCHA failure', (done) => { |
| 139 | + fetchStub.resolves({ json: () => Promise.resolve({ success: false }) }); |
| 140 | + request(app) |
| 141 | + .post('/contact') |
| 142 | + .type('form') |
| 143 | + .send({ _csrf: 'testcsrf', name: 'Test', email: '[email protected]', message: 'Hello', 'g-recaptcha-response': 'token' }) |
| 144 | + .expect(302) |
| 145 | + .expect('Location', '/contact') |
| 146 | + .end((err) => { |
| 147 | + if (err) return done(err); |
| 148 | + expect(sendMailStub.called).to.be.false; |
| 149 | + done(); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments