99 lines
3.1 KiB
JavaScript
99 lines
3.1 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { buildApp } from '../dist/app.js';
|
|
import { loadConfig } from '../dist/config.js';
|
|
import { signAccessToken } from '../dist/auth/jwt.js';
|
|
|
|
assert.equal(loadConfig({
|
|
NODE_ENV: 'production',
|
|
QIPAI_MQTT_USERNAME: 'backend-test',
|
|
QIPAI_MQTT_PASSWORD: 'not-a-real-secret',
|
|
QIPAI_JWT_SECRET: 'production-test-secret-that-is-long-enough',
|
|
QIPAI_TEST_PAYMENT_ENABLED: 'true'
|
|
}).payment.testAdapterEnabled, false);
|
|
assert.equal(loadConfig({
|
|
NODE_ENV: 'test',
|
|
QIPAI_TEST_PAYMENT_ENABLED: 'true'
|
|
}).payment.testAdapterEnabled, true);
|
|
|
|
const secret = 'test-only-payment-jwt-secret-32-chars';
|
|
const token = signAccessToken({
|
|
sub: '21', sid: '5c4d3af8-c63c-4edb-bf95-b84127bb3f6e',
|
|
tid: '7', aid: '9', rv: 1
|
|
}, secret, 900);
|
|
let createInput;
|
|
let callbackInput;
|
|
const authRepository = {
|
|
async validateSession() {
|
|
return {
|
|
id: '5c4d3af8-c63c-4edb-bf95-b84127bb3f6e',
|
|
tenantId: '7', platformAppId: '9', expiresAt: new Date(Date.now() + 60000),
|
|
user: {
|
|
id: '21', tenantId: '7', userType: 'CUSTOMER', status: 'ACTIVE',
|
|
roleVersion: 1, nickname: '', avatarUrl: '', phone: ''
|
|
}
|
|
};
|
|
}
|
|
};
|
|
const app = await buildApp({
|
|
payment: {
|
|
jwtSecret: secret,
|
|
authRepository,
|
|
testAdapterEnabled: true,
|
|
repository: {
|
|
async createPayment(input) {
|
|
createInput = input;
|
|
return {
|
|
paymentId: '51', orderId: input.orderId, provider: input.provider,
|
|
status: 'PENDING', amountCents: 3600
|
|
};
|
|
},
|
|
async processTestCallback(input) {
|
|
callbackInput = input;
|
|
return { paymentId: input.paymentId, status: 'SUCCEEDED', idempotent: false };
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const rejectedAmount = await app.inject({
|
|
method: 'POST',
|
|
url: '/app-api/payments',
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: {
|
|
orderId: '31', provider: 'TEST', clientRequestId: 'request-0001',
|
|
amountCents: 1
|
|
}
|
|
});
|
|
assert.equal(rejectedAmount.statusCode, 400);
|
|
|
|
const created = await app.inject({
|
|
method: 'POST',
|
|
url: '/app-api/payments',
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { orderId: '31', provider: 'TEST', clientRequestId: 'request-0001' }
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
assert.equal(createInput.platformAppId, '9');
|
|
assert.equal('amountCents' in createInput, false);
|
|
|
|
const balanceCreated = await app.inject({
|
|
method: 'POST',
|
|
url: '/app-api/payments',
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { orderId: '31', provider: 'BALANCE', clientRequestId: 'request-balance-0001' }
|
|
});
|
|
assert.equal(balanceCreated.statusCode, 201);
|
|
assert.equal(createInput.provider, 'BALANCE');
|
|
|
|
const completed = await app.inject({
|
|
method: 'POST',
|
|
url: '/app-api/payments/51/test-complete',
|
|
headers: { authorization: `Bearer ${token}`, 'x-trace-id': 'm05a-test-callback' },
|
|
payload: { callbackId: 'callback-0001', amountCents: 3600 }
|
|
});
|
|
assert.equal(completed.statusCode, 200);
|
|
assert.equal(callbackInput.traceId, 'm05a-test-callback');
|
|
|
|
await app.close();
|
|
console.log('PASS: M05-A payment routes trust server amounts and gate the test adapter.');
|