Fixed startup bug with admin in tests

This commit is contained in:
Chris Troutner
2019-05-20 20:24:26 -07:00
parent 91861e657a
commit 2c34e56bc3
7 changed files with 72 additions and 7 deletions
+6 -1
View File
@@ -1,3 +1,4 @@
// npm libraries
const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const convert = require('koa-convert')
@@ -8,8 +9,10 @@ const passport = require('koa-passport')
const mount = require('koa-mount')
const serve = require('koa-static')
const cors = require('kcors')
// Local libraries
const config = require('../config') // this first.
const adminLib = require('../src/lib/admin')
const config = require('../config')
const errorMiddleware = require('../src/middleware')
async function startServer () {
@@ -47,6 +50,8 @@ async function startServer () {
// MIDDLEWARE END
console.log(`Running server in environment: ${config.env}`)
await app.listen(config.port)
console.log(`Server started on ${config.port}`)
+3 -2
View File
@@ -5,11 +5,12 @@
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "KOA_ENV=test nyc --reporter=text --timeout 15000 ./node_modules/.bin/mocha --exit",
"test": "npm run prep-test && KOA_ENV=test nyc --reporter=text --timeout 15000 ./node_modules/.bin/mocha --exit",
"lint": "eslint src/**/*.js",
"docs": "./node_modules/.bin/apidoc -i src/ -o docs",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"coverage:report": "nyc --reporter=html mocha --exit"
"coverage:report": "nyc --reporter=html mocha --exit",
"prep-test":"node util/users/delete-all-test-users.js"
},
"keywords": [
"koa-api-boilerplate",
+1 -1
View File
@@ -126,7 +126,7 @@ async function loginAdmin () {
try {
// Read the exising file
existingUser = await jsonFiles.readJSON(JSON_PATH)
// console.log(`existingUser: ${JSON.stringify(existingUser, null, 2)}`)
console.log(`existingUser: ${JSON.stringify(existingUser, null, 2)}`)
// Log in as the user.
let options = {
+1 -1
View File
@@ -11,7 +11,7 @@ const LOCALHOST = `http://localhost:${config.port}`
describe('Auth', () => {
before(async () => {
utils.cleanDb() // This should be first instruction.
// await utils.cleanDb() // This should be first instruction.
await app.startServer() // This should be second instruction.
+2 -2
View File
@@ -5,10 +5,10 @@ const config = require('../config')
const LOCALHOST = 'http://localhost:5000'
// Remove all collections from the DB.
function cleanDb () {
async function cleanDb () {
for (const collection in mongoose.connection.collections) {
if (mongoose.connection.collections.hasOwnProperty(collection)) {
mongoose.connection.collections[collection].deleteMany()
await mongoose.connection.collections[collection].deleteMany()
}
}
}
+27
View File
@@ -0,0 +1,27 @@
const mongoose = require('mongoose')
// Force test environment
process.env.KOA_ENV = 'test'
const config = require('../../config')
const User = require('../../src/models/users')
async function deleteUsers () {
// Connect to the Mongo Database.
mongoose.Promise = global.Promise
mongoose.set('useCreateIndex', true) // Stop deprecation warning.
await mongoose.connect(config.database, { useNewUrlParser: true })
// Get all the users in the DB.
const users = await User.find({}, '-password')
// console.log(`users: ${JSON.stringify(users, null, 2)}`)
// Delete each user.
for (let i = 0; i < users.length; i++) {
const thisUser = users[i]
await thisUser.remove()
}
mongoose.connection.close()
}
deleteUsers()
+32
View File
@@ -0,0 +1,32 @@
/*
Utility app to wipe the test database.
*/
'use strict'
const mongoose = require('mongoose')
// Force test environment
process.env.KOA_ENV = 'test'
const config = require('../config')
async function cleanDb () {
// Connect to the Mongo Database.
mongoose.Promise = global.Promise
mongoose.set('useCreateIndex', true) // Stop deprecation warning.
await mongoose.connect(config.database, { useNewUrlParser: true })
console.log(`mongoose.connection.collections: ${JSON.stringify(mongoose.connection.collections, null, 2)}`)
for (const collection in mongoose.connection.collections) {
if (mongoose.connection.collections.hasOwnProperty(collection)) {
const thisCollection = mongoose.connection.collections[collection]
console.log(`thisCollection: ${JSON.stringify(thisCollection, null, 2)}`)
await collection.deleteMany()
}
}
mongoose.connection.close()
}
cleanDb()