From 8cbad61e8a94f1dedfa308dc83da50d7f30d1879 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 3 Sep 2021 16:46:14 -0400 Subject: [PATCH] test: added test for external image via new change picture API --- test/helpers/index.js | 10 ++++++++++ test/user.js | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/test/helpers/index.js b/test/helpers/index.js index ecb2ebd0a4..79a0e9e41e 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -10,6 +10,16 @@ const utils = require('../../public/src/utils'); const helpers = module.exports; +helpers.getCsrfToken = async (jar) => { + const { csrf_token: token } = await requestAsync({ + url: `${nconf.get('url')}/api/config`, + json: true, + jar, + }); + + return token; +}; + helpers.loginUser = function (username, password, callback) { const jar = request.jar(); diff --git a/test/user.js b/test/user.js index 4c9a61cd86..e74eb8252d 100644 --- a/test/user.js +++ b/test/user.js @@ -5,6 +5,7 @@ const async = require('async'); 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'); @@ -1070,6 +1071,28 @@ 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`, { + jar, + method: 'put', + json: true, + headers: { + 'x-csrf-token': token, + }, + body: { + type: 'external', + url: 'https://example.org/picture.jpg', + }, + }); + + assert(body && body.status && body.response); + assert.strictEqual(body.status.code, 'ok'); + + const picture = await User.getUserField(uid, 'picture'); + assert.strictEqual(picture, validator.escape('https://example.org/picture.jpg')); + }); + it('should fail to change user picture with invalid data', (done) => { socketUser.changePicture({ uid: uid }, null, (err) => { assert.equal(err.message, '[[error:invalid-data]]');