Skip to content

Commit f13b1c9

Browse files
authored
refactor: improve test isolation for Axios API logic (#13125)
* wip: commit progress on code split-up * wip: commit more progress * wip: finish initial version of class implementation * chore: update all import paths to go through client instance * fix: remove temp comments * refactor: smoooooooosh the API * refactor: update import setup for tests
1 parent 5ddbedd commit f13b1c9

File tree

95 files changed

+1926
-1858
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1926
-1858
lines changed

site/e2e/api.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Page } from "@playwright/test";
22
import { expect } from "@playwright/test";
33
import { formatDuration, intervalToDuration } from "date-fns";
4-
import * as API from "api/api";
4+
import { type DeploymentConfig, API } from "api/api";
55
import type { SerpentOption } from "api/typesGenerated";
66
import { coderPort } from "./constants";
77
import { findSessionToken, randomName } from "./helpers";
@@ -15,6 +15,7 @@ export const setupApiCalls = async (page: Page) => {
1515
} catch {
1616
// If this fails, we have an unauthenticated client.
1717
}
18+
1819
API.setHost(`http://127.0.0.1:${coderPort}`);
1920
};
2021

@@ -53,7 +54,7 @@ export const createGroup = async (orgId: string) => {
5354

5455
export async function verifyConfigFlagBoolean(
5556
page: Page,
56-
config: API.DeploymentConfig,
57+
config: DeploymentConfig,
5758
flag: string,
5859
) {
5960
const opt = findConfigOption(config, flag);
@@ -68,7 +69,7 @@ export async function verifyConfigFlagBoolean(
6869

6970
export async function verifyConfigFlagNumber(
7071
page: Page,
71-
config: API.DeploymentConfig,
72+
config: DeploymentConfig,
7273
flag: string,
7374
) {
7475
const opt = findConfigOption(config, flag);
@@ -80,7 +81,7 @@ export async function verifyConfigFlagNumber(
8081

8182
export async function verifyConfigFlagString(
8283
page: Page,
83-
config: API.DeploymentConfig,
84+
config: DeploymentConfig,
8485
flag: string,
8586
) {
8687
const opt = findConfigOption(config, flag);
@@ -100,7 +101,7 @@ export async function verifyConfigFlagEmpty(page: Page, flag: string) {
100101

101102
export async function verifyConfigFlagArray(
102103
page: Page,
103-
config: API.DeploymentConfig,
104+
config: DeploymentConfig,
104105
flag: string,
105106
) {
106107
const opt = findConfigOption(config, flag);
@@ -116,7 +117,7 @@ export async function verifyConfigFlagArray(
116117

117118
export async function verifyConfigFlagEntries(
118119
page: Page,
119-
config: API.DeploymentConfig,
120+
config: DeploymentConfig,
120121
flag: string,
121122
) {
122123
const opt = findConfigOption(config, flag);
@@ -138,7 +139,7 @@ export async function verifyConfigFlagEntries(
138139

139140
export async function verifyConfigFlagDuration(
140141
page: Page,
141-
config: API.DeploymentConfig,
142+
config: DeploymentConfig,
142143
flag: string,
143144
) {
144145
const opt = findConfigOption(config, flag);
@@ -157,7 +158,7 @@ export async function verifyConfigFlagDuration(
157158
}
158159

159160
export function findConfigOption(
160-
config: API.DeploymentConfig,
161+
config: DeploymentConfig,
161162
flag: string,
162163
): SerpentOption {
163164
const opt = config.options.find((option) => option.flag === flag);

site/e2e/global.setup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from "@playwright/test";
2-
import { hasFirstUser } from "api/api";
2+
import { API } from "api/api";
33
import { Language } from "pages/CreateUserPage/CreateUserForm";
44
import { setupApiCalls } from "./api";
55
import * as constants from "./constants";
@@ -9,7 +9,7 @@ import { storageState } from "./playwright.config";
99
test("setup deployment", async ({ page }) => {
1010
await page.goto("/", { waitUntil: "domcontentloaded" });
1111
await setupApiCalls(page);
12-
const exists = await hasFirstUser();
12+
const exists = await API.hasFirstUser();
1313
// First user already exists, abort early. All tests execute this as a dependency,
1414
// if you run multiple tests in the UI, this will fail unless we check this.
1515
if (exists) {

site/e2e/helpers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import capitalize from "lodash/capitalize";
66
import path from "path";
77
import * as ssh from "ssh2";
88
import { Duplex } from "stream";
9-
import { axiosInstance } from "api/api";
9+
import { API } from "api/api";
1010
import type {
1111
WorkspaceBuildParameter,
1212
UpdateTemplateMeta,
@@ -423,6 +423,7 @@ export const waitUntilUrlIsNotResponding = async (url: string) => {
423423
const retryIntervalMs = 1000;
424424
let retries = 0;
425425

426+
const axiosInstance = API.getAxiosInstance();
426427
while (retries < maxRetries) {
427428
try {
428429
await axiosInstance.get(url);

site/e2e/reporter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from "@playwright/test/reporter";
1111
import * as fs from "fs/promises";
1212
import type { Writable } from "stream";
13-
import { axiosInstance } from "api/api";
13+
import { API } from "api/api";
1414
import { coderdPProfPort, enterpriseLicense } from "./constants";
1515

1616
class CoderReporter implements Reporter {
@@ -143,6 +143,7 @@ const logLines = (chunk: string | Buffer): string[] => {
143143
};
144144

145145
const exportDebugPprof = async (outputFile: string) => {
146+
const axiosInstance = API.getAxiosInstance();
146147
const response = await axiosInstance.get(
147148
`http://127.0.0.1:${coderdPProfPort}/debug/pprof/goroutine?debug=1`,
148149
);

site/e2e/tests/deployment/general.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from "@playwright/test";
2-
import * as API from "api/api";
2+
import { API } from "api/api";
33
import { setupApiCalls } from "../../api";
44
import { e2eFakeExperiment1, e2eFakeExperiment2 } from "../../constants";
55

site/e2e/tests/deployment/network.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from "@playwright/test";
2-
import { getDeploymentConfig } from "api/api";
2+
import { API } from "api/api";
33
import {
44
setupApiCalls,
55
verifyConfigFlagArray,
@@ -11,7 +11,7 @@ import {
1111

1212
test("enabled network settings", async ({ page }) => {
1313
await setupApiCalls(page);
14-
const config = await getDeploymentConfig();
14+
const config = await API.getDeploymentConfig();
1515

1616
await page.goto("/deployment/network", { waitUntil: "domcontentloaded" });
1717

site/e2e/tests/deployment/observability.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from "@playwright/test";
2-
import { getDeploymentConfig } from "api/api";
2+
import { API } from "api/api";
33
import {
44
setupApiCalls,
55
verifyConfigFlagArray,
@@ -11,7 +11,7 @@ import {
1111

1212
test("enabled observability settings", async ({ page }) => {
1313
await setupApiCalls(page);
14-
const config = await getDeploymentConfig();
14+
const config = await API.getDeploymentConfig();
1515

1616
await page.goto("/deployment/observability", {
1717
waitUntil: "domcontentloaded",

site/e2e/tests/deployment/security.spec.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { Page } from "@playwright/test";
22
import { expect, test } from "@playwright/test";
3-
import type * as API from "api/api";
4-
import { getDeploymentConfig } from "api/api";
3+
import { type DeploymentConfig, API } from "api/api";
54
import {
65
findConfigOption,
76
setupApiCalls,
@@ -12,7 +11,7 @@ import {
1211

1312
test("enabled security settings", async ({ page }) => {
1413
await setupApiCalls(page);
15-
const config = await getDeploymentConfig();
14+
const config = await API.getDeploymentConfig();
1615

1716
await page.goto("/deployment/security", { waitUntil: "domcontentloaded" });
1817

@@ -31,7 +30,7 @@ test("enabled security settings", async ({ page }) => {
3130

3231
async function verifyStrictTransportSecurity(
3332
page: Page,
34-
config: API.DeploymentConfig,
33+
config: DeploymentConfig,
3534
) {
3635
const flag = "strict-transport-security";
3736
const opt = findConfigOption(config, flag);

site/e2e/tests/deployment/userAuth.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from "@playwright/test";
2-
import { getDeploymentConfig } from "api/api";
2+
import { API } from "api/api";
33
import {
44
setupApiCalls,
55
verifyConfigFlagArray,
@@ -10,7 +10,7 @@ import {
1010

1111
test("login with OIDC", async ({ page }) => {
1212
await setupApiCalls(page);
13-
const config = await getDeploymentConfig();
13+
const config = await API.getDeploymentConfig();
1414

1515
await page.goto("/deployment/userauth", { waitUntil: "domcontentloaded" });
1616

site/e2e/tests/deployment/workspaceProxies.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect, type Page } from "@playwright/test";
2-
import { createWorkspaceProxy } from "api/api";
2+
import { API } from "api/api";
33
import { setupApiCalls } from "../../api";
44
import { coderPort, workspaceProxyPort } from "../../constants";
55
import { randomName, requiresEnterpriseLicense } from "../../helpers";
@@ -34,7 +34,7 @@ test("custom proxy is online", async ({ page }) => {
3434
const proxyName = randomName();
3535

3636
// Register workspace proxy
37-
const proxyResponse = await createWorkspaceProxy({
37+
const proxyResponse = await API.createWorkspaceProxy({
3838
name: proxyName,
3939
display_name: "",
4040
icon: "/emojis/1f1e7-1f1f7.png",

site/e2e/tests/groups/removeMember.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from "@playwright/test";
2-
import * as API from "api/api";
2+
import { API } from "api/api";
33
import {
44
createGroup,
55
createUser,

site/e2e/tests/templates/updateTemplateSchedule.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from "@playwright/test";
2-
import { createTemplate, createTemplateVersion, getTemplate } from "api/api";
2+
import { API } from "api/api";
33
import { getCurrentOrgId, setupApiCalls } from "../../api";
44
import { beforeCoderTest } from "../../hooks";
55

@@ -11,14 +11,14 @@ test("update template schedule settings without override other settings", async
1111
}) => {
1212
await setupApiCalls(page);
1313
const orgId = await getCurrentOrgId();
14-
const templateVersion = await createTemplateVersion(orgId, {
14+
const templateVersion = await API.createTemplateVersion(orgId, {
1515
storage_method: "file" as const,
1616
provisioner: "echo",
1717
user_variable_values: [],
1818
example_id: "docker",
1919
tags: {},
2020
});
21-
const template = await createTemplate(orgId, {
21+
const template = await API.createTemplate(orgId, {
2222
name: "test-template",
2323
display_name: "Test Template",
2424
template_version_id: templateVersion.id,
@@ -33,7 +33,7 @@ test("update template schedule settings without override other settings", async
3333
await page.getByRole("button", { name: "Submit" }).click();
3434
await expect(page.getByText("Template updated successfully")).toBeVisible();
3535

36-
const updatedTemplate = await getTemplate(template.id);
36+
const updatedTemplate = await API.getTemplate(template.id);
3737
// Validate that the template data remains consistent, with the exception of
3838
// the 'default_ttl_ms' field (updated during the test) and the 'updated at'
3939
// field (automatically updated by the backend).

0 commit comments

Comments
 (0)