From e16bc8f7ad54cc3ee832b9c9975ed5047a221adc Mon Sep 17 00:00:00 2001 From: gaba Date: Tue, 20 Dec 2016 12:18:30 -0800 Subject: [PATCH] Adds agent to persist session in tests. --- app.js | 8 +------- tests/routes/api/auth/index.js | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app.js b/app.js index f64880e64..602c36320 100644 --- a/app.js +++ b/app.js @@ -70,15 +70,9 @@ app.use(session(session_opts)); // CSRF MIDDLEWARE //============================================================================== -// Use CSRF only if we are not testing. -if (app.get('env') !== 'test') { - console.log('DEBUGT NO TEST'); - app.use(cookieParser()); -} +app.use(cookieParser()); app.use((err, req, res, next) => { - console.log('DEBUG EN SERVER', req.csrfToken()); - res.locals._csrf = req.csrfToken(); return next(); }); diff --git a/tests/routes/api/auth/index.js b/tests/routes/api/auth/index.js index dd408d135..d99574721 100644 --- a/tests/routes/api/auth/index.js +++ b/tests/routes/api/auth/index.js @@ -4,6 +4,8 @@ const expect = chai.expect; chai.use(require('chai-http')); +const agent = chai.request.agent(app); + const User = require('../../../../models/user'); describe('/api/v1/auth', () => { @@ -12,8 +14,8 @@ describe('/api/v1/auth', () => { return chai.request(app) .get('/api/v1/auth') .then((res) => { - expect(res.status).to.be.equal(204); - expect(res.body).to.be.empty; + expect(res.status).to.be.equal(200); + expect(res.body).to.have.property('csrfToken'); }); }); }); @@ -27,14 +29,20 @@ describe('/api/v1/auth/local', () => { describe('#post', () => { it('should send back the user on a successful login', () => { - return chai.request(app) - .post('/api/v1/auth/local') - .send({email: 'maria@gmail.com', password: 'password!'}) - .catch((res) => { - expect(res).to.have.status(200); - expect(res).to.be.json; - expect(res.body).to.have.property('user'); - expect(res.body.user).to.have.property('displayName', 'Maria'); + + agent + .get('/api/v1/auth') + .then((res) => { + expect(res.status).to.be.equal(200); + expect(res.body).to.have.property('csrfToken'); + return agent.post('/api/v1/auth/local') + .send({email: 'maria@gmail.com', password: 'password!', csrfToken: res.body.csrfToken}) + .catch((res2) => { + expect(res2).to.have.status(200); + expect(res2).to.be.json; + expect(res2.body).to.have.property('user'); + expect(res2.body.user).to.have.property('displayName', 'Maria'); + }); }); });