diff --git a/requirements/popupEditorBackupController/createPopupEditorBackup.md b/requirements/popupEditorBackupController/createPopupEditorBackup.md new file mode 100644 index 000000000..bebe0e2c5 --- /dev/null +++ b/requirements/popupEditorBackupController/createPopupEditorBackup.md @@ -0,0 +1,15 @@ +Check mark: ✅ +Cross Mark: ❌ + +# createPopupEditorBackup + +> ## Positive case +1. ✅ Receives a POST request in the **/api/backup/popupeditors/** route. +2. ✅ Return 201 if create new popup successfully. + +> ## Negative case +1. ✅ Returns 403 when no permission. +2. ✅ Returns 400 when missing popupName, popupContent. +3. ✅ Returns 500 when any error in saving. + +> ## Edge case \ No newline at end of file diff --git a/requirements/popupEditorBackupController/getAllPopupEditorBackups.md b/requirements/popupEditorBackupController/getAllPopupEditorBackups.md new file mode 100644 index 000000000..dfcebed3f --- /dev/null +++ b/requirements/popupEditorBackupController/getAllPopupEditorBackups.md @@ -0,0 +1,13 @@ +Check mark: ✅ +Cross Mark: ❌ + +# getAllPopupEditorBackups + +> ## Positive case +1. ✅ Receives a GET request in the **/api/backup/popupeditors/** route. +2. ✅ Return 200 if get all ppopup editor backup successfully. + +> ## Negative case +1. ✅ Returns 400 when catching any error in finding. + +> ## Edge case \ No newline at end of file diff --git a/requirements/popupEditorBackupController/getPopupEditorBackupById.md b/requirements/popupEditorBackupController/getPopupEditorBackupById.md new file mode 100644 index 000000000..0b6e1297a --- /dev/null +++ b/requirements/popupEditorBackupController/getPopupEditorBackupById.md @@ -0,0 +1,13 @@ +Check mark: ✅ +Cross Mark: ❌ + +# getPopupEditorBackupById + +> ## Positive case +1. ✅ Receives a GET request in the **/api/backup/popupeditor/:id** route. +2. ✅ Return 200 if get get PopupEditor Backup ById successfully. + +> ## Negative case +1. ✅ Returns 404 when catching any error in finding by id. + +> ## Edge case \ No newline at end of file diff --git a/requirements/popupEditorBackupController/updatePopupEditorBackup.md b/requirements/popupEditorBackupController/updatePopupEditorBackup.md new file mode 100644 index 000000000..d20ce867a --- /dev/null +++ b/requirements/popupEditorBackupController/updatePopupEditorBackup.md @@ -0,0 +1,17 @@ +Check mark: ✅ +Cross Mark: ❌ + +# updatePopupEditorBackup + +> ## Positive case +1. ✅ Receives a POST request in the **/backup/popupeditor/:id** route. +2. ✅ Return 201 if find popup and update popup successfully. +3. ✅ Return 201 if no find and update popup successfully. + +> ## Negative case +1. ✅ Returns 403 when no permission. +2. ✅ Returns 400 when missing popupName, popupContent. +3. ✅ Returns 500 when any error in finding. +4. ✅ Returns 500 when any error in saving. + +> ## Edge case \ No newline at end of file diff --git a/src/controllers/popupEditorBackupController.js b/src/controllers/popupEditorBackupController.js index 8c4a7349c..daa270955 100644 --- a/src/controllers/popupEditorBackupController.js +++ b/src/controllers/popupEditorBackupController.js @@ -1,28 +1,25 @@ -const { hasPermission } = require('../utilities/permissions'); +const helper = require('../utilities/permissions'); const popupEditorBackupController = function (PopupEditorBackups) { const getAllPopupEditorBackups = function (req, res) { PopupEditorBackups.find() - .then(results => res.status(200).send(results)) - .catch(error => res.status(404).send(error)); + .then((results) => res.status(200).send(results)) + .catch((error) => res.status(404).send(error)); }; - const getPopupEditorBackupById = function (req, res) { + const getPopupEditorBackupById = async function (req, res) { const popupId = req.params.id; try { - PopupEditorBackups.find({ popupId: { $in: popupId } }, (error, popupBackup) => { - res.status(200).send(popupBackup[0]); - }); + const popupBackup = await PopupEditorBackups.findById(popupId); + res.status(200).send(popupBackup); } catch (error) { res.status(404).send(error); } }; const createPopupEditorBackup = async function (req, res) { - if (!await hasPermission(req.body.requestor, 'createPopup')) { - res - .status(403) - .send({ error: 'You are not authorized to create new popup' }); + if (!(await helper.hasPermission(req.body.requestor, 'createPopup'))) { + res.status(403).send({ error: 'You are not authorized to create new popup' }); return; } @@ -38,16 +35,15 @@ const popupEditorBackupController = function (PopupEditorBackups) { popup.popupName = req.body.popupName; popup.popupContent = req.body.popupContent; - popup.save() - .then(results => res.status(201).send(results)) - .catch(error => res.status(500).send({ error })); + popup + .save() + .then((results) => res.status(201).send(results)) + .catch((error) => res.status(500).send({ error })); }; const updatePopupEditorBackup = async function (req, res) { - if (!await hasPermission(req.body.requestor, 'updatePopup')) { - res - .status(403) - .send({ error: 'You are not authorized to create new popup' }); + if (!(await helper.hasPermission(req.body.requestor, 'updatePopup'))) { + res.status(403).send({ error: 'You are not authorized to create new popup' }); return; } @@ -64,15 +60,16 @@ const popupEditorBackupController = function (PopupEditorBackups) { PopupEditorBackups.find({ popupId: { $in: popupId } }, (error, popupBackup) => { if (popupBackup.length > 0) { popupBackup[0].popupContent = req.body.popupContent; - popupBackup[0].save().then(results => res.status(201).send(results)); + popupBackup[0].save().then((results) => res.status(201).send(results)); } else { const popup = new PopupEditorBackups(); popup.popupId = req.params.id; popup.popupContent = req.body.popupContent; popup.popupName = req.body.popupName; - popup.save() - .then(results => res.status(201).send(results)) - .catch(err => res.status(500).send({ err })); + popup + .save() + .then((results) => res.status(201).send(results)) + .catch((err) => res.status(500).send({ err })); } }); } catch (error) { diff --git a/src/controllers/popupEditorBackupController.spec.js b/src/controllers/popupEditorBackupController.spec.js new file mode 100644 index 000000000..4ebd5cefb --- /dev/null +++ b/src/controllers/popupEditorBackupController.spec.js @@ -0,0 +1,388 @@ +const popupEditorBackupController = require('./popupEditorBackupController'); +const { mockReq, mockRes, assertResMock } = require('../test'); +const PopupEditorBackups = require('../models/popupEditorBackup'); +const helper = require('../utilities/permissions'); + +const makeSut = () => { + const { + createPopupEditorBackup, + getAllPopupEditorBackups, + getPopupEditorBackupById, + updatePopupEditorBackup, + } = popupEditorBackupController(PopupEditorBackups); + return { + createPopupEditorBackup, + getAllPopupEditorBackups, + getPopupEditorBackupById, + updatePopupEditorBackup, + }; +}; + +const flushPromises = () => new Promise(setImmediate); +describe('popupEditorBackup Controller', () => { + beforeEach(() => { + jest.clearAllMocks(); + mockReq.params.id = '6437f9af9820a0134ca79c5e'; + }); + describe('createPopupEditorBackup method', () => { + test("Ensure createPopupEditorBackup returns 403 if user doesn't have permissions for createPopup", async () => { + const { createPopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(false)); + + const response = await createPopupEditorBackup(mockReq, mockRes); + + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'createPopup'); + + assertResMock( + 403, + { error: 'You are not authorized to create new popup' }, + response, + mockRes, + ); + }); + test('Ensure createPopupEditorBackup returns 400 if missing popupName', async () => { + const { createPopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupContent: 'some popupContent', + }, + }; + const response = await createPopupEditorBackup(newMockReq, mockRes); + + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'createPopup'); + + assertResMock( + 400, + { + error: 'popupName , popupContent are mandatory fields', + }, + response, + mockRes, + ); + }); + test('Ensure createPopupEditorBackup returns 400 if missing popupContent', async () => { + const { createPopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupName: 'some popupName', + }, + }; + const response = await createPopupEditorBackup(newMockReq, mockRes); + + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'createPopup'); + + assertResMock( + 400, + { + error: 'popupName , popupContent are mandatory fields', + }, + response, + mockRes, + ); + }); + test('Ensure createPopupEditorBackup returns 500 if any error in saving', async () => { + const { createPopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'some popupContent', + }, + }; + jest + .spyOn(PopupEditorBackups.prototype, 'save') + .mockImplementationOnce(() => Promise.reject(new Error('Error when saving'))); + const response = await createPopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'createPopup'); + + assertResMock( + 500, + { + error: new Error('Error when saving'), + }, + response, + mockRes, + ); + }); + test('Ensure createPopupEditorBackup returns 201 if create new popup successfully', async () => { + const { createPopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + const data = { + popupId: 'test randomId334', + popupName: 'testpopupName', + popupContent: 'test popupContent', + }; + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'some popupContent', + }, + }; + jest + .spyOn(PopupEditorBackups.prototype, 'save') + .mockImplementationOnce(() => Promise.resolve(data)); + const response = await createPopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'createPopup'); + + assertResMock(201, data, response, mockRes); + }); + }); + describe('getAllPopupEditorBackups method', () => { + test('Ensure getAllPopupEditorBackup returns 400 if error in finding', async () => { + const { getAllPopupEditorBackups } = makeSut(); + jest + .spyOn(PopupEditorBackups, 'find') + .mockImplementationOnce(() => Promise.reject(new Error('Error when finding'))); + const response = getAllPopupEditorBackups(mockReq, mockRes); + await flushPromises(); + + assertResMock(404, new Error('Error when finding'), response, mockRes); + }); + test('Ensure getAllPopupEditorBackup returns 200 if get all popupeditor backups successfully', async () => { + const { getAllPopupEditorBackups } = makeSut(); + const data = { + poupEditorId: 'randomId345', + }; + jest.spyOn(PopupEditorBackups, 'find').mockImplementationOnce(() => Promise.resolve(data)); + const response = getAllPopupEditorBackups(mockReq, mockRes); + await flushPromises(); + + assertResMock(200, data, response, mockRes); + }); + }); + describe('getPopupEditorBackupById method', () => { + test('Ensure getPopupEditorBackupById returns 404 if error in finding', async () => { + const { getPopupEditorBackupById } = makeSut(); + const findByIdSpy = jest + .spyOn(PopupEditorBackups, 'findById') + .mockImplementationOnce(() => Promise.reject(new Error('Error when finding by id'))); + + const response = await getPopupEditorBackupById(mockReq, mockRes); + await flushPromises(); + assertResMock(404, new Error('Error when finding by id'), response, mockRes); + expect(findByIdSpy).toHaveBeenCalledWith(mockReq.params.id); + }); + test('Ensure getPopupEditorBackupById returns 200 ifget popupEditorBackup by id successfully', async () => { + const { getPopupEditorBackupById } = makeSut(); + const data = { + popupId: 'test randomId334', + popupName: 'testpopupName', + popupContent: 'test popupContent', + }; + const findByIdSpy = jest + .spyOn(PopupEditorBackups, 'findById') + .mockImplementationOnce(() => Promise.resolve(data)); + + const response = await getPopupEditorBackupById(mockReq, mockRes); + await flushPromises(); + expect(findByIdSpy).toHaveBeenCalledWith(mockReq.params.id); + assertResMock(200, data, response, mockRes); + }); + }); + describe('updatePopupEditorBackup method', () => { + test("Ensure updatePopupEditorBackup returns 403 if user doesn't have permissions for createPopup", async () => { + const { updatePopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(false)); + + const response = await updatePopupEditorBackup(mockReq, mockRes); + + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'updatePopup'); + + assertResMock( + 403, + { error: 'You are not authorized to create new popup' }, + response, + mockRes, + ); + }); + test('Ensure updatePopupEditorBackup returns 400 if missing popupContent', async () => { + const { updatePopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: 'randomId334', + popupName: 'some popupName', + }, + }; + + const response = await updatePopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'updatePopup'); + assertResMock(400, { error: 'popupContent is mandatory field' }, response, mockRes); + }); + test('Ensure updatePopupEditorBackup returns 500 if error in finding', async () => { + const { updatePopupEditorBackup } = makeSut(); + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'some popupContent', + }, + }; + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + const findByIdSpy = jest + .spyOn(PopupEditorBackups, 'find') + .mockImplementationOnce((_, cb) => cb(true, null)); + + await updatePopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(mockReq.body.requestor, 'updatePopup'); + expect(findByIdSpy).toHaveBeenCalledWith( + { popupId: { $in: mockReq.params.id } }, + expect.anything(), + ); + expect(mockRes.status).toHaveBeenCalledWith(500); + }); + test('Ensure updatePopupEditorBackup returns 201 if no find and update successfully', async () => { + const { updatePopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'update popupContent', + }, + }; + const data = { + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'some popupContent', + }; + + const findByIdSpy = jest + .spyOn(PopupEditorBackups, 'find') + .mockImplementationOnce((_, cb) => cb(null, [])); + const saveSpy = jest + .spyOn(PopupEditorBackups.prototype, 'save') + .mockImplementationOnce(() => Promise.resolve(data)); + const response = await updatePopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(newMockReq.body.requestor, 'updatePopup'); + expect(findByIdSpy).toHaveBeenCalledWith( + { popupId: { $in: mockReq.params.id } }, + expect.anything(), + ); + expect(saveSpy).toHaveBeenCalledWith(); + assertResMock(201, data, response, mockRes); + }); + test('Ensure updatePopupEditorBackup returns 500 if no find and any error in saving', async () => { + const { updatePopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'some popupContent', + }, + }; + const findByIdSpy = jest + .spyOn(PopupEditorBackups, 'find') + .mockImplementationOnce((_, cb) => cb(null, [])); + const saveSpy = jest + .spyOn(PopupEditorBackups.prototype, 'save') + .mockImplementationOnce(() => Promise.reject(new Error('Error when saving'))); + const response = await updatePopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(newMockReq.body.requestor, 'updatePopup'); + expect(findByIdSpy).toHaveBeenCalledWith( + { popupId: { $in: mockReq.params.id } }, + expect.anything(), + ); + expect(saveSpy).toHaveBeenCalledWith(); + assertResMock( + 500, + { + err: new Error('Error when saving'), + }, + response, + mockRes, + ); + }); + test('Ensure updatePopupEditorBackup returns 201 if find some and update successfully', async () => { + const { updatePopupEditorBackup } = makeSut(); + const hasPermissionSpy = jest + .spyOn(helper, 'hasPermission') + .mockImplementationOnce(() => Promise.resolve(true)); + const newMockReq = { + ...mockReq, + body: { + ...mockReq.body, + popupId: '6437f9af9820a0134ca79c5e', + popupName: 'some popupName', + popupContent: 'update popupContent', + }, + }; + const updateData = { + popupId: 'randomId334', + popupName: 'some popupName', + popupContent: 'update popupContent', + }; + const findData = [ + { + popupId: '6437f9af9820a0134ca79c5e', + popupName: 'some popupName', + popupContent: 'some popupContent', + save: jest.fn().mockImplementationOnce(() => Promise.resolve(updateData)), + }, + ]; + const findByIdSpy = jest + .spyOn(PopupEditorBackups, 'find') + .mockImplementationOnce((_, cb) => cb(null, findData)); + const response = await updatePopupEditorBackup(newMockReq, mockRes); + await flushPromises(); + expect(hasPermissionSpy).toHaveBeenCalledWith(newMockReq.body.requestor, 'updatePopup'); + expect(findByIdSpy).toHaveBeenCalledWith( + { popupId: { $in: mockReq.params.id } }, + expect.anything(), + ); + expect(findData[0].save).toHaveBeenCalledWith(); + assertResMock(201, updateData, response, mockRes); + }); + }); +}); diff --git a/src/controllers/userProfileController.js b/src/controllers/userProfileController.js index 7d0017944..8226aee65 100644 --- a/src/controllers/userProfileController.js +++ b/src/controllers/userProfileController.js @@ -1231,6 +1231,7 @@ const userProfileController = function (UserProfile, Project) { endDate, user.email, recipients, + isSet, ); auditIfProtectedAccountUpdated( req.body.requestor.requestorId, diff --git a/src/helpers/userHelper.js b/src/helpers/userHelper.js index 4fbe3376e..ed9c52131 100644 --- a/src/helpers/userHelper.js +++ b/src/helpers/userHelper.js @@ -2030,9 +2030,16 @@ const userHelper = function () { }); }; - const sendDeactivateEmailBody = function (firstName, lastName, endDate, email, recipients) { - if (endDate) { - const subject = `IMPORTANT:${firstName} ${lastName} has been deactivated in the Highest Good Network`; + const sendDeactivateEmailBody = function ( + firstName, + lastName, + endDate, + email, + recipients, + isSet, + ) { + if (endDate && !isSet) { + const subject = `IMPORTANT: ${firstName} ${lastName} has been deactivated in the Highest Good Network`; const emailBody = `

Management,

Please note that ${firstName} ${lastName} has been made inactive in the Highest Good Network as of ${endDate}. @@ -2040,6 +2047,19 @@ const userHelper = function () {

With Gratitude,

+

One Community

`; + recipients.push('onecommunityglobal@gmail.com'); + recipients = recipients.toString(); + emailSender(recipients, subject, emailBody, null, null, email); + } else if (isSet) { + const subject = `IMPORTANT: ${firstName} ${lastName} has been deactivated in the Highest Good Network`; + const emailBody = `

Management,

+ +

Please note that the final day for ${firstName} ${lastName} has been set in the Highest Good Network ${endDate}. + For a smooth transition, please confirm all your work is being wrapped up with this individual and nothing further will be needed on their part after this date.

+ +

With Gratitude,

+

One Community

`; recipients.push('onecommunityglobal@gmail.com'); recipients = recipients.toString(); @@ -2056,7 +2076,7 @@ const userHelper = function () { const recipients = emailReceivers.map((receiver) => receiver.email); const users = await userProfile.find( { isActive: true, endDate: { $exists: true } }, - '_id isActive endDate', + '_id isActive endDate isSet', ); for (let i = 0; i < users.length; i += 1) { const user = users[i]; @@ -2079,7 +2099,7 @@ const userHelper = function () { const id = user._id; const person = await userProfile.findById(id); const lastDay = moment(person.endDate).format('YYYY-MM-DD'); - logger.logInfo(`User with id: ${user._id} was de-acticated at ${moment().format()}.`); + logger.logInfo(`User with id: ${user._id} was de-activated at ${moment().format()}.`); person.teams.map(async (teamId) => { const managementEmails = await userHelper.getTeamManagementEmail(teamId); if (Array.isArray(managementEmails) && managementEmails.length > 0) { @@ -2094,6 +2114,7 @@ const userHelper = function () { lastDay, person.email, recipients, + person.isSet, ); } } diff --git a/src/routes/popupEditorBackupRouter.test.js b/src/routes/popupEditorBackupRouter.test.js new file mode 100644 index 000000000..4ccd82453 --- /dev/null +++ b/src/routes/popupEditorBackupRouter.test.js @@ -0,0 +1,208 @@ +const request = require('supertest'); +const { jwtPayload } = require('../test'); +const { app } = require('../app'); +const { + mockReq, + createUser, + createRole, + mongoHelper: { dbConnect, dbDisconnect, dbClearCollections, dbClearAll }, +} = require('../test'); +const PopupEditorBackups = require('../models/popupEditorBackup'); + +const agent = request.agent(app); + +describe('PopupEditorBackups routes', () => { + let adminUser; + let adminToken; + let volunteerUser; + let volunteerToken; + let reqBody = { + ...mockReq.body, + }; + beforeAll(async () => { + await dbConnect(); + adminUser = await createUser(); + volunteerUser = await createUser(); + volunteerUser.role = 'Volunteer'; + adminToken = jwtPayload(adminUser); + volunteerToken = jwtPayload(volunteerUser); + // create 2 roles. One with permission and one without + await createRole('Administrator', ['updatePopup', 'createPopup']); + await createRole('Volunteer', []); + }); + beforeEach(async () => { + await dbClearCollections('popupEditorBackup'); + reqBody = { + ...reqBody, + popupId: '6437f9af9820a0134ca79c5e', + popupName: 'popupName', + popupContent: 'some popupContent', + }; + }); + afterAll(async () => { + await dbClearAll(); + await dbDisconnect(); + }); + describe('PopupEditorBackupRoutes', () => { + it('should return 401 if authorization header is not present', async () => { + await agent.post('/api/backup/popupeditors/').send(reqBody).expect(401); + await agent.get('/api/backup/popupeditors/').send(reqBody).expect(401); + await agent.post('/api/backup/popupeditor/randomId').send(reqBody).expect(401); + await agent.get('/api/backup/popupeditor/randomId').send(reqBody).expect(401); + }); + }); + describe('Post PopupEditorBackups route', () => { + it('Should return 403 if user does not have permissions', async () => { + const response = await agent + .post('/api/backup/popupeditors/') + .send(reqBody) + .set('Authorization', volunteerToken) + .expect(403); + expect(response.body).toEqual({ error: 'You are not authorized to create new popup' }); + }); + it('Should return 400 if missing popupName', async () => { + reqBody.popupName = null; + const response = await agent + .post('/api/backup/popupeditors/') + .send(reqBody) + .set('Authorization', adminToken) + .expect(400); + + expect(response.body).toEqual({ + error: 'popupName , popupContent are mandatory fields', + }); + }); + it('Should return 400 if missing popupContent', async () => { + reqBody.popupContent = null; + const response = await agent + .post('/api/backup/popupeditors/') + .send(reqBody) + .set('Authorization', adminToken) + .expect(400); + + expect(response.body).toEqual({ + error: 'popupName , popupContent are mandatory fields', + }); + }); + it('Should return 201 if the PopupEditorBackup is successfully created', async () => { + const _popupEditorBackups = new PopupEditorBackups(); + _popupEditorBackups.popupId = reqBody.popupId; + _popupEditorBackups.popupName = reqBody.popupName; + _popupEditorBackups.popupContent = reqBody.popupContent; + const popupEditorBackups = await _popupEditorBackups.save(); + const response = await agent + .post('/api/backup/popupeditors/') + .send(reqBody) + .set('Authorization', adminToken) + .expect(201); + + expect(response.body).toEqual({ + _id: expect.anything(), + __v: expect.anything(), + popupId: popupEditorBackups.popupId.toString(), + popupName: popupEditorBackups.popupName, + popupContent: popupEditorBackups.popupContent, + }); + }); + }); + describe('getAllPopupEditorBackups route', () => { + it('Should return 201 if the get AllPopupEditorBackups successfully ', async () => { + const _popupEditorBackups = new PopupEditorBackups(); + _popupEditorBackups.popupName = reqBody.popupName; + _popupEditorBackups.popupContent = reqBody.popupContent; + _popupEditorBackups.popupId = reqBody.popupId; + const popupEditorBackups = await _popupEditorBackups.save(); + const response = await agent + .get('/api/backup/popupeditors/') + .set('Authorization', adminToken) + .expect(200); + + expect(response.body).toEqual([ + { + _id: expect.anything(), + __v: expect.anything(), + popupId: popupEditorBackups.popupId.toString(), + popupName: popupEditorBackups.popupName, + popupContent: popupEditorBackups.popupContent, + }, + ]); + }); + }); + describe('getPopupEditorBackupById route', () => { + it('Should return 201 if the get PopupEditorBackup by id is successfully created', async () => { + const _popupEditorBackups = new PopupEditorBackups(); + _popupEditorBackups.popupId = reqBody.popupId; + _popupEditorBackups.popupName = reqBody.popupName; + _popupEditorBackups.popupContent = reqBody.popupContent; + const popupEditorBackups = await _popupEditorBackups.save(); + + const response = await agent + .get(`/api/backup/popupeditor/${popupEditorBackups._id}`) + .set('Authorization', adminToken) + .expect(200); + expect(response.body).toEqual({ + _id: expect.anything(), + __v: expect.anything(), + popupId: popupEditorBackups.popupId.toString(), + popupName: popupEditorBackups.popupName, + popupContent: popupEditorBackups.popupContent, + }); + }); + }); + describe('updatePopupEditorBackup route', () => { + it('Should return 403 if user does not have permissions', async () => { + const response = await agent + .post('/api/backup/popupeditor/randomId334') + .send(reqBody) + .set('Authorization', volunteerToken) + .expect(403); + expect(response.body).toEqual({ error: 'You are not authorized to create new popup' }); + }); + it('Should return 400 if missing popupContent', async () => { + reqBody.popupContent = null; + const response = await agent + .post(`/api/backup/popupeditor/${reqBody.popupId}`) + .send(reqBody) + .set('Authorization', adminToken) + .expect(400); + + expect(response.body).toEqual({ + error: 'popupContent is mandatory field', + }); + }); + it('Should return 201 if find and update the PopupEditorBackup successfully', async () => { + const _popupEditorBackups = new PopupEditorBackups(); + _popupEditorBackups.popupId = reqBody.popupId; + _popupEditorBackups.popupName = reqBody.popupName; + _popupEditorBackups.popupContent = 'some content'; + const popupEditorBackups = await _popupEditorBackups.save(); + const response = await agent + .post(`/api/backup/popupeditor/${popupEditorBackups.popupId}`) + .send(reqBody) + .set('Authorization', adminToken) + .expect(201); + + expect(response.body).toEqual({ + _id: expect.anything(), + __v: expect.anything(), + popupId: popupEditorBackups.popupId.toString(), + popupName: popupEditorBackups.popupName, + popupContent: reqBody.popupContent, + }); + }); + it('Should return 201 if the no find and create PopupEditorBackup successfully', async () => { + const response = await agent + .post(`/api/backup/popupeditor/${reqBody.popupId}`) + .send(reqBody) + .set('Authorization', adminToken) + .expect(201); + expect(response.body).toEqual({ + _id: expect.anything(), + __v: expect.anything(), + popupId: reqBody.popupId.toString(), + popupName: reqBody.popupName, + popupContent: reqBody.popupContent, + }); + }); + }); +});