mirror of
https://github.com/nasa/openmct.git
synced 2024-12-23 15:02:23 +00:00
[Identity] Add identity aggregator
Add an aggregator to handle exposing the user's identity, nasa/openmctweb#92
This commit is contained in:
parent
e4d136d345
commit
29c9b2a08f
11
platform/identity/bundle.json
Normal file
11
platform/identity/bundle.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"extensions": {
|
||||
"components": [
|
||||
{
|
||||
"type": "aggregator",
|
||||
"provides": "identityService",
|
||||
"depends": [ "$q" ]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
65
platform/identity/src/IdentityAggregator.js
Normal file
65
platform/identity/src/IdentityAggregator.js
Normal file
@ -0,0 +1,65 @@
|
||||
/*global define*/
|
||||
|
||||
define(
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Provides information about the currently logged-in
|
||||
* user, if available.
|
||||
*
|
||||
* @interface IdentityService
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get information about the current user. This returns a promise
|
||||
* which will resolve to metadata about the user, or undefined if
|
||||
* no information about the user is available.
|
||||
*
|
||||
* @method IdentityService#getUser
|
||||
* @returns {Promise.<UserMetadata>} metadata about the current user
|
||||
*/
|
||||
|
||||
/**
|
||||
* Metadata about a user.
|
||||
*
|
||||
* @typedef UserMetadata
|
||||
* @property {string} name the user's human-readable name
|
||||
* @property {string} key the user's machine-readable name
|
||||
*/
|
||||
|
||||
/**
|
||||
* Aggregator for multiple identity services. Exposes the first
|
||||
* defined identity provided by any provider, according to
|
||||
* priority order.
|
||||
*
|
||||
* @implements {IdentityService}
|
||||
* @memberof platform/identity
|
||||
*/
|
||||
function IdentityAggregator($q, providers) {
|
||||
this.providers = providers;
|
||||
this.$q = $q;
|
||||
}
|
||||
|
||||
function delegateGetUser(provider) {
|
||||
return provider.getUser();
|
||||
}
|
||||
|
||||
function identity(value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function giveFirst(results) {
|
||||
return results.filter(identity)[0];
|
||||
}
|
||||
|
||||
IdentityAggregator.prototype.getUser = function () {
|
||||
var $q = this.$q,
|
||||
promises = this.providers.map(delegateGetUser);
|
||||
|
||||
return $q.all(promises).then(giveFirst);
|
||||
};
|
||||
|
||||
return IdentityAggregator;
|
||||
}
|
||||
);
|
122
platform/identity/test/IdentityAggregatorSpec.js
Normal file
122
platform/identity/test/IdentityAggregatorSpec.js
Normal file
@ -0,0 +1,122 @@
|
||||
/*global define,Promise,describe,it,expect,beforeEach,waitsFor,jasmine*/
|
||||
|
||||
define(
|
||||
["../src/IdentityAggregator"],
|
||||
function (IdentityAggregator) {
|
||||
"use strict";
|
||||
|
||||
describe("The identity aggregator", function () {
|
||||
var mockProviders,
|
||||
mockQ,
|
||||
resolves,
|
||||
mockPromise,
|
||||
mockCallback,
|
||||
testUsers,
|
||||
aggregator;
|
||||
|
||||
function callbackCalled() {
|
||||
return mockCallback.calls.length > 0;
|
||||
}
|
||||
|
||||
function resolveProviderPromises() {
|
||||
['a', 'b', 'c'].forEach(function (id, i) {
|
||||
resolves[id](testUsers[i]);
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
testUsers = [
|
||||
{ key: "user0", name: "User Zero" },
|
||||
{ key: "user1", name: "User One" },
|
||||
{ key: "user2", name: "User Two" }
|
||||
];
|
||||
|
||||
resolves = {};
|
||||
|
||||
mockProviders = ['a', 'b', 'c'].map(function (id) {
|
||||
var mockProvider = jasmine.createSpyObj(
|
||||
'provider-' + id,
|
||||
[ 'getUser' ]
|
||||
);
|
||||
|
||||
mockProvider.getUser.andReturn(new Promise(function (r) {
|
||||
resolves[id] = r;
|
||||
}));
|
||||
|
||||
return mockProvider;
|
||||
});
|
||||
|
||||
mockQ = jasmine.createSpyObj('$q', ['all']);
|
||||
mockQ.all.andCallFake(function (promises) {
|
||||
return Promise.all(promises);
|
||||
});
|
||||
|
||||
mockCallback = jasmine.createSpy('callback');
|
||||
|
||||
aggregator = new IdentityAggregator(
|
||||
mockQ,
|
||||
mockProviders
|
||||
);
|
||||
});
|
||||
|
||||
it("delegates to the aggregated providers", function () {
|
||||
// Verify precondition
|
||||
mockProviders.forEach(function (p) {
|
||||
expect(p.getUser).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
aggregator.getUser();
|
||||
|
||||
mockProviders.forEach(function (p) {
|
||||
expect(p.getUser).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it("returns the first result when it is defined", function () {
|
||||
aggregator.getUser().then(mockCallback);
|
||||
|
||||
resolveProviderPromises();
|
||||
|
||||
waitsFor(callbackCalled);
|
||||
runs(function () {
|
||||
expect(mockCallback).toHaveBeenCalledWith(testUsers[0]);
|
||||
});
|
||||
});
|
||||
|
||||
it("returns a later result when earlier results are undefined", function () {
|
||||
testUsers[0] = undefined;
|
||||
|
||||
aggregator.getUser().then(mockCallback);
|
||||
|
||||
resolveProviderPromises();
|
||||
|
||||
waitsFor(callbackCalled);
|
||||
runs(function () {
|
||||
expect(mockCallback).toHaveBeenCalledWith(testUsers[1]);
|
||||
});
|
||||
});
|
||||
|
||||
it("returns undefined when no providers expose users", function () {
|
||||
testUsers = [ undefined, undefined, undefined ];
|
||||
|
||||
aggregator.getUser().then(mockCallback);
|
||||
|
||||
resolveProviderPromises();
|
||||
|
||||
waitsFor(callbackCalled);
|
||||
runs(function () {
|
||||
expect(mockCallback).toHaveBeenCalledWith(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
it("returns undefined when there are no providers", function () {
|
||||
new IdentityAggregator(mockQ, []).getUser().then(mockCallback);
|
||||
waitsFor(callbackCalled);
|
||||
runs(function () {
|
||||
expect(mockCallback).toHaveBeenCalledWith(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
);
|
3
platform/identity/test/suite.json
Normal file
3
platform/identity/test/suite.json
Normal file
@ -0,0 +1,3 @@
|
||||
[
|
||||
"IdentityAggregator"
|
||||
]
|
Loading…
Reference in New Issue
Block a user