From 609b6014ebacad029743f270e2a5802cfd2f970f Mon Sep 17 00:00:00 2001 From: David Brownman Date: Thu, 1 Nov 2018 20:41:44 -0400 Subject: [PATCH 1/2] update cursor generation in tests --- package-lock.json | 4 ++-- .../before/add-digest-auth-header.js | 9 +------ src/tools/create-app-tester.js | 8 +++++-- src/tools/data.js | 24 ++++++++++++------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3f319ae..b5e6e7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -397,7 +397,7 @@ }, "babel-eslint": { "version": "8.2.3", - "resolved": "http://registry.npmjs.org/babel-eslint/-/babel-eslint-8.2.3.tgz", + "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-8.2.3.tgz", "integrity": "sha512-0HeSTtaXg/Em7FCUWxwOT+KeFSO1O7LuRuzhk7g+1BjwdlQGlHq4OyMi3GqGxrNfEq8jEi6Hmt5ylEQUhurgiQ==", "dev": true, "requires": { @@ -4163,7 +4163,7 @@ }, "should-equal": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/should-equal/-/should-equal-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/should-equal/-/should-equal-1.0.1.tgz", "integrity": "sha1-C26VFvJgGp+wuy3MNpr6HH4gCvc=", "dev": true, "requires": { diff --git a/src/http-middlewares/before/add-digest-auth-header.js b/src/http-middlewares/before/add-digest-auth-header.js index f778819..ee56e66 100644 --- a/src/http-middlewares/before/add-digest-auth-header.js +++ b/src/http-middlewares/before/add-digest-auth-header.js @@ -1,11 +1,10 @@ 'use strict'; -const crypto = require('crypto'); const urllib = require('url'); - const fetch = require('node-fetch'); const { NotImplementedError } = require('../../errors'); +const { md5 } = require('../../tools/data'); // This function splits a comma-separated string described by RFC 2068 Section 2. // Ported from https://github.com/python/cpython/blob/f081fd83/Lib/urllib/request.py#L1399-L1440 @@ -80,12 +79,6 @@ const parseDictHeader = s => { return res; }; -const md5 = s => - crypto - .createHash('md5') - .update(s) - .digest('hex'); - const buildDigestHeader = (username, password, url, method, creds) => { if (creds.algorithm && creds.algorithm.toUpperCase() !== 'MD5') { throw new NotImplementedError( diff --git a/src/tools/create-app-tester.js b/src/tools/create-app-tester.js index 1a3b441..b070d64 100644 --- a/src/tools/create-app-tester.js +++ b/src/tools/create-app-tester.js @@ -4,7 +4,7 @@ const createLambdaHandler = require('./create-lambda-handler'); const resolveMethodPath = require('./resolve-method-path'); const ZapierPromise = require('./promise'); const { get } = require('lodash'); -const { genId } = require('./data'); +const { md5 } = require('./data'); // this is (annoyingly) mirrored in cli/api_base, so that test functions only // have a storeKey when canPaginate is true. otherwise, a test would work but a @@ -45,11 +45,15 @@ const createAppTester = appRaw => { const method = resolveMethodPath(appRaw, methodOrFunc); + // we need a value that's unique to this test (but consistent across test runs) so we don't lose cursors + const username = process.env.USER || process.env.USERNAME || 'fallback'; + const unique = md5(`${method}-${username}`); + const event = { command: 'execute', method, bundle, - storeKey: shouldPaginate(appRaw, method) ? `testKey-${genId()}` : null + storeKey: shouldPaginate(appRaw, method) ? `testKey-${unique}` : null }; if (process.env.LOG_TO_STDOUT) { diff --git a/src/tools/data.js b/src/tools/data.js index ed9c9ca..8c3826c 100644 --- a/src/tools/data.js +++ b/src/tools/data.js @@ -3,6 +3,7 @@ const _ = require('lodash'); const memoize = require('./memoize'); const plainModule = require('./plain'); +const crypto = require('crypto'); const isPlainObj = o => { return ( @@ -173,16 +174,23 @@ const simpleTruncate = (string, length, suffix) => { const genId = () => parseInt(Math.random() * 100000000); +const md5 = s => + crypto + .createHash('md5') + .update(s) + .digest('hex'); + module.exports = { - isPlainObj, - findMapDeep, - memoizedFindMapDeep, deepCopy, - jsonCopy, deepFreeze, - recurseReplace, - recurseExtract, + findMapDeep, flattenPaths, - simpleTruncate, - genId + genId, + isPlainObj, + jsonCopy, + md5, + memoizedFindMapDeep, + recurseExtract, + recurseReplace, + simpleTruncate }; From 1002266a808300ce5a983aa47b7a90699e120210 Mon Sep 17 00:00:00 2001 From: David Brownman Date: Tue, 6 Nov 2018 11:02:51 -0500 Subject: [PATCH 2/2] use a simpler curosr remembering mechanism --- .../before/add-digest-auth-header.js | 2 +- src/tools/create-app-tester.js | 13 +++++++------ src/tools/data.js | 8 -------- src/tools/hashing.js | 7 +++++++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/http-middlewares/before/add-digest-auth-header.js b/src/http-middlewares/before/add-digest-auth-header.js index ee56e66..714e98f 100644 --- a/src/http-middlewares/before/add-digest-auth-header.js +++ b/src/http-middlewares/before/add-digest-auth-header.js @@ -4,7 +4,7 @@ const urllib = require('url'); const fetch = require('node-fetch'); const { NotImplementedError } = require('../../errors'); -const { md5 } = require('../../tools/data'); +const { md5 } = require('../../tools/hashing'); // This function splits a comma-separated string described by RFC 2068 Section 2. // Ported from https://github.com/python/cpython/blob/f081fd83/Lib/urllib/request.py#L1399-L1440 diff --git a/src/tools/create-app-tester.js b/src/tools/create-app-tester.js index b070d64..7d48f08 100644 --- a/src/tools/create-app-tester.js +++ b/src/tools/create-app-tester.js @@ -4,7 +4,7 @@ const createLambdaHandler = require('./create-lambda-handler'); const resolveMethodPath = require('./resolve-method-path'); const ZapierPromise = require('./promise'); const { get } = require('lodash'); -const { md5 } = require('./data'); +const { genId } = require('./data'); // this is (annoyingly) mirrored in cli/api_base, so that test functions only // have a storeKey when canPaginate is true. otherwise, a test would work but a @@ -40,20 +40,21 @@ const createAppTester = appRaw => { const handler = createLambdaHandler(appRaw); const createHandlerPromise = promisifyHandler(handler); + const randomSeed = genId(); + return (methodOrFunc, bundle) => { bundle = bundle || {}; const method = resolveMethodPath(appRaw, methodOrFunc); - // we need a value that's unique to this test (but consistent across test runs) so we don't lose cursors - const username = process.env.USER || process.env.USERNAME || 'fallback'; - const unique = md5(`${method}-${username}`); - const event = { command: 'execute', method, bundle, - storeKey: shouldPaginate(appRaw, method) ? `testKey-${unique}` : null + storeKey: shouldPaginate(appRaw, method) + ? // this key will be consistent across runs but unique to each test so we don't lose cursors + `testKey-${method}-${randomSeed}` + : null }; if (process.env.LOG_TO_STDOUT) { diff --git a/src/tools/data.js b/src/tools/data.js index 8c3826c..87013a0 100644 --- a/src/tools/data.js +++ b/src/tools/data.js @@ -3,7 +3,6 @@ const _ = require('lodash'); const memoize = require('./memoize'); const plainModule = require('./plain'); -const crypto = require('crypto'); const isPlainObj = o => { return ( @@ -174,12 +173,6 @@ const simpleTruncate = (string, length, suffix) => { const genId = () => parseInt(Math.random() * 100000000); -const md5 = s => - crypto - .createHash('md5') - .update(s) - .digest('hex'); - module.exports = { deepCopy, deepFreeze, @@ -188,7 +181,6 @@ module.exports = { genId, isPlainObj, jsonCopy, - md5, memoizedFindMapDeep, recurseExtract, recurseReplace, diff --git a/src/tools/hashing.js b/src/tools/hashing.js index 0249011..debd9ee 100644 --- a/src/tools/hashing.js +++ b/src/tools/hashing.js @@ -22,7 +22,14 @@ const snipify = s => { return `:censored:${length}:${result.substr(0, 10)}:`; }; +const md5 = s => + crypto + .createHash('md5') + .update(s) + .digest('hex'); + module.exports = { hashify, + md5, snipify };