Skip to content

Commit

Permalink
refactor: modernize the sample tests (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Feb 12, 2019
1 parent 59e0870 commit 7b9c68f
Show file tree
Hide file tree
Showing 11 changed files with 747 additions and 1,004 deletions.
7 changes: 5 additions & 2 deletions dlp/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "dlp-samples",
"description": "Code samples for Google Cloud Platform's Data Loss Prevention API",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": "googleapis/nodejs-dlp",
"files": [
"*.js"
],
"engines": {
"node": ">=8"
},
Expand All @@ -19,7 +21,8 @@
"yargs": "^12.0.1"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.2.0",
"pixelmatch": "^4.0.2",
"pngjs": "^3.3.3",
Expand Down
2 changes: 0 additions & 2 deletions dlp/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
---
env:
mocha: true
rules:
node/no-unpublished-require: off
274 changes: 119 additions & 155 deletions dlp/system-test/deid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,174 +16,138 @@
'use strict';

const path = require('path');
const assert = require('assert');
const {assert} = require('chai');
const fs = require('fs');
const tools = require('@google-cloud/nodejs-repo-tools');
const execa = require('execa');

const cmd = 'node deid.js';
const cwd = path.join(__dirname, '..');

const exec = async cmd => {
const res = await execa.shell(cmd);
if (res.stderr) {
throw new Error(res.stderr);
}
return res.stdout;
};
const harmfulString = 'My SSN is 372819127';
const harmlessString = 'My favorite color is blue';

const surrogateType = 'SSN_TOKEN';
let labeledFPEString;

const wrappedKey = process.env.DLP_DEID_WRAPPED_KEY;
const keyName = process.env.DLP_DEID_KEY_NAME;

const csvFile = 'resources/dates.csv';
const tempOutputFile = path.join(__dirname, 'temp.result.csv');
const csvContextField = 'name';
const dateShiftAmount = 30;
const dateFields = 'birth_date register_date';

before(tools.checkCredentials);

// deidentify_masking
it('should mask sensitive data in a string', async () => {
const output = await tools.runAsync(
`${cmd} deidMask "${harmfulString}" -m x -n 5`,
cwd
);
assert.strictEqual(output, 'My SSN is xxxxx9127');
});

it('should ignore insensitive data when masking a string', async () => {
const output = await tools.runAsync(
`${cmd} deidMask "${harmlessString}"`,
cwd
);
assert.strictEqual(output, harmlessString);
});

it('should handle masking errors', async () => {
const output = await tools.runAsync(
`${cmd} deidMask "${harmfulString}" -n -1`,
cwd
);
assert.strictEqual(
new RegExp(/Error in deidentifyWithMask/).test(output),
true
);
});

// deidentify_fpe
it('should FPE encrypt sensitive data in a string', async () => {
const output = await tools.runAsync(
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} ${keyName} -a NUMERIC`,
cwd
);
assert.strictEqual(new RegExp(/My SSN is \d{9}/).test(output), true);
assert.notStrictEqual(output, harmfulString);
});

it('should use surrogate info types in FPE encryption', async () => {
const output = await tools.runAsync(
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} ${keyName} -a NUMERIC -s ${surrogateType}`,
cwd
);
assert.strictEqual(
new RegExp(/My SSN is SSN_TOKEN\(9\):\d{9}/).test(output),
true
);
labeledFPEString = output;
});

it('should ignore insensitive data when FPE encrypting a string', async () => {
const output = await tools.runAsync(
`${cmd} deidFpe "${harmlessString}" ${wrappedKey} ${keyName}`,
cwd
);
assert.strictEqual(output, harmlessString);
});

it('should handle FPE encryption errors', async () => {
const output = await tools.runAsync(
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} BAD_KEY_NAME`,
cwd
);
assert.strictEqual(
new RegExp(/Error in deidentifyWithFpe/).test(output),
true
);
});

// reidentify_fpe
it('should FPE decrypt surrogate-typed sensitive data in a string', async () => {
assert.ok(labeledFPEString, 'Verify that FPE encryption succeeded.');
const output = await tools.runAsync(
`${cmd} reidFpe "${labeledFPEString}" ${surrogateType} ${wrappedKey} ${keyName} -a NUMERIC`,
cwd
);
assert.strictEqual(output, harmfulString);
});

it('should handle FPE decryption errors', async () => {
const output = await tools.runAsync(
`${cmd} reidFpe "${harmfulString}" ${surrogateType} ${wrappedKey} BAD_KEY_NAME -a NUMERIC`,
cwd
);
assert.strictEqual(
new RegExp(/Error in reidentifyWithFpe/).test(output),
true
);
});

// deidentify_date_shift
it('should date-shift a CSV file', async () => {
const outputCsvFile = 'dates.actual.csv';
const output = await tools.runAsync(
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields}`,
cwd
);
assert.strictEqual(
output.includes(`Successfully saved date-shift output to ${outputCsvFile}`),
true
);
assert.notStrictEqual(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(csvFile).toString()
);
});

it('should date-shift a CSV file using a context field', async () => {
const outputCsvFile = 'dates-context.actual.csv';
const expectedCsvFile =
'system-test/resources/date-shift-context.expected.csv';
const output = await tools.runAsync(
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName} -w ${wrappedKey}`,
cwd
);
assert.strictEqual(
output.includes(`Successfully saved date-shift output to ${outputCsvFile}`),
true
);
assert.strictEqual(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(expectedCsvFile).toString()
);
});

it('should require all-or-none of {contextField, wrappedKey, keyName}', async () => {
const output = await tools.runAsync(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName}`,
cwd
);

assert.strictEqual(
output.includes('You must set either ALL or NONE of'),
true
);
});

it('should handle date-shift errors', async () => {
const output = await tools.runAsync(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount}`,
cwd
);
assert.strictEqual(
new RegExp(/Error in deidentifyWithDateShift/).test(output),
true
);
describe('deid', () => {
// deidentify_masking
it('should mask sensitive data in a string', async () => {
const output = await exec(`${cmd} deidMask "${harmfulString}" -m x -n 5`);
assert.strictEqual(output, 'My SSN is xxxxx9127');
});

it('should ignore insensitive data when masking a string', async () => {
const output = await exec(`${cmd} deidMask "${harmlessString}"`);
assert.strictEqual(output, harmlessString);
});

it('should handle masking errors', async () => {
const output = await exec(`${cmd} deidMask "${harmfulString}" -n -1`);
assert.match(output, /Error in deidentifyWithMask/);
});

// deidentify_fpe
it('should FPE encrypt sensitive data in a string', async () => {
const output = await exec(
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} ${keyName} -a NUMERIC`
);
assert.match(output, /My SSN is \d{9}/);
assert.notStrictEqual(output, harmfulString);
});

it('should use surrogate info types in FPE encryption', async () => {
const output = await exec(
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} ${keyName} -a NUMERIC -s ${surrogateType}`
);
assert.match(output, /My SSN is SSN_TOKEN\(9\):\d{9}/);
labeledFPEString = output;
});

it('should ignore insensitive data when FPE encrypting a string', async () => {
const output = await exec(
`${cmd} deidFpe "${harmlessString}" ${wrappedKey} ${keyName}`
);
assert.strictEqual(output, harmlessString);
});

it('should handle FPE encryption errors', async () => {
const output = await exec(
`${cmd} deidFpe "${harmfulString}" ${wrappedKey} BAD_KEY_NAME`
);
assert.match(output, /Error in deidentifyWithFpe/);
});

// reidentify_fpe
it('should FPE decrypt surrogate-typed sensitive data in a string', async () => {
assert.ok(labeledFPEString, 'Verify that FPE encryption succeeded.');
const output = await exec(
`${cmd} reidFpe "${labeledFPEString}" ${surrogateType} ${wrappedKey} ${keyName} -a NUMERIC`
);
assert.strictEqual(output, harmfulString);
});

it('should handle FPE decryption errors', async () => {
const output = await exec(
`${cmd} reidFpe "${harmfulString}" ${surrogateType} ${wrappedKey} BAD_KEY_NAME -a NUMERIC`
);
assert.match(output, /Error in reidentifyWithFpe/);
});

// deidentify_date_shift
it('should date-shift a CSV file', async () => {
const outputCsvFile = 'dates.actual.csv';
const output = await exec(
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields}`
);
assert.match(
output,
new RegExp(`Successfully saved date-shift output to ${outputCsvFile}`)
);
assert.notStrictEqual(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(csvFile).toString()
);
});

it('should date-shift a CSV file using a context field', async () => {
const outputCsvFile = 'dates-context.actual.csv';
const expectedCsvFile =
'system-test/resources/date-shift-context.expected.csv';
const output = await exec(
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName} -w ${wrappedKey}`
);
assert.match(
output,
new RegExp(`Successfully saved date-shift output to ${outputCsvFile}`)
);
assert.strictEqual(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(expectedCsvFile).toString()
);
});

it('should require all-or-none of {contextField, wrappedKey, keyName}', async () => {
const output = await exec(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName}`
);
assert.match(output, /You must set either ALL or NONE of/);
});

it('should handle date-shift errors', async () => {
const output = await exec(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount}`
);
assert.match(output, /Error in deidentifyWithDateShift/);
});
});
Loading

0 comments on commit 7b9c68f

Please sign in to comment.