2021-08-12 15:04:37 +02:00
|
|
|
import dbInit from '../../helpers/database-init';
|
OpenAPI: addon operations (#3421)
This PR updates the OpenAPI schemas for all the operations tagged with
"addons". In doing so, I also uncovered a few bugs and inconsistencies.
These have also been fixed.
## Changes
I've added inline comments to the changed files to call out anything
that I think is worth clarifying specifically. As an overall
description, this PR does the following:
Splits `addon-schema` into `addon-schema` and
`addon-create-update-schema`. The former is used when describing addons
that exist within Unleash and contain IDs and `created_at` timestamps.
The latter is used when creating or updating addons.
Adds examples and descriptions to all relevant schemas (and their
dependencies).
Updates addons operations descriptions and response codes (including the
recently introduced 413 and 415).
Fixes a bug where the server would crash if it didn't recognize the
addon provider (test added).
Fixes a bug where updating an addon wouldn't return anything, even if
the API said that it would. (test added)
Resolves some inconsistencies in handling of addon description. (tests
added)
### Addon descriptions
when creating addons, descriptions are optional. The original
`addonSchema` said they could be `null | string | undefined`. This
caused some inconsistencies in return values. Sometimes they were
returned, other times not. I've made it so that `descriptions` are now
always returned from the API. If it's not defined or if it's set to
`null`, the API will return `description: null`.
### `IAddonDto`
`IAddonDto`, the type we used internally to model the incoming addons
(for create and update) says that `description` is required. This hasn't
been true at least since we introduced OpenAPI schemas. As such, the
update and insert methods that the service uses were incompatible with
the **actual** data that we require.
I've changed the type to reflect reality for now. Assuming the tests
pass, this **should** all be good, but I'd like the reviewer(s) to give
this a think too.
---------
Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2023-04-18 12:50:34 +02:00
|
|
|
import { setupAppWithCustomConfig } from '../../helpers/test-helper';
|
2021-08-12 15:04:37 +02:00
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-02-04 11:02:58 +01:00
|
|
|
const MASKED_VALUE = '*****';
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
let app;
|
2021-03-19 22:25:21 +01:00
|
|
|
let db;
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('addon_api_serial', getLogger);
|
OpenAPI: addon operations (#3421)
This PR updates the OpenAPI schemas for all the operations tagged with
"addons". In doing so, I also uncovered a few bugs and inconsistencies.
These have also been fixed.
## Changes
I've added inline comments to the changed files to call out anything
that I think is worth clarifying specifically. As an overall
description, this PR does the following:
Splits `addon-schema` into `addon-schema` and
`addon-create-update-schema`. The former is used when describing addons
that exist within Unleash and contain IDs and `created_at` timestamps.
The latter is used when creating or updating addons.
Adds examples and descriptions to all relevant schemas (and their
dependencies).
Updates addons operations descriptions and response codes (including the
recently introduced 413 and 415).
Fixes a bug where the server would crash if it didn't recognize the
addon provider (test added).
Fixes a bug where updating an addon wouldn't return anything, even if
the API said that it would. (test added)
Resolves some inconsistencies in handling of addon description. (tests
added)
### Addon descriptions
when creating addons, descriptions are optional. The original
`addonSchema` said they could be `null | string | undefined`. This
caused some inconsistencies in return values. Sometimes they were
returned, other times not. I've made it so that `descriptions` are now
always returned from the API. If it's not defined or if it's set to
`null`, the API will return `description: null`.
### `IAddonDto`
`IAddonDto`, the type we used internally to model the incoming addons
(for create and update) says that `description` is required. This hasn't
been true at least since we introduced OpenAPI schemas. As such, the
update and insert methods that the service uses were incompatible with
the **actual** data that we require.
I've changed the type to reflect reality for now. Assuming the tests
pass, this **should** all be good, but I'd like the reviewer(s) to give
this a think too.
---------
Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2023-04-18 12:50:34 +02:00
|
|
|
app = await setupAppWithCustomConfig(db.stores, {
|
|
|
|
experimental: {
|
|
|
|
flags: {
|
|
|
|
strictSchemaValidation: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.destroy();
|
2021-03-19 22:25:21 +01:00
|
|
|
await db.destroy();
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('gets all addons', async () => {
|
|
|
|
expect.assertions(3);
|
|
|
|
|
|
|
|
return app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.get('/api/admin/addons')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(res.body.addons.length).toBe(0);
|
|
|
|
expect(res.body.providers.length).toBe(4);
|
|
|
|
expect(res.body.providers[0].name).toBe('webhook');
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not be able to create invalid addon', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
return app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send({ invalid: 'field' })
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should create addon configuration', async () => {
|
|
|
|
expect.assertions(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
events: ['feature-updated', 'feature-created'],
|
|
|
|
};
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
return app.request.post('/api/admin/addons').send(config).expect(201);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should delete addon configuration', async () => {
|
|
|
|
expect.assertions(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
events: ['feature-updated', 'feature-created'],
|
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const res = await app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send(config)
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const { id } = res.body;
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request.delete(`/api/admin/addons/${id}`).expect(200);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should update addon configuration', async () => {
|
|
|
|
expect.assertions(2);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
events: ['feature-updated', 'feature-created'],
|
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const res = await app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send(config)
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const { id } = res.body;
|
|
|
|
|
|
|
|
const updatedConfig = {
|
|
|
|
parameters: {
|
|
|
|
url: 'http://example.com',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
...config,
|
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.put(`/api/admin/addons/${id}`)
|
|
|
|
.send(updatedConfig)
|
|
|
|
.expect(200);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
return app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.get(`/api/admin/addons/${id}`)
|
|
|
|
.send(config)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((r) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(r.body.parameters.url).toBe(MASKED_VALUE);
|
|
|
|
expect(r.body.parameters.bodyTemplate).toBe(
|
2021-02-04 11:02:58 +01:00
|
|
|
updatedConfig.parameters.bodyTemplate,
|
|
|
|
);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not update with invalid addon configuration', async () => {
|
|
|
|
expect.assertions(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
events: ['feature-updated', 'feature-created'],
|
|
|
|
};
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
await app.request.put('/api/admin/addons/1').send(config).expect(400);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not update unknown addon configuration', async () => {
|
|
|
|
expect.assertions(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
events: ['feature-updated', 'feature-created'],
|
|
|
|
};
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
await app.request.put('/api/admin/addons/123123').send(config).expect(404);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should get addon configuration', async () => {
|
|
|
|
expect.assertions(3);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
bodyTemplate: "{'name': '{{event.data.name}}' }",
|
|
|
|
},
|
|
|
|
events: ['feature-updated', 'feature-created'],
|
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const res = await app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send(config)
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
const { id } = res.body;
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request
|
2021-01-19 10:42:45 +01:00
|
|
|
.get(`/api/admin/addons/${id}`)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((r) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(r.body.provider).toBe(config.provider);
|
|
|
|
expect(r.body.parameters.bodyTemplate).toBe(
|
2021-02-04 11:02:58 +01:00
|
|
|
config.parameters.bodyTemplate,
|
|
|
|
);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(r.body.parameters.url).toBe(MASKED_VALUE);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not get unknown addon configuration', async () => {
|
|
|
|
expect.assertions(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
await app.request.get('/api/admin/addons/445').expect(404);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not delete unknown addon configuration', async () => {
|
|
|
|
expect.assertions(0);
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
return app.request.delete('/api/admin/addons/21231').expect(404);
|
2021-01-19 10:42:45 +01:00
|
|
|
});
|
OpenAPI: addon operations (#3421)
This PR updates the OpenAPI schemas for all the operations tagged with
"addons". In doing so, I also uncovered a few bugs and inconsistencies.
These have also been fixed.
## Changes
I've added inline comments to the changed files to call out anything
that I think is worth clarifying specifically. As an overall
description, this PR does the following:
Splits `addon-schema` into `addon-schema` and
`addon-create-update-schema`. The former is used when describing addons
that exist within Unleash and contain IDs and `created_at` timestamps.
The latter is used when creating or updating addons.
Adds examples and descriptions to all relevant schemas (and their
dependencies).
Updates addons operations descriptions and response codes (including the
recently introduced 413 and 415).
Fixes a bug where the server would crash if it didn't recognize the
addon provider (test added).
Fixes a bug where updating an addon wouldn't return anything, even if
the API said that it would. (test added)
Resolves some inconsistencies in handling of addon description. (tests
added)
### Addon descriptions
when creating addons, descriptions are optional. The original
`addonSchema` said they could be `null | string | undefined`. This
caused some inconsistencies in return values. Sometimes they were
returned, other times not. I've made it so that `descriptions` are now
always returned from the API. If it's not defined or if it's set to
`null`, the API will return `description: null`.
### `IAddonDto`
`IAddonDto`, the type we used internally to model the incoming addons
(for create and update) says that `description` is required. This hasn't
been true at least since we introduced OpenAPI schemas. As such, the
update and insert methods that the service uses were incompatible with
the **actual** data that we require.
I've changed the type to reflect reality for now. Assuming the tests
pass, this **should** all be good, but I'd like the reviewer(s) to give
this a think too.
---------
Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2023-04-18 12:50:34 +02:00
|
|
|
|
|
|
|
test("should return 400 if it doesn't recognize the provider", async () => {
|
|
|
|
const payload = {
|
|
|
|
provider: 'htni',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {},
|
|
|
|
events: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
return app.request.post('/api/admin/addons').send(payload).expect(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updating an addon returns the new addon configuration', async () => {
|
|
|
|
const config = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
},
|
|
|
|
events: [],
|
|
|
|
};
|
|
|
|
const { body } = await app.request.post('/api/admin/addons').send(config);
|
|
|
|
|
|
|
|
const updatedConfig = {
|
|
|
|
...config,
|
|
|
|
enabled: false,
|
|
|
|
parameters: { url: 'http://new-url:4343' },
|
|
|
|
};
|
|
|
|
|
|
|
|
return app.request
|
|
|
|
.put(`/api/admin/addons/${body.id}`)
|
|
|
|
.send(updatedConfig)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body).toMatchObject(updatedConfig);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('missing descriptions', () => {
|
|
|
|
const addonWithoutDescription = {
|
|
|
|
provider: 'webhook',
|
|
|
|
enabled: true,
|
|
|
|
parameters: {
|
|
|
|
url: 'http://localhost:4242/webhook',
|
|
|
|
},
|
|
|
|
events: ['feature-created', 'feature-updated'],
|
|
|
|
};
|
|
|
|
|
|
|
|
test('creating an addon without a description, sets the description to `null`', async () => {
|
|
|
|
const { body } = await app.request
|
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send(addonWithoutDescription)
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.description).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
return app.request
|
|
|
|
.get(`/api/admin/addons/${body.id}`)
|
|
|
|
.expect((getResponse) => {
|
|
|
|
expect(getResponse.body.description).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updating an addon without touching `description` keeps the original value', async () => {
|
|
|
|
const { body } = await app.request
|
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send(addonWithoutDescription);
|
|
|
|
|
|
|
|
const newUrl = 'http://localhost:4242/newUrl';
|
|
|
|
return app.request
|
|
|
|
.put(`/api/admin/addons/${body.id}`)
|
|
|
|
.send({ ...addonWithoutDescription, parameters: { url: newUrl } })
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.description).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test.each(['', null])(
|
|
|
|
'sending a description value of "%s", sets a `null` sets the description to an empty string',
|
|
|
|
async (description) => {
|
|
|
|
const { body } = await app.request
|
|
|
|
.post('/api/admin/addons')
|
|
|
|
.send(addonWithoutDescription);
|
|
|
|
|
|
|
|
return app.request
|
|
|
|
.put(`/api/admin/addons/${body.id}`)
|
|
|
|
.send({
|
|
|
|
...addonWithoutDescription,
|
|
|
|
description,
|
|
|
|
})
|
|
|
|
.expect((res) => {
|
|
|
|
expect(res.body.description).toStrictEqual('');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|