Skip to content

Commit

Permalink
Add interface to load user grants
Browse files Browse the repository at this point in the history
  • Loading branch information
jkintscher committed Feb 21, 2016
1 parent d93bc51 commit 6b9a357
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ti-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const TiAuth = {
}
retrieve_user(resolve);
},
loadGrants(resolve) {
API.get(TiAuth.API_URL + '/api/v1/grants',
(response) => { resolve.call(null, response.grants[0].controls); },
Auth.unauthorize
);
},
reauthorize() {
Auth.unauthorize();
},
Expand Down
33 changes: 33 additions & 0 deletions test/ti-auth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,39 @@ describe('TiAuth', () => {
}));
});

describe('#loadGrants', () => {
it('makes correct request to API', test(function() {
this.mock(API).expects('get').withArgs('https://api.test/api/v1/grants');
TiAuth.loadGrants(noop);
}));

it('resolves with parsed Grants on success', test(function() {
const api_mock = {
grants: [{
user: '[email protected]',
admin: true,
controls: [{
oneThing: true
}, {
andAnother: false
}]
}]
};
const stub = spy();
this.stub(API, 'get', (url, success) => { success(api_mock); });
TiAuth.loadGrants(stub);
assert.calledWith(stub, api_mock.grants[0].controls);
}));

it('unauthorizes if request to API failed', test(function() {
this.stub(API, 'get', (url, _, error) => { error('Nope.'); });
const stub = spy();
this.stub(Authentication, 'unauthorize', stub);
TiAuth.loadGrants();
assert.calledOnce(stub);
}));
});

describe('#reauthorize', () => {
it('delegates to Authentication#unauthorize', test(function() {
const stub = spy();
Expand Down

0 comments on commit 6b9a357

Please sign in to comment.