Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Commit

Permalink
Improve auth tests organization
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrohee committed Mar 27, 2017
1 parent 3d1cad0 commit 843f23c
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions server/api/auth/test/auth.api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,70 @@ describe('api: auth', () => {
app = new Server({ isTest: true }).getApp()
})

describe('When requesting /api/auth/ with user missing', () => {
it('should return 401 given email and password are missing', (done) => {
supertest(app)
.post('/api/auth/')
.expect(401, { message: 'Missing credentials' }, done)
describe('When requesting /api/auth/', () => {
describe('When user is missing', () => {
it('should return 401', (done) => {
supertest(app)
.post('/api/auth/')
.expect(401, { message: 'Missing credentials' }, done)
})
})
it('should return 401 given email and password are invalid', (done) => {
supertest(app)
.post('/api/auth/')
.send({ email: 'anonymous', password: 'wrongPassword' })
.expect(401, { 'message': 'Email ou mot de passe incorrect' }, done)
describe('When email and password are invalid', () => {
it('should return 401', (done) => {
supertest(app)
.post('/api/auth/')
.send({ email: 'anonymous', password: 'wrongPassword' })
.expect(401, { 'message': 'Email ou mot de passe incorrect' }, done)
})
})
it('should return 401 given password is invalid', (done) => {
supertest(app)
.post('/api/auth/')
.send({ email: 'peel@univ-lorraine.fr', password: 'wrongPassword' })
.expect(401, { message: 'Email ou mot de passe incorrect' }, done)
describe('When password is invalid', () => {
it('should return 401', (done) => {
supertest(app)
.post('/api/auth/')
.send({ email: 'peel@univ-lorraine.fr', password: 'wrongPassword' })
.expect(401, { message: 'Email ou mot de passe incorrect' }, done)
})
})
it('should return 200 and a token given email and password are valid', (done) => {
supertest(app)
.post('/api/auth/')
.send({ email: 'peel@univ-lorraine.fr', password: 'test' })
.expect(200)
.expect((res) => {
if (!('token' in res.body)) {
throw new Error('Missing token key')
}
validToken = res.body.token
})
.end(done)
describe('When email and password are valid', () => {
it('should return 200 and a token given', (done) => {
supertest(app)
.post('/api/auth/')
.send({ email: 'peel@univ-lorraine.fr', password: 'test' })
.expect(200)
.expect((res) => {
if (!('token' in res.body)) {
throw new Error('Missing token key')
}
validToken = res.body.token
})
.end(done)
})
})
})

describe('When requesting route requiring authentication', () => {
it('should return 401 given no token is provided', (done) => {
supertest(app)
.get('/api/auth/authProtected')
.expect(401, done)
describe('When no token is provided', () => {
it('should return 401', (done) => {
supertest(app)
.get('/api/auth/authProtected')
.expect(401, done)
})
})
it('should return 401 given an invalid token is provided', (done) => {
supertest(app)
.get('/api/auth/authProtected')
.set('Authorization', 'Bearer invalidToken')
.expect(401, 'The provided token is invalid', done)
describe('When an invalid token is provided', () => {
it('should return 401 ', (done) => {
supertest(app)
.get('/api/auth/authProtected')
.set('Authorization', 'Bearer invalidToken')
.expect(401, 'The provided token is invalid', done)
})
})
it('should return 401 given an invalid token is provided', (done) => {
supertest(app)
.get('/api/auth/authProtected')
.set('Authorization', `Bearer ${validToken}`)
.expect(200, '"pong"', done)
describe('When a valid token is provided', () => {
it('should return 200', (done) => {
supertest(app)
.get('/api/auth/authProtected')
.set('Authorization', `Bearer ${validToken}`)
.expect(200, '"pong"', done)
})
})
})
})

0 comments on commit 843f23c

Please sign in to comment.