agmission/Development/server/tests/integration/billing.integration.test.js
Devin Major 6ff07c9d74
All checks were successful
Server Tests / Mocha – Unit & Utility Tests (push) Successful in 1m13s
Server Tests / Jest – Integration Tests (push) Successful in 1m43s
additional integration tests
2026-05-08 14:51:04 -04:00

149 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* Integration tests billing controller (controllers/billing.js)
*/
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
const { mockApplicator, mockReq, mockRes } = require('./mock_data');
let Customer;
beforeAll(async () => {
await connectDB();
Customer = require('../../model/customer');
});
afterAll(async () => {
await disconnectDB();
});
const billingCtl = require('../../controllers/billing');
describe('billing controller getCustUsage_post', () => {
let applicator;
beforeAll(async () => {
applicator = await Customer.create(mockApplicator());
});
afterAll(async () => {
await Customer.deleteMany({ _id: applicator._id });
});
const makeReq = (bodyOverrides = {}) =>
mockReq({
uid: applicator._id,
puid: applicator._id,
body: {
from: '2024-01-01',
to: '2024-12-31',
tz: 'America/Chicago',
...bodyOverrides,
},
});
it('throws AppParamError when body is null', async () => {
const req = mockReq({ body: null });
const res = mockRes();
await expect(billingCtl.getCustUsage_post(req, res)).rejects.toThrow();
});
it('throws AppParamError when from date is missing', async () => {
const req = makeReq({ from: undefined });
const res = mockRes();
await expect(billingCtl.getCustUsage_post(req, res)).rejects.toThrow();
});
it('throws AppParamError when to date is missing', async () => {
const req = makeReq({ to: undefined });
const res = mockRes();
await expect(billingCtl.getCustUsage_post(req, res)).rejects.toThrow();
});
it('throws AppParamError when from is an invalid date string', async () => {
const req = makeReq({ from: 'not-a-date' });
const res = mockRes();
await expect(billingCtl.getCustUsage_post(req, res)).rejects.toThrow();
});
it('returns an empty array when no customers have jobs in the date range', async () => {
const req = makeReq();
const res = mockRes();
await billingCtl.getCustUsage_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
// No active customers have any jobs in the test DB for this date range
expect(res._data.length).toBe(0);
});
it('accepts ISO datetime strings for from/to', async () => {
const req = makeReq({
from: '2024-01-01T00:00:00.000Z',
to: '2024-12-31T23:59:59.999Z',
});
const res = mockRes();
await billingCtl.getCustUsage_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
it('billable filter is accepted without error', async () => {
const req = makeReq({ billable: true });
const res = mockRes();
await billingCtl.getCustUsage_post(req, res);
expect(res.json).toHaveBeenCalled();
expect(Array.isArray(res._data)).toBe(true);
});
});
describe('billing controller exportUsageDetail_post', () => {
let applicator;
beforeAll(async () => {
applicator = await Customer.create(mockApplicator());
});
afterAll(async () => {
await Customer.deleteMany({ _id: applicator._id });
});
it('throws when from date is missing', async () => {
const req = mockReq({
uid: applicator._id,
puid: applicator._id,
body: { to: '2024-12-31' },
});
const res = mockRes();
await expect(billingCtl.exportUsageDetail_post(req, res)).rejects.toThrow();
});
it('throws when body is null', async () => {
const req = mockReq({ body: null });
const res = mockRes();
await expect(billingCtl.exportUsageDetail_post(req, res)).rejects.toThrow();
});
});
describe('billing controller getCustBillingStatus_get', () => {
it('returns a billing status object with expected fields', async () => {
const req = mockReq();
const res = mockRes();
billingCtl.getCustBillingStatus_get(req, res);
expect(res.json).toHaveBeenCalled();
const data = res._data;
expect(data).toHaveProperty('premium');
expect(data).toHaveProperty('status');
expect(data).toHaveProperty('start');
expect(data).toHaveProperty('next_cycle');
});
});