1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-07-17 13:46:47 +02:00
unleash.unleash/src/test/e2e/api/admin/metrics.e2e.test.ts
Gastón Fournier bdb763c9d5
chore!: remove deprecated default env from new installs (#10080)
**BREAKING CHANGE**: DEFAULT_ENV changed from `default` (should not be
used anymore) to `development`

## About the changes
- Only delete default env if the install is fresh new.
- Consider development the new default. The main consequence of this
change is that the default is no longer considered `type=production`
environment but also for frontend tokens due to this assumption:
724c4b78a2/src/lib/schema/api-token-schema.test.ts (L54-L59)
(I believe this is mostly due to the [support for admin
tokens](https://github.com/Unleash/unleash/pull/10080#discussion_r2126871567))
- `feature_toggle_update_total` metric reports `n/a` in environment and
environment type as it's not environment specific
2025-06-06 12:02:21 +02:00

198 lines
5.4 KiB
TypeScript

import dbInit, { type ITestDb } from '../../helpers/database-init.js';
import {
type IUnleashTest,
setupAppWithCustomConfig,
} from '../../helpers/test-helper.js';
import getLogger from '../../../fixtures/no-logger.js';
import { ApiTokenType } from '../../../../lib/types/model.js';
import { DEFAULT_ENV } from '../../../../lib/server-impl.js';
let app: IUnleashTest;
let db: ITestDb;
beforeAll(async () => {
db = await dbInit('metrics_serial', getLogger);
app = await setupAppWithCustomConfig(
db.stores,
{
experimental: {
flags: {
strictSchemaValidation: true,
},
},
},
db.rawDatabase,
);
});
beforeEach(async () => {
await app.services.clientInstanceService.createApplication({
appName: 'demo-app-1',
strategies: ['default'],
//@ts-ignore
announced: true,
});
await app.services.clientInstanceService.createApplication({
appName: 'demo-app-2',
strategies: ['default', 'extra'],
description: 'hello',
//@ts-ignore
announced: true,
});
await app.services.clientInstanceService.createApplication({
appName: 'deletable-app',
strategies: ['default'],
description: 'Some desc',
//@ts-ignore
announced: true,
});
await db.stores.clientInstanceStore.insert({
appName: 'demo-app-1',
instanceId: 'test-1',
});
await db.stores.clientInstanceStore.insert({
appName: 'demo-seed-2',
instanceId: 'test-2',
});
await db.stores.clientInstanceStore.insert({
appName: 'deletable-app',
instanceId: 'inst-1',
});
await app.services.clientInstanceService.createApplication({
appName: 'usage-app',
strategies: ['default'],
description: 'Some desc',
projects: ['default'],
environment: 'dev',
});
});
afterAll(async () => {
if (db) {
await db.destroy();
}
});
afterEach(async () => {
await db.reset();
});
test('should get application details', async () => {
return app.request
.get('/api/admin/metrics/applications/demo-app-1')
.expect('Content-Type', /json/)
.expect(200)
.expect((res) => {
expect(res.body.appName).toBe('demo-app-1');
expect(res.body.instances).toHaveLength(1);
});
});
test('should get list of applications', async () => {
expect.assertions(1);
return app.request
.get('/api/admin/metrics/applications')
.expect('Content-Type', /json/)
.expect(200)
.expect((res) => {
expect(res.body.applications).toHaveLength(4);
});
});
test('should delete application', async () => {
expect.assertions(2);
await app.request
.delete('/api/admin/metrics/applications/deletable-app')
.expect((res) => {
expect(res.status).toBe(200);
});
return app.request
.get('/api/admin/metrics/applications')
.expect('Content-Type', /json/)
.expect((res) => {
expect(res.body.applications).toHaveLength(3);
});
});
test('deleting an application should be idempotent, so expect 200', async () => {
expect.assertions(1);
return app.request
.delete('/api/admin/metrics/applications/unknown')
.expect((res) => {
expect(res.status).toBe(200);
});
});
test('should get list of application usage', async () => {
const { body } = await app.request
.get('/api/admin/metrics/applications')
.expect('Content-Type', /json/)
.expect(200);
const application = body.applications.find(
(selectableApp) => selectableApp.appName === 'usage-app',
);
expect(application).toMatchObject({
appName: 'usage-app',
usage: [
{
project: 'default',
environments: ['dev'],
},
],
});
});
test('should save multiple projects from token', async () => {
await db.reset();
await db.stores.projectStore.create({
id: 'mainProject',
name: 'mainProject',
});
const multiProjectToken =
await app.services.apiTokenService.createApiTokenWithProjects({
type: ApiTokenType.CLIENT,
projects: ['default', 'mainProject'],
environment: DEFAULT_ENV,
tokenName: 'tester',
});
await app.request
.post('/api/client/register')
.set('Authorization', multiProjectToken.secret)
.send({
appName: 'multi-project-app',
instanceId: 'instance-1',
strategies: ['default'],
started: Date.now(),
interval: 10,
});
await app.services.clientInstanceService.bulkAdd();
const { body } = await app.request
.get('/api/admin/metrics/applications')
.expect('Content-Type', /json/)
.expect(200);
expect(body.applications).toEqual(
expect.arrayContaining([
expect.objectContaining({
appName: 'multi-project-app',
usage: [
{
environments: [DEFAULT_ENV],
project: 'default',
},
{
environments: [DEFAULT_ENV],
project: 'mainProject',
},
],
}),
]),
);
});