mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix(servers): test edited server URLs with the stored password
Test always checks the draft URL. When the password is left blank, the stored password is reused only if the draft URL has the same origin as the saved server.
This commit is contained in:
1 parent
e707e80b91
commit
1659664d53
6 files changed
+131
-22
No files matched your search
@@ -487,6 +487,76 @@ describe('OpenCodeServerRegistry', () => {
|
||||
expect(headers.get('authorization')).toBe(`Basic ${btoa('alice:super-secret')}`)
|
||||
})
|
||||
|
||||
it('reuses the stored password for a draft on the same origin', async () => {
|
||||
const server = await createServer({
|
||||
name: 'Studio',
|
||||
baseUrl: 'https://studio.example.com',
|
||||
password: 'super-secret',
|
||||
})
|
||||
fetchFn.mockResolvedValueOnce(jsonResponse({ version: '2.0.15' }))
|
||||
|
||||
const result = await registry.test({
|
||||
baseUrl: 'https://studio.example.com/opencode',
|
||||
username: 'alice',
|
||||
serverId: server.id,
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(fetchFn.mock.calls[0]?.[0]).toBe('https://studio.example.com/opencode/api/info')
|
||||
const headers = new Headers(fetchFn.mock.calls[0]?.[1]?.headers)
|
||||
expect(headers.get('authorization')).toBe(`Basic ${btoa('alice:super-secret')}`)
|
||||
})
|
||||
|
||||
it('does not send the stored password to a draft on a different origin', async () => {
|
||||
const server = await createServer({
|
||||
name: 'Studio',
|
||||
baseUrl: 'https://studio.example.com',
|
||||
password: 'super-secret',
|
||||
})
|
||||
fetchFn.mockResolvedValueOnce(jsonResponse({ version: '2.0.15' }))
|
||||
|
||||
const result = await registry.test({
|
||||
baseUrl: 'https://relocated.example.com',
|
||||
username: 'alice',
|
||||
serverId: server.id,
|
||||
})
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(fetchFn.mock.calls[0]?.[0]).toBe('https://relocated.example.com/api/info')
|
||||
const headers = new Headers(fetchFn.mock.calls[0]?.[1]?.headers)
|
||||
expect(headers.get('authorization')).toBeNull()
|
||||
})
|
||||
|
||||
it('prefers an explicit draft password over the stored one', async () => {
|
||||
const server = await createServer({
|
||||
name: 'Studio',
|
||||
baseUrl: 'https://studio.example.com',
|
||||
password: 'super-secret',
|
||||
})
|
||||
fetchFn.mockResolvedValueOnce(jsonResponse({ version: '2.0.15' }))
|
||||
|
||||
await registry.test({
|
||||
baseUrl: 'https://studio.example.com',
|
||||
username: 'alice',
|
||||
password: 'draft-secret',
|
||||
serverId: server.id,
|
||||
})
|
||||
|
||||
const headers = new Headers(fetchFn.mock.calls[0]?.[1]?.headers)
|
||||
expect(headers.get('authorization')).toBe(`Basic ${btoa('alice:draft-secret')}`)
|
||||
})
|
||||
|
||||
it('throws a not-found error for an unknown server id on a draft', async () => {
|
||||
await expect(
|
||||
registry.test({
|
||||
baseUrl: 'https://studio.example.com',
|
||||
username: 'opencode',
|
||||
serverId: 'srv_missing',
|
||||
}),
|
||||
).rejects.toBeInstanceOf(OpenCodeServerNotFoundError)
|
||||
expect(fetchFn).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reports a failed request without throwing', async () => {
|
||||
fetchFn.mockResolvedValueOnce(new Response(null, { status: 500 }))
|
||||
|
||||
|
||||
@@ -249,7 +249,7 @@ export class OpenCodeServerRegistry {
|
||||
}
|
||||
|
||||
private resolveTestTarget(input: TestServerSource): TestTarget {
|
||||
if ('serverId' in input) {
|
||||
if (!('baseUrl' in input)) {
|
||||
const server = this.requireServer(input.serverId)
|
||||
return {
|
||||
baseUrl: server.baseUrl,
|
||||
@@ -258,10 +258,22 @@ export class OpenCodeServerRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
const baseUrl = trimUrlTrailingSlashes(input.baseUrl)
|
||||
if (input.password !== undefined || input.serverId === undefined) {
|
||||
return {
|
||||
baseUrl,
|
||||
username: input.username,
|
||||
password: input.password ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
const server = this.requireServer(input.serverId)
|
||||
return {
|
||||
baseUrl: trimUrlTrailingSlashes(input.baseUrl),
|
||||
baseUrl,
|
||||
username: input.username,
|
||||
password: input.password ?? null,
|
||||
password: haveSameUrlOrigin(server.baseUrl, baseUrl)
|
||||
? readOpenCodeServerPassword(this.db, input.serverId)
|
||||
: null,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -213,6 +213,36 @@ describe('opencode server routes', () => {
|
||||
expect(registry.test).toHaveBeenCalledWith({ baseUrl: 'https://studio.example.com', username: 'opencode' })
|
||||
})
|
||||
|
||||
it('tests a draft server against a saved server id', async () => {
|
||||
registry.test.mockResolvedValue(TEST_RESULT)
|
||||
|
||||
const response = await app.request('/test', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ baseUrl: 'https://studio.example.com/opencode', serverId: 'srv_1' }),
|
||||
})
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(registry.test).toHaveBeenCalledWith({
|
||||
baseUrl: 'https://studio.example.com/opencode',
|
||||
username: 'opencode',
|
||||
serverId: 'srv_1',
|
||||
})
|
||||
})
|
||||
|
||||
it('returns 404 when testing a draft against an unknown server id', async () => {
|
||||
registry.test.mockRejectedValue(new OpenCodeServerNotFoundError('srv_missing'))
|
||||
|
||||
const response = await app.request('/test', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ baseUrl: 'https://studio.example.com', serverId: 'srv_missing' }),
|
||||
})
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
expect(await response.json()).toEqual({ error: 'OpenCode server not found: srv_missing' })
|
||||
})
|
||||
|
||||
it('returns the remote event statuses from the provider', async () => {
|
||||
const statuses = [{ serverId: 'srv_1', connected: true, version: '2.0.15', error: null }]
|
||||
const statusApp = createOpenCodeServerRoutes(
|
||||
|
||||
@@ -67,16 +67,21 @@ describe('OpenCodeServersSettings', () => {
|
||||
mocks.testByIdAsync.mockResolvedValue(testResult)
|
||||
})
|
||||
|
||||
it('tests the saved server when the edited target and credentials are unchanged', async () => {
|
||||
it('tests the draft with the saved server id when the password is left blank', async () => {
|
||||
const user = await openEditDialog()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Test connection' }))
|
||||
|
||||
expect(mocks.testByIdAsync).toHaveBeenCalledWith('srv_1')
|
||||
expect(mocks.testAsync).not.toHaveBeenCalled()
|
||||
expect(mocks.testAsync).toHaveBeenCalledWith({
|
||||
baseUrl: 'https://office.example.com',
|
||||
username: 'opencode',
|
||||
password: undefined,
|
||||
serverId: 'srv_1',
|
||||
})
|
||||
expect(mocks.testByIdAsync).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('tests the edited address when the base URL changes', async () => {
|
||||
it('tests the edited address with the saved server id', async () => {
|
||||
const user = await openEditDialog()
|
||||
|
||||
await user.clear(screen.getByLabelText('URL'))
|
||||
@@ -87,11 +92,12 @@ describe('OpenCodeServersSettings', () => {
|
||||
baseUrl: 'https://relocated.example.com',
|
||||
username: 'opencode',
|
||||
password: undefined,
|
||||
serverId: 'srv_1',
|
||||
})
|
||||
expect(mocks.testByIdAsync).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('tests the edited address when the stored password is being cleared', async () => {
|
||||
it('tests the edited address without the saved server id when the stored password is being cleared', async () => {
|
||||
const user = await openEditDialog()
|
||||
|
||||
await user.click(screen.getByLabelText('Clear stored password'))
|
||||
@@ -101,6 +107,7 @@ describe('OpenCodeServersSettings', () => {
|
||||
baseUrl: 'https://office.example.com',
|
||||
username: 'opencode',
|
||||
password: undefined,
|
||||
serverId: undefined,
|
||||
})
|
||||
expect(mocks.testByIdAsync).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
@@ -6,7 +6,6 @@ import type {
|
||||
OpenCodeServerTestResult,
|
||||
UpdateOpenCodeServerRequest,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import { trimUrlTrailingSlashes } from '@opencode-manager/shared/utils'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
@@ -108,23 +107,13 @@ export function OpenCodeServersSettings() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleTest = async (values: OpenCodeServerFormValues) => {
|
||||
const testsSavedServer =
|
||||
editingServer !== null &&
|
||||
trimUrlTrailingSlashes(values.baseUrl) === editingServer.baseUrl &&
|
||||
values.username === editingServer.username &&
|
||||
!values.password &&
|
||||
!values.clearPassword &&
|
||||
!values.pairing
|
||||
if (editingServer && testsSavedServer) {
|
||||
return testByIdAsync(editingServer.id)
|
||||
}
|
||||
return testAsync({
|
||||
const handleTest = async (values: OpenCodeServerFormValues) =>
|
||||
testAsync({
|
||||
baseUrl: values.baseUrl,
|
||||
username: values.username,
|
||||
password: values.password || undefined,
|
||||
serverId: editingServer && !values.password && !values.clearPassword ? editingServer.id : undefined,
|
||||
})
|
||||
}
|
||||
|
||||
const toggleEnabled = async (server: OpenCodeServer, enabled: boolean) => {
|
||||
try {
|
||||
|
||||
@@ -53,6 +53,7 @@ export const TestOpenCodeServerRequestSchema = z.object({
|
||||
baseUrl: HttpUrlSchema,
|
||||
username: z.string().trim().min(1).max(100).default(OPENCODE_SERVER_USERNAME),
|
||||
password: z.string().max(1024).optional(),
|
||||
serverId: z.string().trim().min(1).optional(),
|
||||
});
|
||||
|
||||
export const OpenCodeServerTestResultSchema = z.object({
|
||||
|
||||
Reference in new issue
Block a user