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

Commit

Permalink
Adding more auth conversion templates
Browse files Browse the repository at this point in the history
This adds auth conversion templates for api keys (header + query string), session, and unknown auths.

It also improves Basic and OAuth conversion templates.

It also improves and adds tests for conversion.
  • Loading branch information
Bruno Bernardino committed Oct 26, 2017
1 parent e7ba4e8 commit fe1f44a
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 156 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"version": "npm run docs && npm run gen-completions && git add docs/* goodies/* README.md",
"postversion": "git push && git push --tags && npm publish",
"prepublish": "npm run build",
"build": "rm -rf lib && node_modules/babel-cli/bin/babel.js src -d lib",
"watch": "rm -rf lib && node_modules/babel-cli/bin/babel.js --watch src -d lib",
"lint": "node_modules/.bin/eslint zapier.js src test",
"build": "rm -rf lib && node_modules/babel-cli/bin/babel.js src -d lib --copy-files",
"watch": "rm -rf lib && node_modules/babel-cli/bin/babel.js --watch src -d lib --copy-files",
"lint": "node_modules/.bin/eslint zapier.js src",
"lint-snippets": "node_modules/.bin/eslint snippets --rule 'no-unused-vars: 0'",
"test": "npm run build && node_modules/mocha/bin/mocha -t 15000 --recursive lib/tests && npm run lint && npm run lint-snippets",
"zapier": "zapier.js",
Expand Down
27 changes: 27 additions & 0 deletions scaffold/convert/header.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<% if (before) { %>const maybeIncludeAuth = (request, z, bundle) => {
<%
Object.keys(mapping).forEach((mapperKey) => {
fields.forEach((field) => {
if (mapping[mapperKey].indexOf(`{{${field}}}`) !== -1) {
if (query) { %>
request.params['<%= mapperKey %>'] = bundle.authData['<%= field %>'];
<% } else { %>
request.headers['<%= mapperKey %>'] = bundle.authData['<%= field %>'];
<% }
}
});
});
%>
return request;
}
<% }

if (after) { %>
const maybeRefresh = (response, z, bundle) => {
if (response.status === 401 || response.status === 403) {
throw new z.errors.RefreshAuthError('Session key needs refreshing.');
}

return response;
}
<% } %>
10 changes: 10 additions & 0 deletions scaffold/convert/index.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

<%= REQUIRES %>

<%= HEADER %>

const App = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,

authentication: <%= AUTHENTICATION %>,

beforeRequest: [
<%= BEFORE_REQUESTS %>
],

afterResponse: [
<%= AFTER_RESPONSES %>
],

resources: {
},

Expand Down
7 changes: 3 additions & 4 deletions scaffold/convert/oauth2.template.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
// TODO: just an example stub - you'll need to complete
type: 'oauth2',
test: {
url: 'http://www.EXAMPLE.com/auth'
},
test: AuthTest.operation.perform,
oauth2Config: {
authorizeUrl: {
method: 'GET',
Expand All @@ -28,6 +26,7 @@
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
},
autoRefresh: <%= AUTO_REFRESH %>
}
}
2 changes: 1 addition & 1 deletion src/tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('utils', () => {
global._babelPolyfill.should.eql(true);
});

it.skip('should print a nice little table', () => {
it('should print a nice little table', () => {
const table = utils.makeTable(
[{id: 123, title: 'hello'}, {id: 456, title: 'world'}],
[
Expand Down
192 changes: 92 additions & 100 deletions src/tests/utils/convert.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
require('should');
const convert = require('../../utils/convert');
const definitions = {
basic: require('./definitions/basic.json'),
apiHeader: require('./definitions/api-header.json'),
// apiQuery: require('./definitions/api-query.json'),
// session: require('./definitions/session.json'),
// oauth2: require('./definitions/oauth2.json'),
};

/* eslint no-eval: 0 */
const s2js = (string) => eval(`(${string})`);
const s2js = (string) => eval(`const AuthTest = { operation: { perform: 'PerformFunction' } }; (${string})`);

describe('convert render functions', () => {

describe('render field', () => {
it('should render a string field', () => {
const v2Key = 'test_field';
const v2Def = {
const wbKey = 'test_field';
const wbDef = {
label: 'test field',
type: 'Unicode',
required: true,
help_text: 'help text goes here'
};

const string = convert.renderField(v2Def, v2Key);
const string = convert.renderField(wbDef, wbKey);
const field = s2js(string);
field.should.eql({
key: 'test_field',
Expand All @@ -28,121 +35,102 @@ describe('convert render functions', () => {
});

it('should pad help text that is too short', () => {
const v2Key = 'test_field';
const v2Def = {
const wbKey = 'test_field';
const wbDef = {
help_text: 'too short'
};

const string = convert.renderField(v2Def, v2Key);
const string = convert.renderField(wbDef, wbKey);
const field = s2js(string);
field.helpText.should.eql('too short (help text must be at least 10 characters)');
});
});

describe('authentication', () => {
it('should render basic auth', (done) => {
const v2Def = {
general: {
auth_type: 'Basic Auth'
},
auth_fields: {
username: {
label: 'Username',
required: true,
type: 'Unicode'
},
password: {
label: 'Password',
placeholder: '***',
required: true,
type: 'Password'
}
}
};
const wbDef = definitions.basic;

convert.renderAuth(v2Def).then(string => {
const auth = s2js(string);
auth.should.eql({
type: 'basic',
test: {
url: 'http://www.example.com/auth'
},
fields: [
{
key: 'username',
type: 'string',
required: true,
label: 'Username',
helpText: '(help text must be at least 10 characters)'
},
{
key: 'password',
type: 'password',
required: true,
label: 'Password',
placeholder: '***',
helpText: '(help text must be at least 10 characters)'
}
]
});
});
done();
convert.renderAuth(wbDef)
.then(string => {
const auth = s2js(string);
auth.should.eql({
type: 'basic',
test: 'PerformFunction',
fields: [
{
key: 'username',
type: 'string',
required: true,
label: 'Username',
helpText: '(help text must be at least 10 characters)'
},
{
key: 'password',
type: 'password',
required: true,
label: 'Password',
helpText: '(help text must be at least 10 characters)'
}
]
});
done();
})
.catch(done);
});
});

it.skip('should render oauth2', (done) => {
const v2Def = {
general: {
auth_type: 'OAuth V2',
auth_urls: {
access_token_url: 'https://example.com/api/v2/oauth2/token',
authorization_url: 'https://example.com/api/oauth2/authorize'
}
}
};

convert.renderAuth(v2Def).then(string => {
const auth = s2js(string);

auth.should.eql({
type: 'oauth2',
test: {
url: 'http://www.example.com/auth'
},
oauth2Config: {
authorizeUrl: {
method: 'GET',
url: 'https://example.com/api/oauth2/authorize',
params: {
client_id: '{{process.env.CLIENT_ID}}',
state: '{{bundle.inputData.state}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
response_type: 'code'
}
},
getAccessToken: {
method: 'POST',
url: 'https://example.com/api/v2/oauth2/token',
body: {
code: '{{bundle.inputData.code}}',
client_id: '{{process.env.CLIENT_ID}}',
client_secret: '{{process.env.CLIENT_SECRET}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
grant_type: 'authorization_code'
// TODO: api keys header
// TODO: api keys query
// TODO: session

it.skip('TODO: should render oauth2', (done) => {
const wbDef = definitions.oauth2;

convert.renderAuth(wbDef)
.then(string => {
const auth = s2js(string);

auth.should.eql({
type: 'oauth2',
test: {
url: 'http://www.example.com/auth'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
oauth2Config: {
authorizeUrl: {
method: 'GET',
url: 'https://example.com/api/oauth2/authorize',
params: {
client_id: '{{process.env.CLIENT_ID}}',
state: '{{bundle.inputData.state}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
response_type: 'code'
}
},
getAccessToken: {
method: 'POST',
url: 'https://example.com/api/v2/oauth2/token',
body: {
code: '{{bundle.inputData.code}}',
client_id: '{{process.env.CLIENT_ID}}',
client_secret: '{{process.env.CLIENT_SECRET}}',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
grant_type: 'authorization_code'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
}
}
}
});
done();
});
done();
})
.catch(done);
});

});

describe('render sample', () => {
it('should render sample output fields', () => {
const v2Def = {
const wbDef = {
sample_result_fields: [
{ type: 'float', key: 'bounds__northeast__lat' },
{ type: 'float', key: 'bounds__northeast__lng' },
Expand All @@ -153,7 +141,7 @@ describe('convert render functions', () => {
]
};

const string = '{' + convert.renderSample(v2Def) + '}';
const string = '{' + convert.renderSample(wbDef) + '}';
const fields = s2js(string);
fields.should.eql({
outputFields: [
Expand All @@ -167,4 +155,8 @@ describe('convert render functions', () => {
});
});
});

// TODO: getHeader

// TODO: renderIndex
});
Loading

0 comments on commit fe1f44a

Please sign in to comment.