ropClient.test.mjs
5.12 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRopClient } from './support/ropClient.mjs'
const TEST_BASE = 'http://dj.test.glsx.com.cn/rop-dj-car-boutique'
const METHOD = 'dj.api.smartcarlife.order.createWithPay'
const jsonResponse = value => ({
ok: true,
status: 200,
async text() {
return JSON.stringify(value)
}
})
const createHarness = ({ response = { code: 0, data: { id: 7 } }, fetchError } = {}) => {
const signerCalls = []
const fetchCalls = []
const logCalls = []
const credentials = {
appKey: 'test-app-key',
secret: 'test-secret',
sign(params, secret, noSignAttr) {
signerCalls.push({ params: { ...params }, secret, noSignAttr })
return { ...params, sign: 'test-signature' }
}
}
const fetchImpl = async (url, options) => {
fetchCalls.push({ url, options })
if (fetchError) throw fetchError
return jsonResponse(response)
}
const logger = {
debug(label, details) {
logCalls.push({ label, details })
}
}
return { credentials, fetchImpl, logger, signerCalls, fetchCalls, logCalls }
}
describe('signed ROP test client', () => {
it('IT-OL-ROP-001: signs and form-encodes required common fields', async () => {
const harness = createHarness()
const client = createRopClient({ baseUrl: TEST_BASE, ...harness })
const result = await client.post(METHOD, { marker: 'case-1' })
assert.deepEqual(result, { code: 0, data: { id: 7 } })
assert.equal(harness.signerCalls.length, 1)
assert.deepEqual(harness.signerCalls[0].params, {
marker: 'case-1',
v: '1.0.0',
method: METHOD,
format: 'json',
locale: 'zh_CN',
source: 'dj-wechat',
appKey: 'test-app-key'
})
assert.equal(harness.signerCalls[0].secret, 'test-secret')
assert.deepEqual(harness.signerCalls[0].noSignAttr, [])
assert.equal(harness.fetchCalls.length, 1)
assert.equal(
harness.fetchCalls[0].url,
`${TEST_BASE}/router?method=${encodeURIComponent(METHOD)}`
)
assert.equal(harness.fetchCalls[0].options.method, 'POST')
assert.equal(
harness.fetchCalls[0].options.headers['content-type'],
'application/x-www-form-urlencoded'
)
const body = new URLSearchParams(harness.fetchCalls[0].options.body)
assert.equal(body.get('marker'), 'case-1')
assert.equal(body.get('method'), METHOD)
assert.equal(body.get('sign'), 'test-signature')
})
it('IT-OL-ROP-002: business failure remains available to workflow assertions', async () => {
const response = { code: -1, message: 'business rejected', data: null }
const harness = createHarness({ response })
const client = createRopClient({ baseUrl: TEST_BASE, ...harness })
assert.deepEqual(await client.post(METHOD, { marker: 'case-2' }), response)
})
it('IT-OL-ROP-003: invalid JSON error names method without leaking request data', async () => {
const harness = createHarness()
harness.fetchImpl = async () => ({
ok: true,
status: 200,
async text() {
return 'not-json'
}
})
const client = createRopClient({ baseUrl: TEST_BASE, ...harness })
await assert.rejects(
client.post(METHOD, { customerPhone: 'private-value' }),
error => {
assert.match(error.message, new RegExp(METHOD.replaceAll('.', '\\.')))
assert.doesNotMatch(error.message, /private-value/)
return true
}
)
})
it('IT-OL-ROP-004: network error is sanitized', async () => {
const harness = createHarness({
fetchError: new Error('network failed with private-value')
})
const client = createRopClient({ baseUrl: TEST_BASE, ...harness })
await assert.rejects(client.post(METHOD, { sessionId: 'private-value' }), error => {
assert.match(error.message, new RegExp(METHOD.replaceAll('.', '\\.')))
assert.doesNotMatch(error.message, /private-value/)
return true
})
})
it('IT-OL-ROP-005: diagnostic logs are recursively redacted', async () => {
const harness = createHarness()
const client = createRopClient({ baseUrl: TEST_BASE, ...harness })
await client.post(METHOD, {
mobile: 'private-mobile',
nested: { sessionId: 'private-session', ordinary: 'visible' }
})
const serialized = JSON.stringify(harness.logCalls)
assert.doesNotMatch(serialized, /private-mobile|private-session|test-app-key|test-signature/)
assert.match(serialized, /visible/)
})
it('IT-OL-ROP-006: project adapter loads the selected test signer without exposing values', async () => {
const { loadProjectRopCredentials } = await import(
'./support/projectRopCredentials.mjs'
)
const credentials = await loadProjectRopCredentials()
assert.equal(typeof credentials.appKey, 'string')
assert.ok(credentials.appKey.length > 0)
assert.equal(typeof credentials.secret, 'string')
assert.ok(credentials.secret.length > 0)
assert.equal(typeof credentials.sign, 'function')
const signed = await credentials.sign(
{ appKey: credentials.appKey, method: 'dj.api.test', marker: 'adapter' },
credentials.secret,
[]
)
assert.match(signed.sign, /^[0-9A-F]{40}$/)
})
})