diff --git a/api/main_endpoints/routes/Auth.js b/api/main_endpoints/routes/Auth.js index 7d7798834..168646298 100644 --- a/api/main_endpoints/routes/Auth.js +++ b/api/main_endpoints/routes/Auth.js @@ -38,6 +38,16 @@ router.post('/register', async (req, res) => { const registrationStatus = await registerUser(req.body); if (registrationStatus.userSaved) { const name = req.body.firstName + ' ' + req.body.lastName; + const user = await User.findOne({email: req.body.email}); + + if (user) { + AuditLog.create({ + userId: user._id, + action: AuditLogActions.SIGN_UP, + details: {email: req.body.email} + }).catch(logger.error); + } + sendVerificationEmail(name, req.body.email); return res.sendStatus(OK); } diff --git a/test/api/Auth.js b/test/api/Auth.js index d93728091..e59ff08c4 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -196,6 +196,29 @@ describe('Auth', () => { await User.deleteOne({email: user.email}); } }); + + it('Should create an audit log entry on successful signup', async () => { + const registerPayload = { + email: 'newuser@example.com', + password: 'Passw0rd123!', + firstName: 'Testfirst', + lastName: 'Testlast' + }; + + // ensure Audit log and User DB starts fresh before this test + await AuditLog.deleteMany({}); + await User.deleteOne({email: registerPayload.email}); + + const res = await test.sendPostRequest('/api/Auth/register', registerPayload); + expect(res).to.have.status(OK); + + const auditEntry = await AuditLog.findOne().lean(); + + expect(auditEntry).to.exist; + expect(auditEntry).to.have.property('userId'); + expect(auditEntry.details).to.have.property('email', registerPayload.email); + + }); }); describe('/POST login', () => {