Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all 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
56 changes: 28 additions & 28 deletions site/e2e/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import http from "node:http";
import type { BrowserContext, Page } from "@playwright/test";
import { coderPort, gitAuth } from "./constants";

Expand Down Expand Up @@ -54,38 +53,39 @@ export const beforeCoderTest = (page: Page) => {
};

export const resetExternalAuthKey = async (context: BrowserContext) => {
// Find the session token so we can destroy the external auth link between tests, to ensure valid authentication happens each time.
// Find the session token so we can destroy the external auth links between
// tests, to ensure valid authentication happens each time.
const cookies = await context.cookies();
const sessionCookie = cookies.find((c) => c.name === "coder_session_token");
const options = {
method: "DELETE",
hostname: "127.0.0.1",
port: coderPort,
path: `/api/v2/external-auth/${gitAuth.webProvider}?coder_session_token=${sessionCookie?.value}`,
};

const req = http.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
// Reset every provider the suite exercises so repeated runs against the same
// coderd instance get a clean slate; otherwise the device-flow link from a
// previous iteration trips the test on its next pass.
const providers = [gitAuth.webProvider, gitAuth.deviceProvider];
await Promise.all(
providers.map((provider) =>
deleteExternalAuthLink(provider, sessionCookie?.value),
),
);
};

res.on("end", () => {
// 200 = link deleted; 404 = no link existed for this provider.
if (res.statusCode !== 200 && res.statusCode !== 404) {
console.error("failed to delete external auth link", data);
throw new Error(
`failed to delete external auth link: HTTP response ${res.statusCode}`,
);
}
});
});
const deleteExternalAuthLink = async (
provider: string,
sessionToken: string | undefined,
): Promise<void> => {
const response = await fetch(
`http://127.0.0.1:${coderPort}/api/v2/external-auth/${provider}?coder_session_token=${sessionToken}`,
{ method: "DELETE" },
);

req.on("error", (err) => {
throw err.message;
});

req.end();
// 200 = link deleted; 404 = no link existed for this provider.
if (response.status !== 200 && response.status !== 404) {
const data = await response.text();
console.error("failed to delete external auth link", data);
throw new Error(
`failed to delete external auth link: HTTP response ${response.status}`,
);
}
};

const isApiCall = (urlString: string): boolean => {
Expand Down
Loading