billing - test polish
MODIFY
test/api/BillingPlatformTest.ts
test/api/BillingPlatformTest.ts
@@ -5,6 +5,7 @@ import chai, { expect } from 'chai';
import { BillingPeriodicOperationTaskConfig } from '../../src/types/TaskConfig';
import BillingStorage from '../../src/storage/mongodb/BillingStorage';
+import { BillingTestConfigHelper } from './StripeTestHelper';
import BillingTestHelper from './BillingTestHelper';
import CompanyFactory from '../factories/CompanyFactory';
import Constants from '../../src/utils/Constants';
@@ -12,7 +13,6 @@ import ContextDefinition from './context/ContextDefinition';
import MongoDBStorage from '../../src/storage/mongodb/MongoDBStorage';
import SiteFactory from '../factories/SiteFactory';
import { StatusCodes } from 'http-status-codes';
-import StripeTestHelper from './StripeTestHelper';
import assert from 'assert';
import chaiSubset from 'chai-subset';
import config from '../config';
@@ -22,16 +22,12 @@ import responseHelper from '../helpers/responseHelper';
chai.use(chaiSubset);
chai.use(responseHelper);
-// TODO - Get rid of the stripe helper which provide redundant convenience methods
-const stripeTestHelper = new StripeTestHelper();
const billingTestHelper = new BillingTestHelper();
// Conditional test execution function
const describeif = (condition) => condition ? describe : describe.skip;
-// Do not run the tests when the settings are not properly set
-const isBillingProperlyConfigured = stripeTestHelper.isBillingProperlyConfigured();
+const isBillingProperlyConfigured = BillingTestConfigHelper.isBillingProperlyConfigured();
describeif(isBillingProperlyConfigured)('Billing', () => {
- // Do not run the tests when the settings are not properly set
jest.setTimeout(60000);
beforeAll(async () => {
@@ -41,8 +37,6 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
describe('Billing Platform (utbillingplatform)', () => {
beforeAll(async () => {
- // TODO - This is ugly and confusing - rethink that part (merge BillingTestHelper and StripeTestHelper methods)
- await stripeTestHelper.initialize(ContextDefinition.TENANT_CONTEXTS.TENANT_BILLING_PLATFORM);
await billingTestHelper.initialize(ContextDefinition.TENANT_CONTEXTS.TENANT_BILLING_PLATFORM);
});

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
/* eslint-disable max-len */
import { BillingChargeInvoiceAction, BillingInvoiceStatus } from '../../src/types/Billing';
import { BillingSettings, BillingSettingsType } from '../../src/types/Setting';
+import StripeTestHelper, { BillingTestConfigHelper } from './StripeTestHelper';
import chai, { expect } from 'chai';
import { BillingPeriodicOperationTaskConfig } from '../../src/types/TaskConfig';
import BillingTestHelper from './BillingTestHelper';
-import CentralServerService from './client/CentralServerService';
import Constants from '../../src/utils/Constants';
import ContextDefinition from './context/ContextDefinition';
-import ContextProvider from './context/ContextProvider';
import Factory from '../factories/Factory';
import MongoDBStorage from '../../src/storage/mongodb/MongoDBStorage';
import { StatusCodes } from 'http-status-codes';
-import StripeTestHelper from './StripeTestHelper';
import TestConstants from './client/utils/TestConstants';
import User from '../../src/types/User';
import assert from 'assert';
@@ -29,11 +27,9 @@ const stripeTestHelper = new StripeTestHelper();
const billingTestHelper = new BillingTestHelper();
// Conditional test execution function
const describeif = (condition) => condition ? describe : describe.skip;
-// Do not run the tests when the settings are not properly set
-const isBillingProperlyConfigured = stripeTestHelper.isBillingProperlyConfigured();
+const isBillingProperlyConfigured = BillingTestConfigHelper.isBillingProperlyConfigured();
describeif(isBillingProperlyConfigured)('Billing', () => {
- // Do not run the tests when the settings are not properly set
jest.setTimeout(60000);
beforeAll(async () => {
@@ -737,7 +733,16 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
});
- describe('When basic user has a free access', () => {
+ });
+
+ describe('when Basic User', () => {
+ beforeAll(async () => {
+ billingTestHelper.setCurrentUserContextAsBasic();
+ // Initialize the Billing module
+ await billingTestHelper.setBillingSystemValidCredentials(true, false /* immediateBillingAllowed OFF, so periodicBilling ON */);
+ });
+
+ describe('has free access', () => {
// eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
// Initialize the charging station context
@@ -745,8 +750,8 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
await billingTestHelper.setBillingSystemValidCredentials();
// Update the freeAccess flag for the basic user:
await billingTestHelper.getAdminUserService().userApi.update({
- id: ContextDefinition.TENANT_USER_LIST[2].id,
- freeAccess: true
+ id: billingTestHelper.getBasicUserContext().id,
+ freeAccess: true // Switch ON freeAccess flag
});
});
@@ -757,24 +762,21 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
const itemsBefore = await billingTestHelper.getNumberOfSessions(billingTestHelper.getCurrentUserContext().id);
const transactionID = await billingTestHelper.generateTransaction();
assert(transactionID, 'transactionID should not be null');
- // await testData.checkTransactionBillingData(transactionID); // TODO - Check not yet possible!
- // await testData.userService.billingApi.synchronizeInvoices({});
const itemsAfter = await billingTestHelper.getNumberOfSessions(billingTestHelper.getCurrentUserContext().id);
expect(itemsAfter).eq(itemsBefore);
}
);
});
- describe('When basic user does not have a free access', () => {
+ describe('does not have a free access', () => {
// eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
// Initialize the charging station context
await billingTestHelper.initChargingStationContext();
await billingTestHelper.setBillingSystemValidCredentials();
- // Update the freeAccess flag for the basic user:
await billingTestHelper.getAdminUserService().userApi.update({
- id: ContextDefinition.TENANT_USER_LIST[2].id,
- freeAccess: false
+ id: billingTestHelper.getBasicUserContext().id,
+ freeAccess: false // Switch OFF freeAccess flag
});
});
@@ -783,13 +785,18 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
const itemsBefore = await billingTestHelper.getNumberOfSessions(billingTestHelper.getCurrentUserContext().id);
const transactionID = await billingTestHelper.generateTransaction();
assert(transactionID, 'transactionID should not be null');
- // await testData.checkTransactionBillingData(transactionID); // TODO - Check not yet possible!
- // await testData.userService.billingApi.synchronizeInvoices({});
const itemsAfter = await billingTestHelper.getNumberOfSessions(billingTestHelper.getCurrentUserContext().id);
expect(itemsAfter).to.be.gt(itemsBefore);
});
});
+ afterAll(async () => {
+ await billingTestHelper.getAdminUserService().userApi.update({
+ id: billingTestHelper.getBasicUserContext().id,
+ freeAccess: false // Restore initial state
+ });
+ });
+
});
describe('with Transaction Billing + Periodic Billing ON', () => {
@@ -802,7 +809,7 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
describe('Where admin user', () => {
// eslint-disable-next-line @typescript-eslint/require-await
beforeAll(async () => {
- // Initialize the charing station context
+ // Initialize the charging station context
await billingTestHelper.initChargingStationContext2TestChargingTime();
});

MODIFY
test/api/StripeTestHelper.ts
test/api/StripeTestHelper.ts
@@ -26,6 +26,43 @@ import responseHelper from '../helpers/responseHelper';
chai.use(chaiSubset);
chai.use(responseHelper);
+export class BillingTestConfigHelper {
+ public static isBillingProperlyConfigured(): boolean {
+ const billingSettings = BillingTestConfigHelper.getLocalSettings(false);
+ // Check that the mandatory settings are properly provided
+ return (!!billingSettings.stripe.publicKey
+ && !!billingSettings.stripe.secretKey
+ && !!billingSettings.stripe.url);
+ }
+
+ public static getLocalSettings(immediateBillingAllowed: boolean): BillingSettings {
+ const billingProperties = {
+ isTransactionBillingActivated: config.get('billing.isTransactionBillingActivated'),
+ immediateBillingAllowed: config.get('billing.immediateBillingAllowed'),
+ periodicBillingAllowed: config.get('billing.periodicBillingAllowed'),
+ taxID: config.get('billing.taxID')
+ };
+ const stripeProperties = {
+ url: config.get('stripe.url'),
+ publicKey: config.get('stripe.publicKey'),
+ secretKey: config.get('stripe.secretKey'),
+ };
+ const settings: BillingSettings = {
+ identifier: TenantComponents.BILLING,
+ type: BillingSettingsType.STRIPE,
+ billing: billingProperties,
+ stripe: stripeProperties,
+ };
+
+ // -----------------------------------------------------------------
+ // Our test may need the immediate billing to be switched off!
+ // Because we want to check the DRAFT state of the invoice
+ settings.billing.immediateBillingAllowed = immediateBillingAllowed;
+ // -----------------------------------------------------------------
+ return settings;
+ }
+}
+
export default class StripeTestHelper {
// Tenant: utbilling
private tenantContext: TenantContext;
@@ -73,7 +110,7 @@ export default class StripeTestHelper {
}
public async setBillingSystemValidCredentials(immediateBilling: boolean) : Promise<void> {
- const billingSettings = this.getLocalSettings(immediateBilling);
+ const billingSettings = BillingTestConfigHelper.getLocalSettings(immediateBilling);
await this.saveBillingSettings(billingSettings);
billingSettings.stripe.secretKey = await Cypher.encrypt(this.getTenant(), billingSettings.stripe.secretKey);
this.billingImpl = StripeBillingIntegration.getInstance(this.getTenant(), billingSettings);
@@ -81,7 +118,7 @@ export default class StripeTestHelper {
}
public async fakeLiveBillingSettings() : Promise<StripeBillingIntegration> {
- const billingSettings = this.getLocalSettings(true);
+ const billingSettings = BillingTestConfigHelper.getLocalSettings(true);
const mode = 'live';
billingSettings.stripe.secretKey = `sk_${mode}_0234567890`;
billingSettings.stripe.publicKey = `pk_${mode}_0234567890`;
@@ -91,33 +128,6 @@ export default class StripeTestHelper {
return billingImpl;
}
- public getLocalSettings(immediateBillingAllowed: boolean): BillingSettings {
- const billingProperties = {
- isTransactionBillingActivated: config.get('billing.isTransactionBillingActivated'),
- immediateBillingAllowed: config.get('billing.immediateBillingAllowed'),
- periodicBillingAllowed: config.get('billing.periodicBillingAllowed'),
- taxID: config.get('billing.taxID')
- };
- const stripeProperties = {
- url: config.get('stripe.url'),
- publicKey: config.get('stripe.publicKey'),
- secretKey: config.get('stripe.secretKey'),
- };
- const settings: BillingSettings = {
- identifier: TenantComponents.BILLING,
- type: BillingSettingsType.STRIPE,
- billing: billingProperties,
- stripe: stripeProperties,
- };
-
- // -----------------------------------------------------------------
- // Our test may need the immediate billing to be switched off!
- // Because we want to check the DRAFT state of the invoice
- settings.billing.immediateBillingAllowed = immediateBillingAllowed;
- // -----------------------------------------------------------------
- return settings;
- }
-
public async saveBillingSettings(billingSettings: BillingSettings) : Promise<void> {
// TODO - rethink that part
const tenantBillingSettings = await this.adminUserService.settingApi.readByIdentifier({ 'Identifier': 'billing' });
@@ -130,14 +140,6 @@ export default class StripeTestHelper {
await this.adminUserService.settingApi.update(componentSetting);
}
- public isBillingProperlyConfigured(): boolean {
- const billingSettings = this.getLocalSettings(false);
- // Check that the mandatory settings are properly provided
- return (!!billingSettings.stripe.publicKey
- && !!billingSettings.stripe.secretKey
- && !!billingSettings.stripe.url);
- }
-
public async assignPaymentMethod(stripe_test_token: string) : Promise<Stripe.CustomerSource> {
// Assign a source using test tokens (instead of test card numbers)
// c.f.: https://stripe.com/docs/testing#cards

billing - test polish