Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit d7a7683

Browse files
authored
Merge pull request #540 from stormpath/533-apiKey-getAccount
Add ApiKey.getAccount() method
2 parents 981bba6 + 2d11145 commit d7a7683

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

lib/resource/ApiKey.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,18 @@ ApiKey.prototype._decrypt = function _decrypt(callback) {
8484
}
8585
};
8686

87+
/**
88+
* Retrieves the account resource associated with the ApiKey.
89+
*
90+
* @param {ExpansionOptions} [options]
91+
* For retrieving linked resources of the {@link Account} during this request.
92+
*
93+
* @param {Function} Callback
94+
* Callback function, will be called with (err, {@link Account account}).
95+
*/
96+
ApiKey.prototype.getAccount = function () {
97+
var args = utils.resolveArgs(arguments, ['options', 'callback'], true);
98+
return this.dataStore.getResource(this.account.href, args.options, require('./Account'), args.callback);
99+
};
100+
87101
module.exports = ApiKey;

test/sp.resource.apikey_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var sinon = require('sinon');
5+
var uuid = require('uuid');
6+
7+
var ApiKey = require('../lib/resource/ApiKey');
8+
var DataStore = require('../lib/ds/DataStore');
9+
10+
describe('resource', function() {
11+
describe('ApiKey', function() {
12+
var clientApiKeySecret, sandbox, cbSpy, apiKey, getResourceStub;
13+
14+
before(function(done) {
15+
clientApiKeySecret = uuid();
16+
sandbox = sinon.sandbox.create();
17+
cbSpy = sandbox.spy();
18+
19+
apiKey = new ApiKey({
20+
id: 'id',
21+
secret: 'secret',
22+
account: {
23+
href: '/boom'
24+
}
25+
});
26+
27+
apiKey.dataStore = new DataStore({
28+
client: {
29+
apiKey: {
30+
id: '1',
31+
secret: clientApiKeySecret
32+
}
33+
}
34+
});
35+
36+
getResourceStub = sinon.stub(apiKey.dataStore, 'getResource', function() {
37+
var args = Array.prototype.slice.call(arguments);
38+
var href = args.shift();
39+
var callback = args.pop();
40+
callback(null, {href: href});
41+
});
42+
43+
done();
44+
});
45+
46+
after(function() {
47+
sandbox.restore();
48+
});
49+
50+
it('should provide a getAccount method', function() {
51+
assert(typeof apiKey.getAccount === 'function');
52+
});
53+
54+
describe('getAccount', function() {
55+
it('should return an Account object', function(done) {
56+
apiKey.getAccount(function(err, account) {
57+
if (err) {
58+
return done(err);
59+
}
60+
61+
assert(account.href === '/boom');
62+
assert(getResourceStub.calledOnce);
63+
64+
done();
65+
});
66+
});
67+
});
68+
});
69+
});

0 commit comments

Comments
 (0)