Files
coolify/e2e/api/environments.spec.ts

116 lines
6.6 KiB
TypeScript

import { expect, test } from '../fixtures.js';
type EnvironmentSummary = { uuid: string; name: string };
type EnvironmentDetail = EnvironmentSummary & { description: string | null; applications: unknown[]; services: unknown[] };
type ValidationError = { message: string; errors: Record<string, string[]> };
type Message = { message: string };
test.describe('environments', () => {
test('a write token adds an environment that reads back by name and by UUID', async ({ api, newProject }) => {
const project = await newProject('env-create');
const created = await (await api.withAbility('write')).post<{ uuid: string }>(`projects/${project.uuid}/environments`, {
data: { name: 'staging' },
});
expect(created.status).toBe(201);
const root = await api.root();
const list = await root.get<EnvironmentSummary[]>(`projects/${project.uuid}/environments`);
expect(list.status).toBe(200);
expect(list.body.map((environment) => environment.name).sort()).toEqual(['production', 'staging']);
for (const key of ['staging', created.body.uuid]) {
const detail = await root.get<EnvironmentDetail>(`projects/${project.uuid}/${key}`);
expect(detail.status).toBe(200);
expect(detail.body).toMatchObject({ uuid: created.body.uuid, name: 'staging', applications: [], services: [] });
}
});
test('rejects a second environment with the same name', async ({ api, newProject }) => {
const project = await newProject('env-duplicate');
const duplicate = await (await api.withAbility('write')).post<Message>(`projects/${project.uuid}/environments`, {
data: { name: 'production' },
});
expect(duplicate.status).toBe(409);
expect(duplicate.body.message).toBe('Environment with this name already exists.');
const list = await (await api.root()).get<EnvironmentSummary[]>(`projects/${project.uuid}/environments`);
expect(list.body.map((environment) => environment.name)).toEqual(['production']);
});
test('a write token renames an environment, but not to a name that is in use', async ({ api, newProject }) => {
const project = await newProject('env-rename');
const write = await api.withAbility('write');
expect((await write.post(`projects/${project.uuid}/environments`, { data: { name: 'staging' } })).status).toBe(201);
const renamed = await write.patch<EnvironmentDetail>(`projects/${project.uuid}/environments/staging`, {
data: { name: 'preview', description: 'Short-lived' },
});
expect(renamed.status).toBe(200);
expect(renamed.body).toMatchObject({ name: 'preview', description: 'Short-lived' });
const conflict = await write.patch<Message>(`projects/${project.uuid}/environments/preview`, { data: { name: 'production' } });
expect(conflict.status).toBe(409);
const root = await api.root();
expect((await root.get(`projects/${project.uuid}/staging`)).status).toBe(404);
const detail = await root.get<EnvironmentDetail>(`projects/${project.uuid}/preview`);
expect(detail.body).toMatchObject({ name: 'preview', description: 'Short-lived' });
});
test('a delete token deletes an empty environment', async ({ api, newProject }) => {
const project = await newProject('env-delete');
const created = await (await api.withAbility('write')).post<{ uuid: string }>(`projects/${project.uuid}/environments`, {
data: { name: 'staging' },
});
const deleted = await (await api.withAbility('delete')).delete<Message>(`projects/${project.uuid}/environments/${created.body.uuid}`);
expect(deleted.status).toBe(200);
expect(deleted.body.message).toBe('Environment deleted.');
const list = await (await api.root()).get<EnvironmentSummary[]>(`projects/${project.uuid}/environments`);
expect(list.body.map((environment) => environment.name)).toEqual(['production']);
});
test('a read token cannot add, rename, or delete an environment', async ({ api, newProject }) => {
const project = await newProject('env-read-only');
const read = await api.withAbility('read');
expect((await read.post<Message>(`projects/${project.uuid}/environments`, { data: { name: 'staging' } })).status).toBe(403);
expect((await read.patch<Message>(`projects/${project.uuid}/environments/production`, { data: { name: 'live' } })).status).toBe(403);
expect((await read.delete<Message>(`projects/${project.uuid}/environments/production`)).status).toBe(403);
const list = await (await api.root()).get<EnvironmentSummary[]>(`projects/${project.uuid}/environments`);
expect(list.body.map((environment) => environment.name)).toEqual(['production']);
});
test('another team cannot see, add, change, or delete the environments', async ({ api, suite, newProject }) => {
const project = await newProject('env-other-team');
const otherTeam = await api.as(suite.tokens.otherTeamRoot);
expect((await otherTeam.get(`projects/${project.uuid}/environments`)).status).toBe(404);
expect((await otherTeam.get(`projects/${project.uuid}/production`)).status).toBe(404);
expect((await otherTeam.post(`projects/${project.uuid}/environments`, { data: { name: 'staging' } })).status).toBe(404);
expect((await otherTeam.patch(`projects/${project.uuid}/environments/production`, { data: { name: 'live' } })).status).toBe(404);
expect((await otherTeam.delete(`projects/${project.uuid}/environments/production`)).status).toBe(404);
const list = await (await api.root()).get<EnvironmentSummary[]>(`projects/${project.uuid}/environments`);
expect(list.body.map((environment) => environment.name)).toEqual(['production']);
});
test('rejects an invalid environment name with the fields that failed', async ({ api, newProject }) => {
const project = await newProject('env-invalid');
const write = await api.withAbility('write');
const created = await write.post<ValidationError>(`projects/${project.uuid}/environments`, { data: { name: 'x', color: 'red' } });
expect(created.status).toBe(422);
expect(created.body.message).toBe('Validation failed.');
expect(Object.keys(created.body.errors).sort()).toEqual(['color', 'name']);
const renamed = await write.patch<ValidationError>(`projects/${project.uuid}/environments/production`, { data: { name: 'live;rm' } });
expect(renamed.status).toBe(422);
expect(Object.keys(renamed.body.errors)).toEqual(['name']);
});
});