tests: improve examples/auth tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent a96924a · Browse files · 1 file·+76 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+76 added−16 removed
- tests:+76 added−16 removed
Provenance
- Commit
1f938c5- Tree
4bbfc5b- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
a96924a
1 file changed+76 added−16 removed
modified:test/acceptance/auth.js+76 added-16 removed
1223456789101145678910111221131415161725261819202122232425262728293031323334353637383940414243444546474849505152532954555657585960616263646566676869707172737434757677787939808182838485868788899091929394959697989910010142102var app = require('../../examples/auth/app') , request = require('supertest');var request = require('supertest')
function redirects(to, fn){ return function(err, res){ res.statusCode.should.equal(302) res.headers.should.have.property('location').match(to); fn() }}
function getCookie(res) { return res.headers['set-cookie'][0].split(';')[0];}
describe('auth', function(){ describe('GET /',function(){ it('should redirect to /login', function(done){ request(app) .get('/') .end(redirects(/\/login$/, done)) .expect('Location', '/login') .expect(302, done) }) })
describe('GET /restricted (w/o cookie)',function(){ it('should redirect to /login', function(done){ describe('GET /login',function(){ it('should render login form', function(done){ request(app) .get('/login') .expect(200, /<form/, done) })
it('should display login error', function(done){ request(app) .post('/login') .type('urlencoded') .send('username=not-tj&password=foobar') .expect('Location', '/login') .expect(302, function(err, res){ if (err) return done(err) request(app) .get('/login') .set('Cookie', getCookie(res)) .expect(200, /Authentication failed/, done) }) }) })
describe('GET /logout',function(){ it('should redirect to /', function(done){ request(app) .get('/logout') .expect('Location', '/') .expect(302, done) }) })
describe('GET /restricted',function(){ it('should redirect to /login without cookie', function(done){ request(app) .get('/restricted') .end(redirects(/\/login$/,done)) .expect('Location', '/login') .expect(302, done) })
it('should succeed with proper cookie', function(done){ request(app) .post('/login') .type('urlencoded') .send('username=tj&password=foobar') .expect('Location', '/') .expect(302, function(err, res){ if (err) return done(err) request(app) .get('/restricted') .set('Cookie', getCookie(res)) .expect(200, done) }) }) })
describe('POST /login', function(){ it('should fail without proper credentials', function(done){ it('should fail without proper username', function(done){ request(app) .post('/login') .type('urlencoded') .send('username=not-tj&password=foobar') .end(redirects(/\/login$/, done)) .expect('Location', '/login') .expect(302, done) })
it('should fail without proper password', function(done){ request(app) .post('/login') .type('urlencoded') .send('username=tj&password=baz') .expect('Location', '/login') .expect(302, done) })
it('should succeed with proper credentials', function(done){ request(app) .post('/login') .type('urlencoded') .send('username=tj&password=foobar') .expect('Location', '/') .expect(302, done) }) })})No newline at end of file})