From 6a6ce5bc104053ed78ff8775f58f092dc2b65f08 Mon Sep 17 00:00:00 2001 From: Brendan Ly Date: Tue, 23 Jul 2024 21:36:40 -0700 Subject: [PATCH 1/4] removed sendVerificationEmail --- api/cloud_api/routes/Mailer.js | 41 ------------------- api/cloud_api/util/auth.js | 1 - api/main_endpoints/routes/Auth.js | 4 +- api/main_endpoints/util/emailHelpers.js | 14 +------ src/APIFunctions/Mailer.js | 32 --------------- .../MembershipApplication/VerifyEmail.js | 1 - src/Pages/UserManager/EditUserInfo.js | 2 - test/api/Auth.js | 1 - test/api/Mailer.js | 25 ----------- 9 files changed, 2 insertions(+), 119 deletions(-) diff --git a/api/cloud_api/routes/Mailer.js b/api/cloud_api/routes/Mailer.js index 2fa2561f5..4afcf4b8f 100644 --- a/api/cloud_api/routes/Mailer.js +++ b/api/cloud_api/routes/Mailer.js @@ -14,47 +14,6 @@ const { googleApiKeys } = require('../../config/config.json'); const { USER, ENABLED } = googleApiKeys; const { MetricsHandler } = require('../../util/metrics'); -// Routing post /sendVerificationEmail calls the sendEmail function -// and sends the verification email with the verification email template -router.post('/sendVerificationEmail', async (req, res) => { - if (!ENABLED && process.env.NODE_ENV !== 'test') { - return res.sendStatus(OK); - } - const scopes = ['https://mail.google.com/']; - const pathToToken = __dirname + '/../../config/token.json'; - const apiHandler = new SceGoogleApiHandler(scopes, pathToToken); - const tokenJson = await apiHandler.checkIfTokenFileExists(); - - if (tokenJson) { - if (apiHandler.checkIfTokenIsExpired(tokenJson)) { - logger.warn('refreshing token'); - apiHandler.refreshToken(); - } - } else { - logger.warn('getting new token! ', { tokenJson }); - apiHandler.getNewToken(); - } - - - await verification(USER, req.body.recipientEmail, req.body.recipientName) - .then((template) => { - apiHandler - .sendEmail(template) - .then((_) => { - res.sendStatus(OK); - MetricsHandler.emailSent.inc({ type: 'verification' }); - }) - .catch((err) => { - logger.error('unable to send verification email:', err); - res.sendStatus(BAD_REQUEST); - }); - }) - .catch((err) => { - logger.error('unable to generate verification template:', err); - res.sendStatus(BAD_REQUEST); - }); -}); - // Routing post /sendPasswordReset calls the sendEmail function // and sends the email with the password reset template router.post('/sendPasswordReset', async (req, res) => { diff --git a/api/cloud_api/util/auth.js b/api/cloud_api/util/auth.js index 79c04dacd..3d57ec5e5 100644 --- a/api/cloud_api/util/auth.js +++ b/api/cloud_api/util/auth.js @@ -5,7 +5,6 @@ const GENERAL_API_URL = process.env.GENERAL_API_URL async function validateVerificationEmail(){ let status = ''; await axios - .post(`${GENERAL_API_URL}/Auth/sendVerificationEmail`) .then(res =>{ status = res.data; }) diff --git a/api/main_endpoints/routes/Auth.js b/api/main_endpoints/routes/Auth.js index 23a6de047..598552429 100644 --- a/api/main_endpoints/routes/Auth.js +++ b/api/main_endpoints/routes/Auth.js @@ -25,7 +25,7 @@ const { CONFLICT } = require('../../util/constants').STATUS_CODES; const membershipState = require('../../util/constants').MEMBERSHIP_STATE; -const { sendVerificationEmail, sendPasswordReset } = require('../util/emailHelpers'); +const { sendPasswordReset } = require('../util/emailHelpers'); const { userWithEmailExists, checkIfPageCountResets, findPasswordReset } = require('../util/userHelpers'); // Register a member @@ -41,7 +41,6 @@ router.post('/register', async (req, res) => { } } else { const name = req.body.firstName + ' ' + req.body.lastName; - sendVerificationEmail(name, req.body.email); res.sendStatus(OK); } }); @@ -57,7 +56,6 @@ router.post('/resendVerificationEmail', async (req, res) => { return res.sendStatus(NOT_FOUND); } let name = maybeUser.firstName + ' ' + maybeUser.lastName; - sendVerificationEmail(name, req.body.email); res.sendStatus(OK); }); diff --git a/api/main_endpoints/util/emailHelpers.js b/api/main_endpoints/util/emailHelpers.js index f9251f951..18d5d749b 100644 --- a/api/main_endpoints/util/emailHelpers.js +++ b/api/main_endpoints/util/emailHelpers.js @@ -16,18 +16,6 @@ async function sendUnsubscribeEmail(users) { return status; } -async function sendVerificationEmail(name, email) { - return new Promise((resolve) => { - axios - .post(`${MAILER_API_URL}/Mailer/sendVerificationEmail`, { - recipientName: name, - recipientEmail: email - }) - .then(() => resolve(true)) - .catch(() => resolve(false)); - }); -} - async function sendPasswordReset(resetToken, email) { return new Promise((resolve) => { axios @@ -40,4 +28,4 @@ async function sendPasswordReset(resetToken, email) { }); } -module.exports = { sendUnsubscribeEmail, sendVerificationEmail, sendPasswordReset }; +module.exports = { sendUnsubscribeEmail, sendPasswordReset }; diff --git a/src/APIFunctions/Mailer.js b/src/APIFunctions/Mailer.js index 79d513f81..17cf2ab4b 100644 --- a/src/APIFunctions/Mailer.js +++ b/src/APIFunctions/Mailer.js @@ -2,38 +2,6 @@ import axios from 'axios'; import { ApiResponse } from './ApiResponses'; import { BASE_API_URL } from '../Enums'; -/** - * Invoke the gmail API to send an email to verify a user. - * @param {string} email - The user's email - * @param {string} firstName - The user's first name - * @returns {ApiResponse} Containing any error information related to the - * request - */ -export async function sendVerificationEmail(email, token) { - let status = new ApiResponse(); - const url = new URL('/cloudapi/Auth/sendVerificationEmail', BASE_API_URL); - await axios - .post( - url.href, - { - email - }, - { - headers: { - Authorization: `Bearer ${token}` - } - }, - ) - .then((response) => { - status.responseData = response; - }) - .catch((error) => { - status.error = true; - status.responseData = error; - }); - return status; -} - /** * Invoke the gmail API to send an email to password reset a user. * @param {string} email - The user's email diff --git a/src/Pages/MembershipApplication/VerifyEmail.js b/src/Pages/MembershipApplication/VerifyEmail.js index 885bf21b8..2c3dfeee3 100644 --- a/src/Pages/MembershipApplication/VerifyEmail.js +++ b/src/Pages/MembershipApplication/VerifyEmail.js @@ -1,5 +1,4 @@ import React from 'react'; -import { sendVerificationEmail } from '../../APIFunctions/Mailer'; import { validateVerificationEmail } from '../../APIFunctions/Auth'; export default class VerifyEmail extends React.Component { diff --git a/src/Pages/UserManager/EditUserInfo.js b/src/Pages/UserManager/EditUserInfo.js index 7ce52651a..256c1b141 100644 --- a/src/Pages/UserManager/EditUserInfo.js +++ b/src/Pages/UserManager/EditUserInfo.js @@ -8,7 +8,6 @@ import MajorDropdown from '../MembershipApplication/MajorDropdown'; import RoleDropdown from './RoleDropdown'; import ExpirationDropdown from './ExpirationDropdown'; import { membershipState, membershipStateToString } from '../../Enums'; -import { sendVerificationEmail } from '../../APIFunctions/Mailer'; export default function EditUserInfo(props) { @@ -293,7 +292,6 @@ export default function EditUserInfo(props) { className="btn btn-success w-auto" checked={emailOptIn} onClick={async () => { - const result = await sendVerificationEmail(email, props.user.token); if (result.error) { return alert( 'unable to send verification email.' + diff --git a/test/api/Auth.js b/test/api/Auth.js index 76425f4ac..2147a433c 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -42,7 +42,6 @@ describe('Auth', () => { before(done => { sendVerificationEmailStub = sandbox.stub( EmailHelpers, - 'sendVerificationEmail', ); initializeTokenMock(); app = tools.initializeServer(__dirname + diff --git a/test/api/Mailer.js b/test/api/Mailer.js index d2f84a731..b6e3950d5 100644 --- a/test/api/Mailer.js +++ b/test/api/Mailer.js @@ -44,31 +44,6 @@ describe('Mailer', () => { recipientName: 'test' }; - describe('/POST sendVerificationEmail', () => { - it('Should return 200 when an email is successfully sent', async () => { - sendEmailStub.resolves({}); - verificationStub.resolves({}); - const result = await test.sendPostRequest( - '/api/Mailer/sendVerificationEmail', VALID_EMAIL_REQUEST); - expect(result).to.have.status(OK); - }); - - it('Should return 400 when we cannot generate a hashed ID', async () => { - sendEmailStub.resolves({}); - verificationStub.rejects({}); - const result = await test.sendPostRequest( - '/api/Mailer/sendVerificationEmail', VALID_EMAIL_REQUEST); - expect(result).to.have.status(BAD_REQUEST); - }); - - it('Should return 400 when sending an email fails', async () => { - sendEmailStub.rejects({}); - const result = await test.sendPostRequest( - '/api/Mailer/sendVerificationEmail', VALID_EMAIL_REQUEST); - expect(result).to.have.status(BAD_REQUEST); - }); - }); - describe('/POST sendBlastEmail', () => { it('Should return 200 when an email is successfully sent', async () => { sendEmailStub.resolves({}); From cb39771f42d109bfee8fd5a6a4e0c2e0acd8d25d Mon Sep 17 00:00:00 2001 From: Brendan Ly Date: Tue, 23 Jul 2024 22:47:53 -0700 Subject: [PATCH 2/4] test --- test/api/Auth.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/api/Auth.js b/test/api/Auth.js index 2147a433c..fa3570163 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -42,6 +42,7 @@ describe('Auth', () => { before(done => { sendVerificationEmailStub = sandbox.stub( EmailHelpers, + 'sendVerificationEmail' ); initializeTokenMock(); app = tools.initializeServer(__dirname + From d455fca7c194ea8c746282384d6f2dd25a2eadef Mon Sep 17 00:00:00 2001 From: Brendan Ly Date: Tue, 23 Jul 2024 22:58:16 -0700 Subject: [PATCH 3/4] edit auth tests --- test/api/Auth.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/test/api/Auth.js b/test/api/Auth.js index fa3570163..746c08615 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -4,7 +4,6 @@ const mongoose = require('mongoose'); const bcrypt = require('bcryptjs'); const User = require('../../api/main_endpoints/models/User'); const PasswordReset = require('../../api/main_endpoints/models/PasswordReset'); -const EmailHelpers = require('../../api/main_endpoints/util/emailHelpers'); // Require the dev-dependencies const chai = require('chai'); const chaiHttp = require('chai-http'); @@ -38,12 +37,7 @@ chai.use(chaiHttp); // Our parent block describe('Auth', () => { - let sendVerificationEmailStub = null; before(done => { - sendVerificationEmailStub = sandbox.stub( - EmailHelpers, - 'sendVerificationEmail' - ); initializeTokenMock(); app = tools.initializeServer(__dirname + '/../../api/main_endpoints/routes/Auth.js'); @@ -54,13 +48,11 @@ describe('Auth', () => { }); after(done => { - if(sendVerificationEmailStub) sendVerificationEmailStub.restore(); restoreTokenMock(); tools.terminateServer(done); }); beforeEach(() => { - if(sendVerificationEmailStub) sendVerificationEmailStub.reset(); setTokenStatus(false); }); @@ -95,7 +87,6 @@ describe('Auth', () => { }; const result = await test.sendPostRequest( '/api/Auth/register', user); - expect(sendVerificationEmailStub.called).to.be.false; expect(result).to.have.status(CONFLICT); }); it('Should not allow registration with a password without' + @@ -108,7 +99,6 @@ describe('Auth', () => { }; const result = await test.sendPostRequest( '/api/Auth/register', user); - expect(sendVerificationEmailStub.called).to.be.false; expect(result).to.have.status(BAD_REQUEST); }); @@ -122,7 +112,6 @@ describe('Auth', () => { }; const result = await test.sendPostRequest( '/api/Auth/register', user); - expect(sendVerificationEmailStub.called).to.be.false; expect(result).to.have.status(BAD_REQUEST); }); it('Should send a verification email after user signs up', async () => { @@ -134,9 +123,7 @@ describe('Auth', () => { }; const result = await test.sendPostRequest( '/api/Auth/register', user); - expect(sendVerificationEmailStub.called).to.be.true; - const verificationArgs = sendVerificationEmailStub.getCall(-1).args; - expect(verificationArgs).to.eql([user.firstName + ' ' + user.lastName, user.email]); + expect(result).to.have.status(OK); }); }); From 4ae8b657dabc40d787aead9b9e785387b716255e Mon Sep 17 00:00:00 2001 From: Brendan Ly Date: Tue, 23 Jul 2024 23:02:05 -0700 Subject: [PATCH 4/4] fix --- test/api/Auth.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/api/Auth.js b/test/api/Auth.js index 746c08615..6b8b003e6 100644 --- a/test/api/Auth.js +++ b/test/api/Auth.js @@ -123,7 +123,6 @@ describe('Auth', () => { }; const result = await test.sendPostRequest( '/api/Auth/register', user); - expect(result).to.have.status(OK); }); });