Skip to content

Commit a5488c8

Browse files
committed
chore: add UTs for the token env
1 parent 0bc801c commit a5488c8

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

src/test/kotlin/com/coder/toolbox/cli/CoderCLIManagerTest.kt

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private val noOpTextProgress: (String) -> Unit = { _ -> }
7474

7575
internal class CoderCLIManagerTest {
7676
private val ui = mockk<ToolboxUi>(relaxed = true)
77+
private val logger = mockk<Logger>(relaxed = true)
7778
private val context = CoderToolboxContext(
7879
ui,
7980
mockk<EnvironmentUiPageManager>(),
@@ -82,12 +83,12 @@ internal class CoderCLIManagerTest {
8283
mockk<ClientHelper>(),
8384
mockk<LocalDesktopManager>(),
8485
mockk<CoroutineScope>(),
85-
mockk<Logger>(relaxed = true),
86+
logger,
8687
mockk<LocalizableStringFactory>(relaxed = true),
8788
CoderSettingsStore(
8889
pluginTestSettingsStore(),
8990
Environment(),
90-
mockk<Logger>(relaxed = true)
91+
logger
9192
),
9293
mockk<CoderSecretsStore>(),
9394
object : ToolboxProxySettings {
@@ -119,6 +120,14 @@ internal class CoderCLIManagerTest {
119120
listOf("#!/bin/sh", str)
120121
}.joinToString(System.lineSeparator())
121122

123+
private fun writeExecutable(path: Path, contents: String) {
124+
path.parent.toFile().mkdirs()
125+
path.toFile().writeText(contents)
126+
if (getOS() != OS.WINDOWS) {
127+
path.toFile().setExecutable(true)
128+
}
129+
}
130+
122131
/**
123132
* Return the contents of a script that outputs JSON containing the version.
124133
*/
@@ -424,6 +433,49 @@ internal class CoderCLIManagerTest {
424433
)
425434
}
426435

436+
@Test
437+
fun `login passes token via environment instead of argv`() {
438+
val binaryFile = tmpdir.resolve("login-env").resolve(if (getOS() == OS.WINDOWS) "coder.bat" else "coder")
439+
val argsFile = tmpdir.resolve("login-env").resolve("argv.txt")
440+
val envFile = tmpdir.resolve("login-env").resolve("env.txt")
441+
val token = "super-secret-token"
442+
val stdout = "login ok"
443+
val script = if (getOS() == OS.WINDOWS) {
444+
mkbin(
445+
"""
446+
echo %* > "${argsFile.toAbsolutePath()}"
447+
echo %CODER_SESSION_TOKEN% > "${envFile.toAbsolutePath()}"
448+
echo $stdout
449+
""".trimIndent()
450+
)
451+
} else {
452+
mkbin(
453+
"""
454+
printf '%s\n' "$*" > '${argsFile.toAbsolutePath()}'
455+
printf '%s\n' "${'$'}CODER_SESSION_TOKEN" > '${envFile.toAbsolutePath()}'
456+
printf '$stdout\n'
457+
""".trimIndent()
458+
)
459+
}
460+
writeExecutable(binaryFile, script)
461+
462+
val settings = CoderSettingsStore(
463+
pluginTestSettingsStore(
464+
BINARY_DESTINATION to binaryFile.toString(),
465+
ENABLE_DOWNLOADS to "false",
466+
),
467+
Environment(),
468+
logger
469+
)
470+
val ccm = CoderCLIManager(context.copy(settingsStore = settings), URL("https://test.coder.com"))
471+
472+
assertEquals("$stdout\n", ccm.login(token))
473+
assertContains(argsFile.toFile().readText(), "login https://test.coder.com --global-config")
474+
assertFalse(argsFile.toFile().readText().contains("--token"))
475+
assertFalse(argsFile.toFile().readText().contains(token))
476+
assertEquals(token, envFile.toFile().readText().trim())
477+
}
478+
427479
@Test
428480
fun testOverwritesWrongVersion() {
429481
val (srv, url) = mockServer()

0 commit comments

Comments
 (0)