From 5db2d43014d8c5ce69f8851e44af5a761a58bc78 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 3 Aug 2024 14:33:57 -0400 Subject: [PATCH 01/14] chore: add requirements.md test outline for test cases --- .../popUpEditorController/createPopPopupEditor.md | 14 ++++++++++++++ .../popUpEditorController/getAllPopupEditors.md | 10 ++++++++++ .../popUpEditorController/getPopupEditorById.md | 10 ++++++++++ .../popUpEditorController/updatePopupEditor.md | 11 +++++++++++ 4 files changed, 45 insertions(+) create mode 100644 requirements/popUpEditorController/createPopPopupEditor.md create mode 100644 requirements/popUpEditorController/getAllPopupEditors.md create mode 100644 requirements/popUpEditorController/getPopupEditorById.md create mode 100644 requirements/popUpEditorController/updatePopupEditor.md diff --git a/requirements/popUpEditorController/createPopPopupEditor.md b/requirements/popUpEditorController/createPopPopupEditor.md new file mode 100644 index 000000000..0afcaa740 --- /dev/null +++ b/requirements/popUpEditorController/createPopPopupEditor.md @@ -0,0 +1,14 @@ +Check mark: ✅ +Cross Mark: ❌ + +# createPopPopupEditor Function + +> ### Positive case + +> 1. ✅ Should return 201 and the new pop-up editor on success + +> ### Negative case + +> 1. ✅ Should return 403 if user does not have permission to create a pop-up editor +> 2. ✅ Should return 400 if the request body is missing required fields +> 3. ✅ Should return 500 if there is an error saving the new pop-up editor to the database diff --git a/requirements/popUpEditorController/getAllPopupEditors.md b/requirements/popUpEditorController/getAllPopupEditors.md new file mode 100644 index 000000000..f42f93c1a --- /dev/null +++ b/requirements/popUpEditorController/getAllPopupEditors.md @@ -0,0 +1,10 @@ +Check mark: ✅ +Cross Mark: ❌ + +# getAllPopupEditors Function + +> ## Positive case +> 1. ✅ Should return 200 and all pop-up editors on success + +> ## Negative case +> 1. ✅ Should return 404 if there is an error retrieving the pop-up editors from the database diff --git a/requirements/popUpEditorController/getPopupEditorById.md b/requirements/popUpEditorController/getPopupEditorById.md new file mode 100644 index 000000000..013096ed9 --- /dev/null +++ b/requirements/popUpEditorController/getPopupEditorById.md @@ -0,0 +1,10 @@ +Check mark: ✅ +Cross Mark: ❌ + +# getPopupEditorById Function + +> ## Positive case +> 1. ✅ Should return 200 and the pop-up editor on success + +> ## Negative case +> 1. ✅ Should return 404 if the pop-up editor is not found diff --git a/requirements/popUpEditorController/updatePopupEditor.md b/requirements/popUpEditorController/updatePopupEditor.md new file mode 100644 index 000000000..c4e5f5904 --- /dev/null +++ b/requirements/popUpEditorController/updatePopupEditor.md @@ -0,0 +1,11 @@ +Check mark: ✅ +Cross Mark: ❌ + +# updatePopupEditor Function + +> ## Positive case +> 1. ✅ Should return 200 and the updated pop-up editor on success + + +> ## Negative case +> 1. ✅ Should return 404 if the pop-up editor is not found From 43f39fc1143667e7183065d89690088ac7026545 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 3 Aug 2024 15:01:40 -0400 Subject: [PATCH 02/14] test(popupEditorController): Add test suite for getAll popup --- src/controllers/popupEditorController.spec.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/controllers/popupEditorController.spec.js diff --git a/src/controllers/popupEditorController.spec.js b/src/controllers/popupEditorController.spec.js new file mode 100644 index 000000000..ed0c3ecc4 --- /dev/null +++ b/src/controllers/popupEditorController.spec.js @@ -0,0 +1,45 @@ +const PopUpEditor = require('../models/popupEditor'); +const { mockReq, mockRes, assertResMock } = require('../test'); + +jest.mock('../utilities/permissions'); + +const helper = require('../utilities/permissions'); +const popupEditorController = require('./popupEditorController'); + +const flushPromises = () => new Promise(setImmediate); + +const mockHasPermission = (value) => + jest.spyOn(helper, 'hasPermission').mockImplementationOnce(() => Promise.resolve(value)); + +const makeSut = () => { + const { getAllPopupEditors, getPopupEditorById, createPopupEditor, updatePopupEditor } = + popupEditorController(PopUpEditor); + return { getAllPopupEditors, getPopupEditorById, createPopupEditor, updatePopupEditor }; +}; + +describe('popupEditorController Controller Unit tests', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + describe(`getAllPopupEditors function`, () => { + test(`Should return 200 and popup editors on success`, async () => { + const { getAllPopupEditors } = makeSut(); + const mockPopupEditors = [{ popupName: 'popup', popupContent: 'content' }]; + jest.spyOn(PopUpEditor, 'find').mockResolvedValue(mockPopupEditors); + const response = await getAllPopupEditors(mockReq, mockRes); + assertResMock(200, mockPopupEditors, response, mockRes); + }); + + test(`Should return 404 on error`, async () => { + const { getAllPopupEditors } = makeSut(); + const error = new Error('Test Error'); + + jest.spyOn(PopUpEditor, 'find').mockRejectedValue(error); + const response = await getAllPopupEditors(mockReq, mockRes); + await flushPromises(); + + assertResMock(404, error, response, mockRes); + }); + }); +}); From e3819623a44ad2f1e66cbbe70ef7e31accc17176 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 3 Aug 2024 15:04:24 -0400 Subject: [PATCH 03/14] test(popupEditorController): Add test suite for getPopupById --- src/controllers/popupEditorController.spec.js | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/controllers/popupEditorController.spec.js b/src/controllers/popupEditorController.spec.js index ed0c3ecc4..c2e43c2f6 100644 --- a/src/controllers/popupEditorController.spec.js +++ b/src/controllers/popupEditorController.spec.js @@ -42,4 +42,26 @@ describe('popupEditorController Controller Unit tests', () => { assertResMock(404, error, response, mockRes); }); }); + + describe(`getPopupEditorById function`, () => { + test(`Should return 200 and popup editor on success`, async () => { + const { getPopupEditorById } = makeSut(); + const mockPopupEditor = { popupName: 'popup', popupContent: 'content' }; + jest.spyOn(PopUpEditor, 'findById').mockResolvedValue(mockPopupEditor); + const response = await getPopupEditorById(mockReq, mockRes); + assertResMock(200, mockPopupEditor, response, mockRes); + }); + + test(`Should return 404 on error`, async () => { + const { getPopupEditorById } = makeSut(); + const error = new Error('Test Error'); + + jest.spyOn(PopUpEditor, 'findById').mockRejectedValue(error); + const response = await getPopupEditorById(mockReq, mockRes); + await flushPromises(); + + assertResMock(404, error, response, mockRes); + }); + }); + }); From 0e83e90310b82d6d19ee5b6e8a9b64de5ce9f04d Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 3 Aug 2024 15:26:26 -0400 Subject: [PATCH 04/14] test(popupEditorController): Add test suite for createPopupEditor --- src/controllers/popupEditorController.spec.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/controllers/popupEditorController.spec.js b/src/controllers/popupEditorController.spec.js index c2e43c2f6..9f87f8029 100644 --- a/src/controllers/popupEditorController.spec.js +++ b/src/controllers/popupEditorController.spec.js @@ -64,4 +64,52 @@ describe('popupEditorController Controller Unit tests', () => { }); }); + describe(`createPopupEditor function`, () => { + test(`Should return 403 if user is not authorized`, async () => { + const { createPopupEditor } = makeSut(); + mockHasPermission(false); + const response = await createPopupEditor(mockReq, mockRes); + assertResMock( + 403, + { error: 'You are not authorized to create new popup' }, + response, + mockRes, + ); + }); + + test(`Should return 400 if popupName or popupContent is missing`, async () => { + const { createPopupEditor } = makeSut(); + mockHasPermission(true); + const response = await createPopupEditor(mockReq, mockRes); + assertResMock( + 400, + { error: 'popupName , popupContent are mandatory fields' }, + response, + mockRes, + ); + }); + + test(`Should return 201 and popup editor on success`, async () => { + const { createPopupEditor } = makeSut(); + mockHasPermission(true); + mockReq.body = { popupName: 'popup', popupContent: 'content' }; + const mockPopupEditor = { save: jest.fn().mockResolvedValue(mockReq.body) }; + jest.spyOn(PopUpEditor.prototype, 'save').mockImplementationOnce(mockPopupEditor.save); + const response = await createPopupEditor(mockReq, mockRes); + expect(mockPopupEditor.save).toHaveBeenCalled(); + assertResMock(201, mockReq.body, response, mockRes); + }); + + test(`Should return 500 on error`, async () => { + const { createPopupEditor } = makeSut(); + mockHasPermission(true); + const error = new Error('Test Error'); + + jest.spyOn(PopUpEditor.prototype, 'save').mockRejectedValue(error); + const response = await createPopupEditor(mockReq, mockRes); + await flushPromises(); + + assertResMock(500, { error }, response, mockRes); + }); + }); }); From e102f05b13a040a6013159c50fb1ec92317a1ac4 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sun, 4 Aug 2024 01:28:39 -0400 Subject: [PATCH 05/14] test(popupEditorController): Add test suite for updatePopupEditor --- src/controllers/popupEditorController.spec.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/controllers/popupEditorController.spec.js b/src/controllers/popupEditorController.spec.js index 9f87f8029..e70b05553 100644 --- a/src/controllers/popupEditorController.spec.js +++ b/src/controllers/popupEditorController.spec.js @@ -112,4 +112,52 @@ describe('popupEditorController Controller Unit tests', () => { assertResMock(500, { error }, response, mockRes); }); }); + describe(`updatePopupEditor function`, () => { + test(`Should return 403 if user is not authorized`, async () => { + const { updatePopupEditor } = makeSut(); + mockHasPermission(false); + const response = await updatePopupEditor(mockReq, mockRes); + assertResMock( + 403, + { error: 'You are not authorized to create new popup' }, + response, + mockRes, + ); + }); + + test(`Should return 400 if popupContent is missing`, async () => { + const { updatePopupEditor } = makeSut(); + mockReq.body = {}; + mockHasPermission(true); + const response = await updatePopupEditor(mockReq, mockRes); + assertResMock(400, { error: 'popupContent is mandatory field' }, response, mockRes); + }); + + test(`Should return 201 and popup editor on success`, async () => { + const { updatePopupEditor } = makeSut(); + mockHasPermission(true); + mockReq.body = { popupContent: 'content' }; + const mockPopupEditor = { save: jest.fn().mockResolvedValue(mockReq.body) }; + jest.spyOn(PopUpEditor, 'findById').mockImplementationOnce((mockReq, callback) => callback(null, mockPopupEditor)); + jest.spyOn(PopUpEditor.prototype, 'save').mockImplementationOnce(mockPopupEditor.save); + const response = await updatePopupEditor(mockReq, mockRes); + expect(mockPopupEditor.save).toHaveBeenCalled(); + assertResMock(201, mockReq.body, response, mockRes); + }); + + test('Should return 500 on popupEditor save error', async () => { + const { updatePopupEditor } = makeSut(); + mockHasPermission(true); + const err = new Error('Test Error'); + mockReq.body = { popupContent: 'content' }; + const mockPopupEditor = { save: jest.fn().mockRejectedValue(err)}; + jest + .spyOn(PopUpEditor, 'findById') + .mockImplementation((mockReq, callback) => callback(null, mockPopupEditor)); + jest.spyOn(PopUpEditor.prototype, 'save').mockImplementationOnce(mockPopupEditor.save); + const response = await updatePopupEditor(mockReq, mockRes); + await flushPromises(); + assertResMock(500, {err}, response, mockRes); + }); + }); }); From 88cd4010eecbb8e1cb9c6b20d8b052915c68ff33 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 17 Aug 2024 23:08:25 -0400 Subject: [PATCH 06/14] test(emailController): Add unit test for sendEmail --- src/controllers/emailController.spec.js | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/controllers/emailController.spec.js diff --git a/src/controllers/emailController.spec.js b/src/controllers/emailController.spec.js new file mode 100644 index 000000000..d0f11377e --- /dev/null +++ b/src/controllers/emailController.spec.js @@ -0,0 +1,39 @@ +const { mockReq, mockRes, assertResMock } = require('../test'); +const emailController = require('./emailController'); + +const makeSut = () => { + const { + sendEmail, + sendEmailToAll, + updateEmailSubscriptions, + addNonHgnEmailSubscription, + removeNonHgnEmailSubscription, + confirmNonHgnEmailSubscription, + } = emailController; + return { + sendEmail, + sendEmailToAll, + updateEmailSubscriptions, + addNonHgnEmailSubscription, + removeNonHgnEmailSubscription, + confirmNonHgnEmailSubscription, + }; +}; +describe('emailController Controller Unit tests', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + test('should send email successfully', async () => { + const { sendEmail } = makeSut(); + const mockReq = { + body: { + to: 'recipient@example.com', + subject: 'Test Subject', + html: '

Test Body

' + } + }; + const response = await sendEmail(mockReq, mockRes); + assertResMock(200, 'Email sent successfully', response, mockRes); + }); +}); From b1140aa0f8d83faa47b90b82c288541943484321 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 14 Sep 2024 19:05:18 -0400 Subject: [PATCH 07/14] test(emailController): Add test suite --- src/controllers/emailController.spec.js | 105 +++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/src/controllers/emailController.spec.js b/src/controllers/emailController.spec.js index d0f11377e..d5daf79d0 100644 --- a/src/controllers/emailController.spec.js +++ b/src/controllers/emailController.spec.js @@ -1,5 +1,17 @@ const { mockReq, mockRes, assertResMock } = require('../test'); const emailController = require('./emailController'); +const jwt = require('jsonwebtoken'); +// const EmailSubcriptionList = require('../models/emailSubcriptionList'); +const userProfile = require('../models/userProfile'); +// const emailSender = require('../utilities/emailSender'); + +jest.mock('jsonwebtoken'); +jest.mock('../models/userProfile'); +jest.mock('../models/emailSubcriptionList'); +jest.mock('../utilities/emailSender'); + + + const makeSut = () => { const { @@ -30,10 +42,99 @@ describe('emailController Controller Unit tests', () => { body: { to: 'recipient@example.com', subject: 'Test Subject', - html: '

Test Body

' - } + html: '

Test Body

', + }, }; const response = await sendEmail(mockReq, mockRes); assertResMock(200, 'Email sent successfully', response, mockRes); }); + + describe('updateEmailSubscriptions function', () => { + test('should handle error when updating email subscriptions', async () => { + const { updateEmailSubscriptions } = makeSut(); + + + userProfile.findOneAndUpdate = jest.fn(); + + userProfile.findOneAndUpdate.mockRejectedValue(new Error('Update failed')); + + const mockReq = { + body: { + emailSubscriptions: ['subscription1', 'subscription2'], + requestor: { + email: 'test@example.com', + }, + }, + }; + + const response = await updateEmailSubscriptions(mockReq, mockRes); + + assertResMock(500, 'Error updating email subscriptions', response, mockRes); + }); + }); + + describe('addNonHgnEmailSubscription function', () => { + + beforeEach(() => { + jest.clearAllMocks(); + }); + }) + + describe('confirmNonHgnEmailSubscription function', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + beforeAll(() => { + jwt.verify = jest.fn(); + }); + + test('should return 400 if token is not provided', async () => { + const { confirmNonHgnEmailSubscription } = makeSut(); + + const mockReq = { body: {} }; + const response = await confirmNonHgnEmailSubscription(mockReq, mockRes); + + assertResMock(400, 'Invalid token', response, mockRes); + }); + + test('should return 401 if token is invalid', async () => { + const { confirmNonHgnEmailSubscription } = makeSut(); + const mockReq = { body: { token: 'invalidToken' } }; + + jwt.verify.mockImplementation(() => { + throw new Error('Token is not valid'); + }); + + await confirmNonHgnEmailSubscription(mockReq, mockRes); + + expect(mockRes.status).toHaveBeenCalledWith(401); + expect(mockRes.json).toHaveBeenCalledWith({ + errors: [ + { msg: 'Token is not valid' }, + ], + }); + }); + + + test('should return 400 if email is missing from payload', async () => { + const { confirmNonHgnEmailSubscription } = makeSut(); + const mockReq = { body: { token: 'validToken' } }; + + // Mocking jwt.verify to return a payload without email + jwt.verify.mockReturnValue({}); + + const response = await confirmNonHgnEmailSubscription(mockReq, mockRes); + + assertResMock(400, 'Invalid token', response, mockRes); + }); + + + + + + }); + + + }); From e139e68d2ebe256d1c8d764b281dceb842de7da1 Mon Sep 17 00:00:00 2001 From: yashwanth170 Date: Mon, 16 Sep 2024 22:13:29 -0400 Subject: [PATCH 08/14] fix: update password error --- src/controllers/userProfileController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/userProfileController.js b/src/controllers/userProfileController.js index 15f36f751..663566872 100644 --- a/src/controllers/userProfileController.js +++ b/src/controllers/userProfileController.js @@ -1035,7 +1035,7 @@ const userProfileController = function (UserProfile, Project) { const hasUpdatePasswordPermission = await hasPermission(requestor, 'updatePassword'); // if they're updating someone else's password, they need the 'updatePassword' permission. - if (!hasUpdatePasswordPermission) { + if (userId !== requestor.requestorId && !hasUpdatePasswordPermission) { return res.status(403).send({ error: "You are unauthorized to update this user's password", }); From 670d1071a2091ab6a679903f6b5064e431c25abe Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 21 Sep 2024 19:03:18 -0400 Subject: [PATCH 09/14] chore: add requirements.md test outline for test cases --- .../addNonHgnEmailSubscription.md | 23 ++++++++++++++++ .../confirmNonHgnEmailSubscription.md | 18 +++++++++++++ .../removeNonHgnEmailSubscription.md | 10 +++++++ requirements/emailController/sendEmail.md | 10 +++++++ .../emailController/sendEmailToAll.md | 26 +++++++++++++++++++ .../updateEmailSubscription.md | 20 ++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 requirements/emailController/addNonHgnEmailSubscription.md create mode 100644 requirements/emailController/confirmNonHgnEmailSubscription.md create mode 100644 requirements/emailController/removeNonHgnEmailSubscription.md create mode 100644 requirements/emailController/sendEmail.md create mode 100644 requirements/emailController/sendEmailToAll.md create mode 100644 requirements/emailController/updateEmailSubscription.md diff --git a/requirements/emailController/addNonHgnEmailSubscription.md b/requirements/emailController/addNonHgnEmailSubscription.md new file mode 100644 index 000000000..f5748f142 --- /dev/null +++ b/requirements/emailController/addNonHgnEmailSubscription.md @@ -0,0 +1,23 @@ +# Add Non-HGN Email Subscription Function + +## Negative Cases + +1. ❌ **Returns error 400 if `email` field is missing from the request** + - Ensures that the function checks for the presence of the `email` field in the request body and responds with a `400` status code if it's missing. + +2. ❌ **Returns error 400 if the provided `email` already exists in the subscription list** + - This case checks that the function responds with a `400` status code and a message indicating that the email is already subscribed. + +3. ❌ **Returns error 500 if there is an internal error while checking the subscription list** + - Covers scenarios where there's an issue querying the `EmailSubscriptionList` collection for the provided email (e.g., database connection issues). + +4. ❌ **Returns error 500 if there is an error sending the confirmation email** + - This case handles any issues that occur while calling the `emailSender` function, such as network errors or service unavailability. + +## Positive Cases + +1. ❌ **Returns status 200 when a new email is successfully subscribed** + - Ensures that the function successfully creates a JWT token, constructs the email, and sends the subscription confirmation email to the user. + +2. ❌ **Successfully sends a confirmation email containing the correct link** + - Verifies that the generated JWT token is correctly included in the confirmation link sent to the user in the email body. diff --git a/requirements/emailController/confirmNonHgnEmailSubscription.md b/requirements/emailController/confirmNonHgnEmailSubscription.md new file mode 100644 index 000000000..d5e1367af --- /dev/null +++ b/requirements/emailController/confirmNonHgnEmailSubscription.md @@ -0,0 +1,18 @@ +# Confirm Non-HGN Email Subscription Function Tests + +## Negative Cases +1. ✅ **Returns error 400 if `token` field is missing from the request** + - (Test: `should return 400 if token is not provided`) + +2. ✅ **Returns error 401 if the provided `token` is invalid or expired** + - (Test: `should return 401 if token is invalid`) + +3. ✅ **Returns error 400 if the decoded `token` does not contain a valid `email` field** + - (Test: `should return 400 if email is missing from payload`) + +4. ❌ **Returns error 500 if there is an internal error while saving the new email subscription** + +## Positive Cases +1. ❌ **Returns status 200 when a new email is successfully subscribed** + +2. ❌ **Returns status 200 if the email is already subscribed (duplicate email)** diff --git a/requirements/emailController/removeNonHgnEmailSubscription.md b/requirements/emailController/removeNonHgnEmailSubscription.md new file mode 100644 index 000000000..af793e2a9 --- /dev/null +++ b/requirements/emailController/removeNonHgnEmailSubscription.md @@ -0,0 +1,10 @@ +# Remove Non-HGN Email Subscription Function Tests + +## Negative Cases +1. ✅ **Returns error 400 if `email` field is missing from the request** + - (Test: `should return 400 if email is missing`) + +2. ❌ **Returns error 500 if there is an internal error while deleting the email subscription** + +## Positive Cases +1. ❌ **Returns status 200 when an email is successfully unsubscribed** diff --git a/requirements/emailController/sendEmail.md b/requirements/emailController/sendEmail.md new file mode 100644 index 000000000..7ca9a482c --- /dev/null +++ b/requirements/emailController/sendEmail.md @@ -0,0 +1,10 @@ +# Send Email Function + +## Negative Cases + +1. ❌ **Returns error 400 if `to`, `subject`, or `html` fields are missing from the request** +2. ❌ **Returns error 500 if there is an internal error while sending the email** + +## Positive Cases + +1. ✅ **Returns status 200 when email is successfully sent with `to`, `subject`, and `html` fields provided** diff --git a/requirements/emailController/sendEmailToAll.md b/requirements/emailController/sendEmailToAll.md new file mode 100644 index 000000000..32a09fed6 --- /dev/null +++ b/requirements/emailController/sendEmailToAll.md @@ -0,0 +1,26 @@ +# Send Email to All Function + +## Negative Cases + +1. ❌ **Returns error 400 if `subject` or `html` fields are missing from the request** + - The request should be rejected if either the `subject` or `html` content is not provided in the request body. + +2. ❌ **Returns error 500 if there is an internal error while fetching users** + - This case covers scenarios where there's an error fetching users from the `userProfile` collection (e.g., database connection issues). + +3. ❌ **Returns error 500 if there is an internal error while fetching the subscription list** + - This case covers scenarios where there's an error fetching emails from the `EmailSubcriptionList` collection. + +4. ❌ **Returns error 500 if there is an error sending emails** + - This case handles any issues that occur while calling the `emailSender` function, such as network errors or service unavailability. + +## Positive Cases + +1. ❌ **Returns status 200 when emails are successfully sent to all active users** + - Ensures that the function sends emails correctly to all users meeting the criteria (`isActive` and `EmailSubcriptionList`). + +2. ❌ **Returns status 200 when emails are successfully sent to all users in the subscription list** + - Verifies that the function sends emails to all users in the `EmailSubcriptionList`, including the unsubscribe link in the email body. + +3. ❌ **Combines user and subscription list emails successfully** + - Ensures that the function correctly sends emails to both active users and the subscription list without issues. diff --git a/requirements/emailController/updateEmailSubscription.md b/requirements/emailController/updateEmailSubscription.md new file mode 100644 index 000000000..bcafa5a28 --- /dev/null +++ b/requirements/emailController/updateEmailSubscription.md @@ -0,0 +1,20 @@ +# Update Email Subscriptions Function + +## Negative Cases + +1. ❌ **Returns error 400 if `emailSubscriptions` field is missing from the request** + - This ensures that the function checks for the presence of the `emailSubscriptions` field in the request body and responds with a `400` status code if it's missing. + +2. ❌ **Returns error 400 if `email` field is missing from the requestor object** + - Ensures that the function requires an `email` field within the `requestor` object in the request body and returns `400` if it's absent. + +3. ❌ **Returns error 404 if the user with the provided `email` is not found** + - This checks that the function correctly handles cases where no user exists with the given `email` and responds with a `404` status code. + +4. ✅ **Returns error 500 if there is an internal error while updating the user profile** + - Covers scenarios where there's a database error while updating the user's email subscriptions. + +## Positive Cases + +1. ❌ **Returns status 200 and the updated user when email subscriptions are successfully updated** + - Ensures that the function updates the `emailSubscriptions` field for the user and returns the updated user document along with a `200` status code. From 40c8b5ee0078ab1da67568b35217cdcb58b44965 Mon Sep 17 00:00:00 2001 From: Jordy Date: Sat, 21 Sep 2024 19:06:48 -0400 Subject: [PATCH 10/14] test(emailController): Add test suite for emailController --- src/controllers/emailController.spec.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/controllers/emailController.spec.js b/src/controllers/emailController.spec.js index d5daf79d0..680a1db8c 100644 --- a/src/controllers/emailController.spec.js +++ b/src/controllers/emailController.spec.js @@ -1,7 +1,7 @@ const { mockReq, mockRes, assertResMock } = require('../test'); const emailController = require('./emailController'); const jwt = require('jsonwebtoken'); -// const EmailSubcriptionList = require('../models/emailSubcriptionList'); +const EmailSubcriptionList = require('../models/emailSubcriptionList'); const userProfile = require('../models/userProfile'); // const emailSender = require('../utilities/emailSender'); @@ -36,6 +36,7 @@ describe('emailController Controller Unit tests', () => { jest.clearAllMocks(); }); + describe('sendEmail function', () => { test('should send email successfully', async () => { const { sendEmail } = makeSut(); const mockReq = { @@ -48,6 +49,7 @@ describe('emailController Controller Unit tests', () => { const response = await sendEmail(mockReq, mockRes); assertResMock(200, 'Email sent successfully', response, mockRes); }); +}); describe('updateEmailSubscriptions function', () => { test('should handle error when updating email subscriptions', async () => { @@ -73,12 +75,6 @@ describe('emailController Controller Unit tests', () => { }); }); - describe('addNonHgnEmailSubscription function', () => { - - beforeEach(() => { - jest.clearAllMocks(); - }); - }) describe('confirmNonHgnEmailSubscription function', () => { afterEach(() => { @@ -134,7 +130,19 @@ describe('emailController Controller Unit tests', () => { }); + describe('removeNonHgnEmailSubscription function', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + test('should return 400 if email is missing', async () => { + const { removeNonHgnEmailSubscription } = makeSut(); + const mockReq = { body: {} }; + const response = await removeNonHgnEmailSubscription(mockReq, mockRes); -}); + assertResMock(400, 'Email is required', response, mockRes); + }); + }); + + }); From 07b7840fe7ee0db768d53937623e249fb1c35178 Mon Sep 17 00:00:00 2001 From: Chetan0606 <62894581+Chetan0606@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:39:10 -0700 Subject: [PATCH 11/14] fixed the issue in the backend --- src/controllers/emailController.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/controllers/emailController.js b/src/controllers/emailController.js index ce6b3990d..ba0506d93 100644 --- a/src/controllers/emailController.js +++ b/src/controllers/emailController.js @@ -2,6 +2,7 @@ const nodemailer = require('nodemailer'); const jwt = require('jsonwebtoken'); const emailSender = require('../utilities/emailSender'); +const { hasPermission } = require('../utilities/permissions'); const EmailSubcriptionList = require('../models/emailSubcriptionList'); const userProfile = require('../models/userProfile'); @@ -9,7 +10,9 @@ const frontEndUrl = process.env.FRONT_END_URL || 'http://localhost:3000'; const jwtSecret = process.env.JWT_SECRET || 'EmailSecret'; const sendEmail = async (req, res) => { + console.log("Check"); const canSendEmail = await hasPermission(req.body.requestor, 'sendEmails'); + console.log("After has permission") if (!canSendEmail) { res.status(403).send('You are not authorized to send emails.'); return; From 5b6c847088b4a6eb649f465fd005c33ad975f289 Mon Sep 17 00:00:00 2001 From: Chetan0606 <62894581+Chetan0606@users.noreply.github.com> Date: Tue, 15 Oct 2024 18:42:40 -0700 Subject: [PATCH 12/14] Removed console statements --- src/controllers/emailController.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/controllers/emailController.js b/src/controllers/emailController.js index ba0506d93..523934448 100644 --- a/src/controllers/emailController.js +++ b/src/controllers/emailController.js @@ -10,9 +10,7 @@ const frontEndUrl = process.env.FRONT_END_URL || 'http://localhost:3000'; const jwtSecret = process.env.JWT_SECRET || 'EmailSecret'; const sendEmail = async (req, res) => { - console.log("Check"); const canSendEmail = await hasPermission(req.body.requestor, 'sendEmails'); - console.log("After has permission") if (!canSendEmail) { res.status(403).send('You are not authorized to send emails.'); return; From 4b734bdcd5e6efbc9477e4e889f95f7f0b21f336 Mon Sep 17 00:00:00 2001 From: Ambika Kabra Date: Sun, 20 Oct 2024 16:06:36 +0530 Subject: [PATCH 13/14] ambika hotfix undefined names in blue square email --- src/controllers/userProfileController.js | 36 ++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/controllers/userProfileController.js b/src/controllers/userProfileController.js index 31f297872..1e6407862 100644 --- a/src/controllers/userProfileController.js +++ b/src/controllers/userProfileController.js @@ -1636,7 +1636,17 @@ const userProfileController = function (UserProfile, Project) { record .save() .then((results) => { - userHelper.notifyInfringements(originalinfringements, results.infringements); + userHelper.notifyInfringements( + originalinfringements, + results.infringements, + results.firstName, + results.lastName, + results.email, + results.role, + results.startDate, + results.jobTitle[0], + results.weeklycommittedHours, + ); res.status(200).json({ _id: record._id, }); @@ -1678,7 +1688,17 @@ const userProfileController = function (UserProfile, Project) { record .save() .then((results) => { - userHelper.notifyInfringements(originalinfringements, results.infringements); + userHelper.notifyInfringements( + originalinfringements, + results.infringements, + results.firstName, + results.lastName, + results.email, + results.role, + results.startDate, + results.jobTitle[0], + results.weeklycommittedHours, + ); res.status(200).json({ _id: record._id, }); @@ -1710,7 +1730,17 @@ const userProfileController = function (UserProfile, Project) { record .save() .then((results) => { - userHelper.notifyInfringements(originalinfringements, results.infringements); + userHelper.notifyInfringements( + originalinfringements, + results.infringements, + results.firstName, + results.lastName, + results.email, + results.role, + results.startDate, + results.jobTitle[0], + results.weeklycommittedHours, + ); res.status(200).json({ _id: record._id, }); From e697b057fe2f491ecdf268cb7f0834a33e00e572 Mon Sep 17 00:00:00 2001 From: Jordy Corporan <95154804+j-corp-25@users.noreply.github.com> Date: Sun, 20 Oct 2024 19:06:06 -0400 Subject: [PATCH 14/14] remove unused variables --- src/controllers/emailController.spec.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/controllers/emailController.spec.js b/src/controllers/emailController.spec.js index 680a1db8c..f5327a328 100644 --- a/src/controllers/emailController.spec.js +++ b/src/controllers/emailController.spec.js @@ -1,13 +1,11 @@ const { mockReq, mockRes, assertResMock } = require('../test'); const emailController = require('./emailController'); const jwt = require('jsonwebtoken'); -const EmailSubcriptionList = require('../models/emailSubcriptionList'); const userProfile = require('../models/userProfile'); -// const emailSender = require('../utilities/emailSender'); + jest.mock('jsonwebtoken'); jest.mock('../models/userProfile'); -jest.mock('../models/emailSubcriptionList'); jest.mock('../utilities/emailSender');