mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
tests(api): Update integration helper in v2 to v3 changes
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
assign,
|
assign,
|
||||||
each,
|
each,
|
||||||
isEmpty,
|
isEmpty,
|
||||||
|
set,
|
||||||
times,
|
times,
|
||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
import { MongoClient as mongo } from 'mongodb';
|
import { MongoClient as mongo } from 'mongodb';
|
||||||
@@ -13,6 +14,15 @@ import i18n from '../../common/script/src/i18n';
|
|||||||
i18n.translations = require('../../website/src/libs/i18n.js').translations;
|
i18n.translations = require('../../website/src/libs/i18n.js').translations;
|
||||||
|
|
||||||
const API_TEST_SERVER_PORT = 3003;
|
const API_TEST_SERVER_PORT = 3003;
|
||||||
|
const API_V = process.env.API_VERSION || 'v2'; // eslint-disable-line no-process-env
|
||||||
|
const ROUTES = {
|
||||||
|
v2: {
|
||||||
|
register: '/register',
|
||||||
|
},
|
||||||
|
v3: {
|
||||||
|
register: '/user/auth/local/register',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
class ApiUser {
|
class ApiUser {
|
||||||
constructor (options) {
|
constructor (options) {
|
||||||
@@ -89,7 +99,7 @@ export function generateUser (update = {}) {
|
|||||||
let request = _requestMaker({}, 'post');
|
let request = _requestMaker({}, 'post');
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request('/register', {
|
request(ROUTES[API_V].register, {
|
||||||
username,
|
username,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
@@ -234,7 +244,7 @@ export function resetHabiticaDB () {
|
|||||||
function _requestMaker (user, method, additionalSets) {
|
function _requestMaker (user, method, additionalSets) {
|
||||||
return (route, send, query) => {
|
return (route, send, query) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let request = superagent[method](`http://localhost:${API_TEST_SERVER_PORT}/api/v2${route}`)
|
let request = superagent[method](`http://localhost:${API_TEST_SERVER_PORT}/api/${API_V}${route}`)
|
||||||
.accept('application/json');
|
.accept('application/json');
|
||||||
|
|
||||||
if (user && user._id && user.apiToken) {
|
if (user && user._id && user.apiToken) {
|
||||||
@@ -254,10 +264,20 @@ function _requestMaker (user, method, additionalSets) {
|
|||||||
if (err) {
|
if (err) {
|
||||||
if (!err.response) return reject(err);
|
if (!err.response) return reject(err);
|
||||||
|
|
||||||
return reject({
|
if (API_V === 'v3') {
|
||||||
code: err.response.status,
|
return reject({
|
||||||
text: err.response.body.err,
|
code: err.status,
|
||||||
});
|
error: err.response.body.error,
|
||||||
|
message: err.response.body.message,
|
||||||
|
});
|
||||||
|
} else if (API_V === 'v2') {
|
||||||
|
return reject({
|
||||||
|
code: err.status,
|
||||||
|
text: err.response.body.err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return reject(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(response.body);
|
resolve(response.body);
|
||||||
@@ -276,11 +296,17 @@ function _updateDocument (collectionName, doc, update, cb) {
|
|||||||
|
|
||||||
let collection = db.collection(collectionName);
|
let collection = db.collection(collectionName);
|
||||||
|
|
||||||
collection.update({ _id: doc._id }, { $set: update }, (updateErr) => {
|
collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => {
|
||||||
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
|
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
|
||||||
assign(doc, update);
|
_updateLocalDocument(doc, update);
|
||||||
db.close();
|
db.close();
|
||||||
cb();
|
cb();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _updateLocalDocument (doc, update) {
|
||||||
|
each(update, (value, param) => {
|
||||||
|
set(doc, param, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user