auth.js
This file
Path
test/acceptance/auth.js
102 lines · 2.5 KiB · JavaScript
Last changed by
Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
History of this fileTake it
The walk stopped after 200 commits: lines older than 34c83d7 point at it.
3.4.7Jonathan Ongbefore adoption34c83d7
line 1why, one step back1var app = require('../../examples/auth/app')Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 2why, one step back2var request = require('supertest')3.4.7Jonathan Ongbefore adoption34c83d7
lines 3–12why, one step back34function getCookie(res) {5 return res.headers['set-cookie'][0].split(';')[0];6}78describe('auth', function(){9 describe('GET /',function(){10 it('should redirect to /login', function(done){11 request(app)12 .get('/')Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 13–14why, one step back13 .expect('Location', '/login')14 .expect(302, done)3.4.7Jonathan Ongbefore adoption34c83d7
lines 15–17why, one step back15 })16 })17Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 18–19why, one step back18 describe('GET /login',function(){19 it('should render login form', function(done){3.4.7Jonathan Ongbefore adoption34c83d7
line 20why, one step back20 request(app)Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 21–52why, one step back21 .get('/login')22 .expect(200, /<form/, done)23 })2425 it('should display login error', function(done){26 request(app)27 .post('/login')28 .type('urlencoded')29 .send('username=not-tj&password=foobar')30 .expect('Location', '/login')31 .expect(302, function(err, res){32 if (err) return done(err)33 request(app)34 .get('/login')35 .set('Cookie', getCookie(res))36 .expect(200, /Authentication failed/, done)37 })38 })39 })4041 describe('GET /logout',function(){42 it('should redirect to /', function(done){43 request(app)44 .get('/logout')45 .expect('Location', '/')46 .expect(302, done)47 })48 })4950 describe('GET /restricted',function(){51 it('should redirect to /login without cookie', function(done){52 request(app)3.4.7Jonathan Ongbefore adoption34c83d7
line 53why, one step back53 .get('/restricted')Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 54–55why, one step back54 .expect('Location', '/login')55 .expect(302, done)3.4.7Jonathan Ongbefore adoption34c83d7
line 56why, one step back56 })Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 57–71why, one step back5758 it('should succeed with proper cookie', function(done){59 request(app)60 .post('/login')61 .type('urlencoded')62 .send('username=tj&password=foobar')63 .expect('Location', '/')64 .expect(302, function(err, res){65 if (err) return done(err)66 request(app)67 .get('/restricted')68 .set('Cookie', getCookie(res))69 .expect(200, done)70 })71 })3.4.7Jonathan Ongbefore adoption34c83d7
lines 72–74why, one step back72 })7374 describe('POST /login', function(){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 75why, one step back75 it('should fail without proper username', function(done){3.4.7Jonathan Ongbefore adoption34c83d7
lines 76–79why, one step back76 request(app)77 .post('/login')78 .type('urlencoded')79 .send('username=not-tj&password=foobar')Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 80–99why, one step back80 .expect('Location', '/login')81 .expect(302, done)82 })8384 it('should fail without proper password', function(done){85 request(app)86 .post('/login')87 .type('urlencoded')88 .send('username=tj&password=baz')89 .expect('Location', '/login')90 .expect(302, done)91 })9293 it('should succeed with proper credentials', function(done){94 request(app)95 .post('/login')96 .type('urlencoded')97 .send('username=tj&password=foobar')98 .expect('Location', '/')99 .expect(302, done)3.4.7Jonathan Ongbefore adoption34c83d7
lines 100–102why, one step back100 })101 })102})