Right now I am trying to test my controllers and I need to access session, I found out that you can login using superagent but my only option for loggin in to the web app is through google oauth, and right now I cannot find any appropriate samples for testing with Mocha. Any help?
It depends on how you implemented your sessions.
In my Sails app, after authentication, I set req.session.authenticated = true
, along with cookies, etc. One thing you can do if you're doing something similar 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 a before
hook, you can use superagent
to make a request to the /login
route to authenticate:
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!
});
});
});
});
It's just an idea - you can adapt this approach to however you're checking sessions. This works for my Sails app using Mocha for tests.