Adding admin delete test case

This commit is contained in:
Chris Troutner
2019-05-20 15:57:28 -07:00
parent 093ae0ab3f
commit fe881f9275
4 changed files with 55 additions and 36 deletions
+29 -1
View File
@@ -20,9 +20,16 @@ describe('Users', () => {
password: 'pass2'
}
const testUser = await testUtils.createUser(userObj)
console.log(`testUser: ${JSON.stringify(testUser, null, 2)}`)
context.user2 = testUser.user
context.token2 = testUser.token
context.id2 = testUser.user._id
// Get the JWT used to log in as the admin 'system' user.
const adminJWT = await testUtils.getAdminJWT()
console.log(`adminJWT: ${adminJWT}`)
context.adminJWT = adminJWT
})
describe('POST /users', () => {
@@ -476,7 +483,7 @@ describe('Users', () => {
}
})
it('should delete user', async () => {
it('should delete own user', async () => {
const {
user: { _id },
token
@@ -498,5 +505,26 @@ describe('Users', () => {
assert.equal(result.body.success, true)
})
it('should delete other account when admin', async () => {
const id = context.id2
const adminJWT = context.adminJWT
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/${id}`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${adminJWT}`
}
}
const result = await rp(options)
console.log(`result: ${util.inspect(result.body)}`)
assert.equal(result.body.success, true)
})
})
})
+20 -1
View File
@@ -1,5 +1,6 @@
const mongoose = require('mongoose')
const rp = require('request-promise')
const config = require('../config')
const LOCALHOST = 'http://localhost:5000'
@@ -128,10 +129,28 @@ async function loginAdminUser () {
}
}
// Retrieve the admin user JWT token from the JSON file it's saved at.
async function getAdminJWT () {
try {
// process.env.KOA_ENV = process.env.KOA_ENV || 'dev'
// console.log(`env: ${process.env.KOA_ENV}`)
const FILENAME = `../config/system-user-test.json`
const adminUserData = require(FILENAME)
// console.log(`adminUserData: ${JSON.stringify(adminUserData, null, 2)}`)
return adminUserData.token
} catch (err) {
console.error('Error in test/utils.js/getAdminJWT()')
throw err
}
}
module.exports = {
cleanDb,
authUser,
createUser,
loginTestUser,
loginAdminUser
loginAdminUser,
getAdminJWT
}
-34
View File
@@ -1,34 +0,0 @@
const mongoose = require('mongoose')
const config = require('../config')
// Connect to the Mongo Database.
mongoose.Promise = global.Promise
mongoose.connect(config.database, () => {
// mongoose.connection.db.dropDatabase()
})
console.log(`config: ${JSON.stringify(config, null, 2)}`)
/*
// Wipe the DB.
function cleanDb () {
for (const collection in mongoose.connection.collections) {
if (mongoose.connection.collections.hasOwnProperty(collection)) {
mongoose.connection.collections[collection].remove()
}
}
console.log(`Database wiped.`)
}
cleanDb()
*/
mongoose.connection.close()
console.log(`
Here's how to wipe the db:
1. mongo
2. use p2pvps-server-dev
3. db.dropDatabase()
4. exit
`)
+6
View File
@@ -0,0 +1,6 @@
Here's how to wipe the db:
1. mongo
2. use koa-server-dev
3. db.dropDatabase()
4. exit