104 lines
2.8 KiB
JavaScript
104 lines
2.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { buildApp } from '../dist/app.js';
|
|
import { signAccessToken } from '../dist/auth/jwt.js';
|
|
|
|
const secret = 'test-only-recharge-route-secret-32';
|
|
const token = signAccessToken({
|
|
sub: '21', sid: '5c4d3af8-c63c-4edb-bf95-b84127bb3f6e',
|
|
tid: '7', aid: '9', rv: 1
|
|
}, secret, 900);
|
|
|
|
let createdInput;
|
|
let listInput;
|
|
const app = await buildApp({
|
|
recharge: {
|
|
jwtSecret: secret,
|
|
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: ''
|
|
}
|
|
};
|
|
}
|
|
},
|
|
accessControl: {
|
|
async getAccessProfile() {
|
|
return { roles: ['CUSTOMER'], capabilities: ['profile.read'], storeIds: [] };
|
|
}
|
|
},
|
|
service: {
|
|
async listAvailablePlans(input) {
|
|
listInput = input;
|
|
return [{
|
|
planId: '31',
|
|
storeId: '11',
|
|
name: 'charge 100 gift 20',
|
|
payAmountCents: 10000,
|
|
giftAmountCents: 2000,
|
|
scopeType: 'STORE'
|
|
}];
|
|
},
|
|
async createRechargeOrder(input) {
|
|
createdInput = input;
|
|
return {
|
|
rechargeOrderId: '901',
|
|
planId: input.planId,
|
|
rechargeNo: 'RCHTEST',
|
|
status: 'PENDING_PAYMENT',
|
|
payAmountCents: 10000,
|
|
giftAmountCents: 2000,
|
|
idempotent: false
|
|
};
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const plans = await app.inject({
|
|
method: 'GET',
|
|
url: '/app-api/recharge/plans?storeId=11',
|
|
headers: { authorization: `Bearer ${token}` }
|
|
});
|
|
assert.equal(plans.statusCode, 200);
|
|
assert.deepEqual(listInput, { tenantId: '7', storeId: '11' });
|
|
assert.equal(plans.json().data[0].payAmountCents, 10000);
|
|
|
|
const created = await app.inject({
|
|
method: 'POST',
|
|
url: '/app-api/recharge/orders',
|
|
headers: { authorization: `Bearer ${token}`, 'x-trace-id': 'm08a-recharge-order' },
|
|
payload: {
|
|
planId: '31',
|
|
storeId: '11',
|
|
clientRequestId: 'miniapp-recharge-001'
|
|
}
|
|
});
|
|
assert.equal(created.statusCode, 201);
|
|
assert.equal(createdInput.tenantId, '7');
|
|
assert.equal(createdInput.userId, '21');
|
|
assert.equal(createdInput.planId, '31');
|
|
assert.equal(createdInput.traceId, 'm08a-recharge-order');
|
|
|
|
const invalid = await app.inject({
|
|
method: 'POST',
|
|
url: '/app-api/recharge/orders',
|
|
headers: { authorization: `Bearer ${token}` },
|
|
payload: { planId: 'bad' }
|
|
});
|
|
assert.equal(invalid.statusCode, 400);
|
|
|
|
await app.close();
|
|
console.log('PASS: M08-A recharge routes expose available plans and customer order creation.');
|