finally fix tests

This commit is contained in:
Matteo Pagliazzi
2016-01-29 12:52:57 +01:00
parent 80160597b0
commit 361d0aa35c
4 changed files with 19 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ import mongoose from 'mongoose';
import { defaultsDeep as defaults } from 'lodash'; import { defaultsDeep as defaults } from 'lodash';
import { model as User } from '../../website/src/models/user'; import { model as User } from '../../website/src/models/user';
import { model as Group } from '../../website/src/models/group'; import { model as Group } from '../../website/src/models/group';
import mongo from './mongo'; // eslint-disable-line
afterEach((done) => { afterEach((done) => {
sandbox.restore(); sandbox.restore();

View File

@@ -18,6 +18,7 @@ import nconf from 'nconf';
//------------------------------ //------------------------------
require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); require('../../website/src/libs/api-v3/setupNconf')('./config.json.example');
nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_test'); nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_test');
nconf.set('NODE_ENV', 'test');
// We require src/server and npt src/index because // We require src/server and npt src/index because
// 1. nconf is already setup // 1. nconf is already setup
// 2. we don't need clustering // 2. we don't need clustering

View File

@@ -20,7 +20,6 @@ export async function checkExistence (collectionName, id) {
// 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 async function resetHabiticaDB () { export async function resetHabiticaDB () {
console.log('calling resetHabiticaDatabase'); // eslint-disable-line
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
mongoose.connection.db.dropDatabase((dbErr) => { mongoose.connection.db.dropDatabase((dbErr) => {
if (dbErr) return reject(dbErr); if (dbErr) return reject(dbErr);

View File

@@ -11,6 +11,7 @@ import { removeFromArray } from '../libs/api-v3/collectionManipulators';
import * as firebase from '../libs/api-v2/firebase'; import * as firebase from '../libs/api-v2/firebase';
import baseModel from '../libs/api-v3/baseModel'; import baseModel from '../libs/api-v3/baseModel';
import Q from 'q'; import Q from 'q';
import nconf from 'nconf';
let Schema = mongoose.Schema; let Schema = mongoose.Schema;
@@ -513,17 +514,19 @@ export const INVITES_LIMIT = 100;
export let model = mongoose.model('Group', schema); export let model = mongoose.model('Group', schema);
// initialize tavern if !exists (fresh installs) // initialize tavern if !exists (fresh installs)
model.count({_id: 'habitrpg'}, (err, ct) => { // do not run when testing as it's handled by the tests and can easily cause a race condition
if (err) throw err; if (nconf.get('NODE_ENV') !== 'test') {
if (ct > 0) return; model.count({_id: 'habitrpg'}, (err, ct) => {
if (err) throw err;
new model({ // eslint-disable-line babel/new-cap if (ct > 0) return;
_id: 'habitrpg', new model({ // eslint-disable-line babel/new-cap
leader: '9', // TODO change this user id _id: 'habitrpg',
name: 'HabitRPG', leader: '9', // TODO change this user id
type: 'guild', name: 'HabitRPG',
privacy: 'public', type: 'guild',
}).save({ privacy: 'public',
validateBeforeSave: false, // _id = 'habitrpg' would not be valid otherwise }).save({
}); // TODO catch/log? validateBeforeSave: false, // _id = 'habitrpg' would not be valid otherwise
}); }); // TODO catch/log?
});
}