Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/resource/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var ApplicationAccountStoreMapping = require('./ApplicationAccountStoreMapping')
var AuthenticationResult = require('./AuthenticationResult');
var AuthRequestParser = require('../authc/AuthRequestParser');
var BasicApiAuthenticator = require('../authc/BasicApiAuthenticator');
var Directory = require('./Directory');
var InstanceResource = require('./InstanceResource');
var OauthAccessTokenAuthenticator = require('../authc/OauthAccessTokenAuthenticator');
var OAuthBasicExchangeAuthenticator = require('../authc/OAuthBasicExchangeAuthenticator');
Expand Down Expand Up @@ -1503,4 +1504,39 @@ Application.prototype.getAccountLinkingPolicy = function getApplicationAccountLi
return this.dataStore.getResource(this.accountLinkingPolicy.href, args.options, require('./AccountLinkingPolicy'), args.callback);
};

/**
* Retrieves the application's linked {@link Directory} instances.
*
* @param {Function} callback
* The function that will be called when the query is finished, with the parameters
* (err, [{@link Directory}]).
*/
Application.prototype.getDirectories = function getDirectories(/* callback */) {
var self = this;
var args = utils.resolveArgs(arguments, ['callback'], true);

this.getAccountStoreMappings({expand: 'accountStore'}, function(err, collection) {
if (err) {
return args.callback(err);
}

return collection.map(function(accountStoreMapping, next) {
next(null, accountStoreMapping.accountStore);
}, function(err, accountStores) {
if (err) {
return args.callback(err);
}

var directories = accountStores.filter(function(store) {
// Determines which are directories by URL matching
return store && store.href && store.href.match(/\/directories\//);
}).map(function(directoryData) {
return new Directory(directoryData, self.dataStore);
});

args.callback(null, directories);
});
});
};

module.exports = Application;
108 changes: 108 additions & 0 deletions test/sp.resource.application_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,114 @@ describe('Resources: ', function () {
});
});

describe('get directories', function () {
var appObj;
var asmsObj;
var app;

before(function () {
asmsObj = {
href: '/account/store/mappings/href',
limit: 25,
size: 3,
offset: 0,
items: [
{
href: '/directories/1',
accountStore: {
href: '/directories/1',
name: 'Directory 1'
}
},
{
href: '/directories/2',
accountStore: {
href: '/directories/2',
name: 'Directory 2'
}
},
{
href: '/organizations/1',
accountStore: {
href: '/organizations/1',
name: 'Organization 1'
}
}
]
};

appObj = {href: 'application/href', accountStoreMappings: {href: asmsObj.href}};
app = new Application(appObj, dataStore);
});

describe('without errors', function () {
beforeEach(function () {
nock(u.BASE_URL).get(u.v1(asmsObj.href + '?expand=accountStore')).reply(200, asmsObj);
});

it('should return a list of directories', function (done) {
app.getDirectories(function (err, dirs) {
if (err) {
return done(err);
}

assert.instanceOf(dirs, Array);
assert.isOk(dirs[0]);
assert.instanceOf(dirs[0], Directory);
done();
});
});

it('should not wrap non-directories into Directories', function (done) {
app.getDirectories(function (err, dirs) {
if (err) {
return done(err);
}

assert.lengthOf(dirs, 2);

var directoryAsmsData = asmsObj.items.slice(0, 2).map(function (asm) {
return asm.accountStore;
});
var directoriesBareData = dirs.map(function (dir) {
return {
href: dir.href,
name: dir.name
};
});
assert.deepEqual(directoriesBareData, directoryAsmsData);
done();
});
});
});

describe('with errors', function () {
var cbSpy;
var errorData;

beforeEach(function () {
cbSpy = sinon.spy();
errorData = {
developerMessage: 'explosions',
status: '400'
};

nock(u.BASE_URL).get(u.v1(asmsObj.href + '?expand=accountStore')).reply(400, errorData);
});

it('should return the error in the callback if one exists', function (done) {
app.getDirectories(function (err, data) {
assert.isOk(err);
assert.isNotOk(data);
assert.equal(err.developerMessage, errorData.developerMessage);
assert.equal(err.status, errorData.status);

done();
});
});
});
});

describe('get default group store', function () {
function getDefaultGroupStore(data) {
return function () {
Expand Down