@@ -20,11 +20,14 @@ const cp = require('child_process');
2020const uuid = require ( 'uuid' ) ;
2121const DLP = require ( '@google-cloud/dlp' ) ;
2222const { Storage} = require ( '@google-cloud/storage' ) ;
23+ const proxyquire = require ( 'proxyquire' ) ;
24+ const sinon = require ( 'sinon' ) ;
25+
26+ const { MOCK_DATA } = require ( './mockdata' ) ;
2327
2428const dataProject = 'bigquery-public-data' ;
2529const dataSetId = 'samples' ;
2630const tableId = 'github_nested' ;
27- const fieldId = 'url' ;
2831
2932const storage = new Storage ( ) ;
3033const testFile = 'resources/test.txt' ;
@@ -35,7 +38,6 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
3538const client = new DLP . DlpServiceClient ( ) ;
3639describe ( 'metadata' , ( ) => {
3740 let projectId , storedInfoTypeId ;
38- const infoTypeCloudStorageFileSet = `gs://${ bucketName } /test.txt` ;
3941
4042 before ( async ( ) => {
4143 projectId = await client . getProjectId ( ) ;
@@ -58,6 +60,7 @@ describe('metadata', () => {
5860
5961 // Delete stored infotypes created in the snippets.
6062 afterEach ( async ( ) => {
63+ sinon . restore ( ) ;
6164 if ( ! storedInfoTypeId ) {
6265 return ;
6366 }
@@ -85,108 +88,147 @@ describe('metadata', () => {
8588 } ) ;
8689
8790 // dlp_create_stored_infotype
88- it ( 'should create a stored infotype' , ( ) => {
89- const infoTypeId = `stored-infoType-${ uuid . v4 ( ) } ` ;
90- const infoTypeOutputPath = `gs://${ bucketName } ` ;
91- const output = execSync (
92- `node createStoredInfoType.js ${ projectId } ${ infoTypeId } ${ infoTypeOutputPath } ${ dataProject } ${ dataSetId } ${ tableId } ${ fieldId } `
91+ it ( 'should create a stored infotype' , async ( ) => {
92+ const storedInfoTypeName = 'MOCK_INFOTYPE' ;
93+ const infoTypeId = 'MOCK_INFOTYPE_ID' ;
94+ const outputPath = 'MOCK_OUTPUT_PATH' ;
95+ const fieldName = 'MOCK_FIELD' ;
96+ const DATA_CONSTANTS = MOCK_DATA . CREATE_STORED_INFOTYPE (
97+ projectId ,
98+ infoTypeId ,
99+ outputPath ,
100+ dataProject ,
101+ dataSetId ,
102+ tableId ,
103+ fieldName ,
104+ storedInfoTypeName
105+ ) ;
106+ const mockCreateStoredInfoType = sinon
107+ . stub ( )
108+ . resolves ( [ { name : storedInfoTypeName } ] ) ;
109+ sinon . replace (
110+ DLP . DlpServiceClient . prototype ,
111+ 'createStoredInfoType' ,
112+ mockCreateStoredInfoType
113+ ) ;
114+
115+ const mockConsoleLog = sinon . stub ( ) ;
116+ sinon . replace ( console , 'log' , mockConsoleLog ) ;
117+
118+ const createStoredInfoType = proxyquire ( '../createStoredInfoType' , {
119+ '@google-cloud/dlp' : { DLP : DLP } ,
120+ } ) ;
121+ await createStoredInfoType (
122+ projectId ,
123+ infoTypeId ,
124+ outputPath ,
125+ dataProject ,
126+ dataSetId ,
127+ tableId ,
128+ fieldName
129+ ) ;
130+ sinon . assert . calledOnceWithExactly (
131+ mockCreateStoredInfoType ,
132+ DATA_CONSTANTS . REQUEST_CREATE_DLP_JOB
133+ ) ;
134+ sinon . assert . calledWithExactly (
135+ mockConsoleLog ,
136+ `InfoType stored successfully: ${ storedInfoTypeName } `
93137 ) ;
94- assert . match ( output , / I n f o T y p e s t o r e d s u c c e s s f u l l y : / ) ;
95- storedInfoTypeId = output . split ( ':' ) [ 1 ] . trim ( ) ;
96138 } ) ;
97139
98- it ( 'should handle stored infotype creation errors' , ( ) => {
99- let output ;
100- const infoTypeId = `stored-infoType-${ uuid . v4 ( ) } ` ;
101- const infoTypeOutputPath = 'INFOTYPE_OUTPUT_PATH' ;
140+ it ( 'should handle error while creating stored infotype' , async ( ) => {
141+ const infoTypeId = 'MOCK_INFOTYPE_ID' ;
142+ const outputPath = 'MOCK_OUTPUT_PATH' ;
143+ const fieldName = 'MOCK_FIELD' ;
144+ const mockCreateStoredInfoType = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
145+ sinon . replace (
146+ DLP . DlpServiceClient . prototype ,
147+ 'createStoredInfoType' ,
148+ mockCreateStoredInfoType
149+ ) ;
150+
151+ const mockConsoleLog = sinon . stub ( ) ;
152+ sinon . replace ( console , 'log' , mockConsoleLog ) ;
153+
154+ const createStoredInfoType = proxyquire ( '../createStoredInfoType' , {
155+ '@google-cloud/dlp' : { DLP : DLP } ,
156+ } ) ;
102157 try {
103- output = execSync (
104- `node createStoredInfoType.js BAD_PROJECT_ID ${ infoTypeId } ${ infoTypeOutputPath } ${ dataProject } ${ dataSetId } ${ tableId } ${ fieldId } `
158+ await createStoredInfoType (
159+ projectId ,
160+ infoTypeId ,
161+ outputPath ,
162+ dataProject ,
163+ dataSetId ,
164+ tableId ,
165+ fieldName
105166 ) ;
106- } catch ( err ) {
107- output = err . message ;
167+ } catch ( error ) {
168+ assert . equal ( error . message , 'Failed' ) ;
108169 }
109- assert . include ( output , 'INVALID_ARGUMENT' ) ;
110170 } ) ;
111171
112172 // dlp_update_stored_infotype
113173 it ( 'should update a stored infotype' , async ( ) => {
114- let output ;
115- const infoTypeId = `stored-infoType-${ uuid . v4 ( ) } ` ;
116- const infoTypeOutputPath = `gs://${ bucketName } ` ;
117- try {
118- // First create a temporary stored infoType
119- const [ response ] = await client . createStoredInfoType ( {
120- parent : `projects/${ projectId } /locations/global` ,
121- config : {
122- displayName : 'GitHub usernames' ,
123- description : 'Dictionary of GitHub usernames used in commits' ,
124- largeCustomDictionary : {
125- outputPath : {
126- path : infoTypeOutputPath ,
127- } ,
128- bigQueryField : {
129- table : {
130- datasetId : dataSetId ,
131- projectId : dataProject ,
132- tableId : tableId ,
133- } ,
134- field : {
135- name : fieldId ,
136- } ,
137- } ,
138- } ,
139- } ,
140- storedInfoTypeId : infoTypeId ,
141- } ) ;
142- storedInfoTypeId = response . name ;
143- // Execute the update script
144- output = execSync (
145- `node updateStoredInfoType.js ${ projectId } ${ infoTypeId } ${ infoTypeOutputPath } ${ infoTypeCloudStorageFileSet } `
146- ) ;
147- } catch ( err ) {
148- output = err . message ;
149- }
150- assert . match ( output , / I n f o T y p e u p d a t e d s u c c e s s f u l l y : / ) ;
174+ const storedInfoTypeName = 'MOCK_INFOTYPE' ;
175+ const infoTypeId = 'MOCK_INFOTYPE_ID' ;
176+ const outputPath = 'MOCK_OUTPUT_PATH' ;
177+ const fileSetUrl = 'MOCK_FILE_SET_URL' ;
178+ const DATA_CONSTANTS = MOCK_DATA . UPDATE_STORED_INFOTYPE (
179+ projectId ,
180+ infoTypeId ,
181+ outputPath ,
182+ fileSetUrl
183+ ) ;
184+ const mockUpdateStoredInfoType = sinon
185+ . stub ( )
186+ . resolves ( [ { name : storedInfoTypeName } ] ) ;
187+ sinon . replace (
188+ DLP . DlpServiceClient . prototype ,
189+ 'updateStoredInfoType' ,
190+ mockUpdateStoredInfoType
191+ ) ;
192+
193+ const mockConsoleLog = sinon . stub ( ) ;
194+ sinon . replace ( console , 'log' , mockConsoleLog ) ;
195+
196+ const updateStoredInfoType = proxyquire ( '../updateStoredInfoType' , {
197+ '@google-cloud/dlp' : { DLP : DLP } ,
198+ } ) ;
199+ await updateStoredInfoType ( projectId , infoTypeId , outputPath , fileSetUrl ) ;
200+
201+ sinon . assert . calledWith (
202+ mockUpdateStoredInfoType ,
203+ DATA_CONSTANTS . REQUEST_UPDATE_STORED_INFOTYPE
204+ ) ;
205+ sinon . assert . calledWithMatch (
206+ mockConsoleLog ,
207+ 'InfoType updated successfully:'
208+ ) ;
151209 } ) ;
152210
153- it ( 'should handle stored infotype update errors' , async ( ) => {
154- let output ;
155- const infoTypeId = `stored-infoType-${ uuid . v4 ( ) } ` ;
156- const infoTypeOutputPath = 'INFOTYPE_OUTPUT_PATH' ;
211+ it ( 'should handle error while updating stored infotype' , async ( ) => {
212+ const infoTypeId = 'MOCK_INFOTYPE_ID' ;
213+ const outputPath = 'MOCK_OUTPUT_PATH' ;
214+ const fileSetUrl = 'MOCK_FILE_SET_URL' ;
215+ const mockUpdateStoredInfoType = sinon . stub ( ) . rejects ( new Error ( 'Failed' ) ) ;
216+ sinon . replace (
217+ DLP . DlpServiceClient . prototype ,
218+ 'updateStoredInfoType' ,
219+ mockUpdateStoredInfoType
220+ ) ;
221+
222+ const mockConsoleLog = sinon . stub ( ) ;
223+ sinon . replace ( console , 'log' , mockConsoleLog ) ;
224+
225+ const updateStoredInfoType = proxyquire ( '../updateStoredInfoType' , {
226+ '@google-cloud/dlp' : { DLP : DLP } ,
227+ } ) ;
157228 try {
158- // First create a temporary stored infoType
159- const [ response ] = await client . createStoredInfoType ( {
160- parent : `projects/${ projectId } /locations/global` ,
161- config : {
162- displayName : 'GitHub usernames' ,
163- description : 'Dictionary of GitHub usernames used in commits' ,
164- largeCustomDictionary : {
165- outputPath : {
166- path : infoTypeOutputPath ,
167- } ,
168- bigQueryField : {
169- table : {
170- datasetId : dataSetId ,
171- projectId : dataProject ,
172- tableId : tableId ,
173- } ,
174- field : {
175- name : fieldId ,
176- } ,
177- } ,
178- } ,
179- } ,
180- storedInfoTypeId : infoTypeId ,
181- } ) ;
182- storedInfoTypeId = response . name ;
183- // Execute the update script
184- output = execSync (
185- `node updateStoredInfoType.js BAD_PROJECT_ID ${ infoTypeId } ${ infoTypeOutputPath } ${ infoTypeCloudStorageFileSet } `
186- ) ;
187- } catch ( err ) {
188- output = err . message ;
229+ await updateStoredInfoType ( projectId , infoTypeId , outputPath , fileSetUrl ) ;
230+ } catch ( error ) {
231+ assert . equal ( error . message , 'Failed' ) ;
189232 }
190- assert . include ( output , 'INVALID_ARGUMENT' ) ;
191233 } ) ;
192234} ) ;
0 commit comments