Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 7d366ff

Browse files
committed
chore: add first finished test
1 parent de00f63 commit 7d366ff

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

windows-rdp/main.test.ts

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { describe, expect, it, test } from "bun:test";
22
import {
3+
JsonValue,
4+
TerraformState,
35
executeScriptInContainer,
46
runTerraformApply,
57
runTerraformInit,
@@ -13,6 +15,11 @@ type TestVariables = Readonly<{
1315
admin_password?: string;
1416
}>;
1517

18+
/**
19+
* @todo It would be nice if we had a way to verify that the Devolutions root
20+
* HTML file is modified to include the import for the patched Coder script,
21+
* but the current test setup doesn't really make that viable
22+
*/
1623
describe("Web RDP", async () => {
1724
await runTerraformInit(import.meta.dir);
1825
testRequiredVariables<TestVariables>(import.meta.dir, {
@@ -29,21 +36,38 @@ describe("Web RDP", async () => {
2936
throw new Error("Not implemented yet");
3037
});
3138

32-
/**
33-
* @todo Verify that the HTML file has been modified, and that the JS file is
34-
* also part of the file system
35-
*/
36-
it("Patches the Devolutions Angular app's .html file to include an import for the custom JS file", async () => {
37-
const state = await runTerraformApply<TestVariables>(import.meta.dir, {
38-
agent_id: "foo",
39-
resource_id: "bar",
40-
});
39+
it("Injects Terraform's username and password into the JS patch file", async () => {
40+
const findInstancesScript = (state: TerraformState): string | null => {
41+
let instancesScript: string | null = null;
42+
for (const resource of state.resources) {
43+
if (resource.type !== "coder_script") {
44+
continue;
45+
}
4146

42-
throw new Error("Not implemented yet");
43-
});
47+
for (const instance of resource.instances) {
48+
if (instance.attributes.display_name === "windows-rdp") {
49+
instancesScript = instance.attributes.script;
50+
}
51+
}
52+
}
4453

45-
it("Injects Terraform's username and password into the JS patch file", async () => {
46-
throw new Error("Not implemented yet");
54+
return instancesScript;
55+
};
56+
57+
/**
58+
* Using a regex as a quick-and-dirty way to get at the username and
59+
* password values.
60+
*
61+
* Tried going through the trouble of extracting out the form entries
62+
* variable from the main output, converting it from Prettier/JS-based JSON
63+
* text to universal JSON text, and exposing it as a parsed JSON value. That
64+
* got to be too much, though.
65+
*
66+
* Written and tested via Regex101
67+
* @see {@link https://regex101.com/r/UMgQpv/2}
68+
*/
69+
const formEntryValuesRe =
70+
/^const formFieldEntries = \{$.*?^\s+username: \{$.*?^\s*?querySelector.*?,$.*?^\s*value: "(?<username>.+?)",$.*?password: \{$.*?^\s+querySelector: .*?,$.*?^\s*value: "(?<password>.+?)",$.*?^};$/ms;
4771

4872
// Test that things work with the default username/password
4973
const defaultState = await runTerraformApply<TestVariables>(
@@ -54,19 +78,35 @@ describe("Web RDP", async () => {
5478
},
5579
);
5680

57-
const output = await executeScriptInContainer(defaultState, "alpine");
81+
const defaultInstancesScript = findInstancesScript(defaultState);
82+
expect(defaultInstancesScript).toBeString();
83+
84+
const { username: defaultUsername, password: defaultPassword } =
85+
formEntryValuesRe.exec(defaultInstancesScript)?.groups ?? {};
86+
87+
expect(defaultUsername).toBe("Administrator");
88+
expect(defaultPassword).toBe("coderRDP!");
5889

5990
// Test that custom usernames/passwords are also forwarded correctly
60-
const customUsername = "crouton";
61-
const customPassword = "VeryVeryVeryVeryVerySecurePassword97!";
91+
const userDefinedUsername = "crouton";
92+
const userDefinedPassword = "VeryVeryVeryVeryVerySecurePassword97!";
6293
const customizedState = await runTerraformApply<TestVariables>(
6394
import.meta.dir,
6495
{
6596
agent_id: "foo",
6697
resource_id: "bar",
67-
admin_username: customUsername,
68-
admin_password: customPassword,
98+
admin_username: userDefinedUsername,
99+
admin_password: userDefinedPassword,
69100
},
70101
);
102+
103+
const customInstancesScript = findInstancesScript(customizedState);
104+
expect(customInstancesScript).toBeString();
105+
106+
const { username: customUsername, password: customPassword } =
107+
formEntryValuesRe.exec(customInstancesScript)?.groups ?? {};
108+
109+
expect(customUsername).toBe(userDefinedUsername);
110+
expect(customPassword).toBe(userDefinedPassword);
71111
});
72112
});

0 commit comments

Comments
 (0)