交付双端基础、实时联系人和隔离浏览器
This commit is contained in:
1 parent
43687d5e7e
commit
67fef3ff87
59 files changed
+5094
-1
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { generateKeyPairSync, randomBytes, sign } from 'node:crypto'
|
||||
import { once } from 'node:events'
|
||||
import test from 'node:test'
|
||||
|
||||
const base = process.env.XHCHAT_TEST_URL ?? 'http://127.0.0.1:8080'
|
||||
const wsBase = base.replace(/^http/, 'ws')
|
||||
const newUsername = () => 'qa_' + randomBytes(6).toString('hex')
|
||||
const newDevice = () => generateKeyPairSync('ed25519')
|
||||
const password = randomBytes(24).toString('base64url')
|
||||
|
||||
async function call(path, { method = 'GET', token, body, origin } = {}) {
|
||||
return fetch(base + '/api/v1' + path, {
|
||||
method,
|
||||
headers: {
|
||||
...(token && { Authorization: 'Bearer ' + token }),
|
||||
...(origin && { Origin: origin }),
|
||||
...(body && { 'Content-Type': 'application/json' }),
|
||||
},
|
||||
body: body && JSON.stringify(body),
|
||||
signal: AbortSignal.timeout(10_000),
|
||||
})
|
||||
}
|
||||
|
||||
async function proof(username, pair, platform = 'web') {
|
||||
const reply = await call('/auth/challenge', { method: 'POST' })
|
||||
assert.equal(reply.status, 200)
|
||||
const { challenge } = await reply.json()
|
||||
return {
|
||||
username, password, challenge, platform, deviceName: '测试设备',
|
||||
publicKey: pair.publicKey.export({ format: 'jwk' }).x,
|
||||
signature: sign(null, Buffer.from('XHChat device authentication v1\n' + challenge), pair.privateKey).toString('base64url'),
|
||||
}
|
||||
}
|
||||
|
||||
async function login(route, body, expected) {
|
||||
const reply = await call('/auth/' + route, { method: 'POST', body })
|
||||
assert.equal(reply.status, expected, await reply.clone().text())
|
||||
return reply.json()
|
||||
}
|
||||
|
||||
async function upgrade(ticket, origin = base) {
|
||||
const { WebSocket } = await import('ws')
|
||||
return new WebSocket(wsBase + '/api/v1/realtime', ['xhchat.v1', 'ticket.' + ticket], { origin })
|
||||
}
|
||||
|
||||
test('真实数据库注册、跨平台设备、单次挑战及 WebSocket 票据、撤销与退出', { timeout: 25000 }, async () => {
|
||||
assert.equal((await fetch(base + '/readyz')).status, 200)
|
||||
const username = newUsername()
|
||||
const web = newDevice()
|
||||
const desktop = newDevice()
|
||||
const other = newDevice()
|
||||
const initial = await proof(username, web)
|
||||
const registered = await login('register', initial, 201)
|
||||
await login('register', initial, 400)
|
||||
await login('register', await proof(username, other), 409)
|
||||
const wrong = await proof(username, web)
|
||||
await login('login', { ...wrong, password: password + 'bad' }, 401)
|
||||
const second = await login('login', await proof(username, desktop, 'windows'), 200)
|
||||
assert.equal(second.user.id, registered.user.id)
|
||||
assert.notEqual(second.device.id, registered.device.id)
|
||||
const foreign = await login('register', await proof(newUsername(), other), 201)
|
||||
const token = registered.token
|
||||
const ticketReply = await call('/realtime/ticket', { method: 'POST', token: second.token })
|
||||
assert.equal(ticketReply.status, 200)
|
||||
const { ticket } = await ticketReply.json()
|
||||
const socket = await upgrade(ticket)
|
||||
try {
|
||||
const [event] = await once(socket, 'message')
|
||||
assert.deepEqual(JSON.parse(event), { type: 'ready', deviceId: second.device.id })
|
||||
const replay = await upgrade(ticket)
|
||||
const [, denied] = await once(replay, 'unexpected-response')
|
||||
assert.equal(denied.statusCode, 401)
|
||||
replay.on('error', () => {})
|
||||
replay.terminate()
|
||||
const devices = await (await call('/devices', { token })).json()
|
||||
assert.equal(devices.length, 2)
|
||||
assert.ok(devices.every((device) => device.signedIn))
|
||||
assert.equal((await call('/devices/' + second.device.id, { method: 'DELETE', token: foreign.token })).status, 404)
|
||||
assert.equal((await call('/me', { token, origin: 'https://untrusted.invalid' })).status, 403)
|
||||
const closed = once(socket, 'close')
|
||||
assert.equal((await call('/devices/' + second.device.id, { method: 'DELETE', token })).status, 204)
|
||||
assert.equal((await call('/me', { token: second.token })).status, 401)
|
||||
const afterRevoke = await (await call('/devices', { token })).json()
|
||||
assert.equal(afterRevoke.find((device) => device.id === second.device.id).signedIn, false)
|
||||
assert.equal((await closed)[0], 1008)
|
||||
await login('login', await proof(username, desktop, 'windows'), 403)
|
||||
assert.equal((await call('/auth/logout', { method: 'POST', token })).status, 204)
|
||||
assert.equal((await call('/me', { token })).status, 401)
|
||||
} finally {
|
||||
socket.terminate()
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user