1414
1515'use strict' ;
1616
17- const path = require ( 'path' ) ;
18- const { assert} = require ( 'chai' ) ;
19- const { describe, it, before} = require ( 'mocha' ) ;
20- const fs = require ( 'fs' ) ;
21- const cp = require ( 'child_process' ) ;
22- const DLP = require ( '@google-cloud/dlp' ) ;
23- const proxyquire = require ( 'proxyquire' ) ;
24- const sinon = require ( 'sinon' ) ;
25- const { MOCK_DATA } = require ( './mockdata' ) ;
26-
27- const execSync = cmd => cp . execSync ( cmd , { encoding : 'utf-8' } ) ;
17+ import { join } from 'path' ;
18+ import { assert } from 'chai' ;
19+ import { describe , it , before } from 'mocha' ;
20+ import { readFileSync } from 'fs' ;
21+ import { execSync as _execSync } from 'child_process' ;
22+ import DLP , { DlpServiceClient } from '@google-cloud/dlp' ;
23+ import proxyquire from 'proxyquire' ;
24+ import { restore , stub , replace , assert as _assert , fake } from 'sinon' ;
25+ import { MOCK_DATA } from './mockdata' ;
26+
27+ const execSync = cmd => _execSync ( cmd , { encoding : 'utf-8' } ) ;
2828
2929const harmfulString = 'My SSN is 372819127' ;
3030const harmlessString = 'My favorite color is blue' ;
3131const surrogateType = 'SSN_TOKEN' ;
3232const csvFile = 'resources/dates.csv' ;
33- const tempOutputFile = path . join ( __dirname , 'temp.result.csv' ) ;
33+ const tempOutputFile = join ( __dirname , 'temp.result.csv' ) ;
3434const dateShiftAmount = 30 ;
3535const dateFields = 'birth_date,register_date' ;
3636const keyName = 'KEY_NAME' ;
@@ -46,7 +46,7 @@ const deidentifyTemplateId = 'MOCK_DEIDENTIFY_TEMPLATE';
4646const structuredDeidentifyTemplateId = 'MOCK_STRUCTURED_ DEIDENTIFY_TEMPLATE' ;
4747const imageRedactTemplateId = 'MOCK_IMAGE_REDACT_TEMPLATE' ;
4848
49- const client = new DLP . DlpServiceClient ( ) ;
49+ const client = new DlpServiceClient ( ) ;
5050describe ( 'deid' , ( ) => {
5151 let projectId ;
5252
@@ -55,7 +55,7 @@ describe('deid', () => {
5555 } ) ;
5656
5757 afterEach ( async ( ) => {
58- sinon . restore ( ) ;
58+ restore ( ) ;
5959 } ) ;
6060
6161 // deidentify_masking
@@ -110,8 +110,8 @@ describe('deid', () => {
110110 `Successfully saved date-shift output to ${ outputCsvFile } `
111111 ) ;
112112 assert . notInclude (
113- fs . readFileSync ( outputCsvFile ) . toString ( ) ,
114- fs . readFileSync ( csvFile ) . toString ( )
113+ readFileSync ( outputCsvFile ) . toString ( ) ,
114+ readFileSync ( csvFile ) . toString ( )
115115 ) ;
116116 } ) ;
117117
@@ -387,37 +387,36 @@ describe('deid', () => {
387387 wrappedKey
388388 ) ;
389389
390- const mockDeidentifyContent = sinon
391- . stub ( )
390+ const mockDeidentifyContent = stub ( )
392391 . resolves ( CONSTANT_DATA . RESPONSE_DEIDENTIFY_CONTENT ) ;
393392
394- sinon . replace (
395- DLP . DlpServiceClient . prototype ,
393+ replace (
394+ DlpServiceClient . prototype ,
396395 'deidentifyContent' ,
397396 mockDeidentifyContent
398397 ) ;
399- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
398+ replace ( console , 'log' , ( ) => stub ( ) ) ;
400399
401400 const deIdentifyTableWithFpe = proxyquire ( '../deIdentifyTableWithFpe' , {
402401 '@google-cloud/dlp' : { DLP : DLP } ,
403402 } ) ;
404403
405404 await deIdentifyTableWithFpe ( projectId , 'NUMERIC' , keyName , wrappedKey ) ;
406405
407- sinon . assert . calledOnceWithExactly (
406+ _assert . calledOnceWithExactly (
408407 mockDeidentifyContent ,
409408 CONSTANT_DATA . REQUEST_DEIDENTIFY_CONTENT
410409 ) ;
411410 } ) ;
412411
413412 it ( 'should handle de-identification errors' , async ( ) => {
414- const mockDeidentifyContent = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
415- sinon . replace (
416- DLP . DlpServiceClient . prototype ,
413+ const mockDeidentifyContent = stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
414+ replace (
415+ DlpServiceClient . prototype ,
417416 'deidentifyContent' ,
418417 mockDeidentifyContent
419418 ) ;
420- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
419+ replace ( console , 'log' , ( ) => stub ( ) ) ;
421420
422421 const deIdentifyTableWithFpe = proxyquire ( '../deIdentifyTableWithFpe' , {
423422 '@google-cloud/dlp' : { DLP : DLP } ,
@@ -494,16 +493,15 @@ describe('deid', () => {
494493 'EMAIL_ADDRESS_TOKEN'
495494 ) ;
496495
497- const mockDeidentifyContent = sinon
498- . stub ( )
496+ const mockDeidentifyContent = stub ( )
499497 . resolves ( CONSTANT_DATA . RESPONSE_DEIDENTIFY_CONTENT ) ;
500498
501- sinon . replace (
502- DLP . DlpServiceClient . prototype ,
499+ replace (
500+ DlpServiceClient . prototype ,
503501 'deidentifyContent' ,
504502 mockDeidentifyContent
505503 ) ;
506- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
504+ replace ( console , 'log' , ( ) => stub ( ) ) ;
507505
508506 const deIdentifyWithDeterministic = proxyquire (
509507 '../deidentifyWithDeterministic.js' ,
@@ -521,7 +519,7 @@ describe('deid', () => {
521519 'EMAIL_ADDRESS_TOKEN'
522520 ) ;
523521
524- sinon . assert . calledOnceWithExactly (
522+ _assert . calledOnceWithExactly (
525523 mockDeidentifyContent ,
526524 CONSTANT_DATA . REQUEST_DEIDENTIFY_CONTENT
527525 ) ;
@@ -531,14 +529,14 @@ describe('deid', () => {
531529 const string =
532530 'My name is Alicia Abernathy, and my email address is [email protected] .' ; 533531
534- const mockDeidentifyContent = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
532+ const mockDeidentifyContent = stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
535533
536- sinon . replace (
537- DLP . DlpServiceClient . prototype ,
534+ replace (
535+ DlpServiceClient . prototype ,
538536 'deidentifyContent' ,
539537 mockDeidentifyContent
540538 ) ;
541- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
539+ replace ( console , 'log' , ( ) => stub ( ) ) ;
542540
543541 const deIdentifyWithDeterministic = proxyquire (
544542 '../deidentifyWithDeterministic.js' ,
@@ -573,16 +571,15 @@ describe('deid', () => {
573571 'EMAIL_ADDRESS_TOKEN'
574572 ) ;
575573
576- const mockReidentifyContent = sinon
577- . stub ( )
574+ const mockReidentifyContent = stub ( )
578575 . resolves ( CONSTANT_DATA . RESPONSE_REIDENTIFY_CONTENT ) ;
579576
580- sinon . replace (
581- DLP . DlpServiceClient . prototype ,
577+ replace (
578+ DlpServiceClient . prototype ,
582579 'reidentifyContent' ,
583580 mockReidentifyContent
584581 ) ;
585- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
582+ replace ( console , 'log' , ( ) => stub ( ) ) ;
586583
587584 const reIdentifyWithDeterministic = proxyquire (
588585 '../reidentifyWithDeterministic.js' ,
@@ -599,7 +596,7 @@ describe('deid', () => {
599596 'EMAIL_ADDRESS_TOKEN'
600597 ) ;
601598
602- sinon . assert . calledOnceWithExactly (
599+ _assert . calledOnceWithExactly (
603600 mockReidentifyContent ,
604601 CONSTANT_DATA . REQUEST_REIDENTIFY_CONTENT
605602 ) ;
@@ -609,14 +606,14 @@ describe('deid', () => {
609606 const string =
610607 'My name is Alicia Abernathy, and my email address is EMAIL_ADDRESS_TOKEN' ;
611608
612- const mockReidentifyContent = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
609+ const mockReidentifyContent = stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
613610
614- sinon . replace (
615- DLP . DlpServiceClient . prototype ,
611+ replace (
612+ DlpServiceClient . prototype ,
616613 'reidentifyContent' ,
617614 mockReidentifyContent
618615 ) ;
619- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
616+ replace ( console , 'log' , ( ) => stub ( ) ) ;
620617
621618 const reIdentifyWithDeterministic = proxyquire (
622619 '../reidentifyWithDeterministic.js' ,
@@ -701,37 +698,36 @@ describe('deid', () => {
701698 wrappedKey
702699 ) ;
703700
704- const mockReidentifyContent = sinon
705- . stub ( )
701+ const mockReidentifyContent = stub ( )
706702 . resolves ( CONSTANT_DATA . RESPONSE_REIDENTIFY_CONTENT ) ;
707703
708- sinon . replace (
709- DLP . DlpServiceClient . prototype ,
704+ replace (
705+ DlpServiceClient . prototype ,
710706 'reidentifyContent' ,
711707 mockReidentifyContent
712708 ) ;
713- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
709+ replace ( console , 'log' , ( ) => stub ( ) ) ;
714710
715711 const reIdentifyTableWithFpe = proxyquire ( '../reidentifyTableWithFpe' , {
716712 '@google-cloud/dlp' : { DLP : DLP } ,
717713 } ) ;
718714
719715 await reIdentifyTableWithFpe ( projectId , 'NUMERIC' , keyName , wrappedKey ) ;
720716
721- sinon . assert . calledOnceWithExactly (
717+ _assert . calledOnceWithExactly (
722718 mockReidentifyContent ,
723719 CONSTANT_DATA . REQUEST_REIDENTIFY_CONTENT
724720 ) ;
725721 } ) ;
726722
727723 it ( 'should handle re-identification errors' , async ( ) => {
728- const mockReidentifyContent = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
729- sinon . replace (
730- DLP . DlpServiceClient . prototype ,
724+ const mockReidentifyContent = stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
725+ replace (
726+ DlpServiceClient . prototype ,
731727 'reidentifyContent' ,
732728 mockReidentifyContent
733729 ) ;
734- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
730+ replace ( console , 'log' , ( ) => stub ( ) ) ;
735731
736732 const reIdentifyTableWithFpe = proxyquire ( '../reidentifyTableWithFpe' , {
737733 '@google-cloud/dlp' : { DLP : DLP } ,
@@ -756,16 +752,15 @@ describe('deid', () => {
756752 'PHONE_TOKEN'
757753 ) ;
758754
759- const mockReidentifyContent = sinon
760- . stub ( )
755+ const mockReidentifyContent = stub ( )
761756 . resolves ( CONSTANT_DATA . RESPONSE_REIDENTIFY_CONTENT ) ;
762757
763- sinon . replace (
764- DLP . DlpServiceClient . prototype ,
758+ replace (
759+ DlpServiceClient . prototype ,
765760 'reidentifyContent' ,
766761 mockReidentifyContent
767762 ) ;
768- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
763+ replace ( console , 'log' , ( ) => stub ( ) ) ;
769764
770765 const reIdentifyTextWithFpe = proxyquire ( '../reidentifyTextWithFpe' , {
771766 '@google-cloud/dlp' : { DLP : DLP } ,
@@ -780,21 +775,21 @@ describe('deid', () => {
780775 'PHONE_TOKEN'
781776 ) ;
782777
783- sinon . assert . calledOnceWithExactly (
778+ _assert . calledOnceWithExactly (
784779 mockReidentifyContent ,
785780 CONSTANT_DATA . REQUEST_REIDENTIFY_CONTENT
786781 ) ;
787782 } ) ;
788783
789784 it ( 'should handle re-identification errors' , async ( ) => {
790- const mockReidentifyContent = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
785+ const mockReidentifyContent = stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
791786 const text = 'My phone number is PHONE_TOKEN(10):9617256398' ;
792- sinon . replace (
793- DLP . DlpServiceClient . prototype ,
787+ replace (
788+ DlpServiceClient . prototype ,
794789 'reidentifyContent' ,
795790 mockReidentifyContent
796791 ) ;
797- sinon . replace ( console , 'log' , ( ) => sinon . stub ( ) ) ;
792+ replace ( console , 'log' , ( ) => stub ( ) ) ;
798793
799794 const reIdentifyTextWithFpe = proxyquire ( '../reidentifyTextWithFpe' , {
800795 '@google-cloud/dlp' : { DLP : DLP } ,
@@ -927,19 +922,19 @@ describe('deid', () => {
927922 imageRedactTemplateId ,
928923 jobName
929924 ) ;
930- const mockCreateDlpJob = sinon . stub ( ) . resolves ( [ { name : jobName } ] ) ;
931- sinon . replace (
932- DLP . DlpServiceClient . prototype ,
925+ const mockCreateDlpJob = stub ( ) . resolves ( [ { name : jobName } ] ) ;
926+ replace (
927+ DlpServiceClient . prototype ,
933928 'createDlpJob' ,
934929 mockCreateDlpJob
935930 ) ;
936931
937- const mockGetDlpJob = sinon . fake . resolves (
932+ const mockGetDlpJob = fake . resolves (
938933 DATA_CONSTANTS . RESPONSE_GET_DLP_JOB_SUCCESS
939934 ) ;
940- sinon . replace ( DLP . DlpServiceClient . prototype , 'getDlpJob' , mockGetDlpJob ) ;
941- const mockConsoleLog = sinon . stub ( ) ;
942- sinon . replace ( console , 'log' , mockConsoleLog ) ;
935+ replace ( DlpServiceClient . prototype , 'getDlpJob' , mockGetDlpJob ) ;
936+ const mockConsoleLog = stub ( ) ;
937+ replace ( console , 'log' , mockConsoleLog ) ;
943938
944939 const deIdentifyCloudStorage = proxyquire ( '../deIdentifyCloudStorage' , {
945940 '@google-cloud/dlp' : { DLP : DLP } ,
@@ -955,11 +950,11 @@ describe('deid', () => {
955950 structuredDeidentifyTemplateId ,
956951 imageRedactTemplateId
957952 ) ;
958- sinon . assert . calledOnceWithExactly (
953+ _assert . calledOnceWithExactly (
959954 mockCreateDlpJob ,
960955 DATA_CONSTANTS . REQUEST_CREATE_DLP_JOB
961956 ) ;
962- sinon . assert . calledOnce ( mockGetDlpJob ) ;
957+ _assert . calledOnce ( mockGetDlpJob ) ;
963958 } ) ;
964959
965960 it ( 'should handle error if inspect cloud storage job fails' , async ( ) => {
@@ -975,19 +970,19 @@ describe('deid', () => {
975970 imageRedactTemplateId ,
976971 jobName
977972 ) ;
978- const mockCreateDlpJob = sinon . stub ( ) . resolves ( [ { name : jobName } ] ) ;
979- sinon . replace (
980- DLP . DlpServiceClient . prototype ,
973+ const mockCreateDlpJob = stub ( ) . resolves ( [ { name : jobName } ] ) ;
974+ replace (
975+ DlpServiceClient . prototype ,
981976 'createDlpJob' ,
982977 mockCreateDlpJob
983978 ) ;
984979
985- const mockGetDlpJob = sinon . fake . resolves (
980+ const mockGetDlpJob = fake . resolves (
986981 DATA_CONSTANTS . RESPONSE_GET_DLP_JOB_FAILED
987982 ) ;
988- sinon . replace ( DLP . DlpServiceClient . prototype , 'getDlpJob' , mockGetDlpJob ) ;
989- const mockConsoleLog = sinon . stub ( ) ;
990- sinon . replace ( console , 'log' , mockConsoleLog ) ;
983+ replace ( DlpServiceClient . prototype , 'getDlpJob' , mockGetDlpJob ) ;
984+ const mockConsoleLog = stub ( ) ;
985+ replace ( console , 'log' , mockConsoleLog ) ;
991986
992987 const deIdentifyCloudStorage = proxyquire ( '../deIdentifyCloudStorage' , {
993988 '@google-cloud/dlp' : { DLP : DLP } ,
@@ -1003,8 +998,8 @@ describe('deid', () => {
1003998 structuredDeidentifyTemplateId ,
1004999 imageRedactTemplateId
10051000 ) ;
1006- sinon . assert . calledOnce ( mockGetDlpJob ) ;
1007- sinon . assert . calledWithMatch (
1001+ _assert . calledOnce ( mockGetDlpJob ) ;
1002+ _assert . calledWithMatch (
10081003 mockConsoleLog ,
10091004 'Job Failed, Please check the configuration.'
10101005 ) ;
0 commit comments