mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
failing unit test
This commit is contained in:
@@ -94,7 +94,7 @@
|
||||
"npm": "^3.3.10"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "gulp lint && (gulp test:nodemon & (sleep 20; mocha test/api/v3 --recursive; killall gulp; killall node;))",
|
||||
"test": "gulp lint && npm run test:api-v3:unit && npm run test:api-v3:integration",
|
||||
"test:api-v2:unit": "mocha test/server_side",
|
||||
"test:api-v2:integration": "mocha test/api/v2 --recursive",
|
||||
"test:api-v3": "mocha test/api/v3 --recursive",
|
||||
|
||||
@@ -13,6 +13,8 @@ import Q from 'q';
|
||||
import runSequence from 'run-sequence';
|
||||
import os from 'os';
|
||||
|
||||
// TODO rewrite
|
||||
|
||||
const TEST_SERVER_PORT = 3003
|
||||
const TEST_DB = 'habitrpg_test'
|
||||
let server;
|
||||
|
||||
@@ -29,7 +29,7 @@ describe('analytics middleware', () => {
|
||||
|
||||
attachAnalytics(req, res, next);
|
||||
|
||||
expect(res.analytics).to.exist;
|
||||
expect(res.analytics).to.not.exist;
|
||||
});
|
||||
|
||||
it('attaches stubbed methods for non-prod environments', () => {
|
||||
@@ -53,4 +53,3 @@ describe('analytics middleware', () => {
|
||||
expect(res.analytics.trackPurchase).to.eql(analyticsService.trackPurchase);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { requester } from './requester';
|
||||
import {
|
||||
getDocument as getDocumentFromMongo,
|
||||
updateDocument as updateDocumentInMongo,
|
||||
} from './mongo';
|
||||
} from '../mongo';
|
||||
import {
|
||||
assign,
|
||||
each,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
import superagent from 'superagent';
|
||||
import nconf from 'nconf';
|
||||
import app from '../../../website/src/server';
|
||||
|
||||
const API_TEST_SERVER_PORT = nconf.get('PORT');
|
||||
let apiVersion;
|
||||
|
||||
@@ -6,7 +6,7 @@ requester.setApiVersion('v3');
|
||||
export { requester };
|
||||
|
||||
export { translate } from '../translate';
|
||||
export { checkExistence, resetHabiticaDB } from '../mongo';
|
||||
export { checkExistence, resetHabiticaDB } from '../../mongo';
|
||||
export * from './object-generators';
|
||||
|
||||
export async function sleep (seconds) {
|
||||
|
||||
@@ -4,21 +4,9 @@ import { defaultsDeep as defaults } from 'lodash';
|
||||
import { model as User } from '../../website/src/models/user';
|
||||
import { model as Group } from '../../website/src/models/group';
|
||||
|
||||
let connection = mongoose.connection;
|
||||
|
||||
before((done) => {
|
||||
connection.on('open', () => {
|
||||
connection.db.dropDatabase(done);
|
||||
});
|
||||
});
|
||||
|
||||
after((done) => {
|
||||
connection.close(done);
|
||||
});
|
||||
|
||||
afterEach((done) => {
|
||||
sandbox.restore();
|
||||
connection.db.dropDatabase(done);
|
||||
mongoose.connection.db.dropDatabase(done);
|
||||
});
|
||||
|
||||
export function generateUser (options = {}) {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import mongoose from 'mongoose';
|
||||
import Q from 'q';
|
||||
|
||||
import { wrap as wrapUser } from '../../common/script/index';
|
||||
import { model as User } from '../../website/src/models/user';
|
||||
|
||||
@@ -11,7 +11,14 @@ global.expect = chai.expect;
|
||||
global.sinon = require('sinon');
|
||||
global.sandbox = sinon.sandbox.create();
|
||||
|
||||
import nconf from 'nconf';
|
||||
|
||||
//------------------------------
|
||||
// Load nconf for unit tests
|
||||
//------------------------------
|
||||
require('../../website/src/libs/api-v3/setupNconf')('./config.json.example');
|
||||
nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_test');
|
||||
// We require src/server and npt src/index because
|
||||
// 1. nconf is already setup
|
||||
// 2. we don't need clustering
|
||||
require('../../website/src/server');
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
/* eslint-disable no-use-before-define */
|
||||
import nconf from 'nconf';
|
||||
import { MongoClient as mongo } from 'mongodb';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
// Useful for checking things that have been deleted,
|
||||
// but you no longer have access to,
|
||||
// like private parties or users
|
||||
export async function checkExistence (collectionName, id) {
|
||||
let db = await connectToMongo();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let collection = db.collection(collectionName);
|
||||
let collection = mongoose.connection.db.collection(collectionName);
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -25,12 +20,10 @@ export async function checkExistence (collectionName, id) {
|
||||
// Specifically helpful for the GET /groups tests,
|
||||
// resets the db to an empty state and creates a tavern document
|
||||
export async function resetHabiticaDB () {
|
||||
let db = await connectToMongo();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
db.dropDatabase((dbErr) => {
|
||||
mongoose.connection.db.dropDatabase((dbErr) => {
|
||||
if (dbErr) return reject(dbErr);
|
||||
let groups = db.collection('groups');
|
||||
let groups = mongoose.connection.db.collection('groups');
|
||||
|
||||
groups.insertOne({
|
||||
_id: 'habitrpg',
|
||||
@@ -42,7 +35,6 @@ export async function resetHabiticaDB () {
|
||||
}, (insertErr) => {
|
||||
if (insertErr) return reject(insertErr);
|
||||
|
||||
db.close();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
@@ -50,39 +42,39 @@ export async function resetHabiticaDB () {
|
||||
}
|
||||
|
||||
export async function updateDocument (collectionName, doc, update) {
|
||||
let db = await connectToMongo();
|
||||
|
||||
let collection = db.collection(collectionName);
|
||||
let collection = mongoose.connection.db.collection(collectionName);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
collection.updateOne({ _id: doc._id }, { $set: update }, (updateErr) => {
|
||||
if (updateErr) throw new Error(`Error updating ${collectionName}: ${updateErr}`);
|
||||
db.close();
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function getDocument (collectionName, doc) {
|
||||
let db = await connectToMongo();
|
||||
|
||||
let collection = db.collection(collectionName);
|
||||
let collection = mongoose.connection.db.collection(collectionName);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
collection.findOne({ _id: doc._id }, (lookupErr, found) => {
|
||||
if (lookupErr) throw new Error(`Error looking up ${collectionName}: ${lookupErr}`);
|
||||
db.close();
|
||||
resolve(found);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function connectToMongo () {
|
||||
return new Promise((resolve, reject) => {
|
||||
mongo.connect(nconf.get('NODE_DB_URI'), (err, db) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
resolve(db);
|
||||
});
|
||||
before((done) => {
|
||||
mongoose.connection.on('open', (err) => {
|
||||
if (err) return done(err);
|
||||
resetHabiticaDB()
|
||||
.then(() => done())
|
||||
.catch(done);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
after((done) => {
|
||||
mongoose.connection.db.dropDatabase((err) => {
|
||||
if (err) return done(err);
|
||||
mongoose.connection.close(done);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user