Skip to content

Commit

Permalink
dry out tests, add tests for error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert committed Nov 13, 2015
1 parent a5f537d commit 585202b
Showing 1 changed file with 163 additions and 62 deletions.
225 changes: 163 additions & 62 deletions test/strategy/EnrichClientFromRemoteConfigStrategyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,54 @@
var stormpath = require('stormpath');

var common = require('../common');
var _ = 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, mockApplications, testStrategy;
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) {
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
Expand All @@ -39,50 +69,73 @@ describe('EnrichClientFromRemoteConfigStrategy', function () {
});
});

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

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

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

var mockItems = {
items: mockApplications
};
it('should return REST API errors if the erorr is not 404', function(done) {

mockItems.detect = function (predicate, callback) {
predicate(mockApplications[0], function (result) {
assert.isFalse(result);
});
predicate(mockApplication, function (result) {
assert.isTrue(result);
var testConfig = {
application: {
name: 'hello'
}
};

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

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

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

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

testStrategy.process(testConfig, function (err, resultConfig) {
assert.isNull(err);
sinon.stub(client, 'getApplications')
.withArgs({ name: mockName })
.yields(null, mockEmptyCollectionResponse);

assert.deepEqual(resultConfig, {
application: mockApplication
testStrategy.process(testConfig, function (err) {
assert.isNotNull(err);
assert.match(err.message,applicationNotFoundError);
done();
});
});

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();
});
});

});

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

Expand All @@ -92,44 +145,92 @@ describe('EnrichClientFromRemoteConfigStrategy', function () {
}
};

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

sinon.stub(client, 'getApplication')
.withArgs(mockHref)
.yields(null, mockApplication);
it('should return REST API errors if the erorr is not 404', function(done) {

testStrategy.process(testConfig, function (err, resultConfig) {
assert.isNull(err);
sinon.stub(client, 'getApplication')
.withArgs(mockHref)
.yields(mockRestApiError);

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

done();
});
});

describe('when neither href or name is specified', function () {
it('should resolve a default application', function (done) {
var testConfig = {};
it('should return an error if the application cannot be found by href', function(done) {

if (client.getApplications.restore) {
client.getApplications.restore();
}
sinon.stub(client, 'getApplication')
.withArgs(mockHref)
.yields(mockApplicationNotFoundResponse);

sinon.stub(client, 'getApplications').yields(null, {
items: mockApplications
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();
});

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

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();
});
});
Expand Down

0 comments on commit 585202b

Please sign in to comment.