Skip to content

Commit 242ead6

Browse files
jmdobryAce Nassri
authored and
Ace Nassri
committed
Cleanup App Engine samples and re-work tests. (#354)
1 parent 7caf7de commit 242ead6

File tree

8 files changed

+4063
-76
lines changed

8 files changed

+4063
-76
lines changed

dlp/inspect.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const API_URL = 'https://dlp.googleapis.com/v2beta1';
1919
const fs = require('fs');
2020
const requestPromise = require('request-promise');
2121
const mime = require('mime');
22+
const Buffer = require('safe-buffer').Buffer;
2223

2324
// Helper function to poll the rest API using exponential backoff
2425
function pollJob (body, initialTimeout, tries, authToken) {
@@ -135,7 +136,7 @@ function inspectFile (authToken, filepath, inspectConfig) {
135136
// Construct file data to inspect
136137
const fileItems = [{
137138
type: mime.lookup(filepath) || 'application/octet-stream',
138-
data: new Buffer(fs.readFileSync(filepath)).toString('base64')
139+
data: Buffer.from(fs.readFileSync(filepath)).toString('base64')
139140
}];
140141

141142
// Construct REST request body
@@ -294,7 +295,7 @@ if (module === require.main) {
294295
process.exit(1);
295296
}
296297

297-
const cli = require(`yargs`)
298+
require(`yargs`) // eslint-disable-line
298299
.demand(1)
299300
.command(
300301
`string <string>`,
@@ -313,12 +314,13 @@ if (module === require.main) {
313314
`Inspects a text file stored on Google Cloud Storage using the Data Loss Prevention API.`,
314315
{
315316
initialTimeout: {
316-
type: 'integer',
317-
alias: '-i',
317+
type: 'number',
318+
alias: 'i',
318319
default: 5000
319320
},
320321
tries: {
321-
type: 'integer',
322+
type: 'number',
323+
alias: 'r',
322324
default: 5
323325
}
324326
},
@@ -330,19 +332,22 @@ if (module === require.main) {
330332
{
331333
projectId: {
332334
type: 'string',
335+
alias: 'p',
333336
default: process.env.GCLOUD_PROJECT
334337
},
335338
namespaceId: {
336339
type: 'string',
340+
alias: 'n',
337341
default: ''
338342
},
339343
initialTimeout: {
340-
type: 'integer',
341-
alias: '-i',
344+
type: 'number',
345+
alias: 'i',
342346
default: 5000
343347
},
344348
tries: {
345-
type: 'integer',
349+
type: 'number',
350+
alias: 'r',
346351
default: 5
347352
}
348353
},
@@ -365,7 +370,7 @@ if (module === require.main) {
365370
.option('f', {
366371
alias: 'maxFindings',
367372
default: 0,
368-
type: 'integer',
373+
type: 'number',
369374
global: true
370375
})
371376
.option('q', {
@@ -394,8 +399,9 @@ if (module === require.main) {
394399
.example(`node $0 gcsFile my-bucket my-file.txt`)
395400
.wrap(120)
396401
.recommendCommands()
397-
.epilogue(`For more information, see https://cloud.google.com/dlp/docs. Optional flags are explained at https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/inspect#InspectConfig`);
398-
399-
cli.help().strict().argv;
402+
.epilogue(`For more information, see https://cloud.google.com/dlp/docs. Optional flags are explained at https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/inspect#InspectConfig`)
403+
.help()
404+
.strict()
405+
.argv;
400406
});
401407
}

dlp/metadata.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ if (module === require.main) {
111111
.recommendCommands()
112112
.epilogue(`For more information, see https://cloud.google.com/dlp/docs`);
113113

114-
cli.help().strict().argv;
114+
cli.help().strict().argv; // eslint-disable-line
115115
});
116116
}

dlp/package.json

+20-22
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,36 @@
33
"description": "Command-line interface for Google Cloud Platform's Data Loss Prevention API",
44
"version": "0.0.1",
55
"private": true,
6-
"license": "Apache Version 2.0",
6+
"license": "Apache-2.0",
77
"author": "Google Inc.",
8-
"contributors": [
9-
{
10-
"name": "Ace Nassri",
11-
"email": "[email protected]"
12-
},
13-
{
14-
"name": "Jason Dobry",
15-
"email": "[email protected]"
16-
},
17-
{
18-
"name": "Jon Wayne Parrott",
19-
"email": "[email protected]"
20-
}
21-
],
228
"repository": {
239
"type": "git",
2410
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
2511
},
26-
"scripts": {
27-
"test": "ava system-test/*.test.js -c 20 -T 240s"
12+
"cloud": {
13+
"requiresKeyFile": true,
14+
"requiresProjectId": true
2815
},
2916
"engines": {
3017
"node": ">=4.3.2"
3118
},
19+
"scripts": {
20+
"lint": "samples lint",
21+
"pretest": "npm run lint",
22+
"system-test": "ava -T 1m --verbose system-test/*.test.js",
23+
"test": "npm run system-test"
24+
},
3225
"dependencies": {
33-
"google-auth-library": "^0.10.0",
34-
"google-auto-auth": "^0.5.2",
26+
"google-auth-library": "0.10.0",
27+
"google-auto-auth": "0.6.0",
3528
"mime": "1.3.4",
36-
"request": "2.79.0",
37-
"request-promise": "4.1.1",
38-
"yargs": "6.6.0"
29+
"request": "2.81.0",
30+
"request-promise": "4.2.0",
31+
"safe-buffer": "5.0.1",
32+
"yargs": "7.1.0"
33+
},
34+
"devDependencies": {
35+
"@google-cloud/nodejs-repo-tools": "1.3.1",
36+
"ava": "0.19.1"
3937
}
4038
}

dlp/redact.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ if (module === require.main) {
125125
.recommendCommands()
126126
.epilogue(`For more information, see https://cloud.google.com/dlp/docs. Optional flags are explained at https://cloud.google.com/dlp/docs/reference/rest/v2beta1/content/inspect#InspectConfig`);
127127

128-
cli.help().strict().argv;
128+
cli.help().strict().argv; // eslint-disable-line
129129
});
130130
}

dlp/system-test/inspect.test.js

+29-27
Original file line numberDiff line numberDiff line change
@@ -15,105 +15,108 @@
1515

1616
'use strict';
1717

18-
require(`../../system-test/_setup`);
1918
const path = require('path');
19+
const test = require('ava');
20+
const tools = require('@google-cloud/nodejs-repo-tools');
2021

2122
const cmd = 'node inspect';
2223
const cwd = path.join(__dirname, `..`);
2324

25+
test.before(tools.checkCredentials);
26+
2427
// inspect_string
2528
test(`should inspect a string`, async (t) => {
26-
const output = await runAsync(`${cmd} string "I'm Gary and my email is [email protected]"`, cwd);
29+
const output = await tools.runAsync(`${cmd} string "I'm Gary and my email is [email protected]"`, cwd);
2730
t.regex(output, /"name": "EMAIL_ADDRESS"/);
2831
});
2932

3033
test(`should handle a string with no sensitive data`, async (t) => {
31-
const output = await runAsync(`${cmd} string "foo"`, cwd);
34+
const output = await tools.runAsync(`${cmd} string "foo"`, cwd);
3235
t.is(output, 'undefined');
3336
});
3437

3538
test(`should report string inspection handling errors`, async (t) => {
36-
const output = await runAsync(`${cmd} string "I'm Gary and my email is [email protected]" -a foo`, cwd);
39+
const output = await tools.runAsync(`${cmd} string "I'm Gary and my email is [email protected]" -a foo`, cwd);
3740
t.regex(output, /Error in inspectString/);
3841
});
3942

4043
// inspect_file
4144
test(`should inspect a local text file`, async (t) => {
42-
const output = await runAsync(`${cmd} file resources/test.txt`, cwd);
45+
const output = await tools.runAsync(`${cmd} file resources/test.txt`, cwd);
4346
t.regex(output, /"name": "PHONE_NUMBER"/);
4447
t.regex(output, /"name": "EMAIL_ADDRESS"/);
4548
});
4649

4750
test(`should inspect a local image file`, async (t) => {
48-
const output = await runAsync(`${cmd} file resources/test.png`, cwd);
51+
const output = await tools.runAsync(`${cmd} file resources/test.png`, cwd);
4952
t.regex(output, /"name": "PHONE_NUMBER"/);
5053
});
5154

5255
test(`should handle a local file with no sensitive data`, async (t) => {
53-
const output = await runAsync(`${cmd} file resources/harmless.txt`, cwd);
56+
const output = await tools.runAsync(`${cmd} file resources/harmless.txt`, cwd);
5457
t.is(output, 'undefined');
5558
});
5659

5760
test(`should report local file handling errors`, async (t) => {
58-
const output = await runAsync(`${cmd} file resources/harmless.txt -a foo`, cwd);
61+
const output = await tools.runAsync(`${cmd} file resources/harmless.txt -a foo`, cwd);
5962
t.regex(output, /Error in inspectFile/);
6063
});
6164

6265
// inspect_gcs_file
6366
test.serial(`should inspect a GCS text file`, async (t) => {
64-
const output = await runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp test.txt`, cwd);
67+
const output = await tools.runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp test.txt`, cwd);
6568
t.regex(output, /"name": "PHONE_NUMBER"/);
6669
t.regex(output, /"name": "EMAIL_ADDRESS"/);
6770
});
6871

6972
test.serial(`should inspect multiple GCS text files`, async (t) => {
70-
const output = await runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp *.txt`, cwd);
73+
const output = await tools.runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp *.txt`, cwd);
7174
t.regex(output, /"name": "PHONE_NUMBER"/);
7275
t.regex(output, /"name": "EMAIL_ADDRESS"/);
7376
t.regex(output, /"name": "CREDIT_CARD_NUMBER"/);
7477
});
7578

7679
test.serial(`should accept try limits for inspecting GCS files`, async (t) => {
77-
const output = await runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp test.txt --tries 0`, cwd);
80+
const output = await tools.runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp test.txt --tries 0`, cwd);
7881
t.regex(output, /polling timed out/);
7982
});
8083

8184
test.serial(`should handle a GCS file with no sensitive data`, async (t) => {
82-
const output = await runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp harmless.txt`, cwd);
85+
const output = await tools.runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp harmless.txt`, cwd);
8386
t.is(output, 'undefined');
8487
});
8588

8689
test.serial(`should report GCS file handling errors`, async (t) => {
87-
const output = await runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp harmless.txt -a foo`, cwd);
90+
const output = await tools.runAsync(`${cmd} gcsFile nodejs-docs-samples-dlp harmless.txt -a foo`, cwd);
8891
t.regex(output, /Error in inspectGCSFile/);
8992
});
9093

9194
// inspect_datastore
9295
test.serial(`should inspect Datastore`, async (t) => {
93-
const output = await runAsync(`${cmd} datastore Person --namespaceId DLP`, cwd);
96+
const output = await tools.runAsync(`${cmd} datastore Person --namespaceId DLP`, cwd);
9497
t.regex(output, /"name": "PHONE_NUMBER"/);
9598
t.regex(output, /"name": "EMAIL_ADDRESS"/);
9699
});
97100

98101
test.serial(`should accept try limits for inspecting Datastore`, async (t) => {
99-
const output = await runAsync(`${cmd} datastore Person --namespaceId DLP --tries 0`, cwd);
102+
const output = await tools.runAsync(`${cmd} datastore Person --namespaceId DLP --tries 0`, cwd);
100103
t.regex(output, /polling timed out/);
101104
});
102105

103106
test.serial(`should handle Datastore with no sensitive data`, async (t) => {
104-
const output = await runAsync(`${cmd} datastore Harmless --namespaceId DLP`, cwd);
107+
const output = await tools.runAsync(`${cmd} datastore Harmless --namespaceId DLP`, cwd);
105108
t.is(output, 'undefined');
106109
});
107110

108111
test.serial(`should report Datastore file handling errors`, async (t) => {
109-
const output = await runAsync(`${cmd} datastore Harmless --namespaceId DLP -a foo`, cwd);
112+
const output = await tools.runAsync(`${cmd} datastore Harmless --namespaceId DLP -a foo`, cwd);
110113
t.regex(output, /Error in inspectDatastore/);
111114
});
112115

113116
// CLI options
114117
test(`should have a minLikelihood option`, async (t) => {
115-
const promiseA = runAsync(`${cmd} string "My phone number is (123) 456-7890." -m POSSIBLE`, cwd);
116-
const promiseB = runAsync(`${cmd} string "My phone number is (123) 456-7890." -m UNLIKELY`, cwd);
118+
const promiseA = tools.runAsync(`${cmd} string "My phone number is (123) 456-7890." -m POSSIBLE`, cwd);
119+
const promiseB = tools.runAsync(`${cmd} string "My phone number is (123) 456-7890." -m UNLIKELY`, cwd);
117120

118121
const outputA = await promiseA;
119122
t.truthy(outputA);
@@ -124,8 +127,8 @@ test(`should have a minLikelihood option`, async (t) => {
124127
});
125128

126129
test(`should have a maxFindings option`, async (t) => {
127-
const promiseA = runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890." -f 1`, cwd);
128-
const promiseB = runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890." -f 2`, cwd);
130+
const promiseA = tools.runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890." -f 1`, cwd);
131+
const promiseB = tools.runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890." -f 2`, cwd);
129132

130133
const outputA = await promiseA;
131134
t.not(outputA.includes('PHONE_NUMBER'), outputA.includes('EMAIL_ADDRESS')); // Exactly one of these should be included
@@ -136,8 +139,8 @@ test(`should have a maxFindings option`, async (t) => {
136139
});
137140

138141
test(`should have an option to include quotes`, async (t) => {
139-
const promiseA = runAsync(`${cmd} string "My phone number is (223) 456-7890." -q false`, cwd);
140-
const promiseB = runAsync(`${cmd} string "My phone number is (223) 456-7890."`, cwd);
142+
const promiseA = tools.runAsync(`${cmd} string "My phone number is (223) 456-7890." -q false`, cwd);
143+
const promiseB = tools.runAsync(`${cmd} string "My phone number is (223) 456-7890."`, cwd);
141144

142145
const outputA = await promiseA;
143146
t.truthy(outputA);
@@ -148,8 +151,8 @@ test(`should have an option to include quotes`, async (t) => {
148151
});
149152

150153
test(`should have an option to filter results by infoType`, async (t) => {
151-
const promiseA = runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890."`, cwd);
152-
const promiseB = runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890." -t PHONE_NUMBER`, cwd);
154+
const promiseA = tools.runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890."`, cwd);
155+
const promiseB = tools.runAsync(`${cmd} string "My email is [email protected] and my phone number is (223) 456-7890." -t PHONE_NUMBER`, cwd);
153156

154157
const outputA = await promiseA;
155158
t.regex(outputA, /EMAIL_ADDRESS/);
@@ -161,8 +164,7 @@ test(`should have an option to filter results by infoType`, async (t) => {
161164
});
162165

163166
test(`should have an option for custom auth tokens`, async (t) => {
164-
const output = await runAsync(`${cmd} string "My name is Gary and my phone number is (223) 456-7890." -a foo`, cwd);
167+
const output = await tools.runAsync(`${cmd} string "My name is Gary and my phone number is (223) 456-7890." -a foo`, cwd);
165168
t.regex(output, /Error in inspectString/);
166169
t.regex(output, /invalid authentication/);
167170
});
168-

dlp/system-test/metadata.test.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,38 @@
1515

1616
'use strict';
1717

18-
require(`../../system-test/_setup`);
1918
const path = require('path');
19+
const test = require('ava');
20+
const tools = require('@google-cloud/nodejs-repo-tools');
2021

2122
const cmd = 'node metadata';
2223
const cwd = path.join(__dirname, `..`);
2324

25+
test.before(tools.checkCredentials);
26+
2427
test(`should list info types for a given category`, async (t) => {
25-
const output = await runAsync(`${cmd} infoTypes GOVERNMENT`, cwd);
28+
const output = await tools.runAsync(`${cmd} infoTypes GOVERNMENT`, cwd);
2629
t.regex(output, /name: 'US_DRIVERS_LICENSE_NUMBER'/);
2730
});
2831

2932
test(`should inspect categories`, async (t) => {
30-
const output = await runAsync(`${cmd} categories`, cwd);
33+
const output = await tools.runAsync(`${cmd} categories`, cwd);
3134
t.regex(output, /name: 'FINANCE'/);
3235
});
3336

3437
test(`should have an option for custom auth tokens`, async (t) => {
35-
const output = await runAsync(`${cmd} categories -a foo`, cwd);
38+
const output = await tools.runAsync(`${cmd} categories -a foo`, cwd);
3639
t.regex(output, /Error in listCategories/);
3740
t.regex(output, /invalid authentication/);
3841
});
3942

4043
// Error handling
4144
test(`should report info type listing handling errors`, async (t) => {
42-
const output = await runAsync(`${cmd} infoTypes GOVERNMENT -a foo`, cwd);
45+
const output = await tools.runAsync(`${cmd} infoTypes GOVERNMENT -a foo`, cwd);
4346
t.regex(output, /Error in listInfoTypes/);
4447
});
4548

4649
test(`should report category listing handling errors`, async (t) => {
47-
const output = await runAsync(`${cmd} categories -a foo`, cwd);
50+
const output = await tools.runAsync(`${cmd} categories -a foo`, cwd);
4851
t.regex(output, /Error in listCategories/);
4952
});

0 commit comments

Comments
 (0)