Got first test working with controlled start

This commit is contained in:
Chris Troutner
2018-03-24 10:52:38 -07:00
parent dfc9cdb0cc
commit bf58f90dad
5 changed files with 141 additions and 42 deletions
+82 -30
View File
@@ -1,19 +1,30 @@
const app = require('../bin/server')
const supertest = require('supertest')
//const { expect, should } = require('chai')
const expect = require('chai').expect;
const should = require('chai').should;
//const { cleanDb, authUser } = require('./utils')
const cleanDb = require('./utils').cleanDb
const authUser = require('./utils').authUser
// const supertest = require('supertest')
// const expect = require('chai').expect
const should = require('chai').should
// const cleanDb = require('./utils').cleanDb
// const authUser = require('./utils').authUser
const utils = require('./utils')
const rp = require('request-promise')
const assert = require('chai').assert
should()
const request = supertest.agent(app.listen())
// const request = supertest.agent(app.listen())
const context = {}
const LOCALHOST = 'http://localhost:5000'
describe('Auth', () => {
before((done) => {
cleanDb()
before(async () => {
await app.startServer()
utils.cleanDb()
/*
authUser(request, (err, { user, token }) => {
if (err) { return done(err) }
@@ -21,34 +32,75 @@ describe('Auth', () => {
context.token = token
done()
})
*/
const userObj = {
username: 'test',
password: 'pass'
}
const testUser = await utils.createUser(userObj)
context.user = testUser.user
context.token = testUser.token
})
describe('POST /auth', () => {
it('should throw 401 if credentials are incorrect', (done) => {
request
.post('/auth')
.set('Accept', 'application/json')
.send({ username: 'supercoolname', password: 'wrongpassword' })
.expect(401, done)
it('should throw 401 if credentials are incorrect', async () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
username: 'test',
password: 'wrongpassword'
}
}
let result = await rp(options)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
console.log(`result stringified: ${JSON.stringify(result, null, 2)}`)
assert(false, 'Unexpected result')
} catch (err) {
if (err.statusCode === 422) {
assert(err.statusCode === 422, 'Error code 422 expected.')
} else if (err.statusCode === 401) {
assert(err.statusCode === 401, 'Error code 401 expected.')
} else {
console.error('Error: ', err)
console.log('Error stringified: ' + JSON.stringify(err, null, 2))
throw err
}
}
})
it('should auth user', (done) => {
request
.post('/auth')
.set('Accept', 'application/json')
.send({ username: 'test', password: 'pass' })
.expect(200, (err, res) => {
if (err) { return done(err) }
it('should auth user', async () => {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/auth`,
resolveWithFullResponse: true,
json: true,
body: {
username: 'test',
password: 'pass'
}
}
res.body.user.should.have.property('username')
res.body.user.username.should.equal('test')
expect(res.body.user.password).to.not.exist
let result = await rp(options)
context.user = res.body.user
context.token = res.body.token
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
done()
})
assert(result.statusCode === 200, 'Status Code 200 expected.')
assert(result.body.user.username === 'test', 'Username of test expected')
assert(result.body.user.password === undefined, 'Password expected to be omited')
} catch (err) {
console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2))
throw err
}
})
})
})
+39 -1
View File
@@ -1,4 +1,7 @@
const mongoose = require('mongoose')
const rp = require('request-promise')
const LOCALHOST = 'http://localhost:5000'
function cleanDb () {
for (const collection in mongoose.connection.collections) {
@@ -23,7 +26,42 @@ function authUser (agent, callback) {
})
}
// This function is used to create new users.
// userObj = {
// username,
// password
// }
async function createUser (userObj) {
try {
const options = {
method: 'POST',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
body: {
user: {
username: userObj.username,
password: userObj.password
}
}
}
let result = await rp(options)
const retObj = {
user: result.body.user,
token: result.body.token
}
return retObj
} catch (err) {
console.log('Error in utils.js/createUser(): ' + JSON.stringify(err, null, 2))
throw err
}
}
module.exports = {
cleanDb,
authUser
authUser,
createUser
}