Skip to content

Node.js based forum software built for the modern web

test: add email interstitial tests

Barış Soner Uşaklı1264dcb55502
diff --git a/src/user/interstitials.js b/src/user/interstitials.js
index 101cdd01c8..a3e7e70868 100644
--- a/src/user/interstitials.js
+++ b/src/user/interstitials.js
@@ -1,5 +1,7 @@
'use strict';
+const winston = require('winston');
+
const user = require('.');
const db = require('../database');
const meta = require('../meta');
@@ -58,6 +60,8 @@ Interstitials.email = async (data) => {
await user.email.sendValidationEmail(userData.uid, {
email: formData.email,
force: true,
+ }).catch((err) => {
+ winston.error(`[user.create] Validation email failed to send\n[emailer.send] ${err.stack}`);
});
data.req.session.emailChanged = 1;
} else {
diff --git a/test/controllers.js b/test/controllers.js
index 0a35838d42..3669dd32e1 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -26,42 +26,31 @@ describe('Controllers', () => {
let cid;
let pid;
let fooUid;
+ let adminUid;
let category;
- before((done) => {
- async.series({
- category: function (next) {
- categories.create({
- name: 'Test Category',
- description: 'Test category created by testing script',
- }, next);
- },
- user: async () => {
- const uid = await user.create({ username: 'foo', password: 'barbar', gdpr_consent: true });
- await user.setUserField(uid, 'email', '[email protected]');
- await user.email.confirmByUid(uid);
- return uid;
- },
- navigation: function (next) {
- const navigation = require('../src/navigation/admin');
- const data = require('../install/data/navigation.json');
+ before(async () => {
+ category = await categories.create({
+ name: 'Test Category',
+ description: 'Test category created by testing script',
+ });
+ cid = category.cid;
- navigation.save(data, next);
- },
- }, (err, results) => {
- if (err) {
- return done(err);
- }
- category = results.category;
- cid = results.category.cid;
- fooUid = results.user;
+ fooUid = await user.create({ username: 'foo', password: 'barbar', gdpr_consent: true });
+ await user.setUserField(fooUid, 'email', '[email protected]');
+ await user.email.confirmByUid(fooUid);
- topics.post({ uid: results.user, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, (err, result) => {
- tid = result.topicData.tid;
- pid = result.postData.pid;
- done(err);
- });
- });
+ adminUid = await user.create({ username: 'admin', password: 'barbar', gdpr_consent: true });
+ await groups.join('administrators', adminUid);
+
+ const navigation = require('../src/navigation/admin');
+ const data = require('../install/data/navigation.json');
+
+ await navigation.save(data);
+
+ const result = await topics.post({ uid: fooUid, title: 'test topic title', content: 'test topic content', cid: cid });
+ tid = result.topicData.tid;
+ pid = result.postData.pid;
});
it('should load /config with csrf_token', (done) => {
@@ -415,6 +404,99 @@ describe('Controllers', () => {
assert.strictEqual(res.statusCode, 302);
assert.strictEqual(res.headers.location, `${nconf.get('relative_path')}/`);
});
+
+ it('should error if userData is falsy', async () => {
+ try {
+ await user.interstitials.email({ userData: null });
+ assert(false);
+ } catch (err) {
+ assert.strictEqual(err.message, '[[error:invalid-data]]');
+ }
+ });
+
+ it('should throw error if email is not valid', async () => {
+ const uid = await user.create({ username: 'interstiuser1' });
+ try {
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: uid },
+ interstitials: [],
+ });
+ assert.strictEqual(result.interstitials[0].template, 'partials/email_update');
+ await result.interstitials[0].callback({ uid: uid }, {
+ email: 'invalidEmail',
+ });
+ assert(false);
+ } catch (err) {
+ assert.strictEqual(err.message, '[[error:invalid-email]]');
+ }
+ });
+
+ it('should set req.session.emailChanged to 1', async () => {
+ const uid = await user.create({ username: 'interstiuser2' });
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: uid, session: {} },
+ interstitials: [],
+ });
+
+ await result.interstitials[0].callback({ uid: uid }, {
+ email: '[email protected]',
+ });
+ assert.strictEqual(result.req.session.emailChanged, 1);
+ });
+
+ it('should set email if admin is changing it', async () => {
+ const uid = await user.create({ username: 'interstiuser3' });
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: adminUid },
+ interstitials: [],
+ });
+
+ await result.interstitials[0].callback({ uid: uid }, {
+ email: '[email protected]',
+ });
+ const userData = await user.getUserData(uid);
+ assert.strictEqual(userData.email, '[email protected]');
+ assert.strictEqual(userData['email:confirmed'], 1);
+ });
+
+ it('should throw error if user tries to edit other users email', async () => {
+ const uid = await user.create({ username: 'interstiuser4' });
+ try {
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: 1000 },
+ interstitials: [],
+ });
+
+ await result.interstitials[0].callback({ uid: uid }, {
+ email: '[email protected]',
+ });
+ assert(false);
+ } catch (err) {
+ assert.strictEqual(err.message, '[[error:no-privileges]]');
+ }
+ });
+
+ it('should remove current email', async () => {
+ const uid = await user.create({ username: 'interstiuser5', email: '[email protected]' });
+ await user.email.confirmByUid(uid);
+
+ const result = await user.interstitials.email({
+ userData: { uid: uid, updateEmail: true },
+ req: { uid: uid, session: { id: 0 } },
+ interstitials: [],
+ });
+
+ await result.interstitials[0].callback({ uid: uid }, {
+ email: '',
+ });
+ const userData = await user.getUserData(uid);
+ assert.strictEqual(userData.email, '');
+ assert.strictEqual(userData['email:confirmed'], 0);
+ });
});
it('should load /robots.txt', (done) => {