初始化
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert/strict')
|
||||
const express = require('express')
|
||||
|
||||
const { createMockContext } = require('../src/context/createMockContext')
|
||||
const registerMemberProfileRoute = require('../src/routes/ewl/edip/memberProfile')
|
||||
|
||||
function withEnv(overrides, fn) {
|
||||
const previousValues = new Map()
|
||||
|
||||
for (const [key, value] of Object.entries(overrides)) {
|
||||
previousValues.set(key, process.env[key])
|
||||
if (value === undefined) {
|
||||
delete process.env[key]
|
||||
} else {
|
||||
process.env[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return fn()
|
||||
} finally {
|
||||
for (const [key, value] of previousValues.entries()) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key]
|
||||
} else {
|
||||
process.env[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function startMemberProfileApp(envOverrides = {}) {
|
||||
return withEnv(envOverrides, async () => {
|
||||
const context = createMockContext()
|
||||
const app = express()
|
||||
app.use(express.json({ limit: '2mb' }))
|
||||
registerMemberProfileRoute({ app, context })
|
||||
|
||||
const server = await new Promise((resolve) => {
|
||||
const instance = app.listen(0, '127.0.0.1', () => resolve(instance))
|
||||
})
|
||||
|
||||
const address = server.address()
|
||||
return {
|
||||
baseUrl: `http://${address.address}:${address.port}`,
|
||||
close: async () =>
|
||||
new Promise((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
return
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
}),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
test('member profile route returns task 15171 active card_num for playerId 600000154', async () => {
|
||||
const app = await startMemberProfileApp({
|
||||
MOCK_TASK_15171_ENABLED: 'true',
|
||||
})
|
||||
|
||||
try {
|
||||
const response = await fetch(`${app.baseUrl}/ewl/edip/v1/member/profile`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ schema: {}, data: { playerId: '600000154' } }),
|
||||
})
|
||||
const body = await response.json()
|
||||
|
||||
assert.equal(response.status, 200)
|
||||
assert.equal(body.status, true)
|
||||
assert.equal(body.data.playerId, '600000154')
|
||||
assert.deepEqual(body.data.card_num, ['2600000154002'])
|
||||
assert.ok(!body.data.card_num.includes('2600000154001'))
|
||||
} finally {
|
||||
await app.close()
|
||||
}
|
||||
})
|
||||
|
||||
test('member profile route returns active card_num when inactive card is queried', async () => {
|
||||
const app = await startMemberProfileApp({
|
||||
MOCK_TASK_15171_ENABLED: 'true',
|
||||
})
|
||||
|
||||
try {
|
||||
const response = await fetch(`${app.baseUrl}/ewl/edip/v1/member/profile`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ schema: {}, data: { accountNum: '2600000154001' } }),
|
||||
})
|
||||
const body = await response.json()
|
||||
|
||||
assert.equal(response.status, 200)
|
||||
assert.equal(body.data.playerId, '600000154')
|
||||
assert.deepEqual(body.data.card_num, ['2600000154002'])
|
||||
} finally {
|
||||
await app.close()
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user