fix(datasource): validate replacement credentials before saving
parent
17f893865d
commit
e42f09d535
|
|
@ -114,7 +114,7 @@ export function validateConnection(id: string) {
|
|||
return post(`/api/v1/datasource/${id}/validate`, {})
|
||||
}
|
||||
|
||||
// Validate credentials without persisting (for "Test Connection" during creation)
|
||||
// Validate credentials without persisting (during creation or credential replacement).
|
||||
export function validateCredentials(type: string, credentials: Record<string, any>) {
|
||||
return post('/api/v1/datasource/validate-credentials', { type, credentials })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,159 @@
|
|||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { createRequire } from 'node:module'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { runInNewContext } from 'node:vm'
|
||||
import test from 'node:test'
|
||||
import { compileScript, parse } from '@vue/compiler-sfc'
|
||||
import ts from 'typescript'
|
||||
import { createRenderer, h, nextTick, reactive, ref } from 'vue'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const filename = fileURLToPath(new URL('./DataSourceEditorDialog.vue', import.meta.url))
|
||||
const { descriptor } = parse(readFileSync(filename, 'utf8'), { filename })
|
||||
const script = compileScript(descriptor, { id: 'datasource-editor-test' }).content
|
||||
.replace('__expose();', '')
|
||||
.replace('return __returned__', '__expose(__returned__); return __returned__')
|
||||
const compiled = ts.transpileModule(script, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
|
||||
}).outputText
|
||||
|
||||
async function fixture({ configured = true, create = false } = {}) {
|
||||
const calls: Array<{ method: string; args: any[] }> = []
|
||||
let storedToken = configured ? 'expired-token' : ''
|
||||
const api = {
|
||||
async validateCredentials(type: string, credentials: Record<string, string>) {
|
||||
calls.push({ method: 'validateCredentials', args: [type, { ...credentials }] })
|
||||
if (credentials.access_token !== 'rotated-token') throw new Error('gitlab API /user: status 401')
|
||||
},
|
||||
async validateConnection(id: string) {
|
||||
calls.push({ method: 'validateConnection', args: [id] })
|
||||
if (storedToken !== 'rotated-token') throw new Error('gitlab API /user: status 401')
|
||||
},
|
||||
async updateDataSource(id: string, data: any) {
|
||||
calls.push({ method: 'updateDataSource', args: [id, JSON.parse(JSON.stringify(data))] })
|
||||
// The main update endpoint deliberately preserves stored credentials.
|
||||
},
|
||||
async putDataSourceCredentials(id: string, credentials: Record<string, string>) {
|
||||
calls.push({ method: 'putDataSourceCredentials', args: [id, { ...credentials }] })
|
||||
storedToken = credentials.access_token
|
||||
},
|
||||
}
|
||||
const props = reactive({
|
||||
visible: false, kbId: 'kb-one',
|
||||
dataSource: create ? null : {
|
||||
id: 'source-one', name: 'GitLab', type: 'gitlab',
|
||||
credentials: { credentials: { configured } },
|
||||
config: { resource_ids: [], settings: { projects: [{ project_id: '123', paths: [] }] } },
|
||||
sync_schedule: '0 0 */6 * * *', sync_mode: 'incremental',
|
||||
conflict_strategy: 'overwrite', sync_deletions: true,
|
||||
},
|
||||
})
|
||||
const exports: any = {}
|
||||
runInNewContext(compiled, {
|
||||
exports,
|
||||
require(name: string) {
|
||||
if (name === 'vue') return require('vue')
|
||||
if (name === 'vue-i18n') return { useI18n: () => ({ t: (key: string) => key }) }
|
||||
if (name === 'tdesign-vue-next') return { MessagePlugin: { warning() {}, success() {}, error() {} } }
|
||||
if (name === '@/api/datasource') return api
|
||||
return { default: {} }
|
||||
},
|
||||
URL, console,
|
||||
})
|
||||
const component = exports.default
|
||||
component.render = () => null
|
||||
const renderer = createRenderer<any, any>({
|
||||
createElement: () => ({}), createText: () => ({}), createComment: () => ({}),
|
||||
insert() {}, remove() {}, setElementText() {}, setText() {}, patchProp() {},
|
||||
parentNode: () => null, nextSibling: () => null,
|
||||
})
|
||||
const instance = ref<any>()
|
||||
const app = renderer.createApp({ render: () => h(component, { ...props, ref: instance }) })
|
||||
app.mount({})
|
||||
props.visible = true
|
||||
await nextTick()
|
||||
const vm = instance.value
|
||||
async function replace(token = 'rotated-token') {
|
||||
vm.enterReplaceCredentials()
|
||||
vm.form.config.credentials = { base_url: 'https://gitlab.example.com', access_token: token }
|
||||
await nextTick()
|
||||
}
|
||||
return { vm, calls, replace, storedToken: () => storedToken, close: () => app.unmount() }
|
||||
}
|
||||
|
||||
test('rotated GitLab credentials are tested without updating the saved data source', async () => {
|
||||
const f = await fixture()
|
||||
try {
|
||||
await f.replace()
|
||||
await f.vm.testConnection()
|
||||
assert.equal(f.vm.testResult, 'success')
|
||||
assert.deepEqual(f.calls, [{ method: 'validateCredentials', args: ['gitlab', {
|
||||
base_url: 'https://gitlab.example.com', access_token: 'rotated-token',
|
||||
}] }])
|
||||
assert.equal(f.storedToken(), 'expired-token')
|
||||
} finally { f.close() }
|
||||
})
|
||||
|
||||
test('Next tests the replacement and final save commits credentials before settings', async () => {
|
||||
const f = await fixture()
|
||||
try {
|
||||
await f.replace()
|
||||
await f.vm.nextStep()
|
||||
assert.equal(f.vm.step, 2)
|
||||
await f.vm.nextStep()
|
||||
assert.equal(f.vm.step, 3)
|
||||
await f.vm.handleSubmit()
|
||||
assert.deepEqual(f.calls.map(call => call.method), [
|
||||
'validateCredentials', 'putDataSourceCredentials', 'updateDataSource',
|
||||
])
|
||||
assert.equal(f.storedToken(), 'rotated-token')
|
||||
assert.deepEqual(f.calls[2].args[1].config.credentials, {})
|
||||
} finally { f.close() }
|
||||
})
|
||||
|
||||
test('invalid replacement stays on the credentials step and can be corrected', async () => {
|
||||
const f = await fixture()
|
||||
try {
|
||||
await f.replace('invalid-token')
|
||||
await f.vm.nextStep()
|
||||
assert.equal(f.vm.step, 1)
|
||||
assert.equal(f.vm.testResult, 'error')
|
||||
assert.match(f.vm.testErrorMsg, /401/)
|
||||
assert.equal(f.storedToken(), 'expired-token')
|
||||
await f.replace()
|
||||
await f.vm.nextStep()
|
||||
assert.equal(f.vm.step, 2)
|
||||
} finally { f.close() }
|
||||
})
|
||||
|
||||
test('testing unchanged credentials still validates the stored token', async () => {
|
||||
const f = await fixture()
|
||||
try {
|
||||
await f.vm.testConnection()
|
||||
assert.equal(f.vm.testResult, 'error')
|
||||
assert.deepEqual(f.calls.map(call => call.method), ['updateDataSource', 'validateConnection'])
|
||||
} finally { f.close() }
|
||||
})
|
||||
|
||||
test('an existing data source with no saved credentials tests the entered token', async () => {
|
||||
const f = await fixture({ configured: false })
|
||||
try {
|
||||
await f.replace()
|
||||
await f.vm.testConnection()
|
||||
assert.equal(f.vm.testResult, 'success')
|
||||
assert.deepEqual(f.calls.map(call => call.method), ['validateCredentials'])
|
||||
assert.equal(f.storedToken(), '')
|
||||
} finally { f.close() }
|
||||
})
|
||||
|
||||
test('new GitLab data sources continue to test credentials without persistence', async () => {
|
||||
const f = await fixture({ create: true })
|
||||
try {
|
||||
f.vm.selectType(f.vm.connectorDefs.find((def: any) => def.type === 'gitlab'))
|
||||
await f.replace()
|
||||
await f.vm.testConnection()
|
||||
assert.equal(f.vm.testResult, 'success')
|
||||
assert.deepEqual(f.calls.map(call => call.method), ['validateCredentials'])
|
||||
} finally { f.close() }
|
||||
})
|
||||
|
|
@ -772,7 +772,7 @@ function selectType(def: ConnectorDef) {
|
|||
step.value = 1
|
||||
}
|
||||
|
||||
// --- Test connection (stateless, no DB write) ---
|
||||
// --- Test connection ---
|
||||
async function testConnection() {
|
||||
syncRssAuthHeadersToCredentials()
|
||||
if (!validateRssFeedUrls()) return
|
||||
|
|
@ -791,7 +791,10 @@ async function testConnection() {
|
|||
testResult.value = ''
|
||||
testErrorMsg.value = ''
|
||||
try {
|
||||
if (isEdit.value && tempDsId.value) {
|
||||
// The main update endpoint ignores credentials. Only use the saved
|
||||
// connection when keeping its credentials; test replacements directly
|
||||
// without persisting them until the user saves the data source.
|
||||
if (isEdit.value && tempDsId.value && !needsConnectionTest()) {
|
||||
await updateDataSource(tempDsId.value, {
|
||||
...form.value,
|
||||
knowledge_base_id: props.kbId,
|
||||
|
|
|
|||
Loading…
Reference in New Issue