diff --git a/test/helpers/index.js b/test/helpers/index.js index 7a26e2942b..38d3810e93 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -163,8 +163,6 @@ helpers.copyFile = function (source, target, callback) { helpers.invite = async function (data, uid, jar, csrf_token) { const { response, body } = await request.post(`${nconf.get('url')}/api/v3/users/${uid}/invites`, { jar: jar, - // using "form" since client "api" module make requests with "application/x-www-form-urlencoded" content-type - // form: body, data: data, headers: { 'x-csrf-token': csrf_token, @@ -173,7 +171,6 @@ helpers.invite = async function (data, uid, jar, csrf_token) { }); console.log(response.status, body); - // res.body = JSON.parse(res.body); return { response, body }; }; diff --git a/test/user.js b/test/user.js index 907a43f388..46ea0065dc 100644 --- a/test/user.js +++ b/test/user.js @@ -6,8 +6,6 @@ const fs = require('fs'); const path = require('path'); const nconf = require('nconf'); const validator = require('validator'); -const request = require('request'); -const requestAsync = require('request-promise-native'); const jwt = require('jsonwebtoken'); const db = require('./mocks/databasemock'); @@ -24,6 +22,7 @@ const socketUser = require('../src/socket.io/user'); const apiUser = require('../src/api/users'); const utils = require('../src/utils'); const privileges = require('../src/privileges'); +const request = require('../src/request'); describe('User', () => { let userData; @@ -355,13 +354,12 @@ describe('User', () => { const titles = new Array(10).fill('topic title'); const res = await Promise.allSettled(titles.map(async (title) => { const { body } = await helpers.request('post', '/api/v3/topics', { - form: { + data: { cid: testCid, title: title, content: 'the content', }, jar: jar, - json: true, }); return body.status; })); @@ -991,14 +989,12 @@ describe('User', () => { it('should let you set an external image', async () => { const token = await helpers.getCsrfToken(jar); - const body = await requestAsync(`${nconf.get('url')}/api/v3/users/${uid}/picture`, { + const { body } = await request.put(`${nconf.get('url')}/api/v3/users/${uid}/picture`, { jar, - method: 'put', - json: true, headers: { 'x-csrf-token': token, }, - body: { + data: { type: 'external', url: 'https://example.org/picture.jpg', }, @@ -1193,46 +1189,35 @@ describe('User', () => { }); }); - it('should load profile page', (done) => { - request(`${nconf.get('url')}/api/user/updatedagain`, { jar: jar, json: true }, (err, res, body) => { - assert.ifError(err); - assert.equal(res.statusCode, 200); - assert(body); - done(); - }); + it('should load profile page', async () => { + const { response, body } = await request.get(`${nconf.get('url')}/api/user/updatedagain`, { jar }); + assert.equal(response.statusCode, 200); + assert(body); }); - it('should load settings page', (done) => { - request(`${nconf.get('url')}/api/user/updatedagain/settings`, { jar: jar, json: true }, (err, res, body) => { - assert.ifError(err); - assert.equal(res.statusCode, 200); - assert(body.settings); - assert(body.languages); - assert(body.homePageRoutes); - done(); - }); + it('should load settings page', async () => { + const { response, body } = await request.get(`${nconf.get('url')}/api/user/updatedagain/settings`, { jar }); + assert.equal(response.statusCode, 200); + assert(body.settings); + assert(body.languages); + assert(body.homePageRoutes); }); - it('should load edit page', (done) => { - request(`${nconf.get('url')}/api/user/updatedagain/edit`, { jar: jar, json: true }, (err, res, body) => { - assert.ifError(err); - assert.equal(res.statusCode, 200); - assert(body); - done(); - }); + it('should load edit page', async () => { + const { response, body } = await request.get(`${nconf.get('url')}/api/user/updatedagain/edit`, { jar }); + assert.equal(response.statusCode, 200); + assert(body); }); it('should load edit/email page', async () => { - const res = await requestAsync(`${nconf.get('url')}/api/user/updatedagain/edit/email`, { jar: jar, json: true, resolveWithFullResponse: true }); - assert.strictEqual(res.statusCode, 200); - assert(res.body); + const { response, body } = await request.get(`${nconf.get('url')}/api/user/updatedagain/edit/email`, { jar }); + assert.strictEqual(response.statusCode, 200); + assert(body); // Accessing this page will mark the user's account as needing an updated email, below code undo's. - await requestAsync({ - uri: `${nconf.get('url')}/register/abort`, + await request.post(`${nconf.get('url')}/register/abort`, { jar, - method: 'POST', - simple: false, + validateStatus: null, headers: { 'x-csrf-token': csrf_token, }, @@ -1246,7 +1231,7 @@ describe('User', () => { }); await groups.join('Test', uid); - const body = await requestAsync(`${nconf.get('url')}/api/user/updatedagain/groups`, { jar: jar, json: true }); + const { body } = await request.get(`${nconf.get('url')}/api/user/updatedagain/groups`, { jar }); assert(Array.isArray(body.groups)); assert.equal(body.groups[0].name, 'Test'); @@ -1549,106 +1534,65 @@ describe('User', () => { }); describe('unsubscribe via POST', () => { - it('should unsubscribe from digest if one-click unsubscribe is POSTed', (done) => { + it('should unsubscribe from digest if one-click unsubscribe is POSTed', async () => { const token = jwt.sign({ template: 'digest', uid: uid, }, nconf.get('secret')); - request({ - method: 'post', - url: `${nconf.get('url')}/email/unsubscribe/${token}`, - }, (err, res) => { - assert.ifError(err); - assert.strictEqual(res.statusCode, 200); - - db.getObjectField(`user:${uid}:settings`, 'dailyDigestFreq', (err, value) => { - assert.ifError(err); - assert.strictEqual(value, 'off'); - done(); - }); - }); + const { response } = await request.post(`${nconf.get('url')}/email/unsubscribe/${token}`); + assert.strictEqual(response.statusCode, 200); + const value = await db.getObjectField(`user:${uid}:settings`, 'dailyDigestFreq'); + assert.strictEqual(value, 'off'); }); - it('should unsubscribe from notifications if one-click unsubscribe is POSTed', (done) => { + it('should unsubscribe from notifications if one-click unsubscribe is POSTed', async () => { const token = jwt.sign({ template: 'notification', type: 'test', uid: uid, }, nconf.get('secret')); - request({ - method: 'post', - url: `${nconf.get('url')}/email/unsubscribe/${token}`, - }, (err, res) => { - assert.ifError(err); - assert.strictEqual(res.statusCode, 200); + const { response } = await request.post(`${nconf.get('url')}/email/unsubscribe/${token}`); + assert.strictEqual(response.statusCode, 200); - db.getObjectField(`user:${uid}:settings`, 'notificationType_test', (err, value) => { - assert.ifError(err); - assert.strictEqual(value, 'notification'); - done(); - }); - }); + const value = await db.getObjectField(`user:${uid}:settings`, 'notificationType_test'); + assert.strictEqual(value, 'notification'); }); - it('should return errors on missing template in token', (done) => { + it('should return errors on missing template in token', async () => { const token = jwt.sign({ uid: uid, }, nconf.get('secret')); - request({ - method: 'post', - url: `${nconf.get('url')}/email/unsubscribe/${token}`, - }, (err, res) => { - assert.ifError(err); - assert.strictEqual(res.statusCode, 404); - done(); - }); + const { response } = await request.post(`${nconf.get('url')}/email/unsubscribe/${token}`, { validateStatus: null }); + assert.strictEqual(response.statusCode, 404); }); - it('should return errors on wrong template in token', (done) => { + it('should return errors on wrong template in token', async () => { const token = jwt.sign({ template: 'user', uid: uid, }, nconf.get('secret')); - request({ - method: 'post', - url: `${nconf.get('url')}/email/unsubscribe/${token}`, - }, (err, res) => { - assert.ifError(err); - assert.strictEqual(res.statusCode, 404); - done(); - }); + const { response } = await request.post(`${nconf.get('url')}/email/unsubscribe/${token}`, { validateStatus: null }); + assert.strictEqual(response.statusCode, 404); }); - it('should return errors on missing token', (done) => { - request({ - method: 'post', - url: `${nconf.get('url')}/email/unsubscribe/`, - }, (err, res) => { - assert.ifError(err); - assert.strictEqual(res.statusCode, 404); - done(); - }); + it('should return errors on missing token', async () => { + const { response } = await request.post(`${nconf.get('url')}/email/unsubscribe/`, { validateStatus: null }); + assert.strictEqual(response.statusCode, 404); }); - it('should return errors on token signed with wrong secret (verify-failure)', (done) => { + it('should return errors on token signed with wrong secret (verify-failure)', async () => { const token = jwt.sign({ template: 'notification', type: 'test', uid: uid, }, `${nconf.get('secret')}aababacaba`); - request({ - method: 'post', - url: `${nconf.get('url')}/email/unsubscribe/${token}`, - }, (err, res) => { - assert.ifError(err); - assert.strictEqual(res.statusCode, 403); - done(); - }); + const { response } = await request.post(`${nconf.get('url')}/email/unsubscribe/${token}`, { validateStatus: null }); + assert.strictEqual(response.statusCode, 403); }); }); }); @@ -1974,89 +1918,66 @@ describe('User', () => { gdpr_consent: true, }); const { jar } = await helpers.loginUser('admin', '123456'); - const { users } = await requestAsync(`${nconf.get('url')}/api/admin/manage/registration`, { jar, json: true }); + const { body: { users } } = await request.get(`${nconf.get('url')}/api/admin/manage/registration`, { jar }); assert.equal(users[0].username, 'rejectme'); assert.equal(users[0].email, '<script>alert("ok")<script>reject@me.com'); }); - it('should fail to add user to queue if username is taken', (done) => { - helpers.registerUser({ + it('should fail to add user to queue if username is taken', async () => { + const { body } = await helpers.registerUser({ username: 'rejectme', password: '123456', 'password-confirm': '123456', email: '