test(subaccounts): added activation tests
MODIFY
src/server/rest/v1/router/api/BillingRouter.ts
src/server/rest/v1/router/api/BillingRouter.ts
@@ -44,6 +44,7 @@ export default class BillingRouter {
// ROUTES for SUB-ACCOUNTS
// -----------------------------------
this.buildRouteBillingCreateSubAccount();
+ this.buildRouteBillingActivateSubAccount();
return this.router;
}
@@ -148,4 +149,12 @@ export default class BillingRouter {
void RouterUtils.handleRestServerAction(BillingService.handleCreateSubAccount.bind(this), ServerAction.BILLING_CREATE_SUB_ACCOUNT, req, res, next);
});
}
+
+
+ private buildRouteBillingActivateSubAccount(): void {
+ this.router.patch(`/${RESTServerRoute.REST_BILLING_ACTIVATE_SUB_ACCOUNT}`, (req: Request, res: Response, next: NextFunction) => {
+ req.params.subAccountID = req.params.id;
+ void RouterUtils.handleRestServerAction(BillingService.handleActivateSubAccount.bind(this), ServerAction.BILLING_ACTIVATE_SUB_ACCOUNT, req, res, next);
+ });
+ }
}

MODIFY
src/server/rest/v1/router/util/UtilRouter.ts
src/server/rest/v1/router/util/UtilRouter.ts
@@ -2,7 +2,6 @@ import { RESTServerRoute, ServerAction } from '../../../../../types/Server';
import express, { NextFunction, Request, Response } from 'express';
import AssetService from '../../service/AssetService';
-import BillingService from '../../service/BillingService';
import CarService from '../../service/CarService';
import ChargingStationService from '../../service/ChargingStationService';
import CompanyService from '../../service/CompanyService';
@@ -28,7 +27,6 @@ export default class UtilRouter {
this.buildRouteChargingStationDownloadFirmware();
this.buildRouteGetSiteImage();
this.buildRouteGetTenantLogo();
- this.buildRouteBillingActivateSubAccount();
return this.router;
}
@@ -86,11 +84,4 @@ export default class UtilRouter {
void RouterUtils.handleRestServerAction(SiteService.handleGetSiteImage.bind(this), ServerAction.SITE_IMAGE, req, res, next);
});
}
-
- private buildRouteBillingActivateSubAccount(): void {
- this.router.patch(`/${RESTServerRoute.REST_BILLING_ACTIVATE_SUB_ACCOUNT}`, (req: Request, res: Response, next: NextFunction) => {
- req.params.subAccountID = req.params.id;
- void RouterUtils.handleRestServerAction(BillingService.handleActivateSubAccount.bind(this), ServerAction.BILLING_ACTIVATE_SUB_ACCOUNT, req, res, next);
- });
- }
}

MODIFY
test/api/BillingTest.ts
test/api/BillingTest.ts
/* eslint-disable max-len */
-import { BillingAccount, BillingChargeInvoiceAction, BillingInvoiceStatus } from '../../src/types/Billing';
+import { BillingChargeInvoiceAction, BillingInvoiceStatus } from '../../src/types/Billing';
import { BillingSettings, BillingSettingsType } from '../../src/types/Setting';
import chai, { expect } from 'chai';
@@ -11,12 +11,12 @@ import Constants from '../../src/utils/Constants';
import ContextDefinition from './context/ContextDefinition';
import ContextProvider from './context/ContextProvider';
import Factory from '../factories/Factory';
+import { HTTPError } from '../../src/types/HTTPError';
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 Utils from '../../src/utils/Utils';
import assert from 'assert';
import chaiSubset from 'chai-subset';
import config from '../config';
@@ -441,6 +441,36 @@ describeif(isBillingProperlyConfigured)('Billing', () => {
});
expect(response.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
});
+
+ it('should create a sub account and activate it', async () => {
+ const response = await billingTestHelper.userService.billingApi.createSubAccount({
+ userID: billingTestHelper.userContext.id,
+ siteID: billingTestHelper.siteContext.getSite().id,
+ });
+ expect(response.status).to.be.eq(StatusCodes.CREATED);
+
+ const activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: response.data.accountID });
+ expect(activationResponse.status).to.be.eq(StatusCodes.OK);
+ expect(activationResponse.data.pending).to.be.false;
+ });
+
+ it('should not activate an inexistent sub account', async () => {
+ const activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: 'xxx' });
+ expect(activationResponse.status).to.be.eq(HTTPError.OBJECT_DOES_NOT_EXIST_ERROR);
+ });
+
+ it('should not activate a sub account twice', async () => {
+ const response = await billingTestHelper.userService.billingApi.createSubAccount({
+ userID: billingTestHelper.userContext.id,
+ siteID: billingTestHelper.siteContext.getSite().id,
+ });
+ expect(response.status).to.be.eq(StatusCodes.CREATED);
+
+ let activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: response.data.accountID });
+ expect(activationResponse.status).to.be.eq(StatusCodes.OK);
+ activationResponse = await billingTestHelper.userService.billingApi.activateSubAccount({ accountID: response.data.accountID });
+ expect(activationResponse.status).to.be.eq(StatusCodes.INTERNAL_SERVER_ERROR);
+ });
});
describe('Where basic user', () => {

MODIFY
test/api/client/BillingApi.ts
test/api/client/BillingApi.ts
@@ -52,7 +52,11 @@ export default class BillingApi extends CrudApi {
return super.readAll(params, paging, ordering, super.buildRestEndpointUrl(RESTServerRoute.REST_BILLING_INVOICES, params));
}
- public async createSubAccount(params, paging = TestConstants.DEFAULT_PAGING, ordering = TestConstants.DEFAULT_ORDERING) {
+ public async createSubAccount(params) {
return super.create(params, super.buildRestEndpointUrl(RESTServerRoute.REST_BILLING_SUB_ACCOUNTS));
}
+
+ public async activateSubAccount(params) {
+ return super.patch(params, super.buildRestEndpointUrl(RESTServerRoute.REST_BILLING_ACTIVATE_SUB_ACCOUNT, { id: params.accountID }));
+ }
}

MODIFY
test/api/client/utils/CrudApi.ts
test/api/client/utils/CrudApi.ts
@@ -65,6 +65,14 @@ export default class CrudApi {
});
}
+ public async patch(data, path): Promise<any> {
+ return this._authenticatedApi.send({
+ method: 'PATCH',
+ url: path,
+ data: data,
+ });
+ }
+
protected buildRestEndpointUrl(urlPatternAsString: RESTServerRoute, params: { [name: string]: string | number | null } = {}, urlPrefix = 'api'): string {
let resolvedUrlPattern = urlPatternAsString as string;
for (const key in params) {

test(subaccounts): added activation tests