Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed sendVerificationEmail #1479

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions api/cloud_api/routes/Mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 0 additions & 1 deletion api/cloud_api/util/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
Expand Down
4 changes: 1 addition & 3 deletions api/main_endpoints/routes/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
});
Expand All @@ -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);
});

Expand Down
14 changes: 1 addition & 13 deletions api/main_endpoints/util/emailHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,4 +28,4 @@ async function sendPasswordReset(resetToken, email) {
});
}

module.exports = { sendUnsubscribeEmail, sendVerificationEmail, sendPasswordReset };
module.exports = { sendUnsubscribeEmail, sendPasswordReset };
32 changes: 0 additions & 32 deletions src/APIFunctions/Mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/Pages/MembershipApplication/VerifyEmail.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/Pages/UserManager/EditUserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.' +
Expand Down
14 changes: 0 additions & 14 deletions test/api/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -54,13 +48,11 @@ describe('Auth', () => {
});

after(done => {
if(sendVerificationEmailStub) sendVerificationEmailStub.restore();
restoreTokenMock();
tools.terminateServer(done);
});

beforeEach(() => {
if(sendVerificationEmailStub) sendVerificationEmailStub.reset();
setTokenStatus(false);
});

Expand Down Expand Up @@ -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' +
Expand All @@ -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);
});

Expand All @@ -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 () => {
Expand All @@ -134,9 +123,6 @@ 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);
});
});
Expand Down
25 changes: 0 additions & 25 deletions test/api/Mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down
Loading