pull/241/head
Garrett Delfosse 1 year ago
parent 36fa871e7b
commit aced7547bc

@ -1,5 +1,14 @@
import { describe, expect, it } from "bun:test"; import { describe, expect, it } from "bun:test";
import { createJSONResponse, execContainer, findResourceInstance, runContainer, runTerraformApply, runTerraformInit, testRequiredVariables, writeCoder } from "../test"; import {
createJSONResponse,
execContainer,
findResourceInstance,
runContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
writeCoder,
} from "../test";
import { Server, serve } from "bun"; import { Server, serve } from "bun";
describe("github-upload-public-key", async () => { describe("github-upload-public-key", async () => {
@ -13,7 +22,7 @@ describe("github-upload-public-key", async () => {
const { instance, id } = await setupContainer(); const { instance, id } = await setupContainer();
await writeCoder(id, "echo foo"); await writeCoder(id, "echo foo");
let exec = await execContainer(id, ["bash", "-c", instance.script]); let exec = await execContainer(id, ["bash", "-c", instance.script]);
expect(exec.stdout).toContain("Coder public SSH key uploaded to GitHub!") expect(exec.stdout).toContain("Coder public SSH key uploaded to GitHub!");
expect(exec.exitCode).toBe(0); expect(exec.exitCode).toBe(0);
}); });
@ -21,7 +30,9 @@ describe("github-upload-public-key", async () => {
const { instance, id } = await setupContainer(); const { instance, id } = await setupContainer();
await writeCoder(id, "echo findkey"); await writeCoder(id, "echo findkey");
let exec = await execContainer(id, ["bash", "-c", instance.script]); let exec = await execContainer(id, ["bash", "-c", instance.script]);
expect(exec.stdout).toContain("Coder public SSH key is already uploaded to GitHub!") expect(exec.stdout).toContain(
"Coder public SSH key is already uploaded to GitHub!",
);
expect(exec.exitCode).toBe(0); expect(exec.exitCode).toBe(0);
}); });
}); });
@ -29,7 +40,7 @@ describe("github-upload-public-key", async () => {
const setupContainer = async ( const setupContainer = async (
image = "lorello/alpine-bash", image = "lorello/alpine-bash",
vars: Record<string, string> = {}, vars: Record<string, string> = {},
) => { ) => {
const server = await setupServer(); const server = await setupServer();
const state = await runTerraformApply(import.meta.dir, { const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo", agent_id: "foo",
@ -57,33 +68,43 @@ const setupServer = async (): Promise<Server> => {
if (url.pathname === "/user/keys") { if (url.pathname === "/user/keys") {
if (req.method === "POST") { if (req.method === "POST") {
return createJSONResponse({ return createJSONResponse(
{
key: "created", key: "created",
}, 201); },
201,
);
} }
// case: key already exists // case: key already exists
if (req.headers.get("Authorization") == "Bearer findkey") { if (req.headers.get("Authorization") == "Bearer findkey") {
return createJSONResponse([{ return createJSONResponse([
{
key: "foo", key: "foo",
}, { },
{
key: "exists", key: "exists",
}]); },
]);
} }
// case: key does not exist // case: key does not exist
return createJSONResponse([{ return createJSONResponse([
{
key: "foo", key: "foo",
}]); },
]);
} }
return createJSONResponse(
return createJSONResponse({ {
error: "not_found" error: "not_found",
}, 404); },
404,
);
}, },
port: 0, port: 0,
}); });
return fakeSlackHost; return fakeSlackHost;
} };

Loading…
Cancel
Save