mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 11:06:28 +00:00
78 lines
3.4 KiB
JavaScript
78 lines
3.4 KiB
JavaScript
const express = require('express');
|
|
const request = require('supertest');
|
|
const helper = require('../helpers/helper');
|
|
const model = require('../modules/reservebalance/reservebalance.model');
|
|
const audit = require('../helpers/response-audit');
|
|
const validate = require('../modules/reservebalance/reservebalance.validation');
|
|
const { reservationV2 } = require('../modules/reservebalance/reservation-create.handlers');
|
|
const { correlation, response } = require('../middlewares/response-telemetry.middleware');
|
|
|
|
jest.mock('../../infrastructure/database/knex', () => jest.fn());
|
|
jest.mock('../helpers/response-audit', () => ({ record: jest.fn() }));
|
|
jest.mock('../helpers/helper', () => ({
|
|
getUniqueCode: jest.fn(() => '123'),
|
|
decryptText: jest.fn(),
|
|
}));
|
|
jest.mock('../helpers/winston.logger', () => ({
|
|
logger: { info: jest.fn(), warn: jest.fn(), error: jest.fn() },
|
|
}));
|
|
jest.mock('../modules/reservebalance/reservebalance.model', () => ({
|
|
getCardBlacklist: jest.fn(),
|
|
getPartner: jest.fn(),
|
|
getCheckByCardProductStatusValue: jest.fn(),
|
|
getLastBalanceFromCardHistory: jest.fn(),
|
|
getBIRegulationStatus: jest.fn(),
|
|
getMaxReservation: jest.fn(),
|
|
getReservationPendingV2: jest.fn(),
|
|
getIssuer: jest.fn(),
|
|
getReservationExpiration: jest.fn(),
|
|
allocateReservationNumber: jest.fn(),
|
|
insertReservation: jest.fn(),
|
|
createReserveBalanceLog: jest.fn(),
|
|
}));
|
|
|
|
const app = express();
|
|
app.use(correlation, response, express.json());
|
|
app.post('/api/reservation/v2', (req, res, next) => {
|
|
req.partner_id_token = 'fixture-partner';
|
|
next();
|
|
}, validate('reservationV2'), reservationV2);
|
|
|
|
function createReservation() {
|
|
return request(app).post('/api/reservation/v2')
|
|
.send({ reservation_data: 'fixture-ciphertext', ref_no: 'fixture-ref' });
|
|
}
|
|
|
|
beforeEach(() => jest.clearAllMocks());
|
|
|
|
test.each(['invalid JSON', 'null', '[]'])('payload reservasi %s ditolak tanpa akses database', async (plaintext) => {
|
|
helper.decryptText.mockReturnValue(plaintext);
|
|
const result = await createReservation().expect(200);
|
|
expect(result.body).toEqual({ code: '435', message: 'bad request. reservation data is invalid.', data: {} });
|
|
expect(model.getCardBlacklist).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test('kegagalan alokasi mempertahankan respons dan mencatat tahap serta kode aman di audit', async () => {
|
|
helper.decryptText.mockReturnValue(JSON.stringify({
|
|
mid: 'A001', tid: 'B001', card_number: '0000000000000001', amount: 10000, transaction_date: '2026-11-25',
|
|
}));
|
|
model.getPartner.mockResolvedValue({ i_partner: 'fixture-partner', partner_code: 'P01' });
|
|
model.getBIRegulationStatus.mockResolvedValue({ c_value: 'N' });
|
|
model.getMaxReservation.mockResolvedValue({ c_value: '100000' });
|
|
model.getReservationPendingV2.mockResolvedValue([]);
|
|
model.getReservationExpiration.mockResolvedValue({ time: 1, measure: 'hours' });
|
|
model.allocateReservationNumber.mockRejectedValue(Object.assign(new Error('private database detail'), { code: '42P01' }));
|
|
|
|
const result = await createReservation().expect(200);
|
|
expect(result.body.code).toBe('500');
|
|
expect(model.insertReservation).not.toHaveBeenCalled();
|
|
const [event] = audit.record.mock.lastCall;
|
|
expect(event).toMatchObject({
|
|
request_id: result.headers['x-request-id'],
|
|
status_code: '500',
|
|
error_code: '42P01',
|
|
error_stage: 'allocate_reservation_number',
|
|
});
|
|
expect(JSON.stringify(event)).not.toContain('private database detail');
|
|
});
|