How to unit test passport google oauth in sails js with Mocha

Now I'm trying to check my controllers, and I need to access the session, I found out that you can log in using superagent , but my only option is to log in to the web application via google oauth, and now I can not find suitable samples for testing with Mocha. Any help?

+6
source share
1 answer

It depends on how you implemented your sessions.

In my Sails application, after authentication, I set req.session.authenticated = true , as well as cookies, etc. One thing you can do if you do something like this is in your /login route, add:

 if (process.env.NODE_ENV === 'test') { req.session.authenticated = true; // do what you would do next after authentication } else { // do normal login procedures } 

Then, in your tests, in before hook, you can use superagent to request a trace /login for authentication:

 describe('MyController', function() { var agent; before(function (done) { agent = require('superagent').agent('YOUR_APP_URL'); // authenticate agent .post('/login') .end(done) }); // in your tests, use the same agent to make future requests describe('#someAction', function() { it('should do something', function(done) { agent. .post('someAction') .end(function (err, res) { // should work! }); }); }); }); 

This is just an idea - you can adapt this approach despite the fact that you are checking sessions. This works for my Sails application, using Mocha for testing.

+1
source

Source: https://habr.com/ru/post/974678/


All Articles