<commit_before>'use strict';

var stormpath = require('stormpath');

var common = require('../common');
var _ = common._;
var assert = common.assert;
var sinon = common.sinon;

var strategy = require('../../lib/strategy');
var EnrichClientFromRemoteConfigStrategy = strategy.EnrichClientFromRemoteConfigStrategy;

describe('EnrichClientFromRemoteConfigStrategy', function () {
  var client, mockApplications, testStrategy;

  before(function (done) {
    mockApplications = [{
      href: 'https://stormpath.mock/applications/stormpath',
      name: 'Stormpath'
    }, {
      href: 'https://stormpath.mock/applications/my-application',
      name: 'My Application'
    }];

    client = new stormpath.Client({
      skipRemoteConfig: true
    });

    testStrategy = new EnrichClientFromRemoteConfigStrategy(function () {
      return client;
    });

    client.on('error', function (err) {
      throw err;
    });

    client.on('ready', function () {
      done();
    });
  });

  it('should resolve application by name', function (done) {
    var mockApplication = mockApplications[1];
    var mockName = mockApplication.name;

    var testConfig = {
      application: {
        name: mockName
      }
    };

    if (client.getApplications.restore) {
      client.getApplications.restore();
    }

    var mockItems = {
      items: mockApplications
    };

    mockItems.detect = function (predicate, callback) {
      predicate(mockApplications[0], function (result) {
        assert.isFalse(result);
      });
      predicate(mockApplication, function (result) {
        assert.isTrue(result);
      });
      callback(mockApplication);
    };

    sinon.stub(client, 'getApplications')
      .withArgs({ name: mockName })
      .yields(null, mockItems);

    testStrategy.process(testConfig, function (err, resultConfig) {
      assert.isNull(err);

      assert.deepEqual(resultConfig, {
        application: mockApplication
      });

      done();
    });
  });

  it('should resolve application by href', function (done) {
    var mockApplication = mockApplications[1];
    var mockHref = mockApplication.href;

    var testConfig = {
      application: {
        href: mockHref
      }
    };

    if (client.getApplication.restore) {
      client.getApplication.restore();
    }

    sinon.stub(client, 'getApplication')
      .withArgs(mockHref)
      .yields(null, mockApplication);

    testStrategy.process(testConfig, function (err, resultConfig) {
      assert.isNull(err);

      assert.deepEqual(resultConfig, {
        application: mockApplication
      });

      done();
    });
  });

  describe('when neither href or name is specified', function () {
    it('should resolve a default application', function (done) {
      var testConfig = {};

      if (client.getApplications.restore) {
        client.getApplications.restore();
      }

      sinon.stub(client, 'getApplications').yields(null, {
        items: mockApplications
      });

      testStrategy.process(testConfig, function (err, resultConfig) {
        assert.isNull(err);

        assert.deepEqual(resultConfig, {
          application: mockApplications[1]
        });

        done();
      });
    });
  });
});<commit_msg>dry out tests, add tests for error cases<commit_after>'use strict';

var stormpath = require('stormpath');

var common = require('../common');
var assert = common.assert;
var sinon = common.sinon;

var Application = require('stormpath/lib/resource/Application');
var Collection = require('stormpath/lib/resource/CollectionResource');

var strategy = require('../../lib/strategy');
var EnrichClientFromRemoteConfigStrategy = strategy.EnrichClientFromRemoteConfigStrategy;

describe('EnrichClientFromRemoteConfigStrategy', function () {
  var client, testStrategy;

  var applicationNotFoundError = /The provided application could not be found/;

  var unresolveableCollectionError = /Could not automatically resolve a Stormpath Application/;

  var mockApplicationNotFoundResponse = {
    status: 404
  };

  var mockRestApiError = {
    err: 'something bad happened'
  };

  var mockApplications = [{
    href: 'https://stormpath.mock/applications/stormpath',
    name: 'Stormpath'
  }, {
    href: 'https://stormpath.mock/applications/my-application',
    name: 'My Application'
  }];

  var mockEmptyCollectionResponse = new Collection({
    items: []
  });

  var mockResolveableApplicationResponse = new Collection({
    items: mockApplications
  });

  var mockUnresolveableApplicationResponse = new Collection({
    items: mockApplications.concat([{
      href: 'https://stormpath.mock/applications/my-other-application',
      name: 'My Other Application'
    }])
  });

  before(function (done) {

    client = new stormpath.Client({
      skipRemoteConfig: true
    });

    testStrategy = new EnrichClientFromRemoteConfigStrategy(function () {
      return client;
    });

    client.on('error', function (err) {
      throw err;
    });

    client.on('ready', function () {
      done();
    });
  });

  describe('when an application name is specified', function(){

    afterEach(function(){
      client.getApplications.restore();
    });

    it('should return REST API errors if the erorr is not 404', function(done) {

      var testConfig = {
        application: {
          name: 'hello'
        }
      };

      sinon.stub(client, 'getApplications').yields(mockRestApiError);

      testStrategy.process(testConfig, function (err) {
        assert.isNotNull(err);
        assert.deepEqual(err,mockRestApiError);
        done();
      });
    });

    it('should return an error if the application cannot be found', function(done) {
      var mockName = 'hello';

      var testConfig = {
        application: {
          name: mockName
        }
      };

      sinon.stub(client, 'getApplications')
        .withArgs({ name: mockName })
        .yields(null, mockEmptyCollectionResponse);

      testStrategy.process(testConfig, function (err) {
        assert.isNotNull(err);
        assert.match(err.message,applicationNotFoundError);
        done();
      });
    });

    it('should return the correct application if it exists', function (done) {
      var mockApplication = mockApplications[1];
      var mockName = mockApplication.name;

      var testConfig = {
        application: {
          name: mockName
        }
      };

      sinon.stub(client, 'getApplications')
        .withArgs({ name: mockName })
        .yields(null, mockResolveableApplicationResponse);

      testStrategy.process(testConfig, function (err, resultConfig) {
        assert.isNull(err);
        assert.equal(resultConfig.application.name, mockApplication.name);
        done();
      });
    });

  });

  describe('when an application href is defined', function(){
    var mockApplication = mockApplications[1];
    var mockHref = mockApplication.href;

    var testConfig = {
      application: {
        href: mockHref
      }
    };

    afterEach(function(){
      client.getApplication.restore();
    });

    it('should return REST API errors if the erorr is not 404', function(done) {

      sinon.stub(client, 'getApplication')
        .withArgs(mockHref)
        .yields(mockRestApiError);

      testStrategy.process(testConfig, function (err) {
        assert.isNotNull(err);
        assert.deepEqual(err,mockRestApiError);
        done();
      });

    });

    it('should return an error if the application cannot be found by href', function(done) {

      sinon.stub(client, 'getApplication')
        .withArgs(mockHref)
        .yields(mockApplicationNotFoundResponse);

      testStrategy.process(testConfig, function (err) {
        assert.isNotNull(err);
        assert.match(err.message,applicationNotFoundError);
        done();
      });

    });

    it('should resolve application by href if the application exists', function (done) {

      sinon.stub(client, 'getApplication')
        .withArgs(mockHref)
        .yields(null, new Application(mockApplication));

      testStrategy.process(testConfig, function (err, resultConfig) {
        assert.isNull(err);
        assert.equal(resultConfig.application.href, mockApplication.href);
        done();
      });

    });
  });

  describe('when neither href or name is specified', function () {

    var testConfig = {};

    afterEach(function(){
      client.getApplications.restore();
    });

    it('should return REST API errors if the erorr is not 404', function(done) {

      sinon.stub(client, 'getApplications').yields(mockRestApiError);

      testStrategy.process(testConfig, function (err) {
        assert.isNotNull(err);
        assert.deepEqual(err,mockRestApiError);
        done();
      });
    });

    it('should return an "unresolveable" error if more than one application exists, other than the `Stormpath` application', function(done) {

      sinon.stub(client, 'getApplications')
        .yields(null, mockUnresolveableApplicationResponse);

      testStrategy.process(testConfig, function (err) {
        assert.isNotNull(err);
        assert.match(err.message,unresolveableCollectionError);
        done();
      });
    });

    it('should return the application that is not the `Stormpath` application, if that is the only other application that exists', function (done) {

      sinon.stub(client, 'getApplications')
        .yields(null, mockResolveableApplicationResponse);

      testStrategy.process(testConfig, function (err, resultConfig) {
        assert.isNull(err);
        assert.equal(resultConfig.application.href, mockApplications[1].href);
        done();
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.12286158631415241, 'fraction_numerical': 0.0019699326075686883, 'mean_word_length': 2.908427876823339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, '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': '5933556', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2599, 'n_words': 586}