1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/src/lib/routes/admin-api/metrics.test.ts
Gastón Fournier 4e0678dfb1
fix: allow empty appName as it may come in the url (#3953)
## About the changes
Edit application under
https://app.unleash-hosted.com/demo/applications/test-app is currently
not working as the appName is expected to come in the request body, but
it's actually part of the url. We have two options here:
1. We change the UI to adapt to the expectations of the request by
adding appName to the request body (and eventually removing appName from
the URL, which would be a breaking change)
2. We remove the restriction of only sending the appName in the body and
take the one that comes in the URL. We have a validation that verifies
that at least one of the two sets the appName
([here](e376088668/src/lib/services/client-metrics/instance-service.ts (L208))
we validate using [this
schema](e376088668/src/lib/services/client-metrics/schema.ts (L55-L70)))

In terms of REST API, we can assume that the appName will be present in
the resource `/api/admin/metrics/applications` (an endpoint we don't
have), but when we're updating an application we should refer to that
application by its URL: `/api/admin/metrics/applications/<appName>` and
the presence of an appName in the body might indicate that we're trying
to change the name of the application (something we currently not
support)

Based on the above, I believe going with the second option is best, as
it adheres to REST principles and does not require a breaking change.
Despite that, we only support updating applications as the creation is
done from metrics ingestion

Fixes: #3580
2023-06-12 10:15:00 +02:00

130 lines
3.4 KiB
TypeScript

import supertest from 'supertest';
import createStores from '../../../test/fixtures/store';
import permissions from '../../../test/fixtures/permissions';
import getApp from '../../app';
import { createTestConfig } from '../../../test/config/test-config';
import { createServices } from '../../services';
async function getSetup() {
const stores = createStores();
const perms = permissions();
const config = createTestConfig({
preRouterHook: perms.hook,
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
return {
request: supertest(app),
stores,
perms,
config,
destroy: () => {
services.versionService.destroy();
services.clientInstanceService.destroy();
},
};
}
let stores;
let request;
let destroy;
beforeEach(async () => {
const setup = await getSetup();
stores = setup.stores;
request = setup.request;
destroy = setup.destroy;
});
afterEach(() => {
destroy();
});
test('/api/admin/metrics/seen-toggles is deprecated', () => {
return request.get('/api/admin/metrics/seen-toggles').expect(410);
});
test('/api/admin/metrics/feature-toggles is deprecated', () => {
return request.get('/api/admin/metrics/feature-toggles').expect(410);
});
test('should return empty list of client applications', () => {
return request
.get('/api/admin/metrics/applications')
.expect(200)
.expect((res) => {
expect(res.body.applications.length === 0).toBe(true);
});
});
test('should return applications', () => {
expect.assertions(2);
const appName = '123!23';
stores.clientApplicationsStore.upsert({ appName });
return request
.get('/api/admin/metrics/applications/')
.expect(200)
.expect((res) => {
const metrics = res.body;
expect(metrics.applications.length === 1).toBe(true);
expect(metrics.applications[0].appName === appName).toBe(true);
});
});
test('should store application', () => {
expect.assertions(0);
const appName = '123!23';
return request
.post(`/api/admin/metrics/applications/${appName}`)
.send({ appName, strategies: ['default'] })
.expect(202);
});
test('should store application coming from edit application form', () => {
expect.assertions(0);
const appName = '123!23';
return request
.post(`/api/admin/metrics/applications/${appName}`)
.send({
url: 'http://test.com',
description: 'This is an optional description',
icon: 'arrow-down',
})
.expect(202);
});
test('should store application details without strategies', () => {
expect.assertions(0);
const appName = '123!23';
return request
.post(`/api/admin/metrics/applications/${appName}`)
.send({ appName, url: 'htto://asd.com' })
.expect(202);
});
test('should accept a delete call to unknown application', () => {
expect.assertions(0);
const appName = 'unknown';
return request
.delete(`/api/admin/metrics/applications/${appName}`)
.expect(200);
});
test('should delete application', () => {
expect.assertions(0);
const appName = 'deletable-test';
stores.clientApplicationsStore.upsert({ appName });
return request
.delete(`/api/admin/metrics/applications/${appName}`)
.expect(200);
});