From 0af8d7359b5ecdcff8879002fbc4cda5c929b550 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Mon, 25 Dec 2023 18:31:23 +0000 Subject: [PATCH] Refactor billing configuration and add database setup in tests --- CommonServer/BillingConfig.ts | 13 ++++++++ CommonServer/EnvironmentConfig.ts | 32 +++++++------------ .../Tests/Services/BillingService.test.ts | 31 +++++++++--------- .../Tests/TestingUtils/Services/Helpers.ts | 2 +- 4 files changed, 41 insertions(+), 37 deletions(-) create mode 100644 CommonServer/BillingConfig.ts diff --git a/CommonServer/BillingConfig.ts b/CommonServer/BillingConfig.ts new file mode 100644 index 0000000000..e9ce519cc8 --- /dev/null +++ b/CommonServer/BillingConfig.ts @@ -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, +} \ No newline at end of file diff --git a/CommonServer/EnvironmentConfig.ts b/CommonServer/EnvironmentConfig.ts index d8d3c26dc8..b6f8a85f18 100644 --- a/CommonServer/EnvironmentConfig.ts +++ b/CommonServer/EnvironmentConfig.ts @@ -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 }` ); diff --git a/CommonServer/Tests/Services/BillingService.test.ts b/CommonServer/Tests/Services/BillingService.test.ts index a780eb2960..ec9fd47510 100644 --- a/CommonServer/Tests/Services/BillingService.test.ts +++ b/CommonServer/Tests/Services/BillingService.test.ts @@ -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() diff --git a/CommonServer/Tests/TestingUtils/Services/Helpers.ts b/CommonServer/Tests/TestingUtils/Services/Helpers.ts index 03ca87b53f..252c1e90a1 100644 --- a/CommonServer/Tests/TestingUtils/Services/Helpers.ts +++ b/CommonServer/Tests/TestingUtils/Services/Helpers.ts @@ -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, };