Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate Supertest requests with Passport?

I'm using Passport.js for authentication (local strategy) and testing with Mocha and Supertest.

How can I create a session and make authenticated requests with Supertest?

like image 294
Gal Ben-Haim Avatar asked Dec 22 '12 08:12

Gal Ben-Haim


People also ask

What does passport authenticate () do?

In this route, passport. authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req. user property is set to the authenticated user, a login session is established, and the next function in the stack is called.

What is passport JWT used for?

Passport JS is authentication middleware for Node and Express JS. Passport JS can be used with any Express JS applications. It provides us with a strategy called Passport JWT that helps us to make authenticated requests and also to verify if the token is valid or not.

What is Passport authentication in node JS?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.

What library does SuperTest make use of to perform requests?

SuperTest is a Node. js library that helps developers test APIs. It extends another library called superagent, a JavaScript HTTP client for Node.


2 Answers

As zeMirco points out, the underlying superagent module supports sessions, automatically maintaining cookies for you. However, it is possible to use the superagent.agent() functionality from supertest, through an undocumented feature.

Simply use require('supertest').agent('url') instead of require('supertest')('url'):

var request = require('supertest'); var server = request.agent('http://localhost:3000');  describe('GET /api/getDir', function(){     it('login', loginUser());     it('uri that requires user to be logged in', function(done){     server         .get('/api/getDir')                                .expect(200)         .end(function(err, res){             if (err) return done(err);             console.log(res.body);             done()         });     }); });   function loginUser() {     return function(done) {         server             .post('/login')             .send({ username: 'admin', password: 'admin' })             .expect(302)             .expect('Location', '/')             .end(onResponse);          function onResponse(err, res) {            if (err) return done(err);            return done();         }     }; }; 
like image 156
Andy Avatar answered Oct 03 '22 13:10

Andy


You should use superagent for that. It is lower level module and used by supertest. Take a look at the section Persisting an agent:

var request = require('superagent'); var user1 = request.agent(); user1   .post('http://localhost:4000/signin')   .send({ user: '[email protected]', password: 'password' })   .end(function(err, res) {     // user1 will manage its own cookies     // res.redirects contains an Array of redirects   }); 

Now you can use user1 to make authenticated requests.

like image 39
zemirco Avatar answered Oct 03 '22 12:10

zemirco