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/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 +++++++++- 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 server/tests/api/server/email.ts create mode 100644 server/tests/utils/miscs/email.ts (limited to 'server/tests') 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