Commit 494f9603 authored by Nathanaël's avatar Nathanaël 💩

fix oauth test (wip)

parent 77ad7490
Pipeline #74712 failed with stage
in 2 minutes and 6 seconds
{
"id": 6451,
"key": "App6451"
"id": 6521,
"key": "App6521"
}
\ No newline at end of file
......@@ -2,6 +2,8 @@
// ie.: if (res.status !== 200) { throw new Error('Unable to fetch access token: ' + res.content); }
const authorizeUrl = async (z, bundle) => {
z.console.log('bundle: ' + JSON.stringify(bundle));
z.console.log('server_name: ' + `${bundle.inputData.server_name}`);
// Fetch clientId and clientSecret
const { clientId, clientSecret } = await z.request({
method: 'POST',
......@@ -9,13 +11,15 @@ const authorizeUrl = async (z, bundle) => {
body: {
client_name: 'Zapier',
website: 'https://framagit.org/roipoussiere/MastoZap',
scopes: `${bundle.inputData.scope}`,
scopes: `${bundle.inputData.scope}`, // Defined in oauth2Config
redirect_uris: `${bundle.inputData.redirect_uri}`, // Zapier generates it automatically
},
}).then(res => res.json);
z.console.log('clientId: ' + clientId + ' - clientSecret: ' + clientSecret);
// Store client_id and client_secret
await z.cursor.set(`${clientId}|${clientSecret}`);
z.console.log('clientId: ' + clientId + ' - clientSecret: ' + clientSecret);
// Return the authorization URL
return {
......@@ -24,13 +28,14 @@ const authorizeUrl = async (z, bundle) => {
params: {
client_id: `${bundle.inputData.client_id}`,
scope: `${bundle.inputData.scope}`,
redirect_uri: `${bundle.inputData.redirect_uri}`, // Zapier generates it automatically
redirect_uri: `${bundle.inputData.redirect_uri}`,
response_type: 'code',
},
};
};
const getAccessToken = async (z, bundle) => {
z.console.log('server url: ' + `https://${bundle.inputData.server_name}/oauth/token`);
// Retrieve clientId and clientSecret
const [clientId, clientSecret] = await z.cursor.get().split('|');
......@@ -55,7 +60,8 @@ const getAccessToken = async (z, bundle) => {
};
};
const testAuth = (z, bundle) => {
const test = (z, bundle) => {
z.console.log('server_name: ' + `${bundle.inputData.server_name}`);
const promise = z.request({
method: 'GET',
url: `${bundle.inputData.server_name}/api/v1/accounts/verify_credentials`,
......@@ -93,7 +99,7 @@ module.exports = {
],
// Execute by Zapier after the OAuth flow is complete to ensure everything is setup properly.
test: testAuth,
test,
// key returned from the test
connectionLabel: '{{username}}',
};
......@@ -19,6 +19,7 @@ describe('oauth2 app', () => {
it('generates an authorize URL', () => {
const bundle = {
inputData: {
server_name: 'framapiaf.org',
// In production, this will be generated by Zapier and set automatically
redirect_uri: 'http://zapier.com/',
},
......@@ -45,6 +46,8 @@ describe('oauth2 app', () => {
// In production, Zapier passes along whatever code your API set in the query
// params when it redirects the user's browser to the `redirect_uri`
code: 'one_time_code',
server_name: 'framapiaf.org',
redirect_uri: 'http://zapier.com/',
},
environment: {
CLIENT_ID: process.env.TEST_CLIENT_ID,
......@@ -68,7 +71,7 @@ describe('oauth2 app', () => {
return appTester(App.authentication.test, bundle)
.then((result) => {
result.should.have.property('username');
result.username.should.eql('Bret');
result.username.should.eql('roipoussiere');
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment