chore: add first finished test

mes/rdp-glitch-repro
Parkreiner 10 months ago
parent de00f6334f
commit 7d366ff92a

@ -1,5 +1,7 @@
import { describe, expect, it, test } from "bun:test"; import { describe, expect, it, test } from "bun:test";
import { import {
JsonValue,
TerraformState,
executeScriptInContainer, executeScriptInContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
@ -13,6 +15,11 @@ type TestVariables = Readonly<{
admin_password?: string; admin_password?: string;
}>; }>;
/**
* @todo It would be nice if we had a way to verify that the Devolutions root
* HTML file is modified to include the import for the patched Coder script,
* but the current test setup doesn't really make that viable
*/
describe("Web RDP", async () => { describe("Web RDP", async () => {
await runTerraformInit(import.meta.dir); await runTerraformInit(import.meta.dir);
testRequiredVariables<TestVariables>(import.meta.dir, { testRequiredVariables<TestVariables>(import.meta.dir, {
@ -29,21 +36,38 @@ describe("Web RDP", async () => {
throw new Error("Not implemented yet"); throw new Error("Not implemented yet");
}); });
/** it("Injects Terraform's username and password into the JS patch file", async () => {
* @todo Verify that the HTML file has been modified, and that the JS file is const findInstancesScript = (state: TerraformState): string | null => {
* also part of the file system let instancesScript: string | null = null;
*/ for (const resource of state.resources) {
it("Patches the Devolutions Angular app's .html file to include an import for the custom JS file", async () => { if (resource.type !== "coder_script") {
const state = await runTerraformApply<TestVariables>(import.meta.dir, { continue;
agent_id: "foo", }
resource_id: "bar",
});
throw new Error("Not implemented yet"); for (const instance of resource.instances) {
}); if (instance.attributes.display_name === "windows-rdp") {
instancesScript = instance.attributes.script;
}
}
}
it("Injects Terraform's username and password into the JS patch file", async () => { return instancesScript;
throw new Error("Not implemented yet"); };
/**
* Using a regex as a quick-and-dirty way to get at the username and
* password values.
*
* Tried going through the trouble of extracting out the form entries
* variable from the main output, converting it from Prettier/JS-based JSON
* text to universal JSON text, and exposing it as a parsed JSON value. That
* got to be too much, though.
*
* Written and tested via Regex101
* @see {@link https://regex101.com/r/UMgQpv/2}
*/
const formEntryValuesRe =
/^const formFieldEntries = \{$.*?^\s+username: \{$.*?^\s*?querySelector.*?,$.*?^\s*value: "(?<username>.+?)",$.*?password: \{$.*?^\s+querySelector: .*?,$.*?^\s*value: "(?<password>.+?)",$.*?^};$/ms;
// Test that things work with the default username/password // Test that things work with the default username/password
const defaultState = await runTerraformApply<TestVariables>( const defaultState = await runTerraformApply<TestVariables>(
@ -54,19 +78,35 @@ describe("Web RDP", async () => {
}, },
); );
const output = await executeScriptInContainer(defaultState, "alpine"); const defaultInstancesScript = findInstancesScript(defaultState);
expect(defaultInstancesScript).toBeString();
const { username: defaultUsername, password: defaultPassword } =
formEntryValuesRe.exec(defaultInstancesScript)?.groups ?? {};
expect(defaultUsername).toBe("Administrator");
expect(defaultPassword).toBe("coderRDP!");
// Test that custom usernames/passwords are also forwarded correctly // Test that custom usernames/passwords are also forwarded correctly
const customUsername = "crouton"; const userDefinedUsername = "crouton";
const customPassword = "VeryVeryVeryVeryVerySecurePassword97!"; const userDefinedPassword = "VeryVeryVeryVeryVerySecurePassword97!";
const customizedState = await runTerraformApply<TestVariables>( const customizedState = await runTerraformApply<TestVariables>(
import.meta.dir, import.meta.dir,
{ {
agent_id: "foo", agent_id: "foo",
resource_id: "bar", resource_id: "bar",
admin_username: customUsername, admin_username: userDefinedUsername,
admin_password: customPassword, admin_password: userDefinedPassword,
}, },
); );
const customInstancesScript = findInstancesScript(customizedState);
expect(customInstancesScript).toBeString();
const { username: customUsername, password: customPassword } =
formEntryValuesRe.exec(customInstancesScript)?.groups ?? {};
expect(customUsername).toBe(userDefinedUsername);
expect(customPassword).toBe(userDefinedPassword);
}); });
}); });

Loading…
Cancel
Save