|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + createJSONResponse, |
| 4 | + execContainer, |
| 5 | + findResourceInstance, |
| 6 | + runContainer, |
| 7 | + runTerraformApply, |
| 8 | + runTerraformInit, |
| 9 | + testRequiredVariables, |
| 10 | + writeCoder, |
| 11 | +} from "../test"; |
| 12 | +import { Server, serve } from "bun"; |
| 13 | + |
| 14 | +describe("github-upload-public-key", async () => { |
| 15 | + await runTerraformInit(import.meta.dir); |
| 16 | + |
| 17 | + testRequiredVariables(import.meta.dir, { |
| 18 | + agent_id: "foo", |
| 19 | + }); |
| 20 | + |
| 21 | + it("creates new key if one does not exist", async () => { |
| 22 | + const { instance, id, server } = await setupContainer(); |
| 23 | + await writeCoder(id, "echo foo"); |
| 24 | + let exec = await execContainer(id, [ |
| 25 | + "env", |
| 26 | + "CODER_ACCESS_URL=" + server.url.toString().slice(0, -1), |
| 27 | + "GITHUB_API_URL=" + server.url.toString().slice(0, -1), |
| 28 | + "CODER_OWNER_SESSION_TOKEN=foo", |
| 29 | + "CODER_EXTERNAL_AUTH_ID=github", |
| 30 | + "bash", |
| 31 | + "-c", |
| 32 | + instance.script, |
| 33 | + ]); |
| 34 | + expect(exec.stdout).toContain( |
| 35 | + "Your Coder public key has been added to GitHub!", |
| 36 | + ); |
| 37 | + expect(exec.exitCode).toBe(0); |
| 38 | + // we need to increase timeout to pull the container |
| 39 | + }, 15000); |
| 40 | + |
| 41 | + it("does nothing if one already exists", async () => { |
| 42 | + const { instance, id, server } = await setupContainer(); |
| 43 | + // use keyword to make server return a existing key |
| 44 | + await writeCoder(id, "echo findkey"); |
| 45 | + let exec = await execContainer(id, [ |
| 46 | + "env", |
| 47 | + "CODER_ACCESS_URL=" + server.url.toString().slice(0, -1), |
| 48 | + "GITHUB_API_URL=" + server.url.toString().slice(0, -1), |
| 49 | + "CODER_OWNER_SESSION_TOKEN=foo", |
| 50 | + "CODER_EXTERNAL_AUTH_ID=github", |
| 51 | + "bash", |
| 52 | + "-c", |
| 53 | + instance.script, |
| 54 | + ]); |
| 55 | + expect(exec.stdout).toContain( |
| 56 | + "Your Coder public key is already on GitHub!", |
| 57 | + ); |
| 58 | + expect(exec.exitCode).toBe(0); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +const setupContainer = async ( |
| 63 | + image = "lorello/alpine-bash", |
| 64 | + vars: Record<string, string> = {}, |
| 65 | +) => { |
| 66 | + const server = await setupServer(); |
| 67 | + const state = await runTerraformApply(import.meta.dir, { |
| 68 | + agent_id: "foo", |
| 69 | + ...vars, |
| 70 | + }); |
| 71 | + const instance = findResourceInstance(state, "coder_script"); |
| 72 | + const id = await runContainer(image); |
| 73 | + return { id, instance, server }; |
| 74 | +}; |
| 75 | + |
| 76 | +const setupServer = async (): Promise<Server> => { |
| 77 | + let url: URL; |
| 78 | + const fakeSlackHost = serve({ |
| 79 | + fetch: (req) => { |
| 80 | + url = new URL(req.url); |
| 81 | + if (url.pathname === "/api/v2/users/me/gitsshkey") { |
| 82 | + return createJSONResponse({ |
| 83 | + public_key: "exists", |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + if (url.pathname === "/user/keys") { |
| 88 | + if (req.method === "POST") { |
| 89 | + return createJSONResponse( |
| 90 | + { |
| 91 | + key: "created", |
| 92 | + }, |
| 93 | + 201, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + // case: key already exists |
| 98 | + if (req.headers.get("Authorization") == "Bearer findkey") { |
| 99 | + return createJSONResponse([ |
| 100 | + { |
| 101 | + key: "foo", |
| 102 | + }, |
| 103 | + { |
| 104 | + key: "exists", |
| 105 | + }, |
| 106 | + ]); |
| 107 | + } |
| 108 | + |
| 109 | + // case: key does not exist |
| 110 | + return createJSONResponse([ |
| 111 | + { |
| 112 | + key: "foo", |
| 113 | + }, |
| 114 | + ]); |
| 115 | + } |
| 116 | + |
| 117 | + return createJSONResponse( |
| 118 | + { |
| 119 | + error: "not_found", |
| 120 | + }, |
| 121 | + 404, |
| 122 | + ); |
| 123 | + }, |
| 124 | + port: 0, |
| 125 | + }); |
| 126 | + |
| 127 | + return fakeSlackHost; |
| 128 | +}; |
0 commit comments