Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

refactor(samples): convert sample tests from ava to mocha #186

Merged
merged 9 commits into from
Nov 23, 2018
2 changes: 1 addition & 1 deletion samples/deid.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ const cli = require(`yargs`)
opts.contextFieldId,
opts.wrappedKey,
opts.keyName
)
).catch(console.log)
)
.option('c', {
type: 'string',
Expand Down
4 changes: 2 additions & 2 deletions samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"node": ">=8"
},
"scripts": {
"test": "ava -T 5m --verbose system-test/*.test.js"
"test": "mocha system-test/*.test.js --timeout=600000"
},
"dependencies": {
"@google-cloud/dlp": "^0.9.0",
Expand All @@ -20,7 +20,7 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"ava": "^0.25.0",
"mocha": "^5.2.0",
"pixelmatch": "^4.0.2",
"pngjs": "^3.3.3",
"uuid": "^3.3.2"
Expand Down
2 changes: 2 additions & 0 deletions samples/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---
env:
mocha: true
rules:
node/no-unpublished-require: off
125 changes: 70 additions & 55 deletions samples/system-test/deid.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017, Google, Inc.
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -16,12 +16,12 @@
'use strict';

const path = require('path');
const test = require('ava');
const assert = require('assert');
const fs = require('fs');
const tools = require('@google-cloud/nodejs-repo-tools');

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

const harmfulString = 'My SSN is 372819127';
const harmlessString = 'My favorite color is blue';
Expand All @@ -32,143 +32,158 @@ 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 csvFile = 'resources/dates.csv';
const tempOutputFile = path.join(__dirname, 'temp.result.csv');
const csvContextField = 'name';
const dateShiftAmount = 30;
const dateFields = `birth_date register_date`;
const dateFields = 'birth_date register_date';

test.before(tools.checkCredentials);
before(tools.checkCredentials);

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

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

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

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

test.serial(`should use surrogate info types in FPE encryption`, async t => {
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
);
t.regex(output, /My SSN is SSN_TOKEN\(9\):\d{9}/);
assert.strictEqual(
new RegExp(/My SSN is SSN_TOKEN\(9\):\d{9}/).test(output),
true
);
labeledFPEString = output;
});

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

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

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

test(`should handle FPE decryption errors`, async t => {
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
);
t.regex(output, /Error in reidentifyWithFpe/);
assert.strictEqual(
new RegExp(/Error in reidentifyWithFpe/).test(output),
true
);
});

// deidentify_date_shift
test(`should date-shift a CSV file`, async t => {
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
);
t.true(
output.includes(`Successfully saved date-shift output to ${outputCsvFile}`)
assert.strictEqual(
output.includes(`Successfully saved date-shift output to ${outputCsvFile}`),
true
);
t.not(
assert.notStrictEqual(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(csvFile).toString()
);
});

test(`should date-shift a CSV file using a context field`, async t => {
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
);
t.true(
output.includes(`Successfully saved date-shift output to ${outputCsvFile}`)
assert.strictEqual(
output.includes(`Successfully saved date-shift output to ${outputCsvFile}`),
true
);
t.is(
assert.strictEqual(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(expectedCsvFile).toString()
);
});

test(`should require all-or-none of {contextField, wrappedKey, keyName}`, async t => {
await t.throws(
tools.runAsync(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields} -f ${csvContextField} -n ${keyName}`,
cwd
),
Error,
/You must set ALL or NONE of/
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
);
});

test(`should handle date-shift errors`, async t => {
it('should handle date-shift errors', async () => {
const output = await tools.runAsync(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount}`,
cwd
);
t.regex(output, /Error in deidentifyWithDateShift/);
assert.strictEqual(
new RegExp(/Error in deidentifyWithDateShift/).test(output),
true
);
});
Loading