Created
March 13, 2013 13:49
-
-
Save joaoneto/5152248 to your computer and use it in GitHub Desktop.
Login session test with mocha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('supertest'), | |
should = require('should'), | |
app = require('../server'); | |
var Cookies; | |
describe('Functional Test <Sessions>:', function () { | |
it('should create user session for valid user', function (done) { | |
request(app) | |
.post('/v1/sessions') | |
.set('Accept','application/json') | |
.send({"email": "[email protected]", "password": "123"}) | |
.expect('Content-Type', /json/) | |
.expect(200) | |
.end(function (err, res) { | |
res.body.id.should.equal('1'); | |
res.body.short_name.should.equal('Test user'); | |
res.body.email.should.equal('[email protected]'); | |
// Save the cookie to use it later to retrieve the session | |
Cookies = res.headers['set-cookie'].pop().split(';')[0]; | |
done(); | |
}); | |
}); | |
it('should get user session for current user', function (done) { | |
var req = request(app).get('/v1/sessions'); | |
// Set cookie to get saved user session | |
req.cookies = Cookies; | |
req.set('Accept','application/json') | |
.expect('Content-Type', /json/) | |
.expect(200) | |
.end(function (err, res) { | |
res.body.id.should.equal('1'); | |
res.body.short_name.should.equal('Test user'); | |
res.body.email.should.equal('[email protected]'); | |
done(); | |
}); | |
}); | |
}); |
Thank you.
Worked a treat 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @rafaelcs,
I did a working test here, with a simple app and changed a few parameters in your test. Take a look in your test, the envData and tokenAuthData may have some missing parameter.
The supertest request should receive your express app and the tokenAuthData login payload
I changed some things in your test, commenting on the lines that may be causing the behavior you mentioned
I hope it helps you to solve the problem.