Skip to content

Commit

Permalink
Merge pull request #1032 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.86]
  • Loading branch information
one-community authored Jul 18, 2024
2 parents 790aff3 + 23112ce commit 34ba186
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 25 deletions.
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions requirements/forgotPwdController/postForgotPwd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Check mark: ✅
Cross Mark: ❌

# Post Forgot Pwd

> ## Positive case
1. ✅ Receives a POST request in the **/api/forgotpassword** route.
2. ✅ Returns **200** if successfully temporary password generated.

> ## Negative case
1. ✅ Returns error 404 if the API does not exist.
2. ✅ Returns 400 user does not exists in database.
3. ✅ Returns 500 if error encountered fetching user details from database.
4. ✅ Returns 500 if error encountered while saving temporary password.
183 changes: 183 additions & 0 deletions src/controllers/forgotPwdcontroller.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
jest.mock('uuid/v4');
jest.mock('../utilities/emailSender');

const uuidv4 = require('uuid/v4');
const emailSender = require('../utilities/emailSender');
const { mockReq, mockRes, assertResMock } = require('../test');
const forgotPwdController = require('./forgotPwdcontroller');
const UserProfile = require('../models/userProfile');
const escapeRegex = require('../utilities/escapeRegex');

uuidv4.mockReturnValue('');
emailSender.mockImplementation(() => undefined);

const flushPromises = () => new Promise(setImmediate);

// Positive
// ✅ Return 200 if successfully generated temporary User password.

// Negative
// ✅ Return 500 if any error encountered while fetching User details.
// ✅ Return 500 if any error encountered while saving User's password.
// ✅ Return 400 if valid user not found.

function getEmailMessageForForgotPassword(user, ranPwd) {
const message = `<b> Hello ${user.firstName} ${user.lastName},</b>
<p>Congratulations on successfully completing the Highest Good Network 3-question Change My Password Challenge. Your reward is this NEW PASSWORD! </p>
<blockquote> ${ranPwd}</blockquote>
<p>Use it now to log in. Then store it in a safe place or change it on your Profile Page to something easier for you to remember. </p>
<p>If it wasn’t you that requested this password change, you can ignore this email. Otherwise, use the password above to log in and you’ll be directed to the “Change Password” page where you can set a new custom one. </p>
<p>Thank you,<p>
<p>One Community</p>`;
return message;
}

const makeSut = () => {
const { forgotPwd } = forgotPwdController(UserProfile);
return { forgotPwd };
};

describe('Unit Tests for forgotPwdcontroller.js', () => {
beforeAll(() => {
mockReq.body.email = '[email protected]';
mockReq.body.firstName = 'Parth';
mockReq.body.lastName = 'Jangid';
});

afterEach(() => {
jest.clearAllMocks();
});

describe('Forgot Pwd Function', () => {
test('Returns 500 if any error encountered while fetching user.', async () => {
const { forgotPwd } = makeSut();

const error = new Error('Database error');
const findOneSpy = jest.spyOn(UserProfile, 'findOne').mockRejectedValueOnce(error);

const response = await forgotPwd(mockReq, mockRes);

assertResMock(500, error, response, mockRes);
expect(findOneSpy).toHaveBeenCalledWith({
// Check Parameters to findOne
email: {
$regex: escapeRegex(mockReq.body.email),
$options: 'i',
},
firstName: {
$regex: escapeRegex(mockReq.body.firstName),
$options: 'i',
},
lastName: {
$regex: escapeRegex(mockReq.body.lastName),
$options: 'i',
},
});
});

test('Returns 400 if No Valid User found', async () => {
const { forgotPwd } = makeSut();

const userObject = null; // or undefined
const error = { error: 'No Valid user was found' };

const findOneSpy = jest.spyOn(UserProfile, 'findOne').mockResolvedValueOnce(userObject);

const response = await forgotPwd(mockReq, mockRes);

assertResMock(400, error, response, mockRes);

expect(findOneSpy).toHaveBeenCalledWith({
email: {
$regex: escapeRegex(mockReq.body.email),
$options: 'i',
},
firstName: {
$regex: escapeRegex(mockReq.body.firstName),
$options: 'i',
},
lastName: {
$regex: escapeRegex(mockReq.body.lastName),
$options: 'i',
},
});
});

test('Return 500 if encountered any error while saving temporary password', async () => {
const { forgotPwd } = makeSut();

const error = new Error('Error Saving User Details');

const mockUser = {
set: jest.fn(), // Mocking the set method
save: jest.fn().mockRejectedValueOnce(error), // Mocked below using spyOn
};

const findOneSpy = jest.spyOn(UserProfile, 'findOne').mockResolvedValueOnce(mockUser);

const response = await forgotPwd(mockReq, mockRes);
await flushPromises();
expect(mockUser.set).toHaveBeenCalled();
expect(mockUser.save).toHaveBeenCalled();
assertResMock(500, error, response, mockRes);
expect(findOneSpy).toHaveBeenCalledWith({
email: {
$regex: escapeRegex(mockReq.body.email),
$options: 'i',
},
firstName: {
$regex: escapeRegex(mockReq.body.firstName),
$options: 'i',
},
lastName: {
$regex: escapeRegex(mockReq.body.lastName),
$options: 'i',
},
});
});

test('Return 200 if a temporary password is generated for the user', async () => {
const { forgotPwd } = makeSut();

const mockUser = {
// denote the User object obtained by find operation on MongoDB
email: mockReq.body.email,
firstName: mockReq.body.firstName,
lastName: mockReq.body.lastName,
set: jest.fn(), // Mocking the set method
save: jest.fn().mockResolvedValueOnce(), // Mocking the save method
};

const message = { message: 'generated new password' };
const findOneSpy = jest.spyOn(UserProfile, 'findOne').mockResolvedValueOnce(mockUser);

const response = await forgotPwd(mockReq, mockRes);
const temporaryPassword = uuidv4().concat('TEMP'); // The source code appends "TEMP" so does this line

expect(mockUser.set).toHaveBeenCalled();
expect(mockUser.save).toHaveBeenCalled();
expect(emailSender).toHaveBeenCalledWith(
mockUser.email,
'Account Password change',
getEmailMessageForForgotPassword(mockUser, temporaryPassword),
null,
null,
);
assertResMock(200, message, response, mockRes);
expect(findOneSpy).toHaveBeenCalledWith({
email: {
$regex: escapeRegex(mockReq.body.email),
$options: 'i',
},
firstName: {
$regex: escapeRegex(mockReq.body.firstName),
$options: 'i',
},
lastName: {
$regex: escapeRegex(mockReq.body.lastName),
$options: 'i',
},
});
});
});
});
Loading

0 comments on commit 34ba186

Please sign in to comment.