Skip to content

Commit cb1c5a3

Browse files
author
Joe Goggins
committed
Introduce api._getEffectiveAuth and use it on listDevices({})
1 parent 30c5c05 commit cb1c5a3

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Particle.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ class Particle {
407407
uri = '/v1/devices';
408408
}
409409

410+
auth = this._getEffectiveAuth(auth);
410411
return this.get({ uri, auth, headers, query, context });
411412
}
412413

@@ -2107,7 +2108,21 @@ class Particle {
21072108
setDefaultAuth(auth){
21082109
this._defaultAuth = auth;
21092110
}
2110-
2111+
/**
2112+
* Return provided token if truthy else use default auth if truthy else undefined
2113+
* @param {*} auth Optional auth token or undefined
2114+
* @private
2115+
* @returns {String|undefined} a Particle auth token or undefined
2116+
*/
2117+
_getEffectiveAuth(auth) {
2118+
if (auth) {
2119+
return auth;
2120+
} else if (this._defaultAuth) {
2121+
return this._defaultAuth;
2122+
} else {
2123+
return undefined;
2124+
}
2125+
}
21112126
/**
21122127
* API URI to access a device
21132128
* @param {Object} options Options for this API call

test/Particle.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,18 @@ describe('ParticleAPI', () => {
370370
});
371371

372372
describe('.listDevices', () => {
373+
describe('uses effective auth', () => {
374+
afterEach(() => {
375+
sinon.restore();
376+
});
377+
it('calls this._getEffectiveAuth', async () => {
378+
sinon.stub(api, '_getEffectiveAuth');
379+
sinon.stub(api, 'get'); // don't actually call the real method
380+
await api.listDevices(props);
381+
expect(api._getEffectiveAuth).to.have.property('callCount', 1);
382+
});
383+
});
384+
373385
describe('user scope', () => {
374386
it('generates request', () => {
375387
return api.listDevices(props).then((results) => {
@@ -2728,4 +2740,29 @@ describe('ParticleAPI', () => {
27282740
expect(api._defaultAuth).to.eql(auth);
27292741
});
27302742
});
2743+
2744+
describe('_getEffectiveAuth(auth)', () => {
2745+
afterEach(() => {
2746+
sinon.restore();
2747+
});
2748+
2749+
it('returns provided value when provided value is truthy', () => {
2750+
const expectedReturnValue = 'pass through';
2751+
expect(api._getEffectiveAuth(expectedReturnValue)).to.eql(expectedReturnValue);
2752+
});
2753+
2754+
it('returns value of _defaultAuth when provided value is NOT truthy', () => {
2755+
const providedValue = undefined;
2756+
const expectedReturnValue = 'default auth value';
2757+
api.setDefaultAuth(expectedReturnValue);
2758+
expect(api._getEffectiveAuth(providedValue)).to.eql(expectedReturnValue);
2759+
});
2760+
2761+
it('returns undefined when both provided value and _defaultAuth are NOT truthy', () => {
2762+
const providedValue = undefined;
2763+
const expectedReturnValue = undefined;
2764+
api.setDefaultAuth(undefined);
2765+
expect(api._getEffectiveAuth(providedValue)).to.eql(expectedReturnValue);
2766+
});
2767+
});
27312768
});

0 commit comments

Comments
 (0)