2016-10-25 17:56:32 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
const request = require('request');
|
2020-11-16 22:47:23 +03:00
|
|
|
const requestAsync = require('request-promise-native');
|
2021-02-04 00:06:15 -07:00
|
|
|
const nconf = require('nconf');
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
const winston = require('winston');
|
2016-10-25 17:56:32 +03:00
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
const utils = require('../../public/src/utils');
|
2016-10-26 16:38:42 +03:00
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
const helpers = module.exports;
|
2016-10-25 17:56:32 +03:00
|
|
|
|
2021-09-03 16:46:14 -04:00
|
|
|
helpers.getCsrfToken = async (jar) => {
|
|
|
|
|
const { csrf_token: token } = await requestAsync({
|
|
|
|
|
url: `${nconf.get('url')}/api/config`,
|
|
|
|
|
json: true,
|
|
|
|
|
jar,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-22 19:23:51 -05:00
|
|
|
helpers.request = async function (method, uri, options) {
|
|
|
|
|
const csrf_token = await helpers.getCsrfToken(options.jar);
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
options.headers = options.headers || {};
|
|
|
|
|
options.headers['x-csrf-token'] = csrf_token;
|
|
|
|
|
request[method](`${nconf.get('url')}${uri}`, options, (err, res, body) => {
|
|
|
|
|
if (err) reject(err);
|
|
|
|
|
else resolve({ res, body });
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-25 17:56:32 +03:00
|
|
|
helpers.loginUser = function (username, password, callback) {
|
2021-02-04 00:06:15 -07:00
|
|
|
const jar = request.jar();
|
2017-05-27 23:32:55 -04:00
|
|
|
|
2016-10-25 17:56:32 +03:00
|
|
|
request({
|
2021-02-03 23:59:08 -07:00
|
|
|
url: `${nconf.get('url')}/api/config`,
|
2016-10-25 17:56:32 +03:00
|
|
|
json: true,
|
2017-02-17 19:31:21 -07:00
|
|
|
jar: jar,
|
2021-02-04 00:01:39 -07:00
|
|
|
}, (err, res, body) => {
|
2016-10-26 16:38:42 +03:00
|
|
|
if (err || res.statusCode !== 200) {
|
2016-10-25 17:56:32 +03:00
|
|
|
return callback(err || new Error('[[error:invalid-response]]'));
|
|
|
|
|
}
|
2021-11-22 23:20:31 -05:00
|
|
|
const { csrf_token } = body;
|
2021-02-03 23:59:08 -07:00
|
|
|
request.post(`${nconf.get('url')}/login`, {
|
2016-10-25 17:56:32 +03:00
|
|
|
form: {
|
|
|
|
|
username: username,
|
|
|
|
|
password: password,
|
|
|
|
|
},
|
|
|
|
|
json: true,
|
|
|
|
|
jar: jar,
|
|
|
|
|
headers: {
|
2021-11-22 23:20:31 -05:00
|
|
|
'x-csrf-token': csrf_token,
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2021-11-22 23:20:31 -05:00
|
|
|
}, (err, res, body) => {
|
|
|
|
|
if (err) {
|
2016-10-25 17:56:32 +03:00
|
|
|
return callback(err || new Error('[[error:invalid-response]]'));
|
|
|
|
|
}
|
2021-11-22 23:20:31 -05:00
|
|
|
callback(null, { jar, res, body, csrf_token: csrf_token });
|
2016-10-26 16:38:42 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-27 23:32:55 -04:00
|
|
|
|
|
|
|
|
helpers.logoutUser = function (jar, callback) {
|
|
|
|
|
request({
|
2021-02-03 23:59:08 -07:00
|
|
|
url: `${nconf.get('url')}/api/config`,
|
2017-05-27 23:32:55 -04:00
|
|
|
json: true,
|
|
|
|
|
jar: jar,
|
2021-02-04 00:01:39 -07:00
|
|
|
}, (err, response, body) => {
|
2017-05-27 23:32:55 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err, response, body);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 23:59:08 -07:00
|
|
|
request.post(`${nconf.get('url')}/logout`, {
|
2017-05-27 23:32:55 -04:00
|
|
|
form: {},
|
|
|
|
|
json: true,
|
|
|
|
|
jar: jar,
|
|
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': body.csrf_token,
|
|
|
|
|
},
|
2021-02-04 00:01:39 -07:00
|
|
|
}, (err, response, body) => {
|
2017-05-27 23:32:55 -04:00
|
|
|
callback(err, response, body);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-25 16:04:04 +03:00
|
|
|
helpers.connectSocketIO = function (res, callback) {
|
2021-02-04 00:06:15 -07:00
|
|
|
const io = require('socket.io-client');
|
2020-02-06 15:52:37 -05:00
|
|
|
let cookies = res.headers['set-cookie'];
|
|
|
|
|
cookies = cookies.filter(c => /express.sid=[^;]+;/.test(c));
|
|
|
|
|
const cookie = cookies[0];
|
2021-02-04 00:06:15 -07:00
|
|
|
const socket = io(nconf.get('base_url'), {
|
2021-02-03 23:59:08 -07:00
|
|
|
path: `${nconf.get('relative_path')}/socket.io`,
|
2017-10-13 13:57:30 -04:00
|
|
|
extraHeaders: {
|
|
|
|
|
Origin: nconf.get('url'),
|
|
|
|
|
Cookie: cookie,
|
|
|
|
|
},
|
2017-02-25 16:04:04 +03:00
|
|
|
});
|
2017-10-13 13:57:30 -04:00
|
|
|
|
2021-02-04 00:01:39 -07:00
|
|
|
socket.on('connect', () => {
|
2017-10-13 13:57:30 -04:00
|
|
|
callback(null, socket);
|
2017-02-25 16:04:04 +03:00
|
|
|
});
|
|
|
|
|
|
2021-02-04 00:01:39 -07:00
|
|
|
socket.on('error', (err) => {
|
2017-02-25 16:04:04 +03:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-01 17:33:19 +03:00
|
|
|
helpers.uploadFile = function (uploadEndPoint, filePath, body, jar, csrf_token, callback) {
|
2021-02-04 00:06:15 -07:00
|
|
|
let formData = {
|
2016-11-01 17:33:19 +03:00
|
|
|
files: [
|
|
|
|
|
fs.createReadStream(filePath),
|
2017-02-17 19:31:21 -07:00
|
|
|
fs.createReadStream(filePath), // see https://github.com/request/request/issues/2445
|
|
|
|
|
],
|
2016-11-01 17:33:19 +03:00
|
|
|
};
|
|
|
|
|
formData = utils.merge(formData, body);
|
|
|
|
|
request.post({
|
|
|
|
|
url: uploadEndPoint,
|
|
|
|
|
formData: formData,
|
|
|
|
|
json: true,
|
|
|
|
|
jar: jar,
|
|
|
|
|
headers: {
|
2017-02-17 19:31:21 -07:00
|
|
|
'x-csrf-token': csrf_token,
|
|
|
|
|
},
|
2021-02-04 00:01:39 -07:00
|
|
|
}, (err, res, body) => {
|
2016-11-01 17:33:19 +03:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2017-05-10 22:05:23 -04:00
|
|
|
if (res.statusCode !== 200) {
|
2020-06-22 12:08:35 -04:00
|
|
|
winston.error(JSON.stringify(body));
|
2017-05-10 22:05:23 -04:00
|
|
|
}
|
|
|
|
|
callback(null, res, body);
|
2016-11-01 17:33:19 +03:00
|
|
|
});
|
2016-12-02 14:05:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
helpers.registerUser = function (data, callback) {
|
2021-02-04 00:06:15 -07:00
|
|
|
const jar = request.jar();
|
2016-12-02 14:05:54 +03:00
|
|
|
request({
|
2021-02-03 23:59:08 -07:00
|
|
|
url: `${nconf.get('url')}/api/config`,
|
2016-12-02 14:05:54 +03:00
|
|
|
json: true,
|
2017-02-17 19:31:21 -07:00
|
|
|
jar: jar,
|
2021-02-04 00:01:39 -07:00
|
|
|
}, (err, response, body) => {
|
2016-12-02 14:05:54 +03:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 15:07:19 -04:00
|
|
|
if (!data.hasOwnProperty('password-confirm')) {
|
|
|
|
|
data['password-confirm'] = data.password;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 23:59:08 -07:00
|
|
|
request.post(`${nconf.get('url')}/register`, {
|
2016-12-02 14:05:54 +03:00
|
|
|
form: data,
|
|
|
|
|
json: true,
|
|
|
|
|
jar: jar,
|
|
|
|
|
headers: {
|
2017-02-17 19:31:21 -07:00
|
|
|
'x-csrf-token': body.csrf_token,
|
|
|
|
|
},
|
2021-02-04 00:01:39 -07:00
|
|
|
}, (err, response, body) => {
|
2017-09-01 18:40:34 -04:00
|
|
|
callback(err, jar, response, body);
|
2016-12-02 14:05:54 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-18 01:25:46 -07:00
|
|
|
// http://stackoverflow.com/a/14387791/583363
|
2016-12-02 14:05:54 +03:00
|
|
|
helpers.copyFile = function (source, target, callback) {
|
2021-02-04 00:06:15 -07:00
|
|
|
let cbCalled = false;
|
2016-12-02 14:05:54 +03:00
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
const rd = fs.createReadStream(source);
|
2021-02-04 00:01:39 -07:00
|
|
|
rd.on('error', (err) => {
|
2016-12-02 14:05:54 +03:00
|
|
|
done(err);
|
|
|
|
|
});
|
2021-02-04 00:06:15 -07:00
|
|
|
const wr = fs.createWriteStream(target);
|
2021-02-04 00:01:39 -07:00
|
|
|
wr.on('error', (err) => {
|
2016-12-02 14:05:54 +03:00
|
|
|
done(err);
|
|
|
|
|
});
|
2021-02-04 00:01:39 -07:00
|
|
|
wr.on('close', () => {
|
2016-12-02 14:05:54 +03:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
rd.pipe(wr);
|
|
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
|
if (!cbCalled) {
|
|
|
|
|
callback(err);
|
|
|
|
|
cbCalled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|
2019-09-20 11:44:49 -04:00
|
|
|
|
2020-11-16 22:47:23 +03:00
|
|
|
helpers.invite = async function (body, uid, jar, csrf_token) {
|
|
|
|
|
const res = await requestAsync.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,
|
|
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': csrf_token,
|
|
|
|
|
},
|
|
|
|
|
simple: false,
|
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.body = JSON.parse(res.body);
|
|
|
|
|
return { res, body };
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-31 16:27:00 +03:00
|
|
|
helpers.createFolder = function (path, folderName, jar, csrf_token) {
|
|
|
|
|
return requestAsync.put(`${nconf.get('url')}/api/v3/files/folder`, {
|
|
|
|
|
jar,
|
|
|
|
|
body: {
|
|
|
|
|
path,
|
|
|
|
|
folderName,
|
|
|
|
|
},
|
|
|
|
|
json: true,
|
|
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': csrf_token,
|
|
|
|
|
},
|
|
|
|
|
simple: false,
|
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-20 11:44:49 -04:00
|
|
|
require('../../src/promisify')(helpers);
|