<commit_before>'use strict';

const httpMocks = require('node-mocks-http');
const sinon = require('sinon');
const storj = require('storj-lib');
const expect = require('chai').expect;
const EventEmitter = require('events').EventEmitter;
const ContactsRouter = require('../../../lib/server/routes/contacts');

describe('ContactsRouter', function() {

  var contactsRouter = new ContactsRouter(
    require('../../_fixtures/router-opts')
  );

  describe('#getContactList', function() {

    it('should return internal error if query fails', function(done) {
      var request = httpMocks.createRequest({
        method: 'GET',
        url: '/contacts',
        query: {
          protocol: '0.10.0',
          nodeID: 'some bad node id'
        }
      });
      var response = httpMocks.createResponse({
        req: request,
        eventEmitter: EventEmitter
      });
      var _contactFind = sinon.stub(
        contactsRouter.storage.models.Contact,
        'find'
      ).returns({
        skip: function() { return this; },
        limit: function() { return this; },
        sort: function() { return this; },
        exec: sinon.stub().callsArgWith(0, new Error('Panic!'))
      });
      contactsRouter.getContactList(request, response, function(err) {
        _contactFind.restore();
        expect(err.message).to.equal('Panic!');
        done();
      });
    });

    it('should return the sorted contact list', function(done) {
      var request = httpMocks.createRequest({
        method: 'GET',
        url: '/contacts',
        query: {
          protocol: '0.10.0'
        }
      });
      var response = httpMocks.createResponse({
        req: request,
        eventEmitter: EventEmitter
      });
      var contact1 = new contactsRouter.storage.models.Contact({
        address: '127.0.0.1',
        port: 1337,
        _id: storj.utils.rmd160('1'),
        lastSeen: Date.now() - 10
      });
      var contact2 = new contactsRouter.storage.models.Contact({
        address: '127.0.0.1',
        port: 1338,
        _id: storj.utils.rmd160('2'),
        lastSeen: Date.now() + 10
      });
      var _contactFind = sinon.stub(
        contactsRouter.storage.models.Contact,
        'find'
      ).returns({
        skip: function() { return this; },
        limit: function() { return this; },
        sort: function() { return this; },
        exec: sinon.stub().callsArgWith(0, null, [
          contact1,
          contact2
        ])
      });
      response.on('end', function() {
        var result = response._getData();
        _contactFind.restore();
        expect(result[0].nodeID).to.equal(storj.utils.rmd160('2'));
        expect(result[1].nodeID).to.equal(storj.utils.rmd160('1'));
        done();
      });
      contactsRouter.getContactList(request, response);
    });

  });

  describe('#patchContactByNodeID', function() {
    const sandbox = sinon.sandbox.create();
    afterEach(() => sandbox.restore());

    it('will send back a response on success', function(done) {
      var request = httpMocks.createRequest({
        method: 'PATCH',
        url: '/contacts/somenodeid',
        body: {
          address: '127.0.0.1',
          port: 9000,
          spaceAvailable: false
        }
      });
      var response = httpMocks.createResponse({
        req: request,
        eventEmitter: EventEmitter
      });
      const contact = {
        nodeID: '13a01f6d2cda65982e452c841690ee32e6f88fb6',
        address: '127.0.0.1',
        port: 9000,
        spaceAvailable: false
      };

      const findOneAndUpdate = sandbox.stub(
        contactsRouter.storage.models.Contact,
        'findOneAndUpdate'
      ).callsArgWith(3, null, contact);

      sandbox.stub(contactsRouter, 'setDefaultResponseTime');
      sandbox.stub(contactsRouter, 'setDefaultReputation');

      response.on('end', function() {
        var result = response._getData();
        expect(result).to.eql({
          nodeID: '13a01f6d2cda65982e452c841690ee32e6f88fb6',
          address: '127.0.0.1',
          port: 9000,
          spaceAvailable: false
        });
        expect(findOneAndUpdate.callCount).to.equal(1);
        expect(findOneAndUpdate.args[0][1]).to.eql({
          $set: {
            address: '127.0.0.1',
            port: 9000,
            spaceAvailable: false
          }
        });
        expect(contactsRouter.setDefaultResponseTime.callCount).to.equal(1);
        expect(contactsRouter.setDefaultReputation.callCount).to.equal(1);
        done();
      });

      contactsRouter.patchContactByNodeID(request, response, function() {});<commit_msg>updates unit test to account for lastSeen in data obj<commit_after>'use strict';

const httpMocks = require('node-mocks-http');
const sinon = require('sinon');
const storj = require('storj-lib');
const expect = require('chai').expect;
const EventEmitter = require('events').EventEmitter;
const ContactsRouter = require('../../../lib/server/routes/contacts');

describe('ContactsRouter', function() {

  var contactsRouter = new ContactsRouter(
    require('../../_fixtures/router-opts')
  );

  describe('#getContactList', function() {

    it('should return internal error if query fails', function(done) {
      var request = httpMocks.createRequest({
        method: 'GET',
        url: '/contacts',
        query: {
          protocol: '0.10.0',
          nodeID: 'some bad node id'
        }
      });
      var response = httpMocks.createResponse({
        req: request,
        eventEmitter: EventEmitter
      });
      var _contactFind = sinon.stub(
        contactsRouter.storage.models.Contact,
        'find'
      ).returns({
        skip: function() { return this; },
        limit: function() { return this; },
        sort: function() { return this; },
        exec: sinon.stub().callsArgWith(0, new Error('Panic!'))
      });
      contactsRouter.getContactList(request, response, function(err) {
        _contactFind.restore();
        expect(err.message).to.equal('Panic!');
        done();
      });
    });

    it('should return the sorted contact list', function(done) {
      var request = httpMocks.createRequest({
        method: 'GET',
        url: '/contacts',
        query: {
          protocol: '0.10.0'
        }
      });
      var response = httpMocks.createResponse({
        req: request,
        eventEmitter: EventEmitter
      });
      var contact1 = new contactsRouter.storage.models.Contact({
        address: '127.0.0.1',
        port: 1337,
        _id: storj.utils.rmd160('1'),
        lastSeen: Date.now() - 10
      });
      var contact2 = new contactsRouter.storage.models.Contact({
        address: '127.0.0.1',
        port: 1338,
        _id: storj.utils.rmd160('2'),
        lastSeen: Date.now() + 10
      });
      var _contactFind = sinon.stub(
        contactsRouter.storage.models.Contact,
        'find'
      ).returns({
        skip: function() { return this; },
        limit: function() { return this; },
        sort: function() { return this; },
        exec: sinon.stub().callsArgWith(0, null, [
          contact1,
          contact2
        ])
      });
      response.on('end', function() {
        var result = response._getData();
        _contactFind.restore();
        expect(result[0].nodeID).to.equal(storj.utils.rmd160('2'));
        expect(result[1].nodeID).to.equal(storj.utils.rmd160('1'));
        done();
      });
      contactsRouter.getContactList(request, response);
    });

  });

  describe('#patchContactByNodeID', function() {
    const sandbox = sinon.sandbox.create();
    sandbox.useFakeTimers(new Date(0).getTime());
    afterEach(() => sandbox.restore());

    it('will send back a response on success', function(done) {
      var request = httpMocks.createRequest({
        method: 'PATCH',
        url: '/contacts/somenodeid',
        body: {
          address: '127.0.0.1',
          port: 9000,
          spaceAvailable: false
        }
      });
      var response = httpMocks.createResponse({
        req: request,
        eventEmitter: EventEmitter
      });
      const contact = {
        nodeID: '13a01f6d2cda65982e452c841690ee32e6f88fb6',
        address: '127.0.0.1',
        port: 9000,
        spaceAvailable: false
      };

      const findOneAndUpdate = sandbox.stub(
        contactsRouter.storage.models.Contact,
        'findOneAndUpdate'
      ).callsArgWith(3, null, contact);

      sandbox.stub(contactsRouter, 'setDefaultResponseTime');
      sandbox.stub(contactsRouter, 'setDefaultReputation');

      response.on('end', function() {
        var result = response._getData();
        expect(result).to.eql({
          nodeID: '13a01f6d2cda65982e452c841690ee32e6f88fb6',
          address: '127.0.0.1',
          port: 9000,
          spaceAvailable: false
        });
        expect(findOneAndUpdate.callCount).to.equal(1);
        expect(findOneAndUpdate.args[0][1]).to.eql({
          $set: {
            address: '127.0.0.1',
            lastSeen: 0,
            port: 9000,
            spaceAvailable: false
          }
        });
        expect(contactsRouter.setDefaultResponseTime.callCount).to.equal(1);
        expect(contactsRouter.setDefaultReputation.callCount).to.equal(1);
        done();
      });

      contactsRouter.patchContactByNodeID(request, response, function() {});
{'fraction_non_alphanumeric': 0.14273440017183975, 'fraction_numerical': 0.033293953388465254, 'mean_word_length': 2.26507713884993, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5432323', 'n_tokens_mistral': 2926, 'n_tokens_neox': 2812, 'n_words': 529}