i'm writing app using sailsjs. has been done far works expected when tested 'manually', doesn't when tested mocha.
i tried follow sailsjs testing guide, calling test npm:
[...] "scripts": { "start": "node app.js", "debug": "node debug app.js", "test": "mocha test/bootstrap.test.js test/unit/**/*.test.js" }, [...] my test directory structure follows:
test ├── bootstrap.test.js ├── mocha.opts └── unit └── controllers └── usercontroller.test.js boostrap.test.js:
var sails = require('sails'); var sails; before(function(done) { sails.lift(function(err, server) { sails = server; if (err) return done(err); done(err, sails); }); }); after(function(done) { sails.lower(done); }); usercontroller.test.js:
var request = require('supertest'); describe('userscontroller', function() { describe('#logout()', function() { it('should respond 401 status because nobody logged in', function (done) { request(sails.hooks.http.app) .put('/user/logout') .expect(401, done) }); }); describe('#signup()', function() { it('should create , log in user', function (done) { request(sails.hooks.http.app) .post('/user') .send({ firstname: 'foo', name: 'bar', email: 'foo@bar.com', sex: true, password: 'foobar', birthdate: '01/01/1991', phonenumber: '+33 3 10 10 10' }) .expect(200, done) }); }); describe('#logout()', function() { it('should log out user', function (done) { request(sails.hooks.http.app) .put('/user/logout') .expect(200, done) }); }); describe('#login()', function() { it('should respond 404 status because credentials invalid', function (done) { request(sails.hooks.http.app) .put('/user/login') .send({ email: 'bar@foo.com', password: 'barfoo' }) .expect(404, done) }); }); describe('#login()', function() { it('should log in user', function (done) { request(sails.hooks.http.app) .put('/user/login') .send({ email: 'foo@bar.com', password: 'foobar' }) .expect(200, done); }); }); describe('#login()', function() { it('should respond 401 status because user logged in', function (done) { request(sails.hooks.http.app) .put('/user/login') .send({ email: 'foo@bar.com', password: 'foobar' }) .expect(401, done); }); }); }); finally, here output when call npm test:
> sails@0.0.0 test /users/fwoelffel/dev/stofma > mocha test/bootstrap.test.js test/unit/**/*.test.js userscontroller #logout() debug: false debug: req.session -> {"cookie":{"originalmaxage":null,"expires":null,"httponly":true,"path":"/"}} info: policy 'authenticated' disallowed proceed next policy ✓ should respond 401 status because nobody logged in (86ms) #signup() debug: null === req.session.authenticated || undefined === req.session.authenticated -> true debug: req.session -> {"cookie":{"originalmaxage":null,"expires":null,"httponly":true,"path":"/"}} info: policy 'unauthenticated' allowed proceed next policy info: user foo@bar.com signed , logged in. ✓ should create , log in user (146ms) #logout() debug: false debug: req.session -> {"cookie":{"originalmaxage":null,"expires":null,"httponly":true,"path":"/"}} info: policy 'authenticated' disallowed proceed next policy 1) should log out user #login() debug: null === req.session.authenticated || undefined === req.session.authenticated -> true debug: req.session -> {"cookie":{"originalmaxage":null,"expires":null,"httponly":true,"path":"/"}} info: policy 'unauthenticated' allowed proceed next policy info: no user matching bar@foo.com. ✓ should respond 404 status because credentials invalid (66ms) #login() debug: null === req.session.authenticated || undefined === req.session.authenticated -> true debug: req.session -> {"cookie":{"originalmaxage":null,"expires":null,"httponly":true,"path":"/"}} info: policy 'unauthenticated' allowed proceed next policy info: found user foo@bar.com. info: foo@bar.com credentials valid. ✓ should log in user (128ms) #login() debug: null === req.session.authenticated || undefined === req.session.authenticated -> true debug: req.session -> {"cookie":{"originalmaxage":null,"expires":null,"httponly":true,"path":"/"}} info: policy 'unauthenticated' allowed proceed next policy info: found user foo@bar.com. info: foo@bar.com credentials valid. 2) should respond 401 status because user logged in 4 passing (1s) 2 failing 1) userscontroller #logout() should log out user: error: expected 200 "ok", got 401 "unauthorized" @ net.js:1419:10 2) userscontroller #login() should respond 401 status because user logged in: error: expected 401 "unauthorized", got 200 "ok" @ net.js:1419:10 npm err! test failed. see above more details. to sum things up, testing auth/unauth api. below policies:
- when logged in user tries log in, 'unauthenticated' policy should throw error (401)
- when logged in user tries sign up, 'unauthenticated' policy should throw error (401)
- when logged out user tries log out, 'authenticated' policy should throw error (401)
i might doing wrong can't figure is. solve issue?
if need more information, please ask. might find code (without tests, since they're failing) on github.
thanks reading, have nice day!
update
thanks elsaar, changed code to:
var request = require('supertest'); var agent; describe('userscontroller', function() { before(function(done) { agent = request.agent(sails.hooks.http.app); done(); }) describe('#logout()', function() { it('should respond 401 status because nobody logged in', function (done) { agent .put('/user/logout') .expect(401, done) }); }); describe('#signup()', function() { it('should create , log in user', function (done) { agent .post('/user') .send({ firstname: 'foo', name: 'bar', email: 'foo@bar.com', sex: true, password: 'foobar', birthdate: '01/01/1991', phonenumber: '+33 3 10 10 10' }) .expect(200, done) }); }); describe('#logout()', function() { it('should log out user', function (done) { agent .put('/user/logout') .expect(200, done) }); }); describe('#login()', function() { it('should respond 404 status because credentials invalid', function (done) { agent .put('/user/login') .send({ email: 'bar@foo.com', password: 'barfoo' }) .expect(404, done) }); }); describe('#login()', function() { it('should log in user', function (done) { agent .put('/user/login') .send({ email: 'foo@bar.com', password: 'foobar' }) .expect(200, done); }); }); describe('#login()', function() { it('should respond 401 status because user logged in', function (done) { agent .put('/user/login') .send({ email: 'foo@bar.com', password: 'foobar' }) .expect(401, done); }); }); });
i think session not being persisted, user loggedin in earlier request won't loggedin in later request. way unit tests should be. you'll have ensure user in desired stated(logged in or out) before test run.
edit - need use same instance of supertest agent persist session - https://github.com/visionmedia/supertest/issues/46#issuecomment-58534736
so @ beginning of tests, , use same agent in tests
var supertest = require('supertest'); agent = supertest.agent(sails.hooks.http.app); // use agent test endpoints
Comments
Post a Comment