Skip to content

Modern Docker Management, Designed for Everyone

21.3 KB
Raw Blame
import { setCodeMirrorValue, waitForDialogReady } from '../utils/playwright.util';
import { expect, test, type Page } from '../fixtures/test.fixture';
import { removeApiResource, readApiData } from '../utils/fetch.util';
import { openRowActionsMenu } from '../utils/table-actions.util';

test.describe('Templates', () => {
	type GlobalVariable = {
		id: string;
		key: string;
		value: string;
		isSecret: boolean;
		allEnvironments: boolean;
		environmentIds: string[];
	};

	type VariableMutation = {
		variable?: GlobalVariable;
	};

	type Template = {
		id: string;
		name: string;
		description: string;
	};

	type Project = {
		id: string;
		name: string;
		status?: string;
		runtimeServices?: Array<{ containerId?: string; containerName?: string }>;
	};

	type ContainerDetails = {
		id: string;
		config: { env?: string[] };
	};

	async function listVariables(page: Page): Promise<GlobalVariable[]> {
		return readApiData<GlobalVariable[]>(
			await page.request.get('/api/variables'),
			'List global variables'
		);
	}

	function variableRow(page: Page, key: string) {
		return page.getByRole('row').filter({ has: page.getByText(key, { exact: true }) });
	}

	async function openVariableSheet(page: Page) {
		await page.getByRole('button', { name: 'Add Variable', exact: true }).click();
		const dialog = page.getByRole('dialog', { name: 'Create Variable' });
		await waitForDialogReady(dialog);
		return dialog;
	}

	async function createSingleVariable(
		page: Page,
		key: string,
		value: string,
		isSecret = false
	): Promise<GlobalVariable> {
		const dialog = await openVariableSheet(page);
		await dialog.locator('#variable-key').fill(key);
		await dialog.locator('#variable-value').fill(value);
		if (isSecret) await dialog.locator('#variable-secret').click();

		const responsePromise = page.waitForResponse(
			(response) =>
				response.request().method() === 'POST' &&
				new URL(response.url()).pathname === '/api/variables'
		);
		await dialog.getByRole('button', { name: 'Add Variable', exact: true }).click();
		const result = await readApiData<VariableMutation>(
			await responsePromise,
			`Create global variable ${key}`
		);
		expect(result.variable).toBeDefined();
		await expect(dialog).toBeHidden();
		return result.variable!;
	}

	async function getProject(page: Page, projectId: string): Promise<Project> {
		return readApiData<Project>(
			await page.request.get(`/api/environments/0/projects/${projectId}`),
			`Get project ${projectId}`
		);
	}

	test('manages variables and deploys an edited template with real substitution', async ({
		page
	}) => {
		test.setTimeout(240_000);
		page.setDefaultTimeout(15_000);

		const suffix = Date.now().toString(36).toUpperCase();
		const publicKey = `E2E_TEMPLATE_VALUE_${suffix}`;
		const publicValue = `resolved-${suffix.toLowerCase()}`;
		const secretKey = `E2E_TEMPLATE_SECRET_${suffix}`;
		const secretValue = `secret-${suffix.toLowerCase()}`;
		const bulkKeyOne = `E2E_BULK_ONE_${suffix}`;
		const bulkKeyTwo = `E2E_BULK_TWO_${suffix}`;
		const templateName = `E2E Variable Template ${suffix}`;
		const updatedTemplateName = `${templateName} Updated`;
		const projectName = `e2e-variable-project-${suffix.toLowerCase()}`;
		const containerName = `e2e-variable-container-${suffix.toLowerCase()}`;
		let templateId: string | null = null;
		let projectId: string | null = null;

		const composeContent = [
			'services:',
			'  verifier:',
			'    image: public.ecr.aws/docker/library/busybox:1.37',
			`    container_name: ${containerName}`,
			'    command:',
			'      - sh',
			'      - -c',
			'      - test "$' + publicKey + '" = "' + publicValue + '" && sleep 3600',
			'    environment:',
			`      ${publicKey}: \${${publicKey}}`,
			''
		].join('\n');

		try {
			const localEnvironment = await readApiData<{ id: string; name: string }>(
				await page.request.get('/api/environments/0'),
				'Get local environment for variable scope'
			);

			await page.goto('/customize/variables');
			const publicVariable = await createSingleVariable(page, publicKey, publicValue);
			expect(publicVariable).toMatchObject({
				key: publicKey,
				value: publicValue,
				isSecret: false,
				allEnvironments: true
			});
			await expect(variableRow(page, publicKey)).toContainText(publicValue);

			const secretVariable = await createSingleVariable(page, secretKey, secretValue, true);
			expect(secretVariable).toMatchObject({ key: secretKey, value: '', isSecret: true });
			const secretRow = variableRow(page, secretKey);
			await expect(secretRow).toContainText('••••••••');
			await expect(secretRow).not.toContainText(secretValue);

			const bulkDialog = await openVariableSheet(page);
			await bulkDialog.getByRole('button', { name: 'Paste .env', exact: true }).click();
			await bulkDialog
				.getByLabel('Paste .env content', { exact: true })
				.fill(`${bulkKeyOne}=first\n${bulkKeyTwo}=second`);
			await bulkDialog.getByRole('radio', { name: /Specific Environments/ }).click();
			const environmentOption = bulkDialog
				.locator('label')
				.filter({ hasText: localEnvironment.name });
			await environmentOption.getByRole('switch').click();
			await bulkDialog.getByRole('button', { name: 'Add Variable', exact: true }).click();
			await expect(bulkDialog).toBeHidden();
			await expect(variableRow(page, bulkKeyOne)).toBeVisible();
			await expect(variableRow(page, bulkKeyTwo)).toBeVisible();

			let variables = await listVariables(page);
			for (const key of [bulkKeyOne, bulkKeyTwo]) {
				const variable = variables.find((candidate) => candidate.key === key);
				expect(variable).toMatchObject({
					allEnvironments: false,
					environmentIds: ['0']
				});
			}

			const editableRow = variableRow(page, bulkKeyOne);
			const editMenu = await openRowActionsMenu(page, editableRow);
			await editMenu.getByRole('menuitem', { name: 'Edit', exact: true }).click();
			const editDialog = page.getByRole('dialog', { name: 'Edit Variable' });
			await waitForDialogReady(editDialog);
			await editDialog.locator('#variable-value').fill('first-edited');
			const editResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'PUT' &&
					/^\/api\/variables\/[^/]+$/.test(new URL(response.url()).pathname)
			);
			await editDialog.getByRole('button', { name: 'Save Changes', exact: true }).click();
			expect((await editResponsePromise).ok()).toBe(true);
			await expect(editableRow).toContainText('first-edited');

			const secretMenu = await openRowActionsMenu(page, secretRow);
			await secretMenu.getByRole('menuitem', { name: 'Delete', exact: true }).click();
			const secretDeleteResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'DELETE' &&
					new URL(response.url()).pathname === `/api/variables/${secretVariable.id}`
			);
			await page.getByRole('dialog').getByRole('button', { name: 'Delete', exact: true }).click();
			expect((await secretDeleteResponsePromise).ok()).toBe(true);
			await expect(secretRow).toHaveCount(0);

			const syncResults = await readApiData<
				Array<{ environmentId: string; status: 'synced' | 'pending' | 'error'; error?: string }>
			>(await page.request.post('/api/variables/sync'), 'Synchronize global variables');
			expect(syncResults).toEqual(
				expect.arrayContaining([expect.objectContaining({ environmentId: '0', status: 'synced' })])
			);

			await page.goto('/customize/templates/create');
			await page.getByRole('button', { name: 'e.g. My Nginx Project', exact: true }).click();
			const templateNameInput = page
				.getByPlaceholder('e.g. My Nginx Project', { exact: true })
				.filter({ visible: true });
			await templateNameInput.fill(templateName);
			await templateNameInput.press('Enter');
			await page
				.getByLabel('Description', { exact: true })
				.fill('Created by the customization browser journey');

			const editors = page.locator('.cm-editor').filter({ visible: true });
			const composeEditor = editors.first();
			const envEditor = editors.nth(1);
			await setCodeMirrorValue(composeEditor, 'services:\n  broken: [\n');
			await setCodeMirrorValue(envEditor, 'NOT VALID ENV');
			await expect(
				page.getByRole('button', { name: 'Create Template', exact: true })
			).toBeDisabled();

			await setCodeMirrorValue(composeEditor, composeContent);
			await setCodeMirrorValue(envEditor, 'TEMPLATE_LOCAL=value\n');
			const createTemplateButton = page.getByRole('button', {
				name: 'Create Template',
				exact: true
			});
			await expect(createTemplateButton).toBeEnabled({ timeout: 20_000 });
			const templateCreateResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'POST' &&
					new URL(response.url()).pathname === '/api/templates'
			);
			await createTemplateButton.click();
			const template = await readApiData<Template>(
				await templateCreateResponsePromise,
				`Create template ${templateName}`
			);
			templateId = template.id;
			await expect(page).toHaveURL(`/customize/templates/${template.id}`);

			await page.getByRole('button', { name: templateName, exact: true }).click();
			const editTemplateNameInput = page
				.getByPlaceholder('e.g. My Nginx Project', { exact: true })
				.filter({ visible: true });
			await editTemplateNameInput.fill(updatedTemplateName);
			await editTemplateNameInput.press('Enter');
			await page.getByLabel('Description', { exact: true }).fill('Updated customization template');
			const templateUpdateResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'PUT' &&
					new URL(response.url()).pathname === `/api/templates/${template.id}`
			);
			await page.getByRole('button', { name: 'Save', exact: true }).click();
			const updatedTemplate = await readApiData<Template>(
				await templateUpdateResponsePromise,
				`Update template ${template.id}`
			);
			expect(updatedTemplate).toMatchObject({
				name: updatedTemplateName,
				description: 'Updated customization template'
			});

			await page.getByRole('button', { name: 'Open menu', exact: true }).click();
			await page.getByRole('menuitem', { name: 'Create Project', exact: true }).click();
			await expect(page).toHaveURL(`/projects/new?templateId=${template.id}`);
			const projectNameButton = page.getByRole('button', {
				name: updatedTemplateName
					.toLowerCase()
					.replace(/[^a-z0-9]+/g, '-')
					.replace(/^-|-$/g, ''),
				exact: true
			});
			await projectNameButton.click();
			const projectNameInput = page
				.getByPlaceholder('My New Project', { exact: true })
				.filter({ visible: true });
			await projectNameInput.fill(projectName);
			await projectNameInput.press('Enter');
			await expect(page.locator('.cm-editor').filter({ visible: true }).first()).toContainText(
				publicKey
			);

			const createProjectButton = page.locator('button[data-action="create"]');
			await expect(createProjectButton).toBeEnabled({ timeout: 20_000 });
			const projectCreateResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'POST' &&
					new URL(response.url()).pathname === '/api/environments/0/projects'
			);
			await createProjectButton.click();
			const project = await readApiData<Project>(
				await projectCreateResponsePromise,
				`Create project from template ${template.id}`
			);
			projectId = project.id;
			await expect(page).toHaveURL((url) => url.pathname === `/projects/${project.id}`);

			const deployResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'POST' &&
					new URL(response.url()).pathname === `/api/environments/0/projects/${project.id}/up`,
				{ timeout: 30_000 }
			);
			await page
				.getByRole('button', { name: 'Up', exact: true })
				.filter({ visible: true })
				.first()
				.click();
			expect((await deployResponsePromise).ok()).toBe(true);
			await expect
				.poll(async () => (await getProject(page, project.id)).status, { timeout: 60_000 })
				.toBe('running');

			let containerId = '';
			await expect
				.poll(
					async () => {
						const runtime = await readApiData<Project>(
							await page.request.get(`/api/environments/0/projects/${project.id}/runtime`),
							`Get runtime services for ${project.id}`
						);
						containerId =
							runtime.runtimeServices?.find((service) => service.containerName === containerName)
								?.containerId ?? '';
						return containerId;
					},
					{ timeout: 30_000 }
				)
				.not.toBe('');
			const container = await readApiData<ContainerDetails>(
				await page.request.get(`/api/environments/0/containers/${containerId}`),
				`Inspect substituted container ${containerId}`
			);
			expect(container.config.env).toContain(`${publicKey}=${publicValue}`);

			await removeApiResource(page, `/api/environments/0/projects/${project.id}/destroy`, {
				data: { removeVolumes: false }
			});
			projectId = null;

			await page.goto(`/customize/templates/${template.id}`);
			await page.getByRole('button', { name: 'Open menu', exact: true }).click();
			await page.getByRole('menuitem', { name: 'Delete Template', exact: true }).click();
			const templateDeleteResponsePromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'DELETE' &&
					new URL(response.url()).pathname === `/api/templates/${template.id}`
			);
			await page
				.getByRole('dialog')
				.getByRole('button', { name: 'Delete Template', exact: true })
				.click();
			expect((await templateDeleteResponsePromise).ok()).toBe(true);
			await expect(page).toHaveURL((url) => url.pathname === '/customize/templates');
			templateId = null;
		} finally {
			if (projectId) {
				await removeApiResource(page, `/api/environments/0/projects/${projectId}/destroy`, {
					data: { removeVolumes: false }
				});
			}
			if (templateId) {
				await removeApiResource(page, `/api/templates/${templateId}`);
			}
			try {
				const variables = await listVariables(page);
				for (const variable of variables.filter((candidate) => candidate.key.endsWith(suffix))) {
					await removeApiResource(page, `/api/variables/${variable.id}`);
				}
			} catch (error) {
				expect.soft(false, `Clean up variables: ${String(error)}`).toBe(true);
			}
		}
	});
});

test.describe('Registries', () => {
	const route = '/customize/registries';
	const TOKEN = process.env.REGISTRY_TEST_TOKEN ?? 'e2e-token';
	const pullUsageRoute = /\/api\/container-registries\/pull-usage(?:\?|$)/;
	const registryListRoute = /\/api\/container-registries(?:\?|$)/;

	async function mockRegistryList(page: Page, registryId = 'dockerhub-registry') {
		await page.context().route(registryListRoute, async (route) => {
			if (route.request().method() !== 'GET') {
				await route.fallback();
				return;
			}

			await route.fulfill({
				status: 200,
				contentType: 'application/json',
				body: JSON.stringify({
					success: true,
					data: [
						{
							id: registryId,
							url: 'docker.io',
							username: 'docker-user',
							description: 'Docker Hub test registry',
							insecure: false,
							enabled: true,
							registryType: 'generic',
							createdAt: new Date().toISOString(),
							updatedAt: new Date().toISOString()
						}
					],
					pagination: {
						totalPages: 1,
						totalItems: 1,
						currentPage: 1,
						itemsPerPage: 20,
						grandTotalItems: 1
					}
				})
			});
		});
	}

	async function mockRegistryPullUsage(
		page: Page,
		options: { registryId?: string; remaining?: number; limit?: number; observedPulls?: number }
	) {
		const registryId = options.registryId ?? 'dockerhub-registry';
		await page.context().route(pullUsageRoute, async (route) => {
			await route.fulfill({
				status: 200,
				contentType: 'application/json',
				body: JSON.stringify({
					success: true,
					data: {
						registries: [
							{
								registryId,
								provider: 'dockerhub',
								registry: 'docker.io',
								displayName: 'Docker Hub',
								repository: 'ratelimitpreview/test',
								...(options.limit === undefined ? {} : { limit: options.limit }),
								...(options.remaining === undefined ? {} : { remaining: options.remaining }),
								observedPulls: options.observedPulls ?? 0,
								authMethod: 'credential',
								authUsername: 'docker-user',
								checkedAt: new Date().toISOString()
							}
						]
					}
				})
			});
		});
	}

	test.describe('Container Registries', () => {
		const registryIds = new Set<string>();
		test.afterEach(async ({ page }) => {
			for (const id of registryIds)
				await removeApiResource(page, `/api/container-registries/${id}`);
			registryIds.clear();
		});
		test.beforeEach(async ({ page }) => {
			await page.goto(route);
			await page.waitForLoadState('load');
		});

		test('should display title and subtitle, and refresh', async ({ page }) => {
			await expect(
				page.getByRole('heading', { name: 'Container Registries', level: 1 })
			).toBeVisible();
			await expect(
				page
					.getByText(
						'Configure access credentials for private Docker registries and container repositories'
					)
					.first()
			).toBeVisible();

			await page.getByRole('button', { name: 'Refresh' }).click();
			await expect(
				page
					.getByRole('region', { name: 'Notifications alt+T', exact: true })
					.getByRole('listitem')
					.filter({ hasText: 'Registries Refreshed!' })
			).toBeVisible({ timeout: 10000 });
		});

		test('should open Add Registry dialog and validate required fields', async ({ page }) => {
			await page.getByRole('button', { name: 'Add Registry' }).first().click();

			const dialog = page.getByRole('dialog');
			await expect(dialog).toBeVisible();

			// Submit without URL -> expect validation error
			await dialog.getByRole('button', { name: 'Add Registry', exact: true }).click();
			await expect(dialog.getByText('Registry URL is required', { exact: true })).toBeVisible();

			// Close dialog
			await page.keyboard.press('Escape');
			await expect(dialog).toBeHidden();
		});

		test('should display registry pull limit usage when headers are available', async ({
			page
		}) => {
			await mockRegistryList(page);
			await mockRegistryPullUsage(page, { remaining: 76, limit: 100, observedPulls: 4 });
			await page.goto(route);
			await page.waitForLoadState('load');

			const table = page.getByRole('table');
			await expect(table.getByText('Pull Usage')).toBeVisible();
			await expect(table.getByText('76/100')).toBeVisible();
		});

		test('should display observed pull count when registry limit headers are unavailable', async ({
			page
		}) => {
			await mockRegistryList(page);
			await mockRegistryPullUsage(page, { observedPulls: 4 });
			await page.goto(route);
			await page.waitForLoadState('load');

			const table = page.getByRole('table');
			await expect(table.getByText('Pull Usage')).toBeVisible();
			await expect(table.getByText('4 pulls')).toBeVisible();
		});

		test('should create, test, edit, and delete a registry', async ({ page }) => {
			// Create
			await page.getByRole('button', { name: 'Add Registry' }).first().click();
			const dialog = page.getByRole('dialog');
			await waitForDialogReady(dialog);

			const url = `e2e.example.com-${Date.now()}`;

			// URL
			await dialog.getByLabel('Registry URL', { exact: true }).fill(url);
			// Username (frontend requires it)
			await dialog.getByLabel('Username', { exact: true }).fill('e2e');
			// Token (backend needs it to avoid 400)
			const tokenInput = dialog.getByLabel('Token', { exact: true });
			await tokenInput.fill(TOKEN);

			// Optional description
			const desc = dialog.getByLabel('Description', { exact: true });
			await desc.fill('E2E test registry');

			const createdPromise = page.waitForResponse(
				(response) =>
					response.request().method() === 'POST' &&
					new URL(response.url()).pathname === '/api/container-registries'
			);
			await dialog.getByRole('button', { name: 'Add Registry', exact: true }).click();

			const created = await readApiData<{ id: string }>(await createdPromise, 'Create registry');
			registryIds.add(created.id);

			// Creation complete when dialog closes
			await expect(dialog).toBeHidden({ timeout: 10000 });

			// Row should appear
			const row = page
				.getByRole('row')
				.filter({ has: page.getByText(url, { exact: true }) })
				.first();
			await expect(row).toBeVisible();

			// Test Connection (accept either success or failure toast)
			const menu = await openRowActionsMenu(page, row);
			await menu.getByRole('menuitem', { name: 'Test Connection' }).click();
		});

		test('should open Remove Selected dialog and cancel (no mutation)', async ({ page }) => {
			const registry = await readApiData<{ id: string }>(
				await page.request.post('/api/container-registries', {
					data: {
						url: `e2e-select-${Date.now()}.example.com`,
						username: 'e2e',
						token: TOKEN,
						description: '',
						insecure: false,
						enabled: true,
						registryType: 'generic',
						repositoryNames: [],
						awsAccessKeyId: '',
						awsSecretAccessKey: '',
						awsRegion: ''
					}
				}),
				'Create registry selection fixture'
			);
			registryIds.add(registry.id);
			await page.goto(route);
			const firstRowCheckbox = page
				.getByRole('row')
				.filter({ has: page.getByRole('button', { name: 'Open menu', exact: true }) })
				.getByRole('checkbox')
				.first();
			await firstRowCheckbox.check();
			await page.getByRole('button', { name: 'Remove Selected (1)', exact: true }).click();
			const confirm = page.getByRole('dialog');
			await expect(
				confirm.getByRole('heading', { name: 'Remove 1 Registry(ies)', exact: true })
			).toBeVisible();
			await confirm.getByRole('button', { name: 'Cancel', exact: true }).click();
			await expect(confirm).toBeHidden();
		});
	});
});