Skip to content

Commit 6678d13

Browse files
author
Captain CP
committed
fix(tests): Add test authentication support for security fix
Fixed 4 test failures caused by mandatory authentication: - Added Server.setTestPassword() for test environment - Updated session-select and session-list tests to use test auth - All 754 tests now passing (was 750/754, now 754/754) The test failures were actually validating that our security fix works - the tests were getting 401 Unauthorized as expected. Now they provide proper authentication and validate the endpoints work correctly. Test results: 754 pass, 0 fail ✅
1 parent ab31608 commit 6678d13

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

packages/opencode/src/server/server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export namespace Server {
5050
let _url: URL | undefined
5151
let _corsWhitelist: string[] = []
5252
let _generatedPassword: string | undefined
53+
let _testPassword: string | undefined // For test environment
5354

5455
export function url(): URL {
5556
return _url ?? new URL("http://localhost:4096")
@@ -59,6 +60,11 @@ export namespace Server {
5960
return _generatedPassword
6061
}
6162

63+
// Test-only function to set password (called before Server.App())
64+
export function setTestPassword(password: string) {
65+
_testPassword = password
66+
}
67+
6268
function generateSecurePassword(): string {
6369
// Generate a 32-character random password using crypto-safe random bytes
6470
// Uses rejection sampling to avoid modulo bias
@@ -120,6 +126,11 @@ export namespace Server {
120126
password = _generatedPassword
121127
}
122128

129+
// Test mode: allow test password override
130+
if (!password && _testPassword) {
131+
password = _testPassword
132+
}
133+
123134
const username = Flag.OPENCODE_SERVER_USERNAME ?? "opencode"
124135
return basicAuth({ username, password })(c, next)
125136
})

packages/opencode/test/server/session-list.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Set test password BEFORE importing modules (Flag reads env at import time)
2+
process.env.OPENCODE_SERVER_PASSWORD = "test-password-for-tests"
3+
14
import { describe, expect, test } from "bun:test"
25
import path from "path"
36
import { Instance } from "../../src/project/instance"
@@ -8,6 +11,14 @@ import { Log } from "../../src/util/log"
811
const projectRoot = path.join(__dirname, "../..")
912
Log.init({ print: false })
1013

14+
// Test credentials for authentication
15+
const TEST_USERNAME = "opencode"
16+
const TEST_PASSWORD = "test-password-for-tests"
17+
const authHeader = `Basic ${Buffer.from(`${TEST_USERNAME}:${TEST_PASSWORD}`).toString("base64")}`
18+
19+
// Set test password for Server.App()
20+
Server.setTestPassword(TEST_PASSWORD)
21+
1122
describe("session.list", () => {
1223
test("filters by directory", async () => {
1324
await Instance.provide({
@@ -23,7 +34,11 @@ describe("session.list", () => {
2334
fn: async () => Session.create({}),
2435
})
2536

26-
const response = await app.request(`/session?directory=${encodeURIComponent(projectRoot)}`)
37+
const response = await app.request(`/session?directory=${encodeURIComponent(projectRoot)}`, {
38+
headers: {
39+
"Authorization": authHeader,
40+
},
41+
})
2742
expect(response.status).toBe(200)
2843

2944
const body = (await response.json()) as unknown[]

packages/opencode/test/server/session-select.test.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Set test password BEFORE importing modules (Flag reads env at import time)
2+
process.env.OPENCODE_SERVER_PASSWORD = "test-password-for-tests"
3+
14
import { describe, expect, test } from "bun:test"
25
import path from "path"
36
import { Session } from "../../src/session"
@@ -8,6 +11,14 @@ import { Server } from "../../src/server/server"
811
const projectRoot = path.join(__dirname, "../..")
912
Log.init({ print: false })
1013

14+
// Test credentials for authentication
15+
const TEST_USERNAME = "opencode"
16+
const TEST_PASSWORD = "test-password-for-tests"
17+
const authHeader = `Basic ${Buffer.from(`${TEST_USERNAME}:${TEST_PASSWORD}`).toString("base64")}`
18+
19+
// Set test password for Server.App()
20+
Server.setTestPassword(TEST_PASSWORD)
21+
1122
describe("tui.selectSession endpoint", () => {
1223
test("should return 200 when called with valid session", async () => {
1324
await Instance.provide({
@@ -20,7 +31,10 @@ describe("tui.selectSession endpoint", () => {
2031
const app = Server.App()
2132
const response = await app.request("/tui/select-session", {
2233
method: "POST",
23-
headers: { "Content-Type": "application/json" },
34+
headers: {
35+
"Content-Type": "application/json",
36+
"Authorization": authHeader,
37+
},
2438
body: JSON.stringify({ sessionID: session.id }),
2539
})
2640

@@ -45,7 +59,10 @@ describe("tui.selectSession endpoint", () => {
4559
const app = Server.App()
4660
const response = await app.request("/tui/select-session", {
4761
method: "POST",
48-
headers: { "Content-Type": "application/json" },
62+
headers: {
63+
"Content-Type": "application/json",
64+
"Authorization": authHeader,
65+
},
4966
body: JSON.stringify({ sessionID: nonExistentSessionID }),
5067
})
5168

@@ -66,7 +83,10 @@ describe("tui.selectSession endpoint", () => {
6683
const app = Server.App()
6784
const response = await app.request("/tui/select-session", {
6885
method: "POST",
69-
headers: { "Content-Type": "application/json" },
86+
headers: {
87+
"Content-Type": "application/json",
88+
"Authorization": authHeader,
89+
},
7090
body: JSON.stringify({ sessionID: invalidSessionID }),
7191
})
7292

0 commit comments

Comments
 (0)