mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Adjust helper files for linting preferences.
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
|
/* eslint-disable no-use-before-define */
|
||||||
|
|
||||||
import {
|
import {
|
||||||
assign,
|
assign,
|
||||||
each,
|
each,
|
||||||
isEmpty,
|
isEmpty,
|
||||||
times,
|
times,
|
||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
import {MongoClient as mongo} from 'mongodb';
|
import { MongoClient as mongo } from 'mongodb';
|
||||||
import {v4 as generateUUID} from 'uuid';
|
import { v4 as generateUUID } from 'uuid';
|
||||||
import superagent from 'superagent';
|
import superagent from 'superagent';
|
||||||
import i18n from '../../common/script/src/i18n';
|
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;
|
||||||
@@ -15,19 +17,19 @@ const API_TEST_SERVER_PORT = 3003;
|
|||||||
// Sets up an abject that can make all REST requests
|
// Sets up an abject that can make all REST requests
|
||||||
// If a user is passed in, the uuid and api token of
|
// If a user is passed in, the uuid and api token of
|
||||||
// the user are used to make the requests
|
// the user are used to make the requests
|
||||||
export function requester(user={}, additionalSets) {
|
export function requester (user = {}, additionalSets) {
|
||||||
return {
|
return {
|
||||||
get: _requestMaker(user, 'get', additionalSets),
|
get: _requestMaker(user, 'get', additionalSets),
|
||||||
post: _requestMaker(user, 'post', additionalSets),
|
post: _requestMaker(user, 'post', additionalSets),
|
||||||
put: _requestMaker(user, 'put', additionalSets),
|
put: _requestMaker(user, 'put', additionalSets),
|
||||||
del: _requestMaker(user, 'del', additionalSets),
|
del: _requestMaker(user, 'del', additionalSets),
|
||||||
}
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
// Use this to verify error messages returned by the server
|
// Use this to verify error messages returned by the server
|
||||||
// That way, if the translated string changes, the test
|
// That way, if the translated string changes, the test
|
||||||
// will not break. NOTE: it checks agains errors with string as well.
|
// will not break. NOTE: it checks agains errors with string as well.
|
||||||
export function translate(key, variables) {
|
export function translate (key, variables) {
|
||||||
const STRING_ERROR_MSG = 'Error processing the string. Please see Help > Report a Bug.';
|
const STRING_ERROR_MSG = 'Error processing the string. Please see Help > Report a Bug.';
|
||||||
const STRING_DOES_NOT_EXIST_MSG = /^String '.*' not found.$/;
|
const STRING_DOES_NOT_EXIST_MSG = /^String '.*' not found.$/;
|
||||||
|
|
||||||
@@ -38,18 +40,22 @@ export function translate(key, variables) {
|
|||||||
expect(translatedString).to.not.match(STRING_DOES_NOT_EXIST_MSG);
|
expect(translatedString).to.not.match(STRING_DOES_NOT_EXIST_MSG);
|
||||||
|
|
||||||
return translatedString;
|
return translatedString;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Useful for checking things that have been deleted,
|
// Useful for checking things that have been deleted,
|
||||||
// but you no longer have access to,
|
// but you no longer have access to,
|
||||||
// like private parties or users
|
// like private parties or users
|
||||||
export function checkExistence(collectionName, id) {
|
export function checkExistence (collectionName, id) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
mongo.connect('mongodb://localhost/habitrpg_test', (connectionError, db) => {
|
||||||
if (err) return reject(err);
|
if (connectionError) return reject(connectionError);
|
||||||
let collection = db.collection(collectionName);
|
let collection = db.collection(collectionName);
|
||||||
collection.find({_id: id}, {_id: 1}).limit(1).toArray((err, docs) => {
|
|
||||||
|
collection.find({_id: id}, {_id: 1}).limit(1).toArray((findError, docs) => {
|
||||||
|
if (findError) return reject(findError);
|
||||||
|
|
||||||
let exists = docs.length > 0;
|
let exists = docs.length > 0;
|
||||||
|
|
||||||
db.close();
|
db.close();
|
||||||
resolve(exists);
|
resolve(exists);
|
||||||
});
|
});
|
||||||
@@ -64,41 +70,41 @@ export function checkExistence(collectionName, id) {
|
|||||||
// paramter, such as the number of wolf eggs the user has,
|
// paramter, such as the number of wolf eggs the user has,
|
||||||
// , you can do so by passing in the full path as a string:
|
// , you can do so by passing in the full path as a string:
|
||||||
// { 'items.eggs.Wolf': 10 }
|
// { 'items.eggs.Wolf': 10 }
|
||||||
export function generateUser(update={}) {
|
export function generateUser (update = {}) {
|
||||||
let username = generateUUID();
|
let username = generateUUID();
|
||||||
let password = 'password'
|
let password = 'password';
|
||||||
let email = username + '@example.com';
|
let email = `${username}@example.com`;
|
||||||
|
|
||||||
let request = _requestMaker({}, 'post');
|
let request = _requestMaker({}, 'post');
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request('/register', {
|
request('/register', {
|
||||||
username: username,
|
username,
|
||||||
email: email,
|
email,
|
||||||
password: password,
|
password,
|
||||||
confirmPassword: password,
|
confirmPassword: password,
|
||||||
}).then((user) => {
|
}).then((user) => {
|
||||||
_updateDocument('users', user, update, () => {
|
_updateDocument('users', user, update, () => {
|
||||||
resolve(user);
|
resolve(user);
|
||||||
});
|
});
|
||||||
});
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
// Generates a new group. Requires a user object, which
|
// Generates a new group. Requires a user object, which
|
||||||
// will will become the groups leader. Takes an update
|
// will will become the groups leader. Takes an update
|
||||||
// argument which will update group
|
// argument which will update group
|
||||||
export function generateGroup(leader, update={}) {
|
export function generateGroup (leader, update = {}) {
|
||||||
let request = _requestMaker(leader, 'post');
|
let request = _requestMaker(leader, 'post');
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request('/groups').then((group) => {
|
request('/groups').then((group) => {
|
||||||
_updateDocument('groups', group, update, () => {
|
_updateDocument('groups', group, update, () => {
|
||||||
resolve(group);
|
resolve(group);
|
||||||
});
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
// This is generate group + the ability to create
|
// This is generate group + the ability to create
|
||||||
// real users to populate it. The settings object
|
// real users to populate it. The settings object
|
||||||
@@ -114,8 +120,12 @@ export function generateGroup(leader, update={}) {
|
|||||||
// invitees: an array of user objects that correspond to the invitees of the group
|
// invitees: an array of user objects that correspond to the invitees of the group
|
||||||
// leader: the leader user object
|
// leader: the leader user object
|
||||||
// group: the group object
|
// group: the group object
|
||||||
export function createAndPopulateGroup(settings={}) {
|
export function createAndPopulateGroup (settings = {}) {
|
||||||
let request, leader, members, invitees, group;
|
let request;
|
||||||
|
let leader;
|
||||||
|
let members;
|
||||||
|
let invitees;
|
||||||
|
let group;
|
||||||
|
|
||||||
let numberOfMembers = settings.members || 0;
|
let numberOfMembers = settings.members || 0;
|
||||||
let numberOfInvites = settings.invites || 0;
|
let numberOfInvites = settings.invites || 0;
|
||||||
@@ -156,37 +166,39 @@ export function createAndPopulateGroup(settings={}) {
|
|||||||
}).then((users) => {
|
}).then((users) => {
|
||||||
invitees = users;
|
invitees = users;
|
||||||
|
|
||||||
let invitePromises = [];
|
let invitationPromises = [];
|
||||||
|
|
||||||
each(invitees, (invitee) => {
|
each(invitees, (invitee) => {
|
||||||
let invitePromise = request(`/groups/${group._id}/invite`, {
|
let invitePromise = request(`/groups/${group._id}/invite`, {
|
||||||
uuids: [invitee._id]
|
uuids: [invitee._id],
|
||||||
});
|
});
|
||||||
invitePromises.push(invitePromise);
|
|
||||||
|
invitationPromises.push(invitePromise);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(invitePromises);
|
return Promise.all(invitationPromises);
|
||||||
}).then((inviteResults) => {
|
}).then(() => {
|
||||||
resolve({
|
resolve({
|
||||||
leader: leader,
|
leader,
|
||||||
group: group,
|
group,
|
||||||
members: members,
|
members,
|
||||||
invitees: invitees,
|
invitees,
|
||||||
});
|
});
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
// Specifically helpful for the GET /groups tests,
|
// Specifically helpful for the GET /groups tests,
|
||||||
// resets the db to an empty state and creates a tavern document
|
// resets the db to an empty state and creates a tavern document
|
||||||
export function resetHabiticaDB() {
|
export function resetHabiticaDB () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
|
|
||||||
db.dropDatabase((err) => {
|
db.dropDatabase((dbErr) => {
|
||||||
if (err) return reject(err);
|
if (dbErr) return reject(dbErr);
|
||||||
let groups = db.collection('groups');
|
let groups = db.collection('groups');
|
||||||
|
|
||||||
groups.insertOne({
|
groups.insertOne({
|
||||||
_id: 'habitrpg',
|
_id: 'habitrpg',
|
||||||
chat: [],
|
chat: [],
|
||||||
@@ -195,8 +207,8 @@ export function resetHabiticaDB() {
|
|||||||
type: 'guild',
|
type: 'guild',
|
||||||
privacy: 'public',
|
privacy: 'public',
|
||||||
members: [],
|
members: [],
|
||||||
}, (err) => {
|
}, (insertErr) => {
|
||||||
if (err) return reject(err);
|
if (insertErr) return reject(insertErr);
|
||||||
|
|
||||||
db.close();
|
db.close();
|
||||||
resolve();
|
resolve();
|
||||||
@@ -206,7 +218,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/v2${route}`)
|
||||||
@@ -228,29 +240,31 @@ function _requestMaker(user, method, additionalSets) {
|
|||||||
.end((err, response) => {
|
.end((err, response) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (!err.response) return reject(err);
|
if (!err.response) return reject(err);
|
||||||
let errorString = JSON.parse(err.response.text).err;
|
|
||||||
return reject({
|
return reject({
|
||||||
code: err.response.statusCode,
|
code: err.response.status,
|
||||||
text: errorString,
|
text: err.response.body.err,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve(response.body);
|
resolve(response.body);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function _updateDocument(collectionName, doc, update, cb) {
|
function _updateDocument (collectionName, doc, update, cb) {
|
||||||
if (isEmpty(update)) { return cb(); }
|
if (isEmpty(update)) {
|
||||||
|
return cb();
|
||||||
|
}
|
||||||
|
|
||||||
mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => {
|
mongo.connect('mongodb://localhost/habitrpg_test', (connectErr, db) => {
|
||||||
if (err) throw `Error connecting to database when updating ${collectionName} collection: ${err}`;
|
if (connectErr) throw new Error(`Error connecting to database when updating ${collectionName} collection: ${connectErr}`);
|
||||||
|
|
||||||
let collection = db.collection(collectionName);
|
let collection = db.collection(collectionName);
|
||||||
|
|
||||||
collection.update({ _id: doc._id }, { $set: update }, (err, result) => {
|
collection.update({ _id: doc._id }, { $set: update }, (updateErr) => {
|
||||||
if (err) throw `Error updating ${collectionName}: ${err}`;
|
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
|
||||||
assign(doc, update);
|
assign(doc, update);
|
||||||
db.close();
|
db.close();
|
||||||
cb();
|
cb();
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export function expectValidTranslationString (attribute) {
|
|||||||
expect(translatedString).to.not.be.empty;
|
expect(translatedString).to.not.be.empty;
|
||||||
expect(translatedString).to.not.eql(STRING_ERROR_MSG);
|
expect(translatedString).to.not.eql(STRING_ERROR_MSG);
|
||||||
expect(translatedString).to.not.match(STRING_DOES_NOT_EXIST_MSG);
|
expect(translatedString).to.not.match(STRING_DOES_NOT_EXIST_MSG);
|
||||||
};
|
}
|
||||||
|
|
||||||
export function describeEachItem (testDescription, set, cb, describeFunction) {
|
export function describeEachItem (testDescription, set, cb, describeFunction) {
|
||||||
// describeFunction allows you to pass in 'only' or 'skip'
|
// describeFunction allows you to pass in 'only' or 'skip'
|
||||||
@@ -34,8 +34,8 @@ export function describeEachItem (testDescription, set, cb, describeFunction) {
|
|||||||
|
|
||||||
describeEachItem.only = (des, set, cb) => {
|
describeEachItem.only = (des, set, cb) => {
|
||||||
describeEachItem(des, set, cb, 'only');
|
describeEachItem(des, set, cb, 'only');
|
||||||
}
|
};
|
||||||
|
|
||||||
describeEachItem.skip = (des, set, cb) => {
|
describeEachItem.skip = (des, set, cb) => {
|
||||||
describeEachItem(des, set, cb, 'skip');
|
describeEachItem(des, set, cb, 'skip');
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
|
/* eslint-disable no-undef */
|
||||||
//------------------------------
|
//------------------------------
|
||||||
// Global modules
|
// Global modules
|
||||||
//------------------------------
|
//------------------------------
|
||||||
|
|
||||||
global._ = require("lodash")
|
global._ = require('lodash');
|
||||||
global.chai = require("chai")
|
global.chai = require('chai');
|
||||||
chai.use(require("sinon-chai"))
|
chai.use(require('sinon-chai'));
|
||||||
chai.use(require("chai-as-promised"));
|
chai.use(require('chai-as-promised'));
|
||||||
global.expect = chai.expect
|
global.expect = chai.expect;
|
||||||
|
|||||||
Reference in New Issue
Block a user