mirror of
https://github.com/Such-Software/wownerogue.git
synced 2026-12-02 11:54:04 +01:00
add error handling and more tests
This commit is contained in:
@@ -1,30 +1,52 @@
|
||||
const PaymentHandlers = require('../src/network/paymentHandlers');
|
||||
|
||||
function makeMocks() {
|
||||
const user = { id: 'user-1', clientId: 'client1', payout_address: 'wow1address' };
|
||||
return {
|
||||
io: { to: () => ({ emit: () => {} }) },
|
||||
gameModeManager: {
|
||||
gameMode: 'PAID_SINGLE',
|
||||
cryptoType: 'WOW',
|
||||
currencyDecimals: 11,
|
||||
singleGamePrice: 1000,
|
||||
createPaymentRequest: async () => ({ id: 'pid123', address: 'addr', amount: 1000, expiresAt: new Date() })
|
||||
creditsPayoutEnabled: false,
|
||||
formatAtomicHuman: jest.fn(() => '0.001'),
|
||||
getOrCreateUser: jest.fn(async () => user),
|
||||
createPaymentRequest: async () => ({
|
||||
id: 'pid123',
|
||||
address: 'addr',
|
||||
amount: 1000,
|
||||
amountFormatted: '0.001',
|
||||
expiresAt: new Date()
|
||||
})
|
||||
},
|
||||
walletService: {
|
||||
startPaymentMonitoring: (addr, cb) => { /* simulate nothing */ },
|
||||
stopPaymentMonitoring: () => {}
|
||||
startPaymentMonitoring: jest.fn(),
|
||||
stopPaymentMonitoring: jest.fn()
|
||||
},
|
||||
debugManager: { getCurrentBlockHeight: () => 123, CONSOLE_LOGGING: false },
|
||||
queueManager: {
|
||||
getUserBySocket: () => ({ clientId: 'client1' }),
|
||||
getUserBySocket: () => user,
|
||||
getPlayerIndex: () => -1,
|
||||
addPlayer: () => {},
|
||||
markConfirmed: () => {}
|
||||
getQueueLength: () => 0,
|
||||
addPlayer: jest.fn(),
|
||||
markConfirmed: jest.fn(),
|
||||
startGameImmediately: jest.fn(() => false)
|
||||
},
|
||||
broadcastManager: { sendStatusUpdate: () => {} }
|
||||
broadcastManager: { sendStatusUpdate: jest.fn() }
|
||||
};
|
||||
}
|
||||
|
||||
describe('PaymentHandlers basic', () => {
|
||||
let ph;
|
||||
|
||||
afterEach(() => {
|
||||
if (ph && typeof ph.dispose === 'function') {
|
||||
ph.dispose();
|
||||
}
|
||||
ph = null;
|
||||
});
|
||||
|
||||
test('createAndShowPaymentRequest emits payment_created', async () => {
|
||||
const mocks = makeMocks();
|
||||
const emitted = {};
|
||||
@@ -33,10 +55,6 @@ describe('PaymentHandlers basic', () => {
|
||||
await ph.createAndShowPaymentRequest({ id: 'sock1' });
|
||||
expect(emitted.payment_created).toBeDefined();
|
||||
expect(emitted.payment_created.paymentId).toBe('pid123');
|
||||
});
|
||||
afterAll(() => {
|
||||
if (ph && typeof ph.dispose === 'function') {
|
||||
ph.dispose();
|
||||
}
|
||||
expect(mocks.walletService.startPaymentMonitoring).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -36,7 +36,7 @@ describe('Payment flow integration: confirm-after-block immediate start', () =>
|
||||
};
|
||||
|
||||
// User lookup
|
||||
const userObj = { id: socketId, clientId: 'client-int' };
|
||||
const userObj = { id: socketId, clientId: 'client-int', payout_address: 'wow1address' };
|
||||
const getUserBySocket = (id) => id === socketId ? userObj : null;
|
||||
|
||||
// createGameForUser mock (records active game & returns state)
|
||||
@@ -52,8 +52,18 @@ describe('Payment flow integration: confirm-after-block immediate start', () =>
|
||||
const gameModeManager = {
|
||||
gameMode: 'PAID_SINGLE',
|
||||
cryptoType: 'WOW',
|
||||
currencyDecimals: 11,
|
||||
singleGamePrice: 1000,
|
||||
createPaymentRequest: async () => ({ id: 'pay-1', address: 'addr-1', amount: 1000, expiresAt: new Date(Date.now()+60000) })
|
||||
creditsPayoutEnabled: false,
|
||||
formatAtomicHuman: () => '0.001',
|
||||
getOrCreateUser: async () => ({ id: socketId, payout_address: 'wow1address', clientId: 'client-int' }),
|
||||
createPaymentRequest: async () => ({
|
||||
id: 'pay-1',
|
||||
address: 'addr-1',
|
||||
amount: 1000,
|
||||
amountFormatted: '0.001',
|
||||
expiresAt: new Date(Date.now()+60000)
|
||||
})
|
||||
};
|
||||
|
||||
// Wallet service mock with manual callback control
|
||||
|
||||
@@ -138,7 +138,8 @@ describe('SQL Injection Prevention', () => {
|
||||
// Verify only safe fields were updated
|
||||
const updatedUser = await sessionManager.getBySocket('test-update-socket');
|
||||
expect(updatedUser.credits).toBe(50);
|
||||
expect(updatedUser.id).toBe(testSession.user.id); // Should not have changed
|
||||
expect(updatedUser.id).not.toBe(dangerousUpdates.id); // Should ignore malicious id change
|
||||
expect(updatedUser.socket_id).toBe('test-update-socket');
|
||||
});
|
||||
|
||||
test('should generate cryptographically secure tokens', () => {
|
||||
|
||||
@@ -4,6 +4,7 @@ const WalletRPCService = require('../src/payments/walletRPCService');
|
||||
const debugManagerMock = { CONSOLE_LOGGING: false };
|
||||
|
||||
describe('WalletRPCService interface', () => {
|
||||
|
||||
test('exposes expected methods', () => {
|
||||
const svc = new WalletRPCService(debugManagerMock);
|
||||
expect(typeof svc.initialize).toBe('function');
|
||||
@@ -20,4 +21,26 @@ describe('WalletRPCService interface', () => {
|
||||
const res = await svc.processBatchPayouts();
|
||||
expect(res).toEqual({ processed: 0 });
|
||||
});
|
||||
|
||||
test('rpcCall wraps failures in ExternalServiceError', async () => {
|
||||
const svc = new WalletRPCService(debugManagerMock);
|
||||
svc.walletEndpoint = 'http://127.0.0.1:65535';
|
||||
|
||||
await expect(svc.rpcCall('get_height')).rejects.toMatchObject({
|
||||
code: 'EXTERNAL_SERVICE_ERROR',
|
||||
safeMessage: 'Wallet RPC call failed.'
|
||||
});
|
||||
});
|
||||
|
||||
test('createPaymentRequest rethrows wrapped errors', async () => {
|
||||
const svc = new WalletRPCService(debugManagerMock);
|
||||
svc.rpcCall = jest.fn().mockRejectedValue(new Error('rpc unavailable'));
|
||||
|
||||
await expect(
|
||||
svc.createPaymentRequest(10, 'test payment', 'user-1', 'socket-1')
|
||||
).rejects.toMatchObject({
|
||||
code: 'EXTERNAL_SERVICE_ERROR',
|
||||
safeMessage: 'Unable to create a payment address at this time.'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user