-
Notifications
You must be signed in to change notification settings - Fork 66
zapier convert: Generate different auth code based on scripting #185
Conversation
const AuthTest = { operation: { perform: 'FAKE_PERFORM_FUNCTION' } }; | ||
const getSessionKey = 'FAKE_GET_SESSION_KEY_FUNCTION'; | ||
(${string}); | ||
`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since now I'm using loadAuthModuleFromString
to eval the generated auth code, we don't need AuthTest
and getSessionKey
in s2js
.
src/tests/utils/convert.js
Outdated
const convert = require('../../utils/convert'); | ||
const definitions = { | ||
basic: require('./definitions/basic.json'), | ||
basicScripting: require('./definitions/basic-scripting.json'), |
There was a problem hiding this comment.
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'), | ||
apiQuery: require('./definitions/api-query.json'), | ||
session: require('./definitions/session.json'), | ||
oauth2: require('./definitions/oauth2.json'), | ||
oauth2Scripting: require('./definitions/oauth2-scripting.json'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oauth2-scripting.json
is same as oauth2.json
except that it defines pre_oauthv2_token
and post_oauthv2_token
method in scripting.
oauth2Refresh: require('./definitions/oauth2-refresh.json'), | ||
oauth2RefreshScripting: require('./definitions/oauth2-refresh-scripting.json'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oauth2-refresh-scripting.json
is same as oauth2-refresh.json
except that it defines pre_oauthv2_token
, post_oauthv2_token
and pre_oauthv2_refresh
method in scripting.
const wbDef = definitions.basic; | ||
|
||
convert.renderAuth(wbDef) | ||
return convert.renderAuth(wbDef) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the code cleaner, instead of using done()
callback, I changed the style of all the auth-related tests by simply returning a Promise.
} | ||
]); | ||
auth.connectionLabel.should.eql('{{username}}'); | ||
auth.test().should.eql('./triggers/test_auth'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test trigger is mocked so that it returns the module path. See loadAuthModuleFromString()
.
src/utils/convert.js
Outdated
@@ -495,15 +559,13 @@ const renderIndex = (legacyApp) => { | |||
AFTER_RESPONSES: getAfterResponses(legacyApp), | |||
}; | |||
|
|||
return renderAuth(legacyApp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authentication has been moved to its own separate file authentication.js
, so we no longer need to call renderAuth()
when rendering index.js
.
@@ -67,6 +68,7 @@ | |||
"mocha": "3.4.2", | |||
"ngrok": "2.2.10", | |||
"node-watch": "0.5.4", | |||
"require-from-string": "2.0.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package is used to load the rendered auth code, i.e., authentication.js
. Since authentication.js
is a CommonJS module (with module.exports = ... ;
at the end), we can't just use s2js()
or eval()
to evaluate the code. require-from-string
allows us to do require()
but from a string instead of a file.
|
||
if (cliType === 'trigger' && _.get(legacyApp, ['general', 'test_trigger_key']) === name) { | ||
importLines.push(`const AuthTest = ${varName};`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test trigger is now imported in authentication.js
, so we can remove it from index.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is amazing @eliangcs ! Awesome tests, refactoring for re-usability, and things are looking neat!
I've got a few suggestions and requests, but it's nothing really major.
<%= FIELDS %> | ||
], | ||
<% if (hasGetConnectionLabelScripting) { %> | ||
connectionLabel: getConnectionLabel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, I'd expect this to not have a trailing comma, since the else
doesn't as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 73e2c37.
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>'); | ||
<% if (hasGetConnectionLabelScripting) { %> | ||
const getConnectionLabel = (z, bundle) => { | ||
const scripting = require('../scripting'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this isn't running from a folder, so scripting.js
should be at the same level, not one level above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch! Fixed in ecb1a36.
scaffold/convert/oauth2.template.js
Outdated
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>'); | ||
<% if (hasPreOAuthTokenScripting && hasPostOAuthTokenScripting) { %> | ||
const getAccessToken = (z, bundle) => { | ||
const scripting = require('../scripting'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above with the relative location of scripting.js
.
src/utils/convert.js
Outdated
@@ -86,7 +87,7 @@ const getAuthType = (definition) => { | |||
return authTypeMap[definition.general.auth_type]; | |||
}; | |||
|
|||
const renderField = (definition, key) => { | |||
const renderField = (definition, key, indent = 0) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
src/utils/convert.js
Outdated
}; | ||
|
||
const renderFields = (fields, indent = 0) => { | ||
const result = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to make this plural results
just to more clearly indicate it's a list of things, not a single thing/object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 990f1c9.
scaffold/convert/oauth2.template.js
Outdated
|
||
if (hasGetConnectionLabelScripting) { %> | ||
const getConnectionLabel = (z, bundle) => { | ||
const scripting = require('../scripting'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above with the relative location of scripting.js
.
scaffold/convert/session.template.js
Outdated
const testTrigger = require('<%= TEST_TRIGGER_MODULE %>'); | ||
|
||
const getSessionKey = (z, bundle) => { | ||
const scripting = require('../scripting'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above with the relative location of scripting.js
.
scaffold/convert/session.template.js
Outdated
}; | ||
<% if (hasGetConnectionLabelScripting) { %> | ||
const getConnectionLabel = (z, bundle) => { | ||
const scripting = require('../scripting'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above with the relative location of scripting.js
.
scaffold/convert/oauth2.template.js
Outdated
}, | ||
<% if (hasGetConnectionLabelScripting) { %> | ||
connectionLabel: getConnectionLabel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above with the trailing comma removal for consistency.
scaffold/convert/session.template.js
Outdated
], | ||
sessionConfig: { | ||
perform: getSessionKey | ||
}, | ||
<% if (hasGetConnectionLabelScripting) { %> | ||
connectionLabel: getConnectionLabel, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above with the trailing comma removal for consistency.
"auth_data": {}, | ||
"auth_label": "", | ||
"auth_mapping": {}, | ||
"auth_type": "Unknown Auth", |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
src/utils/convert.js
Outdated
@@ -87,6 +87,10 @@ const getAuthType = (definition) => { | |||
return authTypeMap[definition.general.auth_type]; | |||
}; | |||
|
|||
const hasAuth = (definition) => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just found one place missing the replacement from ../scripting
to ./scripting
and then I'll pull this down and test it around.
scaffold/convert/oauth2.template.js
Outdated
}; | ||
<% } else if (hasPreOAuthTokenScripting && !hasPostOAuthTokenScripting) { %> | ||
const getAccessToken = (z, bundle) => { | ||
const scripting = require('../scripting'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this one still needs to be changed?
@BrunoBernardino Good catch! Fixed in fecf3c0. |
Alright! I added a new app to the convert test (let me know if you want any help or pointers on doing that for new convert work that gets added), and all looks great: Individually converting the app and running
And running
|
Summary:
index.js
to its own separate fileauthentication.js
getAccessToken()
ifpre_oauthv2_token
orpost_oauthv2_token
method existsrefreshAccessToken()
ifpre_oauthv2_refresh
method existsgetSessionKey()
that invokesget_session_info
methodgetConnectionLabel()
ifget_connection_label
method existsSee more detail in the following comments.