Skip to content

Commit bc940ab

Browse files
author
Joe Goggins
committed
setDefaultAuth throws error when auth is not a non-empty string
1 parent 0305f4f commit bc940ab

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Particle.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,11 @@ class Particle {
21092109
* @returns {undefined}
21102110
*/
21112111
setDefaultAuth(auth){
2112-
this._defaultAuth = auth;
2112+
if (typeof auth === 'string' && auth.length !== 0) {
2113+
this._defaultAuth = auth;
2114+
} else {
2115+
throw new Error('Must pass a non-empty string');
2116+
}
21132117
}
21142118
/**
21152119
* Return provided token if truthy else use default auth if truthy else undefined

test/Particle.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2747,6 +2747,17 @@ describe('ParticleAPI', () => {
27472747
api.setDefaultAuth(auth);
27482748
expect(api._defaultAuth).to.eql(auth);
27492749
});
2750+
2751+
it('throws error unless given a non-empty string', () => {
2752+
let error;
2753+
try {
2754+
api.setDefaultAuth(undefined);
2755+
} catch (e) {
2756+
error = e;
2757+
}
2758+
expect(error).to.be.an.instanceOf(Error);
2759+
expect(error.message).to.eql('Must pass a non-empty string');
2760+
});
27502761
});
27512762

27522763
describe('_getActiveAuthToken(auth)', () => {
@@ -2769,7 +2780,7 @@ describe('ParticleAPI', () => {
27692780
it('returns undefined when both provided value and _defaultAuth are NOT truthy', () => {
27702781
const providedValue = undefined;
27712782
const expectedReturnValue = undefined;
2772-
api.setDefaultAuth(undefined);
2783+
api._defaultAuth = undefined;
27732784
expect(api._getActiveAuthToken(providedValue)).to.eql(expectedReturnValue);
27742785
});
27752786
});

0 commit comments

Comments
 (0)