Skip to content

Commit

Permalink
Merge pull request #438 from zapier/PDE-2854-empty-auth-mapping
Browse files Browse the repository at this point in the history
PDE-2584 fix(legacy-scripting-runner): default to auth fields if auth mapping is empty
  • Loading branch information
eliangcs authored Oct 27, 2021
2 parents 729538e + a65d627 commit 161eb87
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/legacy-scripting-runner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.8.2

- :bug: Merge `request.url` to `request.params` before making a request ([#435](https://github.com/zapier/zapier-platform/pull/435))
- :bug: Default to auth fields if auth mapping is empty ([#438](https://github.com/zapier/zapier-platform/pull/438))

## 3.8.1

- :bug: Add support for `z.reqeust({ json: true, body: {...} })` ([#418](https://github.com/zapier/zapier-platform/pull/418))
Expand Down
20 changes: 16 additions & 4 deletions packages/legacy-scripting-runner/middleware-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,31 @@ const getLowerHeaders = (headers) =>
return result;
}, {});

const renderAuthMapping = (authMapping, authData) => {
if (_.isEmpty(authMapping)) {
return authData;
}
return Object.entries(authMapping).reduce((result, [k, v]) => {
result[k] = renderTemplate(v, authData);
return result;
}, {});
};

const applyAuthMappingInHeaders = (authMapping, req, authData) => {
const rendered = renderAuthMapping(authMapping, authData);
const lowerHeaders = getLowerHeaders(req.headers);
Object.entries(authMapping).forEach(([k, v]) => {
Object.entries(rendered).forEach(([k, v]) => {
const lowerKey = k.toLowerCase();
if (!lowerHeaders[lowerKey]) {
req.headers[k] = renderTemplate(v, authData);
req.headers[k] = v;
}
});
};

const applyAuthMappingInQuerystring = (authMapping, req, authData) => {
Object.entries(authMapping).forEach(([k, v]) => {
req.params[k] = req.params[k] || renderTemplate(v, authData);
const rendered = renderAuthMapping(authMapping, authData);
Object.entries(rendered).forEach(([k, v]) => {
req.params[k] = req.params[k] || v;
});
};

Expand Down
2 changes: 1 addition & 1 deletion packages/legacy-scripting-runner/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zapier-platform-legacy-scripting-runner",
"version": "3.8.1",
"version": "3.8.2",
"description": "Zapier's Legacy Scripting Runner, used by Web Builder apps converted to CLI.",
"repository": "zapier/zapier-platform",
"homepage": "https://platform.zapier.com/",
Expand Down
28 changes: 28 additions & 0 deletions packages/legacy-scripting-runner/test/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,34 @@ describe('Integration Test', () => {
});
});

it('scriptingless, empty auth mapping', () => {
const appDef = _.cloneDeep(appDefinition);
appDef.legacy.scriptingSource = appDef.legacy.scriptingSource.replace(
'movie_post_poll_make_array',
'movie_post_poll'
);
const _appDefWithAuth = withAuth(appDef, sessionAuthConfig);
_appDefWithAuth.legacy.authentication.mapping = {};
_appDefWithAuth.legacy.authentication.placement = 'querystring';
_appDefWithAuth.legacy.triggers.movie.operation.url = `${HTTPBIN_URL}/get`;

const _compiledApp = schemaTools.prepareApp(_appDefWithAuth);
const _app = createApp(_appDefWithAuth);

const input = createTestInput(
_compiledApp,
'triggers.movie.operation.perform'
);
input.bundle.authData = {
api_key: 'hello',
};
return _app(input).then((output) => {
const echoed = output.results[0];
// Default to authData if auth mapping is empty
should.deepEqual(echoed.args, { api_key: ['hello'] });
});
});

it('KEY_poll', () => {
const input = createTestInput(
compiledApp,
Expand Down

0 comments on commit 161eb87

Please sign in to comment.