Skip to content

Commit 70a7103

Browse files
committed
Allow json method to be called multiple times
1 parent db04bcc commit 70a7103

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/user-data.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export default class UserData {
1717
json() {
1818
const verificationSecret = this.getVerificationSecret();
1919
const identifier = this.getIdentifier();
20-
this.setUserHash(verificationSecret, identifier);
20+
if (this.settings.user_hash === undefined) {
21+
this.setUserHash(verificationSecret, identifier);
22+
}
2123
return this.escapedSettings(this.settings);
2224
}
2325
getVerificationSecret() {
@@ -41,6 +43,7 @@ export default class UserData {
4143
secretKey: verificationSecret,
4244
identifier
4345
});
46+
4447
this.settings.user_hash = userHash;
4548
}
4649
escapedSettings(settings) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
],
2020
"dependencies": {
2121
"bluebird": "^3.3.4",
22+
"htmlencode": "^0.0.4",
2223
"request": "^2.83.0",
23-
"htmlencode": "^0.0.4"
24+
"sinon": "^4.1.3"
2425
},
2526
"devDependencies": {
2627
"babel-core": "^6.7.4",

test/user-data.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import UserData from '../lib/user-data';
22
import assert from 'assert';
3+
import sinon from 'sinon';
34

45
describe('userData', () => {
56
it('should be able to grab the verification secret', () => {
@@ -110,6 +111,20 @@ describe('userData', () => {
110111
assert.equal(Object.keys(result).indexOf(settings.verificationSecret), -1);
111112
});
112113

114+
it('should skip setUserHash if user_hash is already defined', () => {
115+
const settings = {
116+
verificationSecret: 'abc123',
117+
app_id: 'xyz789',
118+
user_id: 1
119+
};
120+
const userData = new UserData(settings);
121+
const setUserHash = sinon.spy(userData, 'setUserHash');
122+
userData.json();
123+
userData.json();
124+
setUserHash.restore();
125+
sinon.assert.calledOnce(setUserHash);
126+
});
127+
113128
it('should return the userData', () => {
114129
const settings = {
115130
verificationSecret: 'abc123',

0 commit comments

Comments
 (0)