From 361d0aa35cf760c01cac94c472837dda66504fed Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 29 Jan 2016 12:52:57 +0100 Subject: [PATCH] finally fix tests --- test/helpers/api-unit.helper.js | 1 + test/helpers/globals.helper.js | 1 + test/helpers/mongo.js | 1 - website/src/models/group.js | 31 +++++++++++++++++-------------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/test/helpers/api-unit.helper.js b/test/helpers/api-unit.helper.js index ffef97ab82..3370bd0887 100644 --- a/test/helpers/api-unit.helper.js +++ b/test/helpers/api-unit.helper.js @@ -3,6 +3,7 @@ import mongoose from 'mongoose'; import { defaultsDeep as defaults } from 'lodash'; import { model as User } from '../../website/src/models/user'; import { model as Group } from '../../website/src/models/group'; +import mongo from './mongo'; // eslint-disable-line afterEach((done) => { sandbox.restore(); diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index 7b4a96070e..2bfe3c8a56 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -18,6 +18,7 @@ import nconf from 'nconf'; //------------------------------ require('../../website/src/libs/api-v3/setupNconf')('./config.json.example'); nconf.set('NODE_DB_URI', 'mongodb://localhost/habitrpg_test'); +nconf.set('NODE_ENV', 'test'); // We require src/server and npt src/index because // 1. nconf is already setup // 2. we don't need clustering diff --git a/test/helpers/mongo.js b/test/helpers/mongo.js index 88535da38b..d99c195c3c 100644 --- a/test/helpers/mongo.js +++ b/test/helpers/mongo.js @@ -20,7 +20,6 @@ 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 () { - console.log('calling resetHabiticaDatabase'); // eslint-disable-line return new Promise((resolve, reject) => { mongoose.connection.db.dropDatabase((dbErr) => { if (dbErr) return reject(dbErr); diff --git a/website/src/models/group.js b/website/src/models/group.js index 0acc03fe32..2d2b4e3e71 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -11,6 +11,7 @@ import { removeFromArray } from '../libs/api-v3/collectionManipulators'; import * as firebase from '../libs/api-v2/firebase'; import baseModel from '../libs/api-v3/baseModel'; import Q from 'q'; +import nconf from 'nconf'; let Schema = mongoose.Schema; @@ -513,17 +514,19 @@ export const INVITES_LIMIT = 100; export let model = mongoose.model('Group', schema); // initialize tavern if !exists (fresh installs) -model.count({_id: 'habitrpg'}, (err, ct) => { - if (err) throw err; - if (ct > 0) return; - - new model({ // eslint-disable-line babel/new-cap - _id: 'habitrpg', - leader: '9', // TODO change this user id - name: 'HabitRPG', - type: 'guild', - privacy: 'public', - }).save({ - validateBeforeSave: false, // _id = 'habitrpg' would not be valid otherwise - }); // TODO catch/log? -}); +// do not run when testing as it's handled by the tests and can easily cause a race condition +if (nconf.get('NODE_ENV') !== 'test') { + model.count({_id: 'habitrpg'}, (err, ct) => { + if (err) throw err; + if (ct > 0) return; + new model({ // eslint-disable-line babel/new-cap + _id: 'habitrpg', + leader: '9', // TODO change this user id + name: 'HabitRPG', + type: 'guild', + privacy: 'public', + }).save({ + validateBeforeSave: false, // _id = 'habitrpg' would not be valid otherwise + }); // TODO catch/log? + }); +}