@@ -4,6 +4,8 @@ import FakeTimers from '@sinonjs/fake-timers';
4
4
5
5
import TelemetryDeck from '../src/telemetrydeck.js' ;
6
6
7
+ const anonymousInHex = Buffer . from ( 'anonymous' ) . toString ( 'hex' ) ;
8
+
7
9
test . beforeEach ( ( t ) => {
8
10
const fake = sinon . fake ( ( ) => new Response ( 'OK LOL' ) ) ;
9
11
@@ -14,9 +16,7 @@ test.beforeEach((t) => {
14
16
const random = sinon . fake . returns ( 0.4 ) ; // chosen by fair dice roll. guaranteed to be random.
15
17
sinon . replace ( Math , 'random' , random ) ;
16
18
17
- t . context . cryptoDigest = sinon . fake . returns (
18
- Promise . resolve ( Buffer . from ( 'THIS IS NOT A REAL HASH' ) )
19
- ) ;
19
+ t . context . cryptoDigest = sinon . fake ( ( _ , value ) => Promise . resolve ( Buffer . from ( value ) ) ) ;
20
20
} ) ;
21
21
22
22
test . afterEach . always ( ( ) => {
@@ -68,7 +68,7 @@ test.serial('Can send a signal', async (t) => {
68
68
fake . firstCall . args [ 1 ] . body ,
69
69
JSON . stringify ( [
70
70
{
71
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
71
+ clientUser : anonymousInHex ,
72
72
sessionID : '255s0' ,
73
73
appID : 'foo' ,
74
74
type : 'test' ,
@@ -117,7 +117,7 @@ test.serial('Can send additional payload attributes', async (t) => {
117
117
fake . firstCall . args [ 1 ] . body ,
118
118
JSON . stringify ( [
119
119
{
120
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
120
+ clientUser : anonymousInHex ,
121
121
sessionID : '255s0' ,
122
122
appID : 'foo' ,
123
123
type : 'test' ,
@@ -143,6 +143,8 @@ test.serial('Can send a signal with salty user', async (t) => {
143
143
144
144
const response = await td . signal ( 'test' ) ;
145
145
146
+ const saltyInHex = Buffer . from ( 'salty' ) . toString ( 'hex' ) ;
147
+
146
148
t . is ( await response . text ( ) , 'OK LOL' ) ;
147
149
t . is ( fake . callCount , 1 ) ;
148
150
t . is ( fake . firstCall . args [ 0 ] , 'https://nom.telemetrydeck.com/v2/' ) ;
@@ -152,7 +154,7 @@ test.serial('Can send a signal with salty user', async (t) => {
152
154
fake . firstCall . args [ 1 ] . body ,
153
155
JSON . stringify ( [
154
156
{
155
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
157
+ clientUser : anonymousInHex + saltyInHex ,
156
158
sessionID : '255s0' ,
157
159
appID : 'foo' ,
158
160
type : 'test' ,
@@ -183,7 +185,7 @@ test.serial('Can send a signal with sessionID', async (t) => {
183
185
fake . firstCall . args [ 1 ] . body ,
184
186
JSON . stringify ( [
185
187
{
186
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
188
+ clientUser : anonymousInHex ,
187
189
sessionID : '1234567890' ,
188
190
appID : 'foo' ,
189
191
type : 'test' ,
@@ -213,15 +215,15 @@ test.serial('Can queue signals and send them later', async (t) => {
213
215
t . deepEqual ( td . store . values ( ) , [
214
216
{
215
217
appID : 'foo' ,
216
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
218
+ clientUser : anonymousInHex ,
217
219
receivedAt : now . toISOString ( ) ,
218
220
sessionID : '1234567890' ,
219
221
telemetryClientVersion : 'JavaScriptSDK __PACKAGE_VERSION__' ,
220
222
type : 'foo' ,
221
223
} ,
222
224
{
223
225
appID : 'foo' ,
224
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
226
+ clientUser : anonymousInHex ,
225
227
receivedAt : now . toISOString ( ) ,
226
228
sessionID : '1234567890' ,
227
229
telemetryClientVersion : 'JavaScriptSDK __PACKAGE_VERSION__' ,
@@ -241,7 +243,7 @@ test.serial('Can queue signals and send them later', async (t) => {
241
243
fake . firstCall . args [ 1 ] . body ,
242
244
JSON . stringify ( [
243
245
{
244
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
246
+ clientUser : anonymousInHex ,
245
247
sessionID : '1234567890' ,
246
248
appID : 'foo' ,
247
249
type : 'test' ,
@@ -253,15 +255,15 @@ test.serial('Can queue signals and send them later', async (t) => {
253
255
fake . secondCall . args [ 1 ] . body ,
254
256
JSON . stringify ( [
255
257
{
256
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
258
+ clientUser : anonymousInHex ,
257
259
sessionID : '1234567890' ,
258
260
appID : 'foo' ,
259
261
type : 'foo' ,
260
262
telemetryClientVersion : `JavaScriptSDK __PACKAGE_VERSION__` ,
261
263
receivedAt : now . toISOString ( ) ,
262
264
} ,
263
265
{
264
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
266
+ clientUser : anonymousInHex ,
265
267
sessionID : '1234567890' ,
266
268
appID : 'foo' ,
267
269
type : 'bar' ,
@@ -302,7 +304,7 @@ test.serial('Can build signal payloads', async (t) => {
302
304
fake . firstCall . args [ 1 ] . body ,
303
305
JSON . stringify ( [
304
306
{
305
- clientUser : '54484953204953204e4f542041205245414c2048415348' ,
307
+ clientUser : anonymousInHex ,
306
308
sessionID : '1234567890' ,
307
309
appID : 'foo' ,
308
310
type : 'test' ,
@@ -337,3 +339,46 @@ test.serial('Can find build-in crypto digest', async (t) => {
337
339
338
340
delete globalThis . crypto ;
339
341
} ) ;
342
+
343
+ test . serial ( 'Changing the salt also changes the hash' , async ( t ) => {
344
+ const { fake, cryptoDigest } = t . context ;
345
+
346
+ const td = new TelemetryDeck ( {
347
+ appID : 'foo' ,
348
+ clientUser : 'anonymous' ,
349
+ sessionID : '1234567890' ,
350
+ cryptoDigest,
351
+ } ) ;
352
+
353
+ await td . signal ( 'test' ) ;
354
+ t . is (
355
+ fake . firstCall . args [ 1 ] . body ,
356
+ JSON . stringify ( [
357
+ {
358
+ clientUser : anonymousInHex ,
359
+ sessionID : '1234567890' ,
360
+ appID : 'foo' ,
361
+ type : 'test' ,
362
+ telemetryClientVersion : 'JavaScriptSDK __PACKAGE_VERSION__' ,
363
+ } ,
364
+ ] )
365
+ ) ;
366
+
367
+ td . salt = 'salz' ;
368
+
369
+ const salzInHex = Buffer . from ( 'salz' ) . toString ( 'hex' ) ;
370
+
371
+ await td . signal ( 'test' ) ;
372
+ t . is (
373
+ fake . secondCall . args [ 1 ] . body ,
374
+ JSON . stringify ( [
375
+ {
376
+ clientUser : anonymousInHex + salzInHex ,
377
+ sessionID : '1234567890' ,
378
+ appID : 'foo' ,
379
+ type : 'test' ,
380
+ telemetryClientVersion : 'JavaScriptSDK __PACKAGE_VERSION__' ,
381
+ } ,
382
+ ] )
383
+ ) ;
384
+ } ) ;
0 commit comments