From 0d266ff44671e079d3dc9a3b306bc13732afeeb0 Mon Sep 17 00:00:00 2001 From: Patrick Hoang Date: Wed, 11 Jun 2025 14:18:32 -0700 Subject: [PATCH 1/3] =?UTF-8?q?Add=20test=20verifying=20login=20succeeds?= =?UTF-8?q?=20with=20correct=20credentials=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/api/Auth.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/api/Auth.js b/test/api/Auth.js index 0445d88cc..37b07d445 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -17,6 +17,8 @@ const { } = require('../../api/util/constants').STATUS_CODES; const SceApiTester = require('../util/tools/SceApiTester'); +const {decodeToken} = require('../../api/main_endpoints/util/token-functions.js') + let app = null; let test = null; let sandbox = sinon.createSandbox(); @@ -171,6 +173,56 @@ describe('Auth', () => { '/api/Auth/login', user); expect(result).to.have.status(UNAUTHORIZED); }); + + it('Should allow login with correct credentials after registering a user', async () => { + const user = { + email: 'logintest@gmail.com', + password: 'ValidTestPass123!', + firstName: 'Test', + lastName: 'User' + } + + // register the user first + const registerUser = await test.sendPostRequest('/api/Auth/register', user); + expect(registerUser).to.have.status(OK); + + // verify the email + await User.updateOne({email: user.email}, {$set: {emailVerified: true}}) + + // then try logging in with the same credentials + const loginUser = await test.sendPostRequest('/api/Auth/login', { + email: user.email, + password: user.password + }); + + expect(loginUser).to.have.status(OK); + expect(loginUser.body).to.have.property('token'); + + const token = loginUser.body.token; + expect(token).to.be.a('string'); + expect(token.startsWith('JWT ')).to.be.true; + + const mockRequest = { + headers: { + authorization: `Bearer ${token}` + } + }; + + const decodedPayload = decodeToken(mockRequest); + const expectedPayload = { + firstName: user.firstName, + lastName: user.lastName, + email: user.email, + accessLevel: decodedPayload.accessLevel, + pagesPrinted: decodedPayload.pagesPrinted, + _id: decodedPayload._id, + iat: decodedPayload.iat, + exp: decodedPayload.exp + } + + expect(decodedPayload).to.deep.equal(expectedPayload) + + }) }); describe('/POST sendPasswordReset', () => { From 8e5fecd11de165de90d9444cafb867869129a541 Mon Sep 17 00:00:00 2001 From: Patrick Hoang Date: Thu, 12 Jun 2025 21:42:18 -0700 Subject: [PATCH 2/3] update expectedPayload for readability --- test/api/Auth.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/api/Auth.js b/test/api/Auth.js index 37b07d445..385e0f5fe 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -210,14 +210,14 @@ describe('Auth', () => { const decodedPayload = decodeToken(mockRequest); const expectedPayload = { - firstName: user.firstName, - lastName: user.lastName, - email: user.email, - accessLevel: decodedPayload.accessLevel, - pagesPrinted: decodedPayload.pagesPrinted, + firstName: 'Test', + lastName: 'User', + email: 'logintest@gmail.com', + accessLevel: MEMBERSHIP_STATE.PENDING, + pagesPrinted: 0, _id: decodedPayload._id, iat: decodedPayload.iat, - exp: decodedPayload.exp + exp: decodedPayload.exp, } expect(decodedPayload).to.deep.equal(expectedPayload) From 1423f72f90ae328932e7a96c0d866220bd3df5be Mon Sep 17 00:00:00 2001 From: Patrick Hoang Date: Mon, 16 Jun 2025 09:10:33 -0700 Subject: [PATCH 3/3] add functionality to remove user after test + lint fixes --- test/api/Auth.js | 85 +++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/test/api/Auth.js b/test/api/Auth.js index 385e0f5fe..95ca5bc06 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -17,7 +17,7 @@ const { } = require('../../api/util/constants').STATUS_CODES; const SceApiTester = require('../util/tools/SceApiTester'); -const {decodeToken} = require('../../api/main_endpoints/util/token-functions.js') +const {decodeToken} = require('../../api/main_endpoints/util/token-functions.js'); let app = null; let test = null; @@ -180,49 +180,52 @@ describe('Auth', () => { password: 'ValidTestPass123!', firstName: 'Test', lastName: 'User' - } - - // register the user first - const registerUser = await test.sendPostRequest('/api/Auth/register', user); - expect(registerUser).to.have.status(OK); - - // verify the email - await User.updateOne({email: user.email}, {$set: {emailVerified: true}}) - - // then try logging in with the same credentials - const loginUser = await test.sendPostRequest('/api/Auth/login', { - email: user.email, - password: user.password - }); - - expect(loginUser).to.have.status(OK); - expect(loginUser.body).to.have.property('token'); - - const token = loginUser.body.token; - expect(token).to.be.a('string'); - expect(token.startsWith('JWT ')).to.be.true; - - const mockRequest = { - headers: { - authorization: `Bearer ${token}` - } }; - const decodedPayload = decodeToken(mockRequest); - const expectedPayload = { - firstName: 'Test', - lastName: 'User', - email: 'logintest@gmail.com', - accessLevel: MEMBERSHIP_STATE.PENDING, - pagesPrinted: 0, - _id: decodedPayload._id, - iat: decodedPayload.iat, - exp: decodedPayload.exp, + try { + // register the user first + const registerUser = await test.sendPostRequest('/api/Auth/register', user); + expect(registerUser).to.have.status(OK); + + // verify the email + await User.updateOne({email: user.email}, {$set: {emailVerified: true}}); + + // then try logging in with the same credentials + const loginUser = await test.sendPostRequest('/api/Auth/login', { + email: user.email, + password: user.password + }); + + expect(loginUser).to.have.status(OK); + expect(loginUser.body).to.have.property('token'); + + const token = loginUser.body.token; + expect(token).to.be.a('string'); + expect(token.startsWith('JWT ')).to.be.true; + + const mockRequest = { + headers: { + authorization: `Bearer ${token}` + } + }; + + const decodedPayload = decodeToken(mockRequest); + const expectedPayload = { + firstName: 'Test', + lastName: 'User', + email: 'logintest@gmail.com', + accessLevel: MEMBERSHIP_STATE.PENDING, + pagesPrinted: 0, + _id: decodedPayload._id, + iat: decodedPayload.iat, + exp: decodedPayload.exp, + }; + + expect(decodedPayload).to.deep.equal(expectedPayload); + } finally { + await User.deleteOne({email: user.email}); } - - expect(decodedPayload).to.deep.equal(expectedPayload) - - }) + }); }); describe('/POST sendPasswordReset', () => {