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

Commit

Permalink
Add tests on accessing a protected route
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrohee committed Nov 9, 2016
1 parent 00889d1 commit d5cf06a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions server/api/auth/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const passport = require('passport')
const auth = require('./auth.service')

class AuthController {
ping(req, res) {
res.json('pong')
}

getToken(req, res, next) {
passport.authenticate('local', function (err, user, info) {
var error = err || info
Expand Down
2 changes: 2 additions & 0 deletions server/api/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const express = require('express')
const passport = require('passport')
const router = express.Router()

const auth = require('./auth.service.js')
const User = require('../user/user.model')
const Controller = require('./auth.controller')

Expand All @@ -21,5 +22,6 @@ require('./passport-strategy').setup(User)
module.exports = (options) => {
const authController = new Controller(options)
router.post('/', authController.getToken)
router.get('/authProtected', auth.isAuthenticated(), authController.ping)
return router
}
34 changes: 28 additions & 6 deletions server/api/auth/test/auth.api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Server = require('../../server')

describe('api: auth', () => {
let app
let validToken

before(() => {
app = new Server({ isTest: true }).getApp()
Expand All @@ -12,31 +13,52 @@ describe('api: auth', () => {
it('should return 401 given email and password are missing', (done) => {
supertest(app)
.post('/api/auth/')
.expect(401, { message: 'Missing credentials'}, done)
.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)
.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)
.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'})
.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)
})
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)
})
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)
})
})
})

0 comments on commit d5cf06a

Please sign in to comment.