Skip to content

Commit

Permalink
fixing acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 18, 2012
1 parent b75bb00 commit ee290d8
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions test/acceptance/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ function redirects(to,fn){
}
}

function getCookie(res) {
return res.headers['set-cookie'][0].split(';')[0];
}

describe('auth', function(){
var cookie;

describe('GET /',function(){
it('should redirect to /login', function(done){
request(app)
.get('/')
.end(redirects(/\/login$/,done))
})
})

describe('GET /restricted (w/o cookie)',function(){
it('should redirect to /login', function(done){
request(app)
.get('/restricted')
.end(redirects(/\/login$/,done))
})
})

describe('POST /login', function(){
it('should fail without proper credentials', function(done){
request(app)
Expand All @@ -33,28 +40,35 @@ describe('auth', function(){
.write('&username=not-tj&password=foobar')
.end(redirects(/\/login$/,done))
})

it('should authenticate', function(done){
request(app)
.post('/login')
.set('content-type','application/x-www-form-urlencoded')
.write('&username=tj&password=foobar')
.set('content-type', 'application/x-www-form-urlencoded')
.write('username=tj&password=foobar')
.end(function(res){
res.statusCode.should.equal(302);
res.headers.should.have.property('location').match(/\/restricted$/);
cookie = res.headers['set-cookie'][0].split(';')[0];
cookie.should.match(/^connect.sid=/)
done();
cookie = getCookie(res);
request(app)
.get('/login')
.set('Cookie', cookie)
.end(function(res){
res.body.should.include('Authenticated as tj');
done();
})
})
})
})

describe('GET /restricted (w. cookie)',function(){
it('should respond with 200',function(done){
it('should respond with 200', function(done){
request(app)
.get('/restricted')
.set('cookie',cookie)
.expect(200,done)
.set('Cookie', cookie)
.expect(200, done);
})
})

describe('GET /logout',function(){
it('should respond with 302 and clear cookie',function(done){
request(app)
Expand Down

0 comments on commit ee290d8

Please sign in to comment.