I have a route which requires authentication. However when I try to test this route, I'm always getting 401 (unauthorized). How can I send my user with the route so that I am authenticated?
I'm using Google passport authentication.
This is my test:
const assert = require('chai').assert;
const expect = require('expect');
const should = require('should');
const liveScore = require('./../lib/live-score');
const superagent = require('superagent');
const theAccount = {
name: 'Stijn Aerts',
role: 'admin',
id: '115039452833383267752'
};
describe('live-score', function() {
it('should return 200 authorized', function (done) {
superagent
.get('http://localhost:3000/api/live-score')
.send(theAccount)
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.end(function (err, res) {
expect(200, done);
assert.equal(res.status, 200) ;
done();
});
});
});
Routes.js:
...
router.get('/api/live-score', loggedIn, liveScore.getData);
...
function loggedIn(req, res, next) {
if (req.user) {
next();
} else {
res.status(401).json({message: 'Session unavailable'});
}
}
...
Aucun commentaire:
Enregistrer un commentaire