<commit_before>/* jshint mocha:true */ 
var assert = require('assert');
var sinon = require('sinon');
var rewire = require('rewire');
var errors = require('../lib/errors');
var Dispatcher = rewire('../lib/dispatcher');
var Authenticator = require('../lib/auth/authenticator');

describe('Dispatcher', function() {
  describe('#new', function() {
    it('should have defaults', function() {
      var client = new Dispatcher();
      assert.equal(client.authenticator, null);
      assert.equal(client.asanaBaseUrl, 'https://app.asana.com/');
    });
    it('should keep the authenticator', function() {
      var authenticator = {};
      var client = new Dispatcher({ authenticator: authenticator });
      assert.equal(client.authenticator, authenticator);
    });
    it('should keep the asana base url', function() {
      var client = new Dispatcher({ asanaBaseUrl: 'fake_url' });
      assert.equal(client.asanaBaseUrl, 'fake_url');
    });
  });

  describe('#url', function() {
    it('should return an Asana url', function() {
      var path = '/users/me';
      var dispatcher = new Dispatcher();
      assert.equal(
          dispatcher.url(path), 'https://app.asana.com/api/1.0' + path);
    });
  });

  describe('#setAuthenticator', function() {
    it('should keep the authenticator', function() {
      var authenticator = {};
      var client = new Dispatcher();
      assert.equal(client.authenticator, null);
      client.setAuthenticator(authenticator);
      assert.equal(client.authenticator, authenticator);
    });
  });

  describe('#authorize', function() {
    it('should delegate to the authenticator', function() {
      var fakePromise = {};
      var authStub = sinon.createStubInstance(Authenticator);
      authStub.ensureCredentials.onFirstCall().returns(fakePromise);
      var client = new Dispatcher({ authenticator: authStub });
      assert.equal(client.authorize(), fakePromise);
    });
  });

  describe('#dispatch', function() {
    it('should use authenticator to add auth to request', function() {
      var request = sinon.stub();
      Dispatcher.__set__('request', request);
      var authFake = {
        authenticateRequest: function(params) {
          params.auth = 'fake_auth';
        }
      };
      var authSpy = sinon.spy(authFake, 'authenticateRequest');
      var dispatcher = new Dispatcher({ authenticator: authFake });
      dispatcher.dispatch({});
      assert(authSpy.calledOnce);
      assert(request.calledWithMatch({
        auth: 'fake_auth'
      }));
    });

    it('should pass an error from request', function() {
      var request = sinon.stub();
      var err = new Error();
      Dispatcher.__set__('request', request);
      var auth = { authenticateRequest: sinon.stub() };
      var dispatcher = new Dispatcher({ authenticator: auth });
      var res = dispatcher.dispatch({});
      request.callArgWith(1, err);
      return res.then(function() {
        throw new Error('Should not have reached here');
      }, function(passedErr) {
        return assert.equal(passedErr, err);
      });
    });

    Object.keys(errors).forEach(function(key) {
      it('should create an error for ' + key, function() {
        var request = sinon.stub();
        var err = new errors[key]();
        Dispatcher.__set__('request', request);
        var auth = { authenticateRequest: sinon.stub() };
        var dispatcher = new Dispatcher({ authenticator: auth });
        var res = dispatcher.dispatch({});
        request.callArgWith(1, null, {
          statusCode: err.status
        });
        return res.then(function() {
          throw new Error('Should not have reached here');
        }, function(passedErr) {
          return assert.deepEqual(passedErr, err);
        });
      });
    });

    it('should retry on rate limit error if option set', function() {
      var fakeData = {};

      var request = sinon.stub();
      request.onFirstCall().callsArgWith(
          1, null, { statusCode: 429 }, { 'retry_after': 42 });
      request.onSecondCall().callsArgWith(
          1, null, { statusCode: 200 }, { data: fakeData });
      Dispatcher.__set__('request', request);

      var setTimeout = sinon.stub();
      setTimeout.onFirstCall().callsArg(0);
      Dispatcher.__set__('setTimeout', setTimeout);

      var auth = { authenticateRequest: sinon.stub() };
      var dispatcher = new Dispatcher({
        authenticator: auth,
        retryOnRateLimit: true
      });

      var res = dispatcher.dispatch({});

      return res.then(function(data) {
        assert.equal(data, fakeData);
        assert.equal(setTimeout.firstCall.args[1], 42000);
      });
    });<commit_msg>fix test<commit_after>/* jshint mocha:true */ 
var assert = require('assert');
var sinon = require('sinon');
var rewire = require('rewire');
var errors = require('../lib/errors');
var Dispatcher = rewire('../lib/dispatcher');
var Authenticator = require('../lib/auth/authenticator');

describe('Dispatcher', function() {
  describe('#new', function() {
    it('should have defaults', function() {
      var client = new Dispatcher();
      assert.equal(client.authenticator, null);
      assert.equal(client.asanaBaseUrl, 'https://app.asana.com/');
    });
    it('should keep the authenticator', function() {
      var authenticator = {};
      var client = new Dispatcher({ authenticator: authenticator });
      assert.equal(client.authenticator, authenticator);
    });
    it('should keep the asana base url', function() {
      var client = new Dispatcher({ asanaBaseUrl: 'fake_url' });
      assert.equal(client.asanaBaseUrl, 'fake_url');
    });
  });

  describe('#url', function() {
    it('should return an Asana url', function() {
      var path = '/users/me';
      var dispatcher = new Dispatcher();
      assert.equal(
          dispatcher.url(path), 'https://app.asana.com/api/1.0' + path);
    });
  });

  describe('#setAuthenticator', function() {
    it('should keep the authenticator', function() {
      var authenticator = {};
      var client = new Dispatcher();
      assert.equal(client.authenticator, null);
      client.setAuthenticator(authenticator);
      assert.equal(client.authenticator, authenticator);
    });
  });

  describe('#authorize', function() {
    it('should delegate to the authenticator', function() {
      var fakePromise = {};
      var authStub = sinon.createStubInstance(Authenticator);
      authStub.ensureCredentials.onFirstCall().returns(fakePromise);
      var client = new Dispatcher({ authenticator: authStub });
      assert.equal(client.authorize(), fakePromise);
    });
  });

  describe('#dispatch', function() {
    it('should use authenticator to add auth to request', function() {
      var request = sinon.stub();
      Dispatcher.__set__('request', request);
      var authFake = {
        authenticateRequest: function(params) {
          params.auth = 'fake_auth';
        }
      };
      var authSpy = sinon.spy(authFake, 'authenticateRequest');
      var dispatcher = new Dispatcher({ authenticator: authFake });
      dispatcher.dispatch({});
      assert(authSpy.calledOnce);
      assert(request.calledWithMatch({
        auth: 'fake_auth'
      }));
    });

    it('should pass an error from request', function() {
      var request = sinon.stub();
      var err = new Error();
      Dispatcher.__set__('request', request);
      var auth = { authenticateRequest: sinon.stub() };
      var dispatcher = new Dispatcher({ authenticator: auth });
      var res = dispatcher.dispatch({});
      request.callArgWith(1, err);
      return res.then(function() {
        throw new Error('Should not have reached here');
      }, function(passedErr) {
        return assert.equal(passedErr, err);
      });
    });

    Object.keys(errors).forEach(function(key) {
      it('should create an error for ' + key, function() {
        var request = sinon.stub();
        var err = new errors[key]();
        Dispatcher.__set__('request', request);
        var auth = { authenticateRequest: sinon.stub() };
        var dispatcher = new Dispatcher({ authenticator: auth });
        var res = dispatcher.dispatch({});
        request.callArgWith(1, null, {
          statusCode: err.status
        });
        return res.then(function() {
          throw new Error('Should not have reached here');
        }, function(passedErr) {
          return assert.deepEqual(passedErr, err);
        });
      });
    });

    it('should retry on rate limit error if option set', function() {
      var fakeData = {};

      var request = sinon.stub();
      request.onFirstCall().callsArgWith(
          1, null, { statusCode: 429 }, { 'retry_after': 42 });
      request.onSecondCall().callsArgWith(
          1, null, { statusCode: 200 }, { data: fakeData });
      Dispatcher.__set__('request', request);

      var setTimeout = sinon.stub();
      setTimeout.onFirstCall().callsArg(0);
      Dispatcher.__set__('setTimeout', setTimeout);

      var auth = { authenticateRequest: sinon.stub() };
      var dispatcher = new Dispatcher({
        authenticator: auth,
        retryOnRateLimit: true
      });

      var res = dispatcher.dispatch({});

      return res.then(function(data) {
        assert.equal(data, fakeData);
        assert.equal(setTimeout.firstCall.args[1], 42500);
      });
    });
{'fraction_non_alphanumeric': 0.1473142489546478, 'fraction_numerical': 0.004503055644901898, 'mean_word_length': 2.876974231088944, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1646406', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2607, 'n_words': 625}