Skip to content

Commit

Permalink
refactor: use execSync for tests (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Apr 12, 2019
1 parent 8f4928d commit 2d50910
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 253 deletions.
1 change: 0 additions & 1 deletion dlp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0",
"pixelmatch": "^4.0.2",
"pngjs": "^3.3.3",
Expand Down
1 change: 1 addition & 0 deletions dlp/risk.js
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ const cli = require(`yargs`) // eslint-disable-line
console.error(
'Number of infoTypes and number of quasi-identifiers must be equal!'
);
process.exitCode = 1;
} else {
return kMapEstimationAnalysis(
opts.callingProjectId,
Expand Down
87 changes: 41 additions & 46 deletions dlp/system-test/deid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@
const path = require('path');
const {assert} = require('chai');
const fs = require('fs');
const execa = require('execa');
const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const cmd = 'node deid.js';
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';
Expand All @@ -42,110 +37,110 @@ const dateFields = 'birth_date register_date';

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 mask sensitive data in a string', () => {
const output = execSync(`${cmd} deidMask "${harmfulString}" -m x -n 5`);
assert.include(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 ignore insensitive data when masking a string', () => {
const output = execSync(`${cmd} deidMask "${harmlessString}"`);
assert.include(output, harmlessString);
});

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

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

it('should use surrogate info types in FPE encryption', async () => {
const output = await exec(
it('should use surrogate info types in FPE encryption', () => {
const output = execSync(
`${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(
it('should ignore insensitive data when FPE encrypting a string', () => {
const output = execSync(
`${cmd} deidFpe "${harmlessString}" ${wrappedKey} ${keyName}`
);
assert.strictEqual(output, harmlessString);
assert.include(output, harmlessString);
});

it('should handle FPE encryption errors', async () => {
const output = await exec(
it('should handle FPE encryption errors', () => {
const output = execSync(
`${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 () => {
it('should FPE decrypt surrogate-typed sensitive data in a string', () => {
assert.ok(labeledFPEString, 'Verify that FPE encryption succeeded.');
const output = await exec(
const output = execSync(
`${cmd} reidFpe "${labeledFPEString}" ${surrogateType} ${wrappedKey} ${keyName} -a NUMERIC`
);
assert.strictEqual(output, harmfulString);
assert.include(output, harmfulString);
});

it('should handle FPE decryption errors', async () => {
const output = await exec(
it('should handle FPE decryption errors', () => {
const output = execSync(
`${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 () => {
it('should date-shift a CSV file', () => {
const outputCsvFile = 'dates.actual.csv';
const output = await exec(
const output = execSync(
`${cmd} deidDateShift "${csvFile}" "${outputCsvFile}" ${dateShiftAmount} ${dateShiftAmount} ${dateFields}`
);
assert.match(
assert.include(
output,
new RegExp(`Successfully saved date-shift output to ${outputCsvFile}`)
`Successfully saved date-shift output to ${outputCsvFile}`
);
assert.notStrictEqual(
assert.notInclude(
fs.readFileSync(outputCsvFile).toString(),
fs.readFileSync(csvFile).toString()
);
});

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

it('should require all-or-none of {contextField, wrappedKey, keyName}', async () => {
const output = await exec(
it('should require all-or-none of {contextField, wrappedKey, keyName}', () => {
const output = execSync(
`${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(
it('should handle date-shift errors', () => {
const output = execSync(
`${cmd} deidDateShift "${csvFile}" "${tempOutputFile}" ${dateShiftAmount} ${dateShiftAmount}`
);
assert.match(output, /Error in deidentifyWithDateShift/);
Expand Down
Loading

0 comments on commit 2d50910

Please sign in to comment.