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

zapier convert: Generate different auth code based on scripting #185

Merged
merged 11 commits into from
Nov 28, 2017
Merged
6 changes: 4 additions & 2 deletions scaffold/convert/index.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
const App = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,

<% if (hasAuth) { %>
authentication,

<% } else { %>
authentication: {},
<% } %>
beforeRequest: [
<%= BEFORE_REQUESTS %>
],
Expand Down
10 changes: 5 additions & 5 deletions scaffold/convert/oauth2.template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');
<% if (hasPreOAuthTokenScripting && hasPostOAuthTokenScripting) { %>
const getAccessToken = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= ACCESS_TOKEN_URL %>';
Expand Down Expand Up @@ -47,7 +47,7 @@ const getAccessToken = (z, bundle) => {
};
<% } else if (!hasPreOAuthTokenScripting && hasPostOAuthTokenScripting) { %>
const getAccessToken = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= ACCESS_TOKEN_URL %>';
Expand Down Expand Up @@ -78,7 +78,7 @@ const getAccessToken = (z, bundle) => {

if (withRefresh && hasPreOAuthRefreshScripting) { %>
const refreshAccessToken = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

bundle._legacyUrl = '<%= REFRESH_TOKEN_URL %>';
Expand All @@ -101,7 +101,7 @@ const refreshAccessToken = (z, bundle) => {

if (hasGetConnectionLabelScripting) { %>
const getConnectionLabel = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_connection_label() from scripting.
Expand Down Expand Up @@ -168,7 +168,7 @@ const authentication = {
<% } %>
},
<% if (hasGetConnectionLabelScripting) { %>
connectionLabel: getConnectionLabel,
connectionLabel: getConnectionLabel
<% } else { %>
connectionLabel: '<%= CONNECTION_LABEL %>'
<% } %>
Expand Down
6 changes: 3 additions & 3 deletions scaffold/convert/session.template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');

const getSessionKey = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_session_info() from scripting.
Expand All @@ -27,7 +27,7 @@ const getSessionKey = (z, bundle) => {
};
<% if (hasGetConnectionLabelScripting) { %>
const getConnectionLabel = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_connection_label() from scripting.
Expand All @@ -48,7 +48,7 @@ const authentication = {
perform: getSessionKey
},
<% if (hasGetConnectionLabelScripting) { %>
connectionLabel: getConnectionLabel,
connectionLabel: getConnectionLabel
<% } else { %>
connectionLabel: '<%= CONNECTION_LABEL %>'
<% } %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>');
<% if (hasGetConnectionLabelScripting) { %>
const getConnectionLabel = (z, bundle) => {
const scripting = require('../scripting');
const scripting = require('./scripting');
const legacyScriptingRunner = require('zapier-platform-legacy-scripting-runner')(scripting);

// Do a get_connection_label() from scripting.
Expand All @@ -19,7 +19,7 @@ const authentication = {
<%= FIELDS %>
],
<% if (hasGetConnectionLabelScripting) { %>
connectionLabel: getConnectionLabel,
connectionLabel: getConnectionLabel
<% } else { %>
connectionLabel: '<%= CONNECTION_LABEL %>'
<% } %>
Expand Down
10 changes: 10 additions & 0 deletions src/tests/utils/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('should');
const requireFromString = require('require-from-string');
const convert = require('../../utils/convert');
const definitions = {
noAuth: require('./definitions/no-auth.json'),
basic: require('./definitions/basic.json'),
basicScripting: require('./definitions/basic-scripting.json'),
Copy link
Member Author

@eliangcs eliangcs Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basic-scripting.json is same as basic.json except that it has a get_connection_info method in scripting.

apiHeader: require('./definitions/api-header.json'),
Expand Down Expand Up @@ -105,6 +106,15 @@ describe('convert render functions', () => {
});

describe('authentication', () => {
it('should not render no auth', () => {
const wbDef = definitions.noAuth;
return convert.renderIndex(wbDef)
.then(string => {
string.should.containEql('authentication: {}');
convert.hasAuth(wbDef).should.be.false();
});
});

it('should render basic auth', () => {
const wbDef = definitions.basic;

Expand Down
85 changes: 85 additions & 0 deletions src/tests/utils/definitions/no-auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"actions": {
"create_recipe": {
"_dump_version": 1,
"custom_fields_result_url": "https://wt-c396b46e7e285c63f4bd6d4f8d32dc2e-0.run.webtask.io/fields-output",
"custom_fields_url": "https://wt-c396b46e7e285c63f4bd6d4f8d32dc2e-0.run.webtask.io/fields-input",
"fields": {
"name": {
"choices": null,
"default": null,
"help_text": null,
"label": "Name",
"list": false,
"parent_key": null,
"placeholder": null,
"prefill": null,
"required": true,
"searchfill": null,
"send_in_json": true,
"sort": null,
"type_of": "Unicode"
}
},
"help_text": "Creates a new Recipe.",
"hide": false,
"important": true,
"label": "Create Recipe",
"noun": "Recipe",
"sample_result_fields": [
{
"key": "authorId",
"type": "int"
},
{
"key": "id",
"type": "int"
},
{
"important": true,
"key": "name",
"type": "unicode"
}
],
"url": "https://wt-c396b46e7e285c63f4bd6d4f8d32dc2e-0.run.webtask.io/recipe-create"
}
},
"auth_fields": {},
"general": {
"auth_data": {},
"auth_label": "",
"auth_mapping": {},
"auth_type": "Unknown Auth",
Copy link
Contributor

@BrunoBernardino BrunoBernardino Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to render a custom auth for Unknown Auth. No Auth would be No Auth Needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made an app https://zapier.com/developer/builder/app/79939/development and specified "No Auth" for the authentication. The dumped result (https://zapier.com/api/developer/v1/apps/79939/dump) is "auth_type": "Unknown Auth". Am I missing something here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in that case let it be, then!

"auth_urls": {},
"cloned_from_app_id": 731,
"description": "Test WB app for `zapier convert`.",
"homepage_url": null,
"image_url": null,
"platform_version": "2015-07-31T00:00:00",
"subscribe_url": null,
"test_trigger_key": "test_auth",
"their_user_count": null,
"title": "Test API Header Cloned 2017-10-26",
"unsubscribe_url": null
},
"js": null,
"searches": {},
"triggers": {
"test_auth": {
"_dump_version": 1,
"custom_fields_url": null,
"data_source": "Polling",
"fields": {},
"help_text": "Test auth trigger.",
"hide": true,
"hook_event": null,
"hook_static_directions": "Log into your service and paste the below URL into the webhook setup field.",
"important": false,
"label": "Test Auth",
"noun": "",
"paging": false,
"sample_result_fields": [],
"url": "http://httpbin.org/basic-auth/user/passwd"
}
}
}
32 changes: 22 additions & 10 deletions src/utils/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const getAuthType = (definition) => {
return authTypeMap[definition.general.auth_type];
};

const hasAuth = (definition) => {
Copy link
Contributor

@BrunoBernardino BrunoBernardino Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be moved to the getAuthMetaData as a returned property instead of its own function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are separate functions because in some situations we only need to quickly check if the app needs authentication or not. I don't want to do all the unnecessary things in getAuthMetaData for those situations. Makes sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright!

return getAuthType(definition) !== 'custom' || !_.isEmpty(definition.auth_fields);
};

const renderField = (definition, key, indent = 0) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

const type = definition.type && typesMap[definition.type.toLowerCase()] || 'string';

Expand Down Expand Up @@ -125,11 +129,11 @@ ${padding}}`;
};

const renderFields = (fields, indent = 0) => {
const result = [];
const results = [];
_.each(fields, (field, key) => {
result.push(renderField(field, key, indent));
results.push(renderField(field, key, indent));
});
return result.join(',\n');
return results.join(',\n');
};

const renderSampleField = (def) => {
Expand Down Expand Up @@ -195,7 +199,7 @@ const renderAuthTemplate = (authType, definition) => {
hasGetConnectionLabelScripting,
};

const templateFile = path.join(TEMPLATE_DIR, '/basic-auth.template.js');
const templateFile = path.join(TEMPLATE_DIR, '/simple-auth.template.js');
return renderTemplate(templateFile, templateContext);
};

Expand Down Expand Up @@ -280,7 +284,7 @@ const renderAuth = (definition) => {
}
};

// write authentiction.js
// write authentication.js
const writeAuth = (definition, newAppDir) => {
const fileName = 'authentication.js';
return renderAuth(definition)
Expand Down Expand Up @@ -549,23 +553,26 @@ const writeStepTest = (type, definition, key, legacyApp, newAppDir) => {
};

const renderIndex = (legacyApp) => {
const _hasAuth = hasAuth(legacyApp);
const templateContext = {
AUTHENTICATION: '',
HEADER: '',
TRIGGERS: '',
SEARCHES: '',
CREATES: '',
BEFORE_REQUESTS: getBeforeRequests(legacyApp),
AFTER_RESPONSES: getAfterResponses(legacyApp),
hasAuth: _hasAuth
};

return getHeader(legacyApp)
.then((header) => {
templateContext.HEADER = header;

const importLines = [
"const authentication = require('./authentication');"
];
const importLines = [];

if (_hasAuth) {
importLines.push("const authentication = require('./authentication');");
}

const dirMap = {
trigger: 'triggers',
Expand Down Expand Up @@ -673,7 +680,10 @@ const convertApp = (legacyApp, newAppDir) => {
promises.push(writeIndex(legacyApp, newAppDir));
promises.push(writePackageJson(legacyApp, newAppDir));
promises.push(writeScripting(legacyApp, newAppDir));
promises.push(writeAuth(legacyApp, newAppDir));

if (hasAuth(legacyApp)) {
promises.push(writeAuth(legacyApp, newAppDir));
}

return Promise.all(promises);
};
Expand All @@ -682,6 +692,7 @@ module.exports = {
convertApp,
renderAuth,
renderField,
renderIndex,
renderSample,
renderStep,
renderTemplate,
Expand All @@ -690,4 +701,5 @@ module.exports = {
getHeader,
getBeforeRequests,
getAfterResponses,
hasAuth,
};