1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/src/lib/services/addon-service.test.ts

757 lines
22 KiB
TypeScript
Raw Normal View History

import { ValidationError } from 'joi';
import getLogger from '../../test/fixtures/no-logger';
import TagTypeService from '../features/tag-type/tag-type-service';
import {
ADDON_CONFIG_CREATED,
ADDON_CONFIG_DELETED,
ADDON_CONFIG_UPDATED,
FEATURE_CREATED,
} from '../types/events';
import createStores from '../../test/fixtures/store';
import AddonService from './addon-service';
import { IAddonDto } from '../types/stores/addon-store';
import SimpleAddon from './addon-service-test-simple-addon';
import { IAddonProviders } from '../addons';
import EventService from './event-service';
const MASKED_VALUE = '*****';
let addonProvider: IAddonProviders;
function getSetup() {
const stores = createStores();
const eventService = new EventService(stores, { getLogger });
const tagTypeService = new TagTypeService(
stores,
{ getLogger },
eventService,
);
addonProvider = { simple: new SimpleAddon() };
return {
addonService: new AddonService(
stores,
{
getLogger,
// @ts-ignore
server: { unleashUrl: 'http://test' },
},
tagTypeService,
eventService,
addonProvider,
),
eventService,
stores,
tagTypeService,
};
}
test('should load addon configurations', async () => {
const { addonService } = getSetup();
const configs = await addonService.getAddons();
expect(configs.length).toBe(0);
});
test('should load provider definitions', async () => {
const { addonService } = getSetup();
const providerDefinitions = addonService.getProviderDefinitions();
const simple = providerDefinitions.find((p) => p.name === 'simple');
expect(providerDefinitions.length).toBe(1);
expect(simple.name).toBe('simple');
});
test('should not allow addon-config for unknown provider', async () => {
const { addonService } = getSetup();
await expect(async () => {
await addonService.createAddon(
{
provider: 'unknown',
enabled: true,
parameters: {},
events: [],
description: '',
},
'test',
);
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
}).rejects.toThrow(ValidationError);
});
test('should trigger simple-addon eventHandler', async () => {
const { addonService, eventService } = getSetup();
const config = {
provider: 'simple',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
await addonService.createAddon(config, 'me@mail.com');
// Feature toggle was created
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-ignore
const events = simpleProvider.getEvents();
expect(events.length).toBe(1);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
});
test('should not trigger event handler if project of event is different from addon', async () => {
const { addonService, eventService } = getSetup();
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: ['someproject'],
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: 'someotherproject',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(0);
});
test('should trigger event handler if project for event is one of the desired projects for addon', async () => {
const { addonService, eventService } = getSetup();
const desiredProject = 'desired';
const otherProject = 'other';
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: [desiredProject],
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProject,
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: otherProject,
data: {
name: 'other-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(1);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
});
test('should trigger events for multiple projects if addon is setup to filter multiple projects', async () => {
const { addonService, eventService } = getSetup();
const desiredProjects = ['desired', 'desired2'];
const otherProject = 'other';
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: desiredProjects,
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[0],
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: otherProject,
data: {
name: 'other-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[1],
data: {
name: 'third-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(2);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
expect(events[1].event.type).toBe(FEATURE_CREATED);
expect(events[1].event.data.name).toBe('third-toggle');
});
test('should filter events on environment if addon is setup to filter for it', async () => {
const { addonService, eventService } = getSetup();
const desiredEnvironment = 'desired';
const otherEnvironment = 'other';
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: [],
environments: [desiredEnvironment],
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredEnvironment,
environment: desiredEnvironment,
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
environment: otherEnvironment,
data: {
name: 'other-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(1);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
});
test('should not filter out global events (no specific environment) even if addon is setup to filter for environments', async () => {
const { addonService, eventService } = getSetup();
const filteredEnvironment = 'filtered';
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: [],
environments: [filteredEnvironment],
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
const globalEventWithNoEnvironment = {
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: 'some-project',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent(globalEventWithNoEnvironment);
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(1);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
});
test('should not filter out global events (no specific project) even if addon is setup to filter for projects', async () => {
const { addonService, eventService } = getSetup();
const filteredProject = 'filtered';
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: [filteredProject],
environments: [],
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
const globalEventWithNoProject = {
type: FEATURE_CREATED,
createdBy: 'some@user.com',
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent(globalEventWithNoProject);
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(1);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
});
test('should support wildcard option for filtering addons', async () => {
const { addonService, eventService } = getSetup();
const desiredProjects = ['desired', 'desired2'];
const otherProject = 'other';
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: ['*'],
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[0],
data: {
name: 'some-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: otherProject,
data: {
name: 'other-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[1],
data: {
name: 'third-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events).toHaveLength(3);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe('some-toggle');
expect(events[1].event.type).toBe(FEATURE_CREATED);
expect(events[1].event.data.name).toBe('other-toggle');
expect(events[2].event.type).toBe(FEATURE_CREATED);
expect(events[2].event.data.name).toBe('third-toggle');
});
test('Should support filtering by both project and environment', async () => {
const { addonService, eventService } = getSetup();
const desiredProjects = ['desired1', 'desired2', 'desired3'];
const desiredEnvironments = ['env1', 'env2', 'env3'];
const config = {
provider: 'simple',
enabled: true,
events: [FEATURE_CREATED],
projects: desiredProjects,
environments: desiredEnvironments,
description: '',
parameters: {
url: 'http://localhost:wh',
},
};
const expectedFeatureNames = [
'desired-toggle1',
'desired-toggle2',
'desired-toggle3',
];
await addonService.createAddon(config, 'me@mail.com');
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[0],
environment: desiredEnvironments[0],
data: {
name: expectedFeatureNames[0],
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[0],
environment: 'wrongenvironment',
data: {
name: 'other-toggle',
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[2],
environment: desiredEnvironments[1],
data: {
name: expectedFeatureNames[1],
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: desiredProjects[2],
environment: desiredEnvironments[2],
data: {
name: expectedFeatureNames[2],
enabled: false,
strategies: [{ name: 'default' }],
},
});
await eventService.storeEvent({
type: FEATURE_CREATED,
createdBy: 'some@user.com',
project: 'wrongproject',
environment: desiredEnvironments[0],
data: {
name: 'not-expected',
enabled: false,
strategies: [{ name: 'default' }],
},
});
const simpleProvider = addonService.addonProviders.simple;
// @ts-expect-error
const events = simpleProvider.getEvents();
expect(events.length).toBe(3);
expect(events[0].event.type).toBe(FEATURE_CREATED);
expect(events[0].event.data.name).toBe(expectedFeatureNames[0]);
expect(events[1].event.type).toBe(FEATURE_CREATED);
expect(events[1].event.data.name).toBe(expectedFeatureNames[1]);
expect(events[2].event.type).toBe(FEATURE_CREATED);
expect(events[2].event.data.name).toBe(expectedFeatureNames[2]);
});
test('should create simple-addon config', async () => {
const { addonService } = getSetup();
const config: IAddonDto = {
provider: 'simple',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
await addonService.createAddon(config, 'me@mail.com');
const addons = await addonService.getAddons();
expect(addons.length).toBe(1);
expect(addons[0].provider).toBe('simple');
});
test('should create tag type for simple-addon', async () => {
const { addonService, tagTypeService } = getSetup();
const config: IAddonDto = {
provider: 'simple',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
await addonService.createAddon(config, 'me@mail.com');
const tagType = await tagTypeService.getTagType('me');
expect(tagType.name).toBe('me');
});
test('should store ADDON_CONFIG_CREATE event', async () => {
const { addonService, eventService } = getSetup();
const config = {
provider: 'simple',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
await addonService.createAddon(config, 'me@mail.com');
const { events } = await eventService.getEvents();
expect(events.length).toBe(2); // Also tag-types where created
expect(events[1].type).toBe(ADDON_CONFIG_CREATED);
expect(events[1].data.provider).toBe('simple');
});
test('should store ADDON_CONFIG_UPDATE event', async () => {
const { addonService, eventService } = getSetup();
const config: IAddonDto = {
description: '',
provider: 'simple',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
};
const addonConfig = await addonService.createAddon(config, 'me@mail.com');
const updated = { ...addonConfig, description: 'test' };
await addonService.updateAddon(addonConfig.id, updated, 'me@mail.com');
const { events } = await eventService.getEvents();
expect(events.length).toBe(3);
expect(events[2].type).toBe(ADDON_CONFIG_UPDATED);
expect(events[2].data.provider).toBe('simple');
});
test('should store ADDON_CONFIG_REMOVE event', async () => {
const { addonService, eventService } = getSetup();
const config: IAddonDto = {
provider: 'simple',
description: '',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
};
const addonConfig = await addonService.createAddon(config, 'me@mail.com');
await addonService.removeAddon(addonConfig.id, 'me@mail.com');
const { events } = await eventService.getEvents();
expect(events.length).toBe(3);
expect(events[2].type).toBe(ADDON_CONFIG_DELETED);
feat: add more events in integrations (#4815) https://linear.app/unleash/issue/2-1253/add-support-for-more-events-in-the-slack-app-integration Adds support for a lot more events in our integrations. Here is how the full list looks like: - ADDON_CONFIG_CREATED - ADDON_CONFIG_DELETED - ADDON_CONFIG_UPDATED - API_TOKEN_CREATED - API_TOKEN_DELETED - CHANGE_ADDED - CHANGE_DISCARDED - CHANGE_EDITED - CHANGE_REQUEST_APPLIED - CHANGE_REQUEST_APPROVAL_ADDED - CHANGE_REQUEST_APPROVED - CHANGE_REQUEST_CANCELLED - CHANGE_REQUEST_CREATED - CHANGE_REQUEST_DISCARDED - CHANGE_REQUEST_REJECTED - CHANGE_REQUEST_SENT_TO_REVIEW - CONTEXT_FIELD_CREATED - CONTEXT_FIELD_DELETED - CONTEXT_FIELD_UPDATED - FEATURE_ARCHIVED - FEATURE_CREATED - FEATURE_DELETED - FEATURE_ENVIRONMENT_DISABLED - FEATURE_ENVIRONMENT_ENABLED - FEATURE_ENVIRONMENT_VARIANTS_UPDATED - FEATURE_METADATA_UPDATED - FEATURE_POTENTIALLY_STALE_ON - FEATURE_PROJECT_CHANGE - FEATURE_REVIVED - FEATURE_STALE_OFF - FEATURE_STALE_ON - FEATURE_STRATEGY_ADD - FEATURE_STRATEGY_REMOVE - FEATURE_STRATEGY_UPDATE - FEATURE_TAGGED - FEATURE_UNTAGGED - GROUP_CREATED - GROUP_DELETED - GROUP_UPDATED - PROJECT_CREATED - PROJECT_DELETED - SEGMENT_CREATED - SEGMENT_DELETED - SEGMENT_UPDATED - SERVICE_ACCOUNT_CREATED - SERVICE_ACCOUNT_DELETED - SERVICE_ACCOUNT_UPDATED - USER_CREATED - USER_DELETED - USER_UPDATED I added the events that I thought were relevant based on my own discretion. Know of any event we should add? Let me know and I'll add it 🙂 For now I only added these events to the new Slack App integration, but we can add them to the other integrations as well since they are now supported. The event formatter was refactored and changed quite a bit in order to make it easier to maintain and add new events in the future. As a result, events are now posted with different text. Do we consider this a breaking change? If so, I can keep the old event formatter around, create a new one and only use it for the new Slack App integration. I noticed we don't have good 404 behaviors in the UI for things that are deleted in the meantime, that's why I avoided some links to specific resources (like feature strategies, integration configurations, etc), but we could add them later if we improve this. This PR also tries to add some consistency to the the way we log events.
2023-09-29 17:11:59 +02:00
expect(events[2].preData.id).toBe(addonConfig.id);
});
test('should hide sensitive fields when fetching', async () => {
const { addonService } = getSetup();
const config: IAddonDto = {
provider: 'simple',
enabled: true,
description: '',
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
sensitiveParam: 'should be hidden when fetching',
},
events: [FEATURE_CREATED],
};
const createdConfig = await addonService.createAddon(config, 'me@mail.com');
const addons = await addonService.getAddons();
const addonRetrieved = await addonService.getAddon(createdConfig.id);
expect(addons.length).toBe(1);
// @ts-ignore
expect(addons[0].parameters.sensitiveParam).toBe(MASKED_VALUE);
// @ts-ignore
expect(addonRetrieved.parameters.sensitiveParam).toBe(MASKED_VALUE);
});
test('should not overwrite masked values when updating', async () => {
const { addonService, stores } = getSetup();
const config: IAddonDto = {
provider: 'simple',
enabled: true,
parameters: {
url: 'http://localhost/wh',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
const addonConfig = await addonService.createAddon(config, 'me@mail.com');
const updated = {
...addonConfig,
parameters: { url: MASKED_VALUE, var: 'some-new-value' },
description: 'test',
};
await addonService.updateAddon(addonConfig.id, updated, 'me@mail.com');
const updatedConfig = await stores.addonStore.get(addonConfig.id);
// @ts-ignore
expect(updatedConfig.parameters.url).toBe('http://localhost/wh');
// @ts-ignore
expect(updatedConfig.parameters.var).toBe('some-new-value');
});
test('should reject addon config with missing required parameter when creating', async () => {
const { addonService } = getSetup();
const config = {
provider: 'simple',
enabled: true,
parameters: {
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
await expect(async () =>
addonService.createAddon(config, 'me@mail.com'),
).rejects.toThrow(ValidationError);
});
test('should reject updating addon config with missing required parameter', async () => {
const { addonService } = getSetup();
const addonConfig = {
provider: 'simple',
enabled: true,
parameters: {
url: 'https://some.site/api',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
const config = await addonService.createAddon(addonConfig, 'me@mail.com');
const updated = {
...config,
parameters: { var: 'some-new-value' },
description: 'test',
};
await expect(async () =>
addonService.updateAddon(config.id, updated, 'me@mail.com'),
).rejects.toThrow(ValidationError);
});
test('Should reject addon config if a required parameter is just the empty string', async () => {
const { addonService } = getSetup();
const config = {
provider: 'simple',
enabled: true,
parameters: {
url: '',
var: 'some-value',
},
events: [FEATURE_CREATED],
description: '',
};
await expect(async () =>
addonService.createAddon(config, 'me@mail.com'),
).rejects.toThrow(ValidationError);
});