1 var app = require('../../examples/auth')
2 var request = require('supertest')
4 function getCookie(res) {
5 return res.headers['set-cookie'][0].split(';')[0];
8 describe('auth', function(){
9 describe('GET /',function(){
10 it('should redirect to /login', function(done){
13 .expect('Location', '/login')
18 describe('GET /login',function(){
19 it('should render login form', function(done){
22 .expect(200, /<form/, done)
25 it('should display login error', function(done){
29 .send('username=not-tj&password=foobar')
30 .expect('Location', '/login')
31 .expect(302, function(err, res){
32 if (err) return done(err)
35 .set('Cookie', getCookie(res))
36 .expect(200, /Authentication failed/, done)
41 describe('GET /logout',function(){
42 it('should redirect to /', function(done){
45 .expect('Location', '/')
50 describe('GET /restricted',function(){
51 it('should redirect to /login without cookie', function(done){
54 .expect('Location', '/login')
58 it('should succeed with proper cookie', function(done){
62 .send('username=tj&password=foobar')
63 .expect('Location', '/')
64 .expect(302, function(err, res){
65 if (err) return done(err)
68 .set('Cookie', getCookie(res))
74 describe('POST /login', function(){
75 it('should fail without proper username', function(done){
79 .send('username=not-tj&password=foobar')
80 .expect('Location', '/login')
84 it('should fail without proper password', function(done){
88 .send('username=tj&password=baz')
89 .expect('Location', '/login')
93 it('should succeed with proper credentials', function(done){
97 .send('username=tj&password=foobar')
98 .expect('Location', '/')