fix(admin bug): fixed a bug with admin account and tests

This commit is contained in:
Chris Troutner
2019-05-20 18:35:02 -07:00
parent fe881f9275
commit 91861e657a
4 changed files with 36 additions and 42 deletions
+15 -5
View File
@@ -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
}
}
+2 -12
View File
@@ -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',
+17 -4
View File
@@ -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(
+2 -21
View File
@@ -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,