{"size": 3692, "ext": "js", "max_stars_repo_path": "test/d1-session-injection.js", "max_stars_repo_name": "FieldServer/happngin", "max_stars_repo_stars_event_min_datetime": "2015-09-03T13:39:21.000Z", "max_stars_repo_stars_event_max_datetime": "2019-10-17T09:07:10.000Z", "max_issues_repo_path": "test/d1-session-injection.js", "max_issues_repo_name": "FieldServer/happngin", "max_issues_repo_issues_event_min_datetime": "2015-08-31T11:28:07.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-08T08:05:31.000Z", "max_forks_repo_path": "test/d1-session-injection.js", "max_forks_repo_name": "FieldServer/happngin", "max_forks_repo_forks_event_min_datetime": "2015-11-02T12:27:05.000Z", "max_forks_repo_forks_event_max_datetime": "2016-08-25T13:52:25.000Z"}
{"max_stars_count": 11, "max_issues_count": 281, "max_forks_count": 4, "avg_line_length": 20.8587570621, "max_line_length": 91, "alphanum_fraction": 0.545503792}
module.exports = TestMesh;

var DONE = false;

function TestMesh() {
}

TestMesh.prototype.method1 = function ($happn, $origin, callback) {
  callback(null, $origin);
}

if (global.TESTING_D1 || global.TESTING_D1_1) return; // When 'requiring' the module above,
// don't run the tests below
//.............

describe('d1-session-injection', function () {

  this.timeout(120000);

  //require('benchmarket').start();
  //after(//require('benchmarket').store());

  var expect = require('expect.js');
  var should = require('chai').should();

  var unsecureMesh;
  var Mesh = require('../');
  var secureMesh = new Mesh();

  var secureClient = new Mesh.MeshClient({secure: true, port: 8000});
  var unsecureClient = new Mesh.MeshClient({port: 8001});

  var test_id = Date.now() + '_' + require('shortid').generate();
  var async = require('async');

  before('starts a secure mesh', function (done) {

    global.TESTING_D1 = true; //.............

    secureMesh.initialize({
      name: 'd1-session-injection-secure',
      datalayer: {
        secure: true,
        adminPassword: test_id,
        port: 8000
      },
      modules: {
        'TestMesh': {
          path: __filename
        }
      },
      components: {
        'TestMesh': {
          moduleName: 'TestMesh',
          schema: {
            exclusive: false
          }
        }
      }

    }, function (err) {

      if (err) return done(err);
      secureMesh.start(function (err) {
        if (err) {
          // console.log(err.stack);
          return done(err);
        }

        // Credentials for the login method
        var credentials = {
          username: '_ADMIN', // pending
          password: test_id
        }

        secureClient.login(credentials).then(function () {
          done();
        }).catch(done);

      });
    });
  });

  before('starts an insecure mesh', function (done) {

    global.TESTING_D1_1 = true; //.............

    unsecureMesh = this.mesh = new Mesh();

    unsecureMesh.initialize({
      name: 'd1-session-injection',
      datalayer: {
        secure: false,
        adminPassword: test_id,
        port: 8001
      },
      modules: {
        'TestMesh': {
          path: __filename
        }
      },
      components: {
        'TestMesh': {
          moduleName: 'TestMesh',
          schema: {
            exclusive: false
          }
        }
      }

    }, function (err) {

      if (err) return done(err);
      unsecureMesh.start(function (err) {
        if (err) {
          // console.log(err.stack);
          return done(err);
        }

        // Credentials for the login method
        var credentials = {
          port: 8001
        }

        unsecureClient.login(credentials).then(function () {
          done();
        }).catch(done);

      });
    });
  });

  after(function (done) {

    delete global.TESTING_D1_1;
    delete global.TESTING_D1;

    unsecureMesh.stop({reconnect: false}, function (e) {

      if (e) return done(e);

      secureMesh.stop({reconnect: false}, done);

    });
  })

  it('fetches the origin info on a secure mesh', function (done) {

    secureClient.exchange.TestMesh.method1(function (e, result) {

      if (e) return done(e);
      expect(result.username).to.equal('_ADMIN');
      done();

    });

  });

  it('fetches the origin info on an unsecure mesh', function (done) {

    unsecureClient.exchange.TestMesh.method1(function (e, result) {

      if (e) return done(e);
      expect(result.id).to.not.equal(null);
      expect(result.id).to.not.equal(undefined);
      expect(result.username).to.equal(undefined);
      done();

    });

  });


  //require('benchmarket').stop();

});
