forked from code-yeongyu/oh-my-openagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-server-auth.test.ts
More file actions
94 lines (71 loc) · 2.85 KB
/
opencode-server-auth.test.ts
File metadata and controls
94 lines (71 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/// <reference types="bun-types" />
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
import { getServerBasicAuthHeader, injectServerAuthIntoClient } from "./opencode-server-auth"
describe("opencode-server-auth", () => {
let originalEnv: Record<string, string | undefined>
beforeEach(() => {
originalEnv = {
OPENCODE_SERVER_PASSWORD: process.env.OPENCODE_SERVER_PASSWORD,
OPENCODE_SERVER_USERNAME: process.env.OPENCODE_SERVER_USERNAME,
}
})
afterEach(() => {
for (const [key, value] of Object.entries(originalEnv)) {
if (value !== undefined) {
process.env[key] = value
} else {
delete process.env[key]
}
}
})
test("#given no server password #when building auth header #then returns undefined", () => {
delete process.env.OPENCODE_SERVER_PASSWORD
const result = getServerBasicAuthHeader()
expect(result).toBeUndefined()
})
test("#given server password without username #when building auth header #then uses default username", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
delete process.env.OPENCODE_SERVER_USERNAME
const result = getServerBasicAuthHeader()
expect(result).toBe("Basic b3BlbmNvZGU6c2VjcmV0")
})
test("#given server password and username #when building auth header #then uses provided username", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
process.env.OPENCODE_SERVER_USERNAME = "dan"
const result = getServerBasicAuthHeader()
expect(result).toBe("Basic ZGFuOnNlY3JldA==")
})
test("#given server password #when injecting into client #then updates client headers", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
delete process.env.OPENCODE_SERVER_USERNAME
let receivedConfig: { headers: Record<string, string> } | undefined
const client = {
_client: {
setConfig: (config: { headers: Record<string, string> }) => {
receivedConfig = config
},
},
}
injectServerAuthIntoClient(client)
expect(receivedConfig).toEqual({
headers: {
Authorization: "Basic b3BlbmNvZGU6c2VjcmV0",
},
})
})
test("#given server password #when client has no _client #then does not throw", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
const client = {}
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
})
test("#given server password #when client._client has no setConfig #then does not throw", () => {
process.env.OPENCODE_SERVER_PASSWORD = "secret"
const client = { _client: {} }
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
})
test("#given no server password #when client is invalid #then does not throw", () => {
delete process.env.OPENCODE_SERVER_PASSWORD
const client = {}
expect(() => injectServerAuthIntoClient(client)).not.toThrow()
})
})