|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + type TerraformState, |
| 4 | + runTerraformApply, |
| 5 | + runTerraformInit, |
| 6 | + testRequiredVariables, |
| 7 | +} from "~test"; |
| 8 | + |
| 9 | +type TestVariables = Readonly<{ |
| 10 | + agent_id: string; |
| 11 | + agent_name: string; |
| 12 | + username?: string; |
| 13 | + password?: string; |
| 14 | + display_name?: string; |
| 15 | + order?: number; |
| 16 | +}>; |
| 17 | + |
| 18 | +function findRdpApp(state: TerraformState) { |
| 19 | + for (const resource of state.resources) { |
| 20 | + const isRdpAppResource = |
| 21 | + resource.type === "coder_app" && resource.name === "rdp_desktop"; |
| 22 | + |
| 23 | + if (!isRdpAppResource) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + for (const instance of resource.instances) { |
| 28 | + if (instance.attributes.slug === "rdp-desktop") { |
| 29 | + return instance.attributes; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return null; |
| 35 | +} |
| 36 | + |
| 37 | +function findRdpScript(state: TerraformState) { |
| 38 | + for (const resource of state.resources) { |
| 39 | + const isRdpScriptResource = |
| 40 | + resource.type === "coder_script" && resource.name === "rdp_setup"; |
| 41 | + |
| 42 | + if (!isRdpScriptResource) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + for (const instance of resource.instances) { |
| 47 | + if (instance.attributes.display_name === "Configure RDP") { |
| 48 | + return instance.attributes; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return null; |
| 54 | +} |
| 55 | + |
| 56 | +describe("local-windows-rdp", async () => { |
| 57 | + await runTerraformInit(import.meta.dir); |
| 58 | + |
| 59 | + testRequiredVariables<TestVariables>(import.meta.dir, { |
| 60 | + agent_id: "test-agent-id", |
| 61 | + agent_name: "test-agent", |
| 62 | + }); |
| 63 | + |
| 64 | + it("should create RDP app with default values", async () => { |
| 65 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 66 | + agent_id: "test-agent-id", |
| 67 | + agent_name: "main", |
| 68 | + }); |
| 69 | + |
| 70 | + const app = findRdpApp(state); |
| 71 | + |
| 72 | + // Verify the app was created |
| 73 | + expect(app).not.toBeNull(); |
| 74 | + expect(app?.slug).toBe("rdp-desktop"); |
| 75 | + expect(app?.display_name).toBe("RDP Desktop"); |
| 76 | + expect(app?.icon).toBe("/icon/desktop.svg"); |
| 77 | + expect(app?.external).toBe(true); |
| 78 | + |
| 79 | + // Verify the URI format |
| 80 | + expect(app?.url).toStartWith("coder://"); |
| 81 | + expect(app?.url).toContain("/v0/open/ws/"); |
| 82 | + expect(app?.url).toContain("/agent/main/rdp"); |
| 83 | + expect(app?.url).toContain("username=Administrator"); |
| 84 | + expect(app?.url).toContain("password=coderRDP!"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should create RDP configuration script", async () => { |
| 88 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 89 | + agent_id: "test-agent-id", |
| 90 | + agent_name: "main", |
| 91 | + }); |
| 92 | + |
| 93 | + const script = findRdpScript(state); |
| 94 | + |
| 95 | + // Verify the script was created |
| 96 | + expect(script).not.toBeNull(); |
| 97 | + expect(script?.display_name).toBe("Configure RDP"); |
| 98 | + expect(script?.icon).toBe("/icon/desktop.svg"); |
| 99 | + expect(script?.run_on_start).toBe(true); |
| 100 | + expect(script?.run_on_stop).toBe(false); |
| 101 | + |
| 102 | + // Verify the script contains PowerShell configuration |
| 103 | + expect(script?.script).toContain("Set-AdminPassword"); |
| 104 | + expect(script?.script).toContain("Enable-RDP"); |
| 105 | + expect(script?.script).toContain("Configure-Firewall"); |
| 106 | + expect(script?.script).toContain("Start-RDPService"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should create RDP app with custom values", async () => { |
| 110 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 111 | + agent_id: "custom-agent-id", |
| 112 | + agent_name: "windows-agent", |
| 113 | + username: "CustomUser", |
| 114 | + password: "CustomPass123!", |
| 115 | + display_name: "Custom RDP", |
| 116 | + order: 5, |
| 117 | + }); |
| 118 | + |
| 119 | + const app = findRdpApp(state); |
| 120 | + |
| 121 | + // Verify custom values |
| 122 | + expect(app?.display_name).toBe("Custom RDP"); |
| 123 | + expect(app?.order).toBe(5); |
| 124 | + |
| 125 | + // Verify custom credentials in URI |
| 126 | + expect(app?.url).toContain("/agent/windows-agent/rdp"); |
| 127 | + expect(app?.url).toContain("username=CustomUser"); |
| 128 | + expect(app?.url).toContain("password=CustomPass123!"); |
| 129 | + }); |
| 130 | + |
| 131 | + it("should pass custom credentials to PowerShell script", async () => { |
| 132 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 133 | + agent_id: "test-agent-id", |
| 134 | + agent_name: "main", |
| 135 | + username: "TestAdmin", |
| 136 | + password: "TestPassword123!", |
| 137 | + }); |
| 138 | + |
| 139 | + const script = findRdpScript(state); |
| 140 | + |
| 141 | + // Verify custom credentials are in the script |
| 142 | + expect(script?.script).toContain('$username = "TestAdmin"'); |
| 143 | + expect(script?.script).toContain('$password = "TestPassword123!"'); |
| 144 | + }); |
| 145 | + |
| 146 | + it("should handle sensitive password variable", async () => { |
| 147 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 148 | + agent_id: "test-agent-id", |
| 149 | + agent_name: "main", |
| 150 | + password: "SensitivePass123!", |
| 151 | + }); |
| 152 | + |
| 153 | + const app = findRdpApp(state); |
| 154 | + |
| 155 | + // Verify password is included in URI even when sensitive |
| 156 | + expect(app?.url).toContain("password=SensitivePass123!"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("should use correct default agent name", async () => { |
| 160 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 161 | + agent_id: "test-agent-id", |
| 162 | + agent_name: "main", |
| 163 | + }); |
| 164 | + |
| 165 | + const app = findRdpApp(state); |
| 166 | + expect(app?.url).toContain("/agent/main/rdp"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should construct proper Coder URI format", async () => { |
| 170 | + const state = await runTerraformApply<TestVariables>(import.meta.dir, { |
| 171 | + agent_id: "test-agent-id", |
| 172 | + agent_name: "test-agent", |
| 173 | + username: "TestUser", |
| 174 | + password: "TestPass", |
| 175 | + }); |
| 176 | + |
| 177 | + const app = findRdpApp(state); |
| 178 | + |
| 179 | + // Verify complete URI structure |
| 180 | + expect(app?.url).toMatch( |
| 181 | + /^coder:\/\/[^\/]+\/v0\/open\/ws\/[^\/]+\/agent\/test-agent\/rdp\?username=TestUser&password=TestPass$/, |
| 182 | + ); |
| 183 | + }); |
| 184 | +}); |
0 commit comments