Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sails.socket testing with npm test

I'm using sails.js v0.11.0 and I am just getting into unit testing. I can test normal controllers through http requests, but I have no idea where to start to test the same calls over a socket requests. If you have a good resource or a sample test using sockets that would be fantastic.

var assert = require('assert');
var request = require('supertest');

describe('Auth Controller', function () {

  describe('#callback()', function () {

    it ('passport-local authentication should succeed if email and password valid', function (done) {

      request(sails.hooks.http.app)
        .post('/auth/local')
        .send({
          identifier: '[email protected]',
          password: 'admin1234'
        })
        .expect(200)
        .end(function(err) {
          done(err);
        });

    });

    it ('passport-local authentication should fail and return error code if email is invalid', function (done) {

      request(sails.hooks.http.app)
        .post('/auth/local')
        .send({
          identifier: '[email protected]',
          password: 'admin1234'
        })
        .expect(403)
        .end(function(err) {
          done(err);
      });

    });

    it ('passport-local authentication should fail and return error code if password is invalid', function (done) {

      request(sails.hooks.http.app)
        .post('/auth/local')
        .send({
          identifier: '[email protected]',
          password: 'invalid1235'
        })
        .expect(403)
        .end(function(err) {
          done(err);
      });

    });

    //Test with Web Sockets from sails.io
    describe('sails.socket', function () {

      describe('With default settings', function() {

        describe('once connected, socket', function () {

          it ('passport-local authentication via web socket should succeed if email and password valid', function (done) {

            //Socket version?
            request(sails.hooks.http.app)
              .post('/auth/local')
              .send({
                identifier: '[email protected]',
                password: 'admin1234'
              })
              .expect(200)
              .end(function(err) {
                done(err);
              });

          });

          it ('passport-local authentication via web socket should fail and return error code if email is invalid', function (done) {

            //Socket version?
            request(sails.hooks.http.app)
              .post('/auth/local')
              .send({
                identifier: '[email protected]',
                password: 'admin1234'
              })
              .expect(403)
              .end(function(err) {
                done(err);
              });

          });

          it ('passport-local authentication via web socket should fail and return error code if password is invalid', function (done) {

            //Socket version?
            request(sails.hooks.http.app)
              .post('/auth/local')
              .send({
                identifier: '[email protected]',
                password: 'invalid1235'
              })
              .expect(403)
              .end(function(err) {
                done(err);
            });

          });

        });

      });
    });

  });

});
like image 480
scott Avatar asked Oct 19 '22 13:10

scott


1 Answers

I found that Sails has what I'd consider good documentation on this subject, but not directly in their normal Sails documentation on the subject (although it's good general knowledge material to have read first). Instead, I had to read their github sails.io.js project. Again, better than the README there is their example. Look in this file to see their setup and teardown for testing sockets, and this file that shows off the tests themselves.

like image 94
Tyler Collier Avatar answered Nov 03 '22 19:11

Tyler Collier