This commit is contained in:
108
scripts/tests/wechatAuthorization.test.mjs
Normal file
108
scripts/tests/wechatAuthorization.test.mjs
Normal file
@@ -0,0 +1,108 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { createRequire } from 'node:module'
|
||||
import ts from 'typescript'
|
||||
import * as vue from 'vue'
|
||||
import { compileScript, parse } from '@vue/compiler-sfc'
|
||||
|
||||
const require = createRequire(import.meta.url)
|
||||
const elementPlusRequire = createRequire(require.resolve('element-plus'))
|
||||
const Schema = elementPlusRequire('async-validator').default
|
||||
const { descriptor } = parse(
|
||||
readFileSync(
|
||||
'src/views/settings/payment-merchant-pools/components/WechatAuthorizationManagement.vue',
|
||||
'utf8'
|
||||
)
|
||||
)
|
||||
const compiled = ts.transpileModule(
|
||||
compileScript(descriptor, { id: 'wechat-authorization-test' }).content,
|
||||
{ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }
|
||||
).outputText
|
||||
|
||||
function harness() {
|
||||
const calls = []
|
||||
const imports = {
|
||||
vue: { ...vue, onMounted: () => {}, onUnmounted: () => {} },
|
||||
'element-plus': { ElMessage: { success: () => {} } },
|
||||
'@/api/modules': {
|
||||
PaymentMerchantPoolsService: {
|
||||
updateWechatAuthorization: async (payload) => {
|
||||
calls.push({ ...payload })
|
||||
return { code: 0, data: payload }
|
||||
}
|
||||
}
|
||||
},
|
||||
'@/composables/useAuth': { useAuth: () => ({ hasAuth: () => true }) },
|
||||
'@/config/constants/paymentMerchantPools': {
|
||||
PAYMENT_MERCHANT_POOL_PERMISSIONS: { wechatAuthEdit: 'wechat_auth_edit' }
|
||||
}
|
||||
}
|
||||
const module = { exports: {} }
|
||||
new Function('require', 'module', 'exports', 'computed', compiled)(
|
||||
(name) => {
|
||||
assert.ok(imports[name], `Unexpected runtime dependency: ${name}`)
|
||||
return imports[name]
|
||||
},
|
||||
module,
|
||||
module.exports,
|
||||
vue.computed
|
||||
)
|
||||
const state = module.exports.default.setup({}, { expose: () => {} })
|
||||
state.formRef.value = {
|
||||
validate: () => new Schema(state.formRules).validate(state.form)
|
||||
}
|
||||
return { state, calls }
|
||||
}
|
||||
|
||||
test('miniapp AppID starts empty without default or placeholder text', () => {
|
||||
const { state } = harness()
|
||||
assert.equal(state.form.miniapp_app_id, '')
|
||||
const input = descriptor.template.content.match(
|
||||
/<ElInput\b[^>]*v-model="form\.miniapp_app_id"[^>]*\/>/
|
||||
)?.[0]
|
||||
assert.ok(input)
|
||||
assert.doesNotMatch(input, /\b(?:placeholder|(?:default-)?value)\s*=/)
|
||||
})
|
||||
|
||||
for (const enabled of [false, true]) {
|
||||
for (const miniappAppId of ['', ' ', null, undefined]) {
|
||||
test(`saves ${enabled ? 'enabled' : 'disabled'} authorization without optional AppID ${JSON.stringify(miniappAppId)}`, async () => {
|
||||
const { state, calls } = harness()
|
||||
Object.assign(state.form, {
|
||||
enabled,
|
||||
miniapp_app_id: miniappAppId,
|
||||
oa_app_id: 'wx-official-account',
|
||||
oa_oauth_redirect_url: 'https://example.com/oauth'
|
||||
})
|
||||
await state.handleSubmit()
|
||||
|
||||
assert.deepEqual(calls, [
|
||||
{
|
||||
enabled,
|
||||
oa_app_id: 'wx-official-account',
|
||||
oa_oauth_redirect_url: 'https://example.com/oauth'
|
||||
}
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
test('submits a provided miniapp AppID after trimming and clears sensitive inputs after saving', async () => {
|
||||
const { state, calls } = harness()
|
||||
state.form.miniapp_app_id = ' wx-miniapp '
|
||||
state.sensitive.miniapp_app_secret = 'new-miniapp-secret'
|
||||
await state.handleSubmit()
|
||||
|
||||
assert.equal(calls[0].miniapp_app_id, 'wx-miniapp')
|
||||
assert.equal(calls[0].miniapp_app_secret, 'new-miniapp-secret')
|
||||
assert.equal(state.sensitive.miniapp_app_secret, '')
|
||||
})
|
||||
|
||||
test('still rejects a miniapp AppID longer than 64 characters', async () => {
|
||||
const { state, calls } = harness()
|
||||
state.form.miniapp_app_id = 'x'.repeat(65)
|
||||
await state.handleSubmit()
|
||||
|
||||
assert.deepEqual(calls, [])
|
||||
})
|
||||
@@ -23,8 +23,8 @@
|
||||
<ElSwitch v-model="form.enabled" active-text="启用" inactive-text="停用" />
|
||||
</ElFormItem>
|
||||
<ElDivider content-position="left">小程序授权</ElDivider>
|
||||
<ElFormItem label="小程序 AppID" prop="miniapp_app_id">
|
||||
<ElInput v-model="form.miniapp_app_id" maxlength="64" placeholder="请输入小程序 AppID" />
|
||||
<ElFormItem label="小程序 AppID(选填)" prop="miniapp_app_id">
|
||||
<ElInput v-model="form.miniapp_app_id" maxlength="64" />
|
||||
</ElFormItem>
|
||||
<ElFormItem label="小程序 AppSecret">
|
||||
<ElInput
|
||||
@@ -163,11 +163,15 @@
|
||||
|
||||
const payload: UpdateWechatAuthorizationRequest = {
|
||||
enabled: form.enabled,
|
||||
miniapp_app_id: form.miniapp_app_id?.trim() || '',
|
||||
oa_app_id: form.oa_app_id?.trim() || '',
|
||||
oa_oauth_redirect_url: form.oa_oauth_redirect_url?.trim() || ''
|
||||
}
|
||||
|
||||
const miniappAppId = form.miniapp_app_id?.trim()
|
||||
if (miniappAppId) {
|
||||
payload.miniapp_app_id = miniappAppId
|
||||
}
|
||||
|
||||
if (sensitive.miniapp_app_secret) {
|
||||
payload.miniapp_app_secret = sensitive.miniapp_app_secret
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user