diff --git a/src/lib/services/addon-service.ts b/src/lib/services/addon-service.ts index e6fc6f23fb..f7251eef91 100644 --- a/src/lib/services/addon-service.ts +++ b/src/lib/services/addon-service.ts @@ -71,10 +71,13 @@ export default class AddonService { } // Memoized private function - this.fetchAddonConfigs = memoizee(async () => addonStore.getAll(), { - promise: true, - maxAge: ADDONS_CACHE_TIME, - }); + this.fetchAddonConfigs = memoizee( + async () => addonStore.getAll({ enabled: true }), + { + promise: true, + maxAge: ADDONS_CACHE_TIME, + }, + ); } loadSensitiveParams(addonProviders: IAddonProviders): ISensitiveParams { @@ -159,7 +162,6 @@ export default class AddonService { providerName, ); } catch (err) { - this.logger.error(err); if (!(err instanceof NameExistsError)) { this.logger.error(err); } diff --git a/src/lib/types/stores/store.ts b/src/lib/types/stores/store.ts index fd1356a6ce..db9b5b9e7b 100644 --- a/src/lib/types/stores/store.ts +++ b/src/lib/types/stores/store.ts @@ -1,6 +1,6 @@ export interface Store { get(key: K): Promise; - getAll(): Promise; + getAll(query?: Object): Promise; exists(key: K): Promise; delete(key: K): Promise; deleteAll(): Promise; diff --git a/src/test/e2e/services/addon-service.e2e.test.ts b/src/test/e2e/services/addon-service.e2e.test.ts new file mode 100644 index 0000000000..c73fd27748 --- /dev/null +++ b/src/test/e2e/services/addon-service.e2e.test.ts @@ -0,0 +1,90 @@ +import dbInit from '../helpers/database-init'; +import getLogger from '../../fixtures/no-logger'; +import { createTestConfig } from '../../config/test-config'; +import AddonService from '../../../lib/services/addon-service'; +import { IUnleashStores } from '../../../lib/types'; + +import SimpleAddon from '../../../lib/services/addon-service-test-simple-addon'; +import TagTypeService from '../../../lib/services/tag-type-service'; +import { FEATURE_CREATED } from '../../../lib/types/events'; + +const addonProvider = { simple: new SimpleAddon() }; + +let db; +let stores: IUnleashStores; +let addonService: AddonService; + +beforeAll(async () => { + const config = createTestConfig({ + server: { baseUriPath: '/test' }, + }); + db = await dbInit('addon_service_serial', getLogger); + stores = db.stores; + const tagTypeService = new TagTypeService(stores, config); + addonService = new AddonService( + stores, + config, + tagTypeService, + addonProvider, + ); +}); + +afterAll(async () => { + if (db) { + await db.destroy(); + } +}); +afterEach(async () => { + const addons = await stores.addonStore.getAll(); + const deleteAll = addons.map((a) => stores.addonStore.delete(a.id)); + await Promise.all(deleteAll); +}); + +test('should only return active addons', async () => { + jest.useFakeTimers(); + const config = { + provider: 'simple', + enabled: false, + parameters: { + url: 'http://localhost/wh', + var: 'some-value', + }, + events: [FEATURE_CREATED], + description: '', + }; + + const config2 = { + provider: 'simple', + enabled: true, + parameters: { + url: 'http://localhost/wh', + var: 'some-value', + }, + events: [FEATURE_CREATED], + description: '', + }; + const config3 = { + provider: 'simple', + enabled: true, + parameters: { + url: 'http://localhost/wh', + var: 'some-value', + }, + events: [FEATURE_CREATED], + description: '', + }; + + await addonService.createAddon(config, 'me@mail.com'); + await addonService.createAddon(config2, 'me@mail.com'); + await addonService.createAddon(config3, 'me@mail.com'); + + jest.advanceTimersByTime(61 * 1000); + + const activeAddons = await addonService.fetchAddonConfigs(); + const allAddons = await addonService.getAddons(); + + expect(activeAddons.length).toBe(2); + expect(allAddons.length).toBe(3); + + jest.useRealTimers(); +});