Skip to content
Myr

auth.js

This file

Path

test/acceptance/auth.js

102 lines · 2.5 KiB · JavaScript

Last changed by

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

History of this file

Take it

The walk stopped after 200 commits: lines older than 6bc7574 point at it.

examples: make main app file names consistentDouglas Christopher Wilsonbefore adoptionb1d0c19

line 1why, one step back
1var app = require('../../examples/auth')

docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574

lines 2–102why, one step back
2var request = require('supertest')
34function 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('/')13 .expect('Location', '/login')14 .expect(302, done)15 })16 })1718 describe('GET /login',function(){19 it('should render login form', function(done){20 request(app)21 .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)53 .get('/restricted')54 .expect('Location', '/login')55 .expect(302, done)56 })5758 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 })72 })7374 describe('POST /login', function(){75 it('should fail without proper username', function(done){76 request(app)77 .post('/login')78 .type('urlencoded')79 .send('username=not-tj&password=foobar')80 .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)100 })101 })102})