2019-07-22 18:12:43 +03:00
|
|
|
const _ = require('lodash');
|
2018-10-18 16:03:56 +03:00
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2019-09-20 18:02:45 +03:00
|
|
|
const testUtils = require('../../utils');
|
2018-10-18 16:03:56 +03:00
|
|
|
const localUtils = require('./utils');
|
|
|
|
|
|
|
|
describe('Integrations API', function () {
|
|
|
|
let request;
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
before(async function () {
|
2021-11-18 11:55:35 +03:00
|
|
|
await localUtils.startGhost();
|
2020-11-30 17:25:22 +03:00
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
await localUtils.doAuth(request, 'integrations');
|
2018-10-18 16:03:56 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const findBy = (prop, val) => object => object[prop] === val;
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can browse all integrations', async function () {
|
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`integrations/`))
|
2019-07-22 15:35:19 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
2019-07-22 18:12:43 +03:00
|
|
|
|
2022-08-09 13:15:30 +03:00
|
|
|
should.equal(res.body.integrations.length, 5);
|
2019-07-22 18:12:43 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
// there is no enforced order for integrations which makes order different on SQLite and MySQL
|
|
|
|
const zapierIntegration = _.find(res.body.integrations, {name: 'Zapier'}); // from migrations
|
|
|
|
should.exist(zapierIntegration);
|
2019-07-22 18:12:43 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const testIntegration = _.find(res.body.integrations, {name: 'Test Integration'}); // from fixtures
|
|
|
|
should.exist(testIntegration);
|
2022-08-09 13:15:30 +03:00
|
|
|
|
|
|
|
const exploreIntegration = _.find(res.body.integrations, {name: 'Test Core Integration'}); // from fixtures
|
|
|
|
should.exist(exploreIntegration);
|
2019-07-22 15:35:19 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can not read internal integration', async function () {
|
|
|
|
await request.get(localUtils.API.getApiQuery(`integrations/${testUtils.DataGenerator.Content.integrations[1].id}/`))
|
2019-07-24 12:52:55 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully create a single integration with auto generated content and admin api key', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Dis-Integrate!!'
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.equal(res.body.integrations.length, 1);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const [integration] = res.body.integrations;
|
|
|
|
should.equal(integration.name, 'Dis-Integrate!!');
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.equal(integration.api_keys.length, 2);
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const contentApiKey = integration.api_keys.find(findBy('type', 'content'));
|
|
|
|
should.equal(contentApiKey.integration_id, integration.id);
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const adminApiKey = integration.api_keys.find(findBy('type', 'admin'));
|
|
|
|
should.equal(adminApiKey.integration_id, integration.id);
|
|
|
|
should.exist(adminApiKey.secret);
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
// check Admin API key secret format
|
|
|
|
const [id, secret] = adminApiKey.secret.split(':');
|
|
|
|
should.exist(id);
|
|
|
|
should.equal(id, adminApiKey.id);
|
|
|
|
should.exist(secret);
|
|
|
|
secret.length.should.equal(64);
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('integrations/')}${res.body.integrations[0].id}/`);
|
2018-10-18 16:03:56 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully create a single integration with a webhook', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Integratatron4000',
|
|
|
|
webhooks: [{
|
|
|
|
event: 'something',
|
2019-07-05 14:40:43 +03:00
|
|
|
target_url: 'http://example.com'
|
2018-10-18 16:03:56 +03:00
|
|
|
}]
|
2019-02-04 17:16:24 +03:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.equal(res.body.integrations.length, 1);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const [integration] = res.body.integrations;
|
|
|
|
should.equal(integration.name, 'Integratatron4000');
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.equal(integration.webhooks.length, 1);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const webhook = integration.webhooks[0];
|
|
|
|
should.equal(webhook.integration_id, integration.id);
|
2020-09-14 13:33:37 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('integrations/')}${res.body.integrations[0].id}/`);
|
2018-10-18 16:03:56 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully get a created integration', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Interrogation Integration'
|
|
|
|
}]
|
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const [createdIntegration] = res.body.integrations;
|
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.equal(res2.body.integrations.length, 1);
|
|
|
|
|
|
|
|
const [integration] = res2.body.integrations;
|
|
|
|
|
|
|
|
should.equal(integration.id, createdIntegration.id);
|
|
|
|
should.equal(integration.name, createdIntegration.name);
|
|
|
|
should.equal(integration.slug, createdIntegration.slug);
|
|
|
|
should.equal(integration.description, createdIntegration.description);
|
|
|
|
should.equal(integration.icon_image, createdIntegration.icon_image);
|
2019-02-04 17:16:24 +03:00
|
|
|
});
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully get *all* created integrations with api_keys', async function () {
|
|
|
|
await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Integrate with this!'
|
|
|
|
}]
|
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
await request.post(localUtils.API.getApiQuery('integrations/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Winter-(is)-great'
|
|
|
|
}]
|
2020-10-19 07:45:26 +03:00
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
2018-10-18 16:03:56 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request.get(localUtils.API.getApiQuery(`integrations/?include=api_keys&limit=all`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const body = res.body;
|
|
|
|
// This is the only page
|
|
|
|
should.equal(body.meta.pagination.page, 1);
|
|
|
|
should.equal(body.meta.pagination.pages, 1);
|
|
|
|
should.equal(body.meta.pagination.next, null);
|
|
|
|
should.equal(body.meta.pagination.prev, null);
|
|
|
|
|
|
|
|
body.integrations.forEach((integration) => {
|
|
|
|
should.exist(integration.api_keys);
|
|
|
|
});
|
|
|
|
});
|
2020-10-19 07:45:26 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully edit a created integration', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Rubbish Integration Name'
|
|
|
|
}]
|
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const [createdIntegration] = res.body.integrations;
|
|
|
|
|
|
|
|
await request.put(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Awesome Integration Name',
|
|
|
|
description: 'Finally got round to writing this...'
|
|
|
|
}]
|
2020-10-19 07:45:26 +03:00
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const [updatedIntegration] = res2.body.integrations;
|
2020-05-05 21:06:21 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
should.equal(updatedIntegration.id, createdIntegration.id);
|
|
|
|
should.equal(updatedIntegration.name, 'Awesome Integration Name');
|
|
|
|
should.equal(updatedIntegration.description, 'Finally got round to writing this...');
|
|
|
|
});
|
2020-10-19 07:45:26 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully refresh an integration api key', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/?include=api_keys'))
|
2020-05-05 21:06:21 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Rubbish Integration Name'
|
|
|
|
}]
|
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const [createdIntegration] = res.body.integrations;
|
|
|
|
const apiKeys = createdIntegration.api_keys;
|
|
|
|
const adminApiKey = apiKeys.find(key => key.type === 'admin');
|
|
|
|
|
|
|
|
await request.post(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/api_key/${adminApiKey.id}/refresh`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
id: createdIntegration.id
|
|
|
|
}]
|
2020-10-19 07:45:26 +03:00
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/?include=api_keys`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const [updatedIntegration] = res2.body.integrations;
|
|
|
|
const updatedAdminApiKey = updatedIntegration.api_keys.find(key => key.type === 'admin');
|
|
|
|
should.equal(updatedIntegration.id, createdIntegration.id);
|
|
|
|
updatedAdminApiKey.secret.should.not.eql(adminApiKey.secret);
|
2019-02-04 17:16:24 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res3 = await request.get(localUtils.API.getApiQuery(`actions/?filter=resource_id:${adminApiKey.id}&include=actor`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const actions = res3.body.actions;
|
|
|
|
const refreshedAction = actions.find((action) => {
|
|
|
|
return action.event === 'refreshed';
|
|
|
|
});
|
|
|
|
should.exist(refreshedAction);
|
|
|
|
});
|
2020-10-19 07:45:26 +03:00
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully add and delete a created integrations webhooks', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
2019-07-05 14:40:43 +03:00
|
|
|
name: 'Webhook-less Integration'
|
2019-02-04 17:16:24 +03:00
|
|
|
}]
|
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const [createdIntegration] = res.body.integrations;
|
|
|
|
|
|
|
|
await request.put(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
webhooks: [{
|
|
|
|
event: 'somestuff',
|
|
|
|
target_url: 'http://example.com'
|
|
|
|
}]
|
|
|
|
}]
|
2020-10-19 07:45:26 +03:00
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const res2 = await request.get(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/?include=webhooks`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const [updatedIntegration] = res2.body.integrations;
|
|
|
|
|
|
|
|
should.equal(updatedIntegration.webhooks.length, 1);
|
|
|
|
|
|
|
|
const webhook = updatedIntegration.webhooks[0];
|
|
|
|
should.equal(webhook.integration_id, updatedIntegration.id);
|
|
|
|
|
|
|
|
await request.put(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
webhooks: []
|
|
|
|
}]
|
2020-10-19 07:45:26 +03:00
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const res3 = await request.get(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/?include=webhooks`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
const [updatedIntegration2] = res3.body.integrations;
|
|
|
|
should.equal(updatedIntegration2.webhooks.length, 0);
|
2018-10-18 16:03:56 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can successfully delete a created integration', async function () {
|
|
|
|
const res = await request.post(localUtils.API.getApiQuery('integrations/'))
|
2019-02-04 17:16:24 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
integrations: [{
|
|
|
|
name: 'Short Lived Integration'
|
|
|
|
}]
|
|
|
|
})
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const [createdIntegration] = res.body.integrations;
|
|
|
|
|
|
|
|
await request.del(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
2022-03-17 18:06:25 +03:00
|
|
|
.expect(204)
|
|
|
|
.expect((_res) => {
|
|
|
|
_res.body.should.be.empty();
|
|
|
|
});
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
await request.get(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect(404);
|
2021-09-16 14:05:48 +03:00
|
|
|
|
|
|
|
const editRes = await request.put(localUtils.API.getApiQuery(`integrations/${createdIntegration.id}/`))
|
|
|
|
.send({
|
|
|
|
integrations: [createdIntegration]
|
|
|
|
})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect(404);
|
|
|
|
|
|
|
|
editRes.body.errors[0].context.should.eql('Integration not found.');
|
2018-10-18 16:03:56 +03:00
|
|
|
});
|
|
|
|
});
|