Refactor billing configuration and add database setup in tests

This commit is contained in:
Simon Larsen
2023-12-25 18:31:23 +00:00
parent e57963cad1
commit 0af8d7359b
4 changed files with 41 additions and 37 deletions

View File

@@ -0,0 +1,13 @@
const IsBillingEnabled: boolean =
process.env['BILLING_ENABLED'] === 'true';
const BillingPublicKey: string = process.env['BILLING_PUBLIC_KEY'] || '';
const BillingPrivateKey: string =
process.env['BILLING_PRIVATE_KEY'] || '';
export default {
IsBillingEnabled,
BillingPublicKey,
BillingPrivateKey,
}

View File

@@ -3,16 +3,16 @@ import Port from 'Common/Types/Port';
import Hostname from 'Common/Types/API/Hostname';
import SubscriptionPlan from 'Common/Types/Billing/SubscriptionPlan';
import { JSONObject } from 'Common/Types/JSON';
import BillingConfig from './BillingConfig';
export const getAllEnvVars: () => JSONObject = (): JSONObject => {
return process.env;
};
export const IsBillingEnabled: boolean =
process.env['BILLING_ENABLED'] === 'true';
export const BillingPublicKey: string = process.env['BILLING_PUBLIC_KEY'] || '';
export const IsBillingEnabled: boolean = BillingConfig.IsBillingEnabled;
export const BillingPublicKey: string = BillingConfig.BillingPublicKey;;
export const BillingPrivateKey: string =
process.env['BILLING_PRIVATE_KEY'] || '';
BillingConfig.BillingPrivateKey;
export const DatabaseHost: Hostname = Hostname.fromString(
process.env['DATABASE_HOST'] || 'postgres'
@@ -62,50 +62,42 @@ export const ClusterKey: ObjectID = new ObjectID(
export const HasClusterKey: boolean = Boolean(process.env['ONEUPTIME_SECRET']);
export const RealtimeHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_REALTIME_HOSTNAME'] || 'localhost'}:${
process.env['REALTIME_PORT'] || 80
`${process.env['SERVER_REALTIME_HOSTNAME'] || 'localhost'}:${process.env['REALTIME_PORT'] || 80
}`
);
export const WorkerHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_WORKERS_HOSTNAME'] || 'localhost'}:${
process.env['WORKERS_PORT'] || 80
`${process.env['SERVER_WORKERS_HOSTNAME'] || 'localhost'}:${process.env['WORKERS_PORT'] || 80
}`
);
export const WorkflowHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_WORKFLOW_HOSTNAME'] || 'localhost'}:${
process.env['WORKFLOW_PORT'] || 80
`${process.env['SERVER_WORKFLOW_HOSTNAME'] || 'localhost'}:${process.env['WORKFLOW_PORT'] || 80
}`
);
export const DashboardApiHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_DASHBOARD_API_HOSTNAME'] || 'localhost'}:${
process.env['DASHBOARD_API_PORT'] || 80
`${process.env['SERVER_DASHBOARD_API_HOSTNAME'] || 'localhost'}:${process.env['DASHBOARD_API_PORT'] || 80
}`
);
export const IngestorHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_INGESTOR_HOSTNAME'] || 'localhost'}:${
process.env['INGESTOR_PORT'] || 80
`${process.env['SERVER_INGESTOR_HOSTNAME'] || 'localhost'}:${process.env['INGESTOR_PORT'] || 80
}`
);
export const AccountsHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_ACCOUNTS_HOSTNAME'] || 'localhost'}:${
process.env['ACCOUNTS_PORT'] || 80
`${process.env['SERVER_ACCOUNTS_HOSTNAME'] || 'localhost'}:${process.env['ACCOUNTS_PORT'] || 80
}`
);
export const HomeHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_HOME_HOSTNAME'] || 'localhost'}:${
process.env['HOME_PORT'] || 80
`${process.env['SERVER_HOME_HOSTNAME'] || 'localhost'}:${process.env['HOME_PORT'] || 80
}`
);
export const DashboardHostname: Hostname = Hostname.fromString(
`${process.env['SERVER_DASHBOARD_HOSTNAME'] || 'localhost'}:${
process.env['DASHBOARD_PORT'] || 80
`${process.env['SERVER_DASHBOARD_HOSTNAME'] || 'localhost'}:${process.env['DASHBOARD_PORT'] || 80
}`
);

View File

@@ -34,6 +34,7 @@ import {
Subscription,
} from '../TestingUtils/Services/Types';
import { ActiveMonitoringMeteredPlan } from '../../Types/Billing/MeteredPlan/AllMeteredPlans';
import Database from '../TestingUtils/Database';
describe('BillingService', () => {
let billingService: BillingService;
@@ -42,9 +43,20 @@ describe('BillingService', () => {
customer.id.toString()
);
beforeEach(() => {
jest.clearAllMocks();
billingService = mockIsBillingEnabled(true);
let database!: Database;
beforeEach(
async () => {
jest.clearAllMocks();
billingService = mockIsBillingEnabled(true);
database = new Database();
await database.createAndConnect();
},
10 * 1000 // 10 second timeout because setting up the DB is slow
);
afterEach(async () => {
await database.disconnectAndDropDatabase();
});
describe('Customer Management', () => {
@@ -194,19 +206,6 @@ describe('BillingService', () => {
);
});
// it.only('should handle invalid metered plans', async () => {
// const mp: MeteredPlan = new MeteredPlan('price_123', 100, 'unit');
// subscription.serverMeteredPlans = [];
// (
// mockStripe.subscriptions.create as jest.Mock
// ).mockResolvedValueOnce(mockSubscription);
// await expect(
// billingService.subscribeToMeteredPlan(subscription)
// ).rejects.toThrow();
// });
it('should handle API errors during subscription creation', async () => {
mockStripe.subscriptions.create = jest
.fn()

View File

@@ -18,7 +18,7 @@ import {
/// @dev consider modifyfing the EnvirontmentConfig to use functions instead of constants so that we can mock them
const mockIsBillingEnabled: Function = (value: boolean): BillingService => {
jest.resetModules();
jest.doMock('../../../EnvironmentConfig', () => {
jest.doMock('../../../BillingConfig', () => {
return {
IsBillingEnabled: value,
};