From f076daa76a32074cba162459e38fa8c130ad42d0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 30 Jan 2018 15:16:24 +0100 Subject: Add tests for emails --- server/controllers/api/users.ts | 26 +++++++--- server/lib/emailer.ts | 14 +++-- server/middlewares/validators/users.ts | 2 +- server/tests/api/check-params/users.ts | 22 ++++++++ server/tests/api/index-fast.ts | 1 + server/tests/api/server/email.ts | 94 ++++++++++++++++++++++++++++++++++ server/tests/utils/miscs/email.ts | 25 +++++++++ server/tests/utils/users/users.ts | 26 +++++++++- 8 files changed, 198 insertions(+), 12 deletions(-) create mode 100644 server/tests/api/server/email.ts create mode 100644 server/tests/utils/miscs/email.ts (limited to 'server') diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 05639fbec..6e5d09695 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -6,21 +6,35 @@ import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRat import { unlinkPromise } from '../../helpers/core-utils' import { retryTransactionWrapper } from '../../helpers/database-utils' import { logger } from '../../helpers/logger' -import { createReqFiles, generateRandomString, getFormattedObjects } from '../../helpers/utils' +import { createReqFiles, getFormattedObjects } from '../../helpers/utils' import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers' import { updateActorAvatarInstance } from '../../lib/activitypub' import { sendUpdateUser } from '../../lib/activitypub/send' import { Emailer } from '../../lib/emailer' -import { EmailPayload } from '../../lib/job-queue/handlers/email' import { Redis } from '../../lib/redis' import { createUserAccountAndChannel } from '../../lib/user' import { - asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort, - setDefaultPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator, - usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator + asyncMiddleware, + authenticate, + ensureUserHasRight, + ensureUserRegistrationAllowed, + paginationValidator, + setDefaultPagination, + setDefaultSort, + token, + usersAddValidator, + usersGetValidator, + usersRegisterValidator, + usersRemoveValidator, + usersSortValidator, + usersUpdateMeValidator, + usersUpdateValidator, + usersVideoRatingValidator } from '../../middlewares' import { - usersAskResetPasswordValidator, usersResetPasswordValidator, usersUpdateMyAvatarValidator, + usersAskResetPasswordValidator, + usersResetPasswordValidator, + usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators' import { AccountVideoRateModel } from '../../models/account/account-video-rate' diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index f5b68640e..317cec706 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -29,15 +29,21 @@ class Emailer { } } + let auth + if (CONFIG.SMTP.USERNAME && CONFIG.SMTP.PASSWORD) { + auth = { + user: CONFIG.SMTP.USERNAME, + pass: CONFIG.SMTP.PASSWORD + } + } + this.transporter = createTransport({ host: CONFIG.SMTP.HOSTNAME, port: CONFIG.SMTP.PORT, secure: CONFIG.SMTP.TLS, + ignoreTLS: isTestInstance(), tls, - auth: { - user: CONFIG.SMTP.USERNAME, - pass: CONFIG.SMTP.PASSWORD - } + auth }) } else { if (!isTestInstance()) { diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 5f44c3b99..cba15c8d3 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -210,7 +210,7 @@ const usersResetPasswordValidator = [ return res .status(403) .send({ error: 'Invalid verification string.' }) - .end + .end() } return next() diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index 28fefe79f..0fbc414c9 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -541,6 +541,28 @@ describe('Test users API validators', function () { }) }) + describe('When asking a password reset', function () { + const path = '/api/v1/users/ask-reset-password' + + it('Should fail with a missing email', async function () { + const fields = {} + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should fail with an invalid email', async function () { + const fields = { email: 'hello' } + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields }) + }) + + it('Should success with the correct params', async function () { + const fields = { email: 'admin@example.com' } + + await makePostBodyRequest({ url: server.url, path, token: server.accessToken, fields, statusCodeExpected: 204 }) + }) + }) + after(async function () { killallServers([ server, serverWithRegistrationDisabled ]) diff --git a/server/tests/api/index-fast.ts b/server/tests/api/index-fast.ts index e591d0fd2..9f52310dd 100644 --- a/server/tests/api/index-fast.ts +++ b/server/tests/api/index-fast.ts @@ -9,3 +9,4 @@ import './videos/video-blacklist-management' import './videos/video-description' import './videos/video-privacy' import './videos/services' +import './server/email' diff --git a/server/tests/api/server/email.ts b/server/tests/api/server/email.ts new file mode 100644 index 000000000..8eb9c0fa4 --- /dev/null +++ b/server/tests/api/server/email.ts @@ -0,0 +1,94 @@ +/* tslint:disable:no-unused-expression */ + +import * as chai from 'chai' +import 'mocha' +import { askResetPassword, createUser, resetPassword, runServer, userLogin, wait } from '../../utils' +import { flushTests, killallServers, ServerInfo, setAccessTokensToServers } from '../../utils/index' +import { mockSmtpServer } from '../../utils/miscs/email' + +const expect = chai.expect + +describe('Test emails', function () { + let server: ServerInfo + let userId: number + let verificationString: string + const emails: object[] = [] + const user = { + username: 'user_1', + password: 'super_password' + } + + before(async function () { + this.timeout(30000) + + await mockSmtpServer(emails) + + await flushTests() + + const overrideConfig = { + smtp: { + hostname: 'localhost' + } + } + server = await runServer(1, overrideConfig) + + await wait(5000) + await setAccessTokensToServers([ server ]) + + const res = await createUser(server.url, server.accessToken, user.username, user.password) + userId = res.body.user.id + }) + + describe('When resetting user password', function () { + + it('Should ask to reset the password', async function () { + this.timeout(10000) + + await askResetPassword(server.url, 'user_1@example.com') + + await wait(3000) + expect(emails).to.have.lengthOf(1) + + const email = emails[0] + + expect(email['from'][0]['address']).equal('test-admin@localhost') + expect(email['to'][0]['address']).equal('user_1@example.com') + expect(email['subject']).contains('password') + + const verificationStringMatches = /verificationString=([a-z0-9]+)/.exec(email['text']) + expect(verificationStringMatches).not.to.be.null + + verificationString = verificationStringMatches[1] + expect(verificationString).to.have.length.above(2) + + const userIdMatches = /userId=([0-9]+)/.exec(email['text']) + expect(userIdMatches).not.to.be.null + + userId = parseInt(userIdMatches[1], 10) + expect(verificationString).to.not.be.undefined + }) + + it('Should not reset the password with an invalid verification string', async function () { + await resetPassword(server.url, userId, verificationString + 'b', 'super_password2', 403) + }) + + it('Should reset the password', async function () { + await resetPassword(server.url, userId, verificationString, 'super_password2') + }) + + it('Should login with this new password', async function () { + user.password = 'super_password2' + + await userLogin(server, user) + }) + }) + + after(async function () { + killallServers([ server ]) + + // Keep the logs if the test failed + if (this['ok']) { + await flushTests() + } + }) +}) diff --git a/server/tests/utils/miscs/email.ts b/server/tests/utils/miscs/email.ts new file mode 100644 index 000000000..21accd09d --- /dev/null +++ b/server/tests/utils/miscs/email.ts @@ -0,0 +1,25 @@ +import * as MailDev from 'maildev' + +function mockSmtpServer (emailsCollection: object[]) { + const maildev = new MailDev({ + ip: '127.0.0.1', + smtp: 1025, + disableWeb: true, + silent: true + }) + maildev.on('new', email => emailsCollection.push(email)) + + return new Promise((res, rej) => { + maildev.listen(err => { + if (err) return rej(err) + + return res() + }) + }) +} + +// --------------------------------------------------------------------------- + +export { + mockSmtpServer +} diff --git a/server/tests/utils/users/users.ts b/server/tests/utils/users/users.ts index 25351e2c7..9e33e6796 100644 --- a/server/tests/utils/users/users.ts +++ b/server/tests/utils/users/users.ts @@ -1,6 +1,6 @@ import { isAbsolute, join } from 'path' import * as request from 'supertest' -import { makePostUploadRequest, makePutBodyRequest } from '../' +import { makePostBodyRequest, makePostUploadRequest, makePutBodyRequest } from '../' import { UserRole } from '../../../../shared/index' @@ -196,6 +196,28 @@ function updateUser (options: { }) } +function askResetPassword (url: string, email: string) { + const path = '/api/v1/users/ask-reset-password' + + return makePostBodyRequest({ + url, + path, + fields: { email }, + statusCodeExpected: 204 + }) +} + +function resetPassword (url: string, userId: number, verificationString: string, password: string, statusCodeExpected = 204) { + const path = '/api/v1/users/' + userId + '/reset-password' + + return makePostBodyRequest({ + url, + path, + fields: { password, verificationString }, + statusCodeExpected + }) +} + // --------------------------------------------------------------------------- export { @@ -210,5 +232,7 @@ export { updateUser, updateMyUser, getUserInformation, + askResetPassword, + resetPassword, updateMyAvatar } -- cgit v1.2.3