From 91861e657af09e6e9cdb98b3705ea036ff008115 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 20 May 2019 18:35:02 -0700 Subject: [PATCH] fix(admin bug): fixed a bug with admin account and tests --- src/lib/admin.js | 20 +++++++++++++++----- test/a01-auth.spec.js | 14 ++------------ test/a02-users.spec.js | 21 +++++++++++++++++---- test/utils.js | 23 ++--------------------- 4 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/lib/admin.js b/src/lib/admin.js index 752924c..0c5033e 100644 --- a/src/lib/admin.js +++ b/src/lib/admin.js @@ -53,10 +53,17 @@ async function createSystemUser () { // Change the user type to admin user.type = 'admin' - // console.log(`user: ${JSON.stringify(user, null, 2)}`) + console.log(`user: ${JSON.stringify(user, null, 2)}`) // Save the user model. - await user.save() + try { + await user.save() + } catch (err) { + console.error(`Error trying to save admin user: `, err) + } + + console.log(`admin user created: ${JSON.stringify(result.body, null, 2)}`) + console.log(`with password: ${context.password}`) // Write out the system user information to a JSON file that external // applications like the Task Manager and the test scripts can access. @@ -114,9 +121,9 @@ async function deleteExistingSystemUser () { async function loginAdmin () { // console.log(`loginAdmin() running.`) - try { - let existingUser + let existingUser + try { // Read the exising file existingUser = await jsonFiles.readJSON(JSON_PATH) // console.log(`existingUser: ${JSON.stringify(existingUser, null, 2)}`) @@ -137,7 +144,10 @@ async function loginAdmin () { return result } catch (err) { - console.error(`Error in admin.js/loginAdmin().`) + console.error(`Error in admin.js/loginAdmin().`, err) + + console.error(`existingUser: ${JSON.stringify(existingUser, null, 2)}`) + throw err } } diff --git a/test/a01-auth.spec.js b/test/a01-auth.spec.js index 2d5bddf..c5c0c36 100644 --- a/test/a01-auth.spec.js +++ b/test/a01-auth.spec.js @@ -11,19 +11,9 @@ const LOCALHOST = `http://localhost:${config.port}` describe('Auth', () => { before(async () => { - await app.startServer() + utils.cleanDb() // This should be first instruction. - utils.cleanDb() - - /* - authUser(request, (err, { user, token }) => { - if (err) { return done(err) } - - context.user = user - context.token = token - done() - }) - */ + await app.startServer() // This should be second instruction. const userObj = { username: 'test', diff --git a/test/a02-users.spec.js b/test/a02-users.spec.js index e656b35..5ee5fbb 100644 --- a/test/a02-users.spec.js +++ b/test/a02-users.spec.js @@ -2,6 +2,7 @@ const testUtils = require('./utils') const rp = require('request-promise') const assert = require('chai').assert const config = require('../config') +const adminLib = require('../src/lib/admin') const util = require('util') util.inspect.defaultOptions = { depth: 1 } @@ -12,7 +13,7 @@ const context = {} describe('Users', () => { before(async () => { - testUtils.cleanDb() + console.log(`config: ${JSON.stringify(config, null, 2)}`) // Create a second test user. const userObj = { @@ -20,7 +21,7 @@ describe('Users', () => { password: 'pass2' } const testUser = await testUtils.createUser(userObj) - console.log(`testUser: ${JSON.stringify(testUser, null, 2)}`) + console.log(`testUser2: ${JSON.stringify(testUser, null, 2)}`) context.user2 = testUser.user context.token2 = testUser.token @@ -30,6 +31,12 @@ describe('Users', () => { const adminJWT = await testUtils.getAdminJWT() console.log(`adminJWT: ${adminJWT}`) context.adminJWT = adminJWT + + // const admin = await testUtils.loginAdminUser() + // context.adminJWT = admin.token + + // const admin = await adminLib.loginAdmin() + // console.log(`admin: ${JSON.stringify(admin, null, 2)}`) }) describe('POST /users', () => { @@ -81,8 +88,14 @@ describe('Users', () => { context.token = result.body.token assert(result.statusCode === 200, 'Status Code 200 expected.') - assert(result.body.user.username === 'supercoolname', 'Username of test expected') - assert(result.body.user.password === undefined, 'Password expected to be omited') + assert( + result.body.user.username === 'supercoolname', + 'Username of test expected' + ) + assert( + result.body.user.password === undefined, + 'Password expected to be omited' + ) assert.property(result.body, 'token', 'Token property exists.') } catch (err) { console.log( diff --git a/test/utils.js b/test/utils.js index 84e14b6..c0a2852 100644 --- a/test/utils.js +++ b/test/utils.js @@ -13,21 +13,6 @@ function cleanDb () { } } -function authUser (agent, callback) { - agent - .post('/users') - .set('Accept', 'application/json') - .send({ user: { username: 'test', password: 'pass' } }) - .end((err, res) => { - if (err) { return callback(err) } - - callback(null, { - user: res.body.user, - token: res.body.token - }) - }) -} - // This function is used to create new users. // userObj = { // username, @@ -94,10 +79,7 @@ async function loginTestUser () { async function loginAdminUser () { try { - process.env.NODE_ENV = process.env.NODE_ENV || 'dev' - console.log(`env: ${process.env.NODE_ENV}`) - - const FILENAME = `../config/system-user-${process.env.NODE_ENV}.json` + const FILENAME = `../config/system-user-${config.env}.json` const adminUserData = require(FILENAME) console.log(`adminUserData: ${JSON.stringify(adminUserData, null, 2)}`) @@ -135,7 +117,7 @@ async function getAdminJWT () { // process.env.KOA_ENV = process.env.KOA_ENV || 'dev' // console.log(`env: ${process.env.KOA_ENV}`) - const FILENAME = `../config/system-user-test.json` + const FILENAME = `../config/system-user-${config.env}.json` const adminUserData = require(FILENAME) // console.log(`adminUserData: ${JSON.stringify(adminUserData, null, 2)}`) @@ -148,7 +130,6 @@ async function getAdminJWT () { module.exports = { cleanDb, - authUser, createUser, loginTestUser, loginAdminUser,