1
1
import { describe , expect , it , test } from "bun:test" ;
2
2
import {
3
+ JsonValue ,
4
+ TerraformState ,
3
5
executeScriptInContainer ,
4
6
runTerraformApply ,
5
7
runTerraformInit ,
@@ -13,6 +15,11 @@ type TestVariables = Readonly<{
13
15
admin_password ?: string ;
14
16
} > ;
15
17
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
+ */
16
23
describe ( "Web RDP" , async ( ) => {
17
24
await runTerraformInit ( import . meta. dir ) ;
18
25
testRequiredVariables < TestVariables > ( import . meta. dir , {
@@ -29,21 +36,38 @@ describe("Web RDP", async () => {
29
36
throw new Error ( "Not implemented yet" ) ;
30
37
} ) ;
31
38
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
+ }
41
46
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
+ }
44
53
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
+ / ^ c o n s t f o r m F i e l d E n t r i e s = \{ $ .* ?^ \s + u s e r n a m e : \{ $ .* ?^ \s * ?q u e r y S e l e c t o r .* ?, $ .* ?^ \s * v a l u e : " (?< username > .+ ?) " , $ .* ?p a s s w o r d : \{ $ .* ?^ \s + q u e r y S e l e c t o r : .* ?, $ .* ?^ \s * v a l u e : " (?< password > .+ ?) " , $ .* ?^ } ; $ / ms;
47
71
48
72
// Test that things work with the default username/password
49
73
const defaultState = await runTerraformApply < TestVariables > (
@@ -54,19 +78,35 @@ describe("Web RDP", async () => {
54
78
} ,
55
79
) ;
56
80
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!" ) ;
58
89
59
90
// 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!" ;
62
93
const customizedState = await runTerraformApply < TestVariables > (
63
94
import . meta. dir ,
64
95
{
65
96
agent_id : "foo" ,
66
97
resource_id : "bar" ,
67
- admin_username : customUsername ,
68
- admin_password : customPassword ,
98
+ admin_username : userDefinedUsername ,
99
+ admin_password : userDefinedPassword ,
69
100
} ,
70
101
) ;
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 ) ;
71
111
} ) ;
72
112
} ) ;
0 commit comments