Merge pull request #2 from christroutner/unstable

Reenabling user endpoint tests
This commit is contained in:
Chris Troutner
2018-11-05 12:03:03 -08:00
committed by GitHub
3 changed files with 264 additions and 130 deletions
+2 -2
View File
@@ -51,8 +51,8 @@ User.methods.generateToken = function generateToken () {
const user = this
const token = jwt.sign({ id: user.id }, config.token)
//console.log(`config.token: ${config.token}`)
//console.log(`generated token: ${token}`)
// console.log(`config.token: ${config.token}`)
// console.log(`generated token: ${token}`)
return token
}
+1 -1
View File
@@ -213,7 +213,7 @@ async function updateUser (ctx) {
async function deleteUser (ctx) {
const user = ctx.body.user
await user.deleteOne()
await user.remove()
ctx.status = 200
ctx.body = {
+261 -127
View File
@@ -5,6 +5,9 @@ const utils = require('./utils')
const rp = require('request-promise')
const assert = require('chai').assert
const util = require('util')
util.inspect.defaultOptions = { depth: 1 }
const LOCALHOST = 'http://localhost:5000'
should()
@@ -65,205 +68,336 @@ describe('Users', () => {
context.user = result.body.user
context.token = result.body.token
} catch (err) {
console.log('Error authenticating test user: ' + JSON.stringify(err, null, 2))
console.log(
'Error authenticating test user: ' + JSON.stringify(err, null, 2)
)
throw err
}
})
})
/*
describe('GET /users', () => {
it('should not fetch users if the authorization header is missing', (done) => {
request
.get('/users')
.set('Accept', 'application/json')
.expect(401, done)
it('should not fetch users if the authorization header is missing', async () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json'
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should not fetch users if the authorization header is missing the scheme', (done) => {
request
.get('/users')
.set({
Accept: 'application/json',
Authorization: '1'
})
.expect(401, done)
it('should not fetch users if the authorization header is missing the scheme', async () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: '1'
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should not fetch users if the authorization header has invalid scheme', (done) => {
it('should not fetch users if the authorization header has invalid scheme', async () => {
const { token } = context
request
.get('/users')
.set({
Accept: 'application/json',
Authorization: `Unknown ${token}`
})
.expect(401, done)
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Unknown ${token}`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should not fetch users if token is invalid', (done) => {
request
.get('/users')
.set({
Accept: 'application/json',
Authorization: 'Bearer 1'
})
.expect(401, done)
it('should not fetch users if token is invalid', async () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should fetch all users', (done) => {
it('should fetch all users', async () => {
const { token } = context
request
.get('/users')
.set({
const options = {
method: 'GET',
uri: `${LOCALHOST}/users`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.expect(200, (err, res) => {
if (err) { return done(err) }
}
}
res.body.should.have.property('users')
const result = await rp(options)
const users = result.body.users
// console.log(`users: ${util.inspect(users)}`)
res.body.users.should.have.length(1)
done()
})
assert.hasAnyKeys(users[0], ['type', '_id', 'username'])
assert.equal(users.length, 1)
})
})
describe('GET /users/:id', () => {
it('should not fetch user if token is invalid', (done) => {
request
.get('/users/1')
.set({
Accept: 'application/json',
Authorization: 'Bearer 1'
})
.expect(401, done)
it('should not fetch user if token is invalid', async () => {
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should throw 404 if user doesn\'t exist', (done) => {
it("should throw 404 if user doesn't exist", async () => {
const { token } = context
request
.get('/users/1')
.set({
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.expect(404, done)
try {
const options = {
method: 'GET',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 404)
}
})
it('should fetch user', (done) => {
it('should fetch user', async () => {
const {
user: { _id },
token
} = context
request
.get(`/users/${_id}`)
.set({
const options = {
method: 'GET',
uri: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.expect(200, (err, res) => {
if (err) { return done(err) }
}
}
// console.log(`res: ${JSON.stringify(res, null, 2)}`)
const result = await rp(options)
const user = result.body.user
// console.log(`user: ${util.inspect(user)}`)
res.body.should.have.property('user')
expect(res.body.user.password).to.not.exist
done()
})
assert.hasAnyKeys(user, ['type', '_id', 'username'])
assert.equal(user._id, _id)
assert.notProperty(
user,
'password',
'Password property should not be returned'
)
})
})
describe('PUT /users/:id', () => {
it('should not update user if token is invalid', (done) => {
request
.put('/users/1')
.set({
Accept: 'application/json',
Authorization: 'Bearer 1'
})
.expect(401, done)
it('should not update user if token is invalid', async () => {
try {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should throw 404 if user doesn\'t exist', (done) => {
it("should throw 404 if user doesn't exist", async () => {
const { token } = context
request
.put('/users/1')
.set({
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.expect(404, done)
try {
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 404)
}
})
it('should update user', (done) => {
it('should update user', async () => {
const {
user: { _id },
token
} = context
request
.put(`/users/${_id}`)
.set({
const options = {
method: 'PUT',
uri: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.send({ user: { username: 'updatedcoolname' } })
.expect(200, (err, res) => {
if (err) { return done(err) }
},
body: {
user: { username: 'updatedcoolname' }
}
}
res.body.user.should.have.property('username')
res.body.user.username.should.equal('updatedcoolname')
expect(res.body.user.password).to.not.exist
const result = await rp(options)
const user = result.body.user
// console.log(`user: ${util.inspect(user)}`)
done()
})
assert.hasAnyKeys(user, ['type', '_id', 'username'])
assert.equal(user._id, _id)
assert.notProperty(
user,
'password',
'Password property should not be returned'
)
assert.equal(user.username, 'updatedcoolname')
})
})
describe('DELETE /users/:id', () => {
it('should not delete user if token is invalid', (done) => {
request
.delete('/users/1')
.set({
Accept: 'application/json',
Authorization: 'Bearer 1'
})
.expect(401, done)
it('should not delete user if token is invalid', async () => {
try {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer 1`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 401)
}
})
it('should throw 404 if user doesn\'t exist', (done) => {
it('should throw 404 if user doesn\'t exist', async () => {
const { token } = context
request
.delete('/users/1')
.set({
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.expect(404, done)
try {
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/1`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
}
await rp(options)
assert.equal(true, false, 'Unexpected behavior')
} catch (err) {
assert.equal(err.statusCode, 404)
}
})
it('should delete user', (done) => {
it('should delete user', async () => {
const {
user: { _id },
token
} = context
request
.delete(`/users/${_id}`)
.set({
const options = {
method: 'DELETE',
uri: `${LOCALHOST}/users/${_id}`,
resolveWithFullResponse: true,
json: true,
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`
})
.expect(200, done)
})
}
}
const result = await rp(options)
// console.log(`result: ${util.inspect(result.body)}`)
assert.equal(result.body.success, true)
})
})
*/
})