Skip to content

refactor: improve test isolation for Axios API logic #13125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions site/e2e/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from "@playwright/test";
import { expect } from "@playwright/test";
import { formatDuration, intervalToDuration } from "date-fns";
import * as API from "api/api";
import { type DeploymentConfig, client } from "api/api";
import type { SerpentOption } from "api/typesGenerated";
import { coderPort } from "./constants";
import { findSessionToken, randomName } from "./helpers";
Expand All @@ -11,25 +11,26 @@ let currentOrgId: string;
export const setupApiCalls = async (page: Page) => {
try {
const token = await findSessionToken(page);
API.setSessionToken(token);
client.setSessionToken(token);
} catch {
// If this fails, we have an unauthenticated client.
}
API.setHost(`http://127.0.0.1:${coderPort}`);

client.setHost(`http://127.0.0.1:${coderPort}`);
};

export const getCurrentOrgId = async (): Promise<string> => {
if (currentOrgId) {
return currentOrgId;
}
const currentUser = await API.getAuthenticatedUser();
const currentUser = await client.api.getAuthenticatedUser();
currentOrgId = currentUser.organization_ids[0];
return currentOrgId;
};

export const createUser = async (orgId: string) => {
const name = randomName();
const user = await API.createUser({
const user = await client.api.createUser({
email: `${name}@coder.com`,
username: name,
password: "s3cure&password!",
Expand All @@ -42,7 +43,7 @@ export const createUser = async (orgId: string) => {

export const createGroup = async (orgId: string) => {
const name = randomName();
const group = await API.createGroup(orgId, {
const group = await client.api.createGroup(orgId, {
name,
display_name: `Display ${name}`,
avatar_url: "/emojis/1f60d.png",
Expand All @@ -53,7 +54,7 @@ export const createGroup = async (orgId: string) => {

export async function verifyConfigFlagBoolean(
page: Page,
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
) {
const opt = findConfigOption(config, flag);
Expand All @@ -68,7 +69,7 @@ export async function verifyConfigFlagBoolean(

export async function verifyConfigFlagNumber(
page: Page,
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
) {
const opt = findConfigOption(config, flag);
Expand All @@ -80,7 +81,7 @@ export async function verifyConfigFlagNumber(

export async function verifyConfigFlagString(
page: Page,
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
) {
const opt = findConfigOption(config, flag);
Expand All @@ -100,7 +101,7 @@ export async function verifyConfigFlagEmpty(page: Page, flag: string) {

export async function verifyConfigFlagArray(
page: Page,
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
) {
const opt = findConfigOption(config, flag);
Expand All @@ -116,7 +117,7 @@ export async function verifyConfigFlagArray(

export async function verifyConfigFlagEntries(
page: Page,
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
) {
const opt = findConfigOption(config, flag);
Expand All @@ -138,7 +139,7 @@ export async function verifyConfigFlagEntries(

export async function verifyConfigFlagDuration(
page: Page,
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
) {
const opt = findConfigOption(config, flag);
Expand All @@ -157,7 +158,7 @@ export async function verifyConfigFlagDuration(
}

export function findConfigOption(
config: API.DeploymentConfig,
config: DeploymentConfig,
flag: string,
): SerpentOption {
const opt = config.options.find((option) => option.flag === flag);
Expand Down
4 changes: 2 additions & 2 deletions site/e2e/global.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@playwright/test";
import { hasFirstUser } from "api/api";
import { client } from "api/api";
import { Language } from "pages/CreateUserPage/CreateUserForm";
import { setupApiCalls } from "./api";
import * as constants from "./constants";
Expand All @@ -9,7 +9,7 @@ import { storageState } from "./playwright.config";
test("setup deployment", async ({ page }) => {
await page.goto("/", { waitUntil: "domcontentloaded" });
await setupApiCalls(page);
const exists = await hasFirstUser();
const exists = await client.api.hasFirstUser();
// First user already exists, abort early. All tests execute this as a dependency,
// if you run multiple tests in the UI, this will fail unless we check this.
if (exists) {
Expand Down
3 changes: 2 additions & 1 deletion site/e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import capitalize from "lodash/capitalize";
import path from "path";
import * as ssh from "ssh2";
import { Duplex } from "stream";
import { axiosInstance } from "api/api";
import { client } from "api/api";
import type {
WorkspaceBuildParameter,
UpdateTemplateMeta,
Expand Down Expand Up @@ -396,6 +396,7 @@ export const waitUntilUrlIsNotResponding = async (url: string) => {
const retryIntervalMs = 1000;
let retries = 0;

const axiosInstance = client.getAxiosInstance();
while (retries < maxRetries) {
try {
await axiosInstance.get(url);
Expand Down
3 changes: 2 additions & 1 deletion site/e2e/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
} from "@playwright/test/reporter";
import * as fs from "fs/promises";
import type { Writable } from "stream";
import { axiosInstance } from "api/api";
import { client } from "api/api";
import { coderdPProfPort, enterpriseLicense } from "./constants";

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

const exportDebugPprof = async (outputFile: string) => {
const axiosInstance = client.getAxiosInstance();
const response = await axiosInstance.get(
`http://127.0.0.1:${coderdPProfPort}/debug/pprof/goroutine?debug=1`,
);
Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/deployment/general.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect, test } from "@playwright/test";
import * as API from "api/api";
import { client } from "api/api";
import { setupApiCalls } from "../../api";
import { e2eFakeExperiment1, e2eFakeExperiment2 } from "../../constants";

test("experiments", async ({ page }) => {
await setupApiCalls(page);

// Load experiments from backend API
const availableExperiments = await API.getAvailableExperiments();
const availableExperiments = await client.api.getAvailableExperiments();

// Verify if the site lists the same experiments
await page.goto("/deployment/general", { waitUntil: "networkidle" });
Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/deployment/network.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "@playwright/test";
import { getDeploymentConfig } from "api/api";
import { client } from "api/api";
import {
setupApiCalls,
verifyConfigFlagArray,
Expand All @@ -11,7 +11,7 @@ import {

test("enabled network settings", async ({ page }) => {
await setupApiCalls(page);
const config = await getDeploymentConfig();
const config = await client.api.getDeploymentConfig();

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

Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/deployment/observability.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "@playwright/test";
import { getDeploymentConfig } from "api/api";
import { client } from "api/api";
import {
setupApiCalls,
verifyConfigFlagArray,
Expand All @@ -11,7 +11,7 @@ import {

test("enabled observability settings", async ({ page }) => {
await setupApiCalls(page);
const config = await getDeploymentConfig();
const config = await client.api.getDeploymentConfig();

await page.goto("/deployment/observability", {
waitUntil: "domcontentloaded",
Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/deployment/security.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from "@playwright/test";
import { expect, test } from "@playwright/test";
import type * as API from "api/api";
import { getDeploymentConfig } from "api/api";
import { client } from "api/api";
import {
findConfigOption,
setupApiCalls,
Expand All @@ -12,7 +12,7 @@ import {

test("enabled security settings", async ({ page }) => {
await setupApiCalls(page);
const config = await getDeploymentConfig();
const config = await client.api.getDeploymentConfig();

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

Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/deployment/userAuth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from "@playwright/test";
import { getDeploymentConfig } from "api/api";
import { client } from "api/api";
import {
setupApiCalls,
verifyConfigFlagArray,
Expand All @@ -10,7 +10,7 @@ import {

test("login with OIDC", async ({ page }) => {
await setupApiCalls(page);
const config = await getDeploymentConfig();
const config = await client.api.getDeploymentConfig();

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

Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/deployment/workspaceProxies.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect, type Page } from "@playwright/test";
import { createWorkspaceProxy } from "api/api";
import { client } from "api/api";
import { setupApiCalls } from "../../api";
import { coderPort, workspaceProxyPort } from "../../constants";
import { randomName, requiresEnterpriseLicense } from "../../helpers";
Expand Down Expand Up @@ -34,7 +34,7 @@ test("custom proxy is online", async ({ page }) => {
const proxyName = randomName();

// Register workspace proxy
const proxyResponse = await createWorkspaceProxy({
const proxyResponse = await client.api.createWorkspaceProxy({
name: proxyName,
display_name: "",
icon: "/emojis/1f1e7-1f1f7.png",
Expand Down
4 changes: 2 additions & 2 deletions site/e2e/tests/groups/removeMember.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "@playwright/test";
import * as API from "api/api";
import { client } from "api/api";
import {
createGroup,
createUser,
Expand All @@ -19,7 +19,7 @@ test("remove member", async ({ page, baseURL }) => {
createGroup(orgId),
createUser(orgId),
]);
await API.addMember(group.id, member.id);
await client.api.addMember(group.id, member.id);

await page.goto(`${baseURL}/groups/${group.id}`, {
waitUntil: "domcontentloaded",
Expand Down
8 changes: 4 additions & 4 deletions site/e2e/tests/templates/updateTemplateSchedule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "@playwright/test";
import { createTemplate, createTemplateVersion, getTemplate } from "api/api";
import { client } from "api/api";
import { getCurrentOrgId, setupApiCalls } from "../../api";
import { beforeCoderTest } from "../../hooks";

Expand All @@ -11,14 +11,14 @@ test("update template schedule settings without override other settings", async
}) => {
await setupApiCalls(page);
const orgId = await getCurrentOrgId();
const templateVersion = await createTemplateVersion(orgId, {
const templateVersion = await client.api.createTemplateVersion(orgId, {
storage_method: "file" as const,
provisioner: "echo",
user_variable_values: [],
example_id: "docker",
tags: {},
});
const template = await createTemplate(orgId, {
const template = await client.api.createTemplate(orgId, {
name: "test-template",
display_name: "Test Template",
template_version_id: templateVersion.id,
Expand All @@ -33,7 +33,7 @@ test("update template schedule settings without override other settings", async
await page.getByRole("button", { name: "Submit" }).click();
await expect(page.getByText("Template updated successfully")).toBeVisible();

const updatedTemplate = await getTemplate(template.id);
const updatedTemplate = await client.api.getTemplate(template.id);
// Validate that the template data remains consistent, with the exception of
// the 'default_ttl_ms' field (updated during the test) and the 'updated at'
// field (automatically updated by the backend).
Expand Down
Loading
Loading