From eded44fea5bda8678e3101553cfde04997c3d52e Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 18 Nov 2015 08:54:34 -0600 Subject: [PATCH] Adjust helper files for linting preferences. --- test/helpers/api-integration.helper.js | 114 ++++++++++++++----------- test/helpers/content.helper.js | 6 +- test/helpers/globals.helper.js | 11 +-- 3 files changed, 73 insertions(+), 58 deletions(-) diff --git a/test/helpers/api-integration.helper.js b/test/helpers/api-integration.helper.js index b6b3464c9b..c8a452f7c3 100644 --- a/test/helpers/api-integration.helper.js +++ b/test/helpers/api-integration.helper.js @@ -1,11 +1,13 @@ +/* eslint-disable no-use-before-define */ + import { assign, each, isEmpty, times, } from 'lodash'; -import {MongoClient as mongo} from 'mongodb'; -import {v4 as generateUUID} from 'uuid'; +import { MongoClient as mongo } from 'mongodb'; +import { v4 as generateUUID } from 'uuid'; import superagent from 'superagent'; import i18n from '../../common/script/src/i18n'; 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 // If a user is passed in, the uuid and api token of // the user are used to make the requests -export function requester(user={}, additionalSets) { +export function requester (user = {}, additionalSets) { return { get: _requestMaker(user, 'get', additionalSets), post: _requestMaker(user, 'post', additionalSets), put: _requestMaker(user, 'put', additionalSets), del: _requestMaker(user, 'del', additionalSets), - } -}; + }; +} // Use this to verify error messages returned by the server // That way, if the translated string changes, the test // 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_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); return translatedString; -}; +} // Useful for checking things that have been deleted, // but you no longer have access to, // like private parties or users -export function checkExistence(collectionName, id) { +export function checkExistence (collectionName, id) { return new Promise((resolve, reject) => { - mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => { - if (err) return reject(err); + mongo.connect('mongodb://localhost/habitrpg_test', (connectionError, db) => { + if (connectionError) return reject(connectionError); 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; + db.close(); resolve(exists); }); @@ -64,41 +70,41 @@ export function checkExistence(collectionName, id) { // paramter, such as the number of wolf eggs the user has, // , you can do so by passing in the full path as a string: // { 'items.eggs.Wolf': 10 } -export function generateUser(update={}) { +export function generateUser (update = {}) { let username = generateUUID(); - let password = 'password' - let email = username + '@example.com'; + let password = 'password'; + let email = `${username}@example.com`; let request = _requestMaker({}, 'post'); return new Promise((resolve, reject) => { request('/register', { - username: username, - email: email, - password: password, + username, + email, + password, confirmPassword: password, }).then((user) => { _updateDocument('users', user, update, () => { resolve(user); }); - }); + }).catch(reject); }); -}; +} // Generates a new group. Requires a user object, which // will will become the groups leader. Takes an update // argument which will update group -export function generateGroup(leader, update={}) { +export function generateGroup (leader, update = {}) { let request = _requestMaker(leader, 'post'); return new Promise((resolve, reject) => { request('/groups').then((group) => { _updateDocument('groups', group, update, () => { resolve(group); - }); + }).catch(reject); }); }); -}; +} // This is generate group + the ability to create // 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 // leader: the leader user object // group: the group object -export function createAndPopulateGroup(settings={}) { - let request, leader, members, invitees, group; +export function createAndPopulateGroup (settings = {}) { + let request; + let leader; + let members; + let invitees; + let group; let numberOfMembers = settings.members || 0; let numberOfInvites = settings.invites || 0; @@ -156,37 +166,39 @@ export function createAndPopulateGroup(settings={}) { }).then((users) => { invitees = users; - let invitePromises = []; + let invitationPromises = []; each(invitees, (invitee) => { let invitePromise = request(`/groups/${group._id}/invite`, { - uuids: [invitee._id] + uuids: [invitee._id], }); - invitePromises.push(invitePromise); + + invitationPromises.push(invitePromise); }); - return Promise.all(invitePromises); - }).then((inviteResults) => { + return Promise.all(invitationPromises); + }).then(() => { resolve({ - leader: leader, - group: group, - members: members, - invitees: invitees, + leader, + group, + members, + invitees, }); }).catch(reject); }); -}; +} // Specifically helpful for the GET /groups tests, // resets the db to an empty state and creates a tavern document -export function resetHabiticaDB() { +export function resetHabiticaDB () { return new Promise((resolve, reject) => { mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => { if (err) return reject(err); - db.dropDatabase((err) => { - if (err) return reject(err); + db.dropDatabase((dbErr) => { + if (dbErr) return reject(dbErr); let groups = db.collection('groups'); + groups.insertOne({ _id: 'habitrpg', chat: [], @@ -195,8 +207,8 @@ export function resetHabiticaDB() { type: 'guild', privacy: 'public', members: [], - }, (err) => { - if (err) return reject(err); + }, (insertErr) => { + if (insertErr) return reject(insertErr); db.close(); resolve(); @@ -206,7 +218,7 @@ export function resetHabiticaDB() { }); } -function _requestMaker(user, method, additionalSets) { +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}`) @@ -228,29 +240,31 @@ function _requestMaker(user, method, additionalSets) { .end((err, response) => { if (err) { if (!err.response) return reject(err); - let errorString = JSON.parse(err.response.text).err; + return reject({ - code: err.response.statusCode, - text: errorString, + code: err.response.status, + text: err.response.body.err, }); } resolve(response.body); }); }); - } + }; } -function _updateDocument(collectionName, doc, update, cb) { - if (isEmpty(update)) { return cb(); } +function _updateDocument (collectionName, doc, update, cb) { + if (isEmpty(update)) { + return cb(); + } - mongo.connect('mongodb://localhost/habitrpg_test', (err, db) => { - if (err) throw `Error connecting to database when updating ${collectionName} collection: ${err}`; + mongo.connect('mongodb://localhost/habitrpg_test', (connectErr, db) => { + if (connectErr) throw new Error(`Error connecting to database when updating ${collectionName} collection: ${connectErr}`); let collection = db.collection(collectionName); - collection.update({ _id: doc._id }, { $set: update }, (err, result) => { - if (err) throw `Error updating ${collectionName}: ${err}`; + collection.update({ _id: doc._id }, { $set: update }, (updateErr) => { + if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`); assign(doc, update); db.close(); cb(); diff --git a/test/helpers/content.helper.js b/test/helpers/content.helper.js index db95a1f0b3..92acd80310 100644 --- a/test/helpers/content.helper.js +++ b/test/helpers/content.helper.js @@ -15,7 +15,7 @@ export function expectValidTranslationString (attribute) { expect(translatedString).to.not.be.empty; expect(translatedString).to.not.eql(STRING_ERROR_MSG); expect(translatedString).to.not.match(STRING_DOES_NOT_EXIST_MSG); -}; +} export function describeEachItem (testDescription, set, cb, describeFunction) { // 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(des, set, cb, 'only'); -} +}; describeEachItem.skip = (des, set, cb) => { describeEachItem(des, set, cb, 'skip'); -} +}; diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index a731f1e8c2..21f60b7f46 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -1,9 +1,10 @@ +/* eslint-disable no-undef */ //------------------------------ // Global modules //------------------------------ -global._ = require("lodash") -global.chai = require("chai") -chai.use(require("sinon-chai")) -chai.use(require("chai-as-promised")); -global.expect = chai.expect +global._ = require('lodash'); +global.chai = require('chai'); +chai.use(require('sinon-chai')); +chai.use(require('chai-as-promised')); +global.expect = chai.expect;