/
environment.test.ts
155 lines (137 loc) · 4.28 KB
/
environment.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import dbInit, { ITestDb } from '../../helpers/database-init';
import getLogger from '../../../fixtures/no-logger';
import {
IUnleashTest,
setupAppWithCustomConfig,
} from '../../helpers/test-helper';
import { DEFAULT_ENV } from '../../../../lib/util/constants';
let app: IUnleashTest;
let db: ITestDb;
beforeAll(async () => {
db = await dbInit('environment_api_serial', getLogger);
app = await setupAppWithCustomConfig(db.stores, {
experimental: {
flags: {
strictSchemaValidation: true,
},
},
});
});
afterAll(async () => {
await app.destroy();
await db.destroy();
});
test('Can list all existing environments', async () => {
await app.request
.get('/api/admin/environments')
.expect(200)
.expect('Content-Type', /json/)
.expect((res) => {
expect(res.body.version).toBe(1);
expect(res.body.environments[0]).toStrictEqual({
name: DEFAULT_ENV,
enabled: true,
sortOrder: 1,
type: 'production',
protected: true,
projectCount: 1,
apiTokenCount: 0,
enabledToggleCount: 0,
});
});
});
test('Can update sort order', async () => {
const envName = 'update-sort-order';
await db.stores.environmentStore.create({
name: envName,
type: 'production',
});
await app.request
.put('/api/admin/environments/sort-order')
.send({
[DEFAULT_ENV]: 2,
[envName]: 1,
})
.expect(200);
await app.request
.get('/api/admin/environments')
.expect(200)
.expect('Content-Type', /json/)
.expect((res) => {
const updatedSort = res.body.environments.find(
(t) => t.name === envName,
);
const defaultEnv = res.body.environments.find(
(t) => t.name === DEFAULT_ENV,
);
expect(updatedSort.sortOrder).toBe(1);
expect(defaultEnv.sortOrder).toBe(2);
});
});
test('Sort order will fail on wrong data format', async () => {
const envName = 'sort-order-env';
await app.request
.put('/api/admin/environments/sort-order')
.send({
[DEFAULT_ENV]: 'test',
[envName]: 1,
})
.expect(400);
});
test('Can update environment enabled status', async () => {
const envName = 'enable-environment';
await db.stores.environmentStore.create({
name: envName,
type: 'production',
});
await app.request
.post(`/api/admin/environments/${envName}/on`)
.set('Content-Type', 'application/json')
.expect(204);
});
test('Can update environment disabled status', async () => {
const envName = 'disable-environment';
await db.stores.environmentStore.create({
name: envName,
type: 'production',
});
await app.request
.post(`/api/admin/environments/${envName}/off`)
.set('Content-Type', 'application/json')
.expect(204);
});
test('Can not update non-existing environment enabled status', async () => {
const envName = 'non-existing-env';
await app.request
.post(`/api/admin/environments/${envName}/on`)
.set('Content-Type', 'application/json')
.expect(404);
});
test('Can not update non-existing environment disabled status', async () => {
const envName = 'non-existing-env';
await app.request
.post(`/api/admin/environments/${envName}/off`)
.set('Content-Type', 'application/json')
.expect(404);
});
test('Can get specific environment', async () => {
const envName = 'get-specific';
await db.stores.environmentStore.create({
name: envName,
type: 'production',
});
await app.request
.get(`/api/admin/environments/${envName}`)
.expect(200)
.expect('Content-Type', /json/)
.expect((res) => {
const { body } = res;
expect(body.name).toBe(envName);
expect(body.type).toBe('production');
});
});
test('Getting a non existing environment yields 404', async () => {
await app.request
.get('/api/admin/environments/this-does-not-exist')
.expect(404);
});