1
1
const cp = require ( "child_process" ) ,
2
2
fs = require ( "fs" ) ;
3
+ const { CLI_ARGS_REGEX , REDACTED } = require ( "../../../../bin/helpers/constants" ) ;
3
4
4
5
const chai = require ( "chai" ) ,
5
6
expect = chai . expect ,
@@ -9,7 +10,8 @@ const chai = require("chai"),
9
10
rewire = require ( "rewire" ) ;
10
11
11
12
const logger = require ( "../../../../bin/helpers/logger" ) . winstonLogger ,
12
- testObjects = require ( "../../support/fixtures/testObjects" ) ;
13
+ testObjects = require ( "../../support/fixtures/testObjects" ) ,
14
+ constant = require ( '../../../../bin/helpers/constants' ) ;
13
15
14
16
const usageReporting = rewire ( "../../../../bin/helpers/usageReporting" ) ;
15
17
@@ -27,6 +29,7 @@ bstack_json_found_in_pwd = usageReporting.__get__("bstack_json_found_in_pwd");
27
29
cypress_json_found_in_pwd = usageReporting . __get__ ( "cypress_json_found_in_pwd" ) ;
28
30
npm_global_path = usageReporting . __get__ ( "npm_global_path" ) ;
29
31
cli_version_and_path = usageReporting . __get__ ( "cli_version_and_path" ) ;
32
+ redactKeys = usageReporting . __get__ ( "redactKeys" ) ;
30
33
31
34
describe ( "usageReporting" , ( ) => {
32
35
describe ( "_os" , ( ) => {
@@ -408,4 +411,77 @@ describe("usageReporting", () => {
408
411
expect ( ci_environment ( ) ) . to . be . null ;
409
412
} ) ;
410
413
} ) ;
414
+
415
+ describe ( "redactKeys" , ( ) => {
416
+ it ( "filters username and access_key from bstack_config" , ( ) => {
417
+ const bstack_config = { auth : { username : "test_123" , access_key : "test_key" } } ;
418
+ const sanitizedbsConfig = redactKeys ( JSON . stringify ( bstack_config ) , constant . AUTH_REGEX , constant . REDACTED_AUTH ) ;
419
+ expect ( sanitizedbsConfig . includes ( "[REDACTED]" ) ) . to . be . true ;
420
+ expect ( sanitizedbsConfig . includes ( "test_123" ) ) . to . be . false ;
421
+ expect ( sanitizedbsConfig . includes ( "test_key" ) ) . to . be . false ;
422
+ } ) ;
423
+
424
+ it ( "filters keys from cli_args" , ( ) => {
425
+ const cli_args = {
426
+ _ : [ 'generate-report' , 'ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf' ] ,
427
+ u : 'test_123' ,
428
+ username : 'test_123' ,
429
+ k : 'test_key' ,
430
+ key : 'test_key' ,
431
+ cf : 'browserstack.json' ,
432
+ 'config-file' : 'browserstack.json' ,
433
+ configFile : 'browserstack.json' ,
434
+ '$0' : 'browserstack-cypress'
435
+ }
436
+ const sanitizedCliArgs = redactKeys ( JSON . stringify ( cli_args ) , constant . CLI_ARGS_REGEX , constant . REDACTED ) ;
437
+ expect ( sanitizedCliArgs . includes ( "[REDACTED]" ) ) . to . be . true ;
438
+ expect ( sanitizedCliArgs . includes ( "test_123" ) ) . to . be . false ;
439
+ expect ( sanitizedCliArgs . includes ( "test_key" ) ) . to . be . false ;
440
+ expect ( sanitizedCliArgs ) . to . be . equal ( "{\"_\":[\"generate-report\",\"ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf\"],\"u\":[REDACTED],\"username\":[REDACTED],\"k\":[REDACTED],\"key\":[REDACTED],\"cf\":\"browserstack.json\",\"config-file\":\"browserstack.json\",\"configFile\":\"browserstack.json\",\"$0\":\"browserstack-cypress\"}" ) ;
441
+ expect ( redactKeys ( JSON . stringify ( {
442
+ u : 'test_123' ,
443
+ username : 'test_123' ,
444
+ k : 'test_key' ,
445
+ key : 'test_key' ,
446
+ cf : 'browserstack.json' ,
447
+ 'config-file' : 'browserstack.json' ,
448
+ configFile : 'browserstack.json' ,
449
+ '$0' : 'browserstack-cypress'
450
+ } ) , CLI_ARGS_REGEX , REDACTED ) ) . to . be . equal ( "{\"u\":[REDACTED],\"username\":[REDACTED],\"k\":[REDACTED],\"key\":[REDACTED],\"cf\":\"browserstack.json\",\"config-file\":\"browserstack.json\",\"configFile\":\"browserstack.json\",\"$0\":\"browserstack-cypress\"}" ) ;
451
+ expect ( redactKeys ( JSON . stringify ( {
452
+ u : 'test_123' ,
453
+ username : 'test_123' ,
454
+ k : 'test_key' ,
455
+ key : 'test_key'
456
+ } ) , CLI_ARGS_REGEX , REDACTED ) ) . to . be . equal ( "{\"u\":[REDACTED],\"username\":[REDACTED],\"k\":[REDACTED],\"key\":[REDACTED]}" ) ;
457
+ } ) ;
458
+
459
+ it ( "filters keys from raw_args" , ( ) => {
460
+ const raw_args = [
461
+ 'generate-report' ,
462
+ 'ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf' ,
463
+ '-u' ,
464
+ 'test_123' ,
465
+ '-k' ,
466
+ 'test_key'
467
+ ]
468
+ let sanitizedRawArgs = redactKeys ( JSON . stringify ( raw_args ) , constant . RAW_ARGS_REGEX , constant . REDACTED ) ;
469
+ expect ( sanitizedRawArgs . includes ( "[REDACTED]" ) ) . to . be . true ;
470
+ expect ( sanitizedRawArgs . includes ( "test_123" ) ) . to . be . false ;
471
+ expect ( sanitizedRawArgs . includes ( "test_key" ) ) . to . be . false ;
472
+ expect ( sanitizedRawArgs ) . to . be . equal ( "[\"generate-report\",\"ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf\",\"-u\",[REDACTED],\"-k\",[REDACTED]]" ) ;
473
+ raw_args . push ( '-files' , "test.txt" ) ;
474
+ sanitizedRawArgs = redactKeys ( JSON . stringify ( raw_args ) , constant . RAW_ARGS_REGEX , constant . REDACTED ) ;
475
+ expect ( sanitizedRawArgs . includes ( "[REDACTED]" ) ) . to . be . true ;
476
+ expect ( sanitizedRawArgs . includes ( "test_123" ) ) . to . be . false ;
477
+ expect ( sanitizedRawArgs . includes ( "test_key" ) ) . to . be . false ;
478
+ expect ( sanitizedRawArgs ) . to . be . equal ( "[\"generate-report\",\"ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf\",\"-u\",[REDACTED],\"-k\",[REDACTED],\"-files\",\"test.txt\"]" ) ;
479
+ raw_args . shift ( ) ; raw_args . shift ( ) ;
480
+ sanitizedRawArgs = redactKeys ( JSON . stringify ( raw_args ) , constant . RAW_ARGS_REGEX , constant . REDACTED ) ;
481
+ expect ( sanitizedRawArgs . includes ( "[REDACTED]" ) ) . to . be . true ;
482
+ expect ( sanitizedRawArgs . includes ( "test_123" ) ) . to . be . false ;
483
+ expect ( sanitizedRawArgs . includes ( "test_key" ) ) . to . be . false ;
484
+ expect ( sanitizedRawArgs ) . to . be . equal ( "[\"-u\",[REDACTED],\"-k\",[REDACTED],\"-files\",\"test.txt\"]" ) ;
485
+ } ) ;
486
+ } )
411
487
} ) ;
0 commit comments