mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
tests(api): Update integration helper in v2 to v3 changes
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
assign,
|
||||
each,
|
||||
isEmpty,
|
||||
set,
|
||||
times,
|
||||
} from 'lodash';
|
||||
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;
|
||||
|
||||
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 {
|
||||
constructor (options) {
|
||||
@@ -89,7 +99,7 @@ export function generateUser (update = {}) {
|
||||
let request = _requestMaker({}, 'post');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
request('/register', {
|
||||
request(ROUTES[API_V].register, {
|
||||
username,
|
||||
email,
|
||||
password,
|
||||
@@ -234,7 +244,7 @@ export function resetHabiticaDB () {
|
||||
function _requestMaker (user, method, additionalSets) {
|
||||
return (route, send, query) => {
|
||||
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');
|
||||
|
||||
if (user && user._id && user.apiToken) {
|
||||
@@ -254,10 +264,20 @@ function _requestMaker (user, method, additionalSets) {
|
||||
if (err) {
|
||||
if (!err.response) return reject(err);
|
||||
|
||||
return reject({
|
||||
code: err.response.status,
|
||||
text: err.response.body.err,
|
||||
});
|
||||
if (API_V === 'v3') {
|
||||
return reject({
|
||||
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);
|
||||
@@ -276,11 +296,17 @@ function _updateDocument (collectionName, doc, update, cb) {
|
||||
|
||||
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}`);
|
||||
assign(doc, update);
|
||||
_updateLocalDocument(doc, update);
|
||||
db.close();
|
||||
cb();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function _updateLocalDocument (doc, update) {
|
||||
each(update, (value, param) => {
|
||||
set(doc, param, value);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user