Add car unit test cases
MODIFY
src/integration/pricing/export/convergent-charging/index.ts
src/integration/pricing/export/convergent-charging/index.ts
-export { default } from '../../convergent-charging';
+export { default } from '../../dummy/DummyPricingIntegration';

MODIFY
src/integration/refund/export/concur/index.ts
src/integration/refund/export/concur/index.ts
-export { default } from '../../concur';
+export { default } from '../../dummy/DummyRefundIntegration';

MODIFY
test/api/CarTest.ts
test/api/CarTest.ts
import chai, { expect } from 'chai';
+import { Car } from '../types/Car';
import CentralServerService from './client/CentralServerService';
-import TestData from './client/utils/TestData';
+import ContextDefinition from './context/ContextDefinition';
+import ContextProvider from './context/ContextProvider';
+import Factory from '../factories/Factory';
+import TenantContext from './context/TenantContext';
+import User from '../types/User';
import chaiSubset from 'chai-subset';
import config from '../config';
chai.use(chaiSubset);
+class TestData {
+ public data: any;
+ public superCentralService: CentralServerService;
+ public centralService: CentralServerService;
+ public credentials: any = {};
+ public newCar: Car;
+ public createdCars: Car[] = [];
+ public tenantContext: TenantContext;
+ public userContext: User;
+}
const testData: TestData = new TestData();
let carID: number;
describe('Car Service', function() {
@@ -13,8 +28,9 @@ describe('Car Service', function() {
before(async function() {
// Init values
testData.superCentralService = new CentralServerService(null, { email: config.get('superadmin.username'), password: config.get('superadmin.password') });
-
- testData.centralService = new CentralServerService('utcar', {
+ testData.tenantContext = await ContextProvider.defaultInstance.getTenantContext(ContextDefinition.TENANT_CONTEXTS.TENANT_CAR);
+ testData.userContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ testData.centralService = new CentralServerService(ContextDefinition.TENANT_CONTEXTS.TENANT_CAR, {
email: config.get('admin.username'),
password: config.get('admin.password')
});
@@ -24,8 +40,9 @@ describe('Car Service', function() {
after(() => {
});
describe('Success cases', () => {
- describe('Where admin user', () => {
+
describe('Where car component is not active', () => {
+ describe('Where admin user', () => {
before(async function() {
testData.centralService = new CentralServerService('utasset', {
@@ -34,18 +51,18 @@ describe('Car Service', function() {
});
});
- it('Should not be able to get cars', async () => {
- const response = await testData.centralService.carApi.readAll({});
+ it('Should not be able to get car catalogs', async () => {
+ const response = await testData.centralService.carApi.readCarCatalogs({});
expect(response.status).to.equal(560);
});
- it('Should not be able to get car by ID', async () => {
- const response = await testData.centralService.carApi.readById();
+ it('Should not be able to get car catalog by ID', async () => {
+ const response = await testData.centralService.carApi.readCarCatalog(null);
expect(response.status).to.equal(560);
});
it('Should not be able to get image of a car', async () => {
- const response = await testData.centralService.carApi.readCarImages();
+ const response = await testData.centralService.carApi.readCarImages(null);
expect(response.status).to.equal(560);
});
@@ -54,28 +71,31 @@ describe('Car Service', function() {
expect(response.status).to.equal(560);
});
- it('Should not be able to get a detailed car', async () => {
- const response = await testData.centralService.carApi.readById();
+ it('Should not be able to get a detailed car catalog', async () => {
+ const response = await testData.centralService.carApi.readCarCatalog(null);
expect(response.status).to.equal(560);
});
});
+ });
+
describe('Where car component is active', () => {
+ describe('Where admin user', () => {
+
before(async function() {
- testData.centralService = new CentralServerService('utcar', {
+ testData.centralService = new CentralServerService(ContextDefinition.TENANT_CONTEXTS.TENANT_CAR, {
email: config.get('admin.username'),
password: config.get('admin.password')
});
-
});
- it('Should be able to get cars', async () => {
- const response = await testData.centralService.carApi.readAll({});
+ it('Should be able to get car catalogs', async () => {
+ const response = await testData.centralService.carApi.readCarCatalogs({});
carID = response.data.result[0].id;
expect(response.status).to.equal(200);
});
- it('Should be able to get car by ID', async () => {
- const response = await testData.centralService.carApi.readById(carID);
+ it('Should be able to get car catalog by ID', async () => {
+ const response = await testData.centralService.carApi.readCarCatalog(carID);
expect(response.status).to.equal(200);
});
@@ -89,22 +109,218 @@ describe('Car Service', function() {
expect(response.status).to.equal(200);
});
- it('Should not be able to get a detailed car without ID', async () => {
- const response = await testData.centralService.carApi.readById();
+ it('Should not be able to get a detailed car catalog without ID', async () => {
+ const response = await testData.centralService.carApi.readCarCatalog(null);
expect(response.status).to.equal(500);
});
- it('Should not be able to get car debug object', async () => {
- const response = await testData.centralService.carApi.readById(carID);
+ it('Should not be able to get car catalog debug object', async () => {
+ const response = await testData.centralService.carApi.readCarCatalog(carID);
expect(response.status).to.equal(200);
expect(response.data).to.not.have.property('hash');
});
+
+ it('Should be able to create a new car', async () => {
+ // Create
+ const newCar = Factory.car.build();
+ newCar['usersUpserted'] = [{
+ user: { id: testData.userContext.id },
+ default: false,
+ owner: false
+ }];
+ testData.newCar = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ newCar
+ );
+ testData.createdCars.push(testData.newCar);
+ });
+
+ it('Should not be able to create a new car without a vin', async () => {
+ // Create
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ Factory.car.build({
+ vin: null,
+ }), false
+ );
+ expect(response.status).to.equal(500);
+ expect(response.data.message).to.equal('Vin Car is mandatory');
+ });
+
+ it('Should not be able to create a new car without a license plate', async () => {
+ // Create
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ Factory.car.build({
+ licensePlate: null,
+ }), false
+ );
+ expect(response.status).to.equal(500);
+ expect(response.data.message).to.equal('License Plate is mandatory');
+ });
+
+ it('Should not be able to create a new car without a car type', async () => {
+ // Create
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ Factory.car.build({
+ type: null,
+ }), false
+ );
+ expect(response.status).to.equal(500);
+ expect(response.data.message).to.equal('Car type is mandatory');
+ });
+
+ it('Should not be able to create a new car without a car catalog ID', async () => {
+ // Create
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ Factory.car.build({
+ carCatalogID: null,
+ }), false
+ );
+ expect(response.status).to.equal(500);
+ expect(response.data.message).to.equal('Car Catalog ID is mandatory');
+ });
+
+ it('Should not be able to create a new car with existent VIN and License Plate', async () => {
+ // Create
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ Factory.car.build({
+ vin: testData.newCar.vin,
+ licensePlate: testData.newCar.licensePlate,
+ }), false
+ );
+ expect(response.status).to.equal(591);
+ });
+
+ it('Should be able to update a car', async () => {
+ // Update
+ const carToUpdate = (await testData.centralService.carApi.readCar(testData.createdCars[0].id)).data;
+ carToUpdate.carCatalogID = 1010;
+ carToUpdate['usersRemoved'] = [carToUpdate.carUsers.find((carUser) => carUser.user.id === testData.userContext.id)];
+ carToUpdate['usersUpserted'] = [];
+ testData.newCar = await testData.centralService.updateEntity(
+ testData.centralService.carApi,
+ carToUpdate
+ );
+ });
+ it('Should not be able to update a car with existent VIN and License Plate', async () => {
+ // Create
+ testData.newCar = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ Factory.car.build()
+ );
+ testData.createdCars.push(testData.newCar);
+ testData.newCar.vin = testData.createdCars[0].vin;
+ testData.newCar.licensePlate = testData.createdCars[0].licensePlate;
+ const response = await testData.centralService.updateEntity(
+ testData.centralService.carApi,
+ testData.newCar,
+ false
+ );
+ expect(response.status).to.equal(591);
+ });
+
+ it('Should be able to get car', async () => {
+ const response = await testData.centralService.carApi.readCar(testData.newCar.id);
+ expect(response.status).to.equal(200);
+ });
+
+ it('Should be able to get cars', async () => {
+ const response = await testData.centralService.carApi.readCars({});
+ expect(response.status).to.equal(200);
+ });
+ });
+ describe('Where basic user', () => {
+ before(async function() {
+ testData.centralService = new CentralServerService(ContextDefinition.TENANT_CONTEXTS.TENANT_CAR, testData.userContext);
+ });
+
+ it('Should be able to create a new car', async () => {
+ // Create
+ const userCar = [{
+ user: { id: testData.userContext.id },
+ default: false
+ }];
+ const carToCreate = Factory.car.build();
+ carToCreate.usersUpserted = userCar;
+ testData.newCar = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ carToCreate
+ );
+ testData.createdCars.push(testData.newCar);
+ });
+
+ it('Should be able to assign himself to an existing car', async () => {
+ // Create
+ const userCar = [{
+ user: { id: testData.userContext.id },
+ default: false
+ }];
+ testData.createdCars[0]['usersUpserted'] = userCar;
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ testData.createdCars[0], false
+ );
+ expect(response.status).to.equal(592);
+ testData.createdCars[0]['forced'] = true;
+ await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ testData.createdCars[0]
+ );
+ });
+
+ it('Should not be able to create a pool car', async () => {
+ // Update
+ const newCar = Factory.car.build({ type: 'PC' });
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApi,
+ newCar, false
+ );
+ expect(response.status).to.equal(500);
+ expect(response.data.message).to.equal('Pool cars can only be created by admin');
+ });
+
+ it('Should be able to update a car that he own', async () => {
+ // Update
+ const carToUpdate = (await testData.centralService.carApi.readCar(testData.newCar.id)).data;
+ carToUpdate.carCatalogID = 1004;
+ const response = await testData.centralService.updateEntity(
+ testData.centralService.carApi,
+ carToUpdate
+ );
+ });
+
+ it('Should not be able to update a car to a pool car', async () => {
+ // Update
+ const carToUpdate = (await testData.centralService.carApi.readCar(testData.newCar.id)).data;
+ carToUpdate.type = 'PC';
+ const response = await testData.centralService.updateEntity(
+ testData.centralService.carApi,
+ carToUpdate,
+ false
+ );
+ expect(response.status).to.equal(500);
+ expect(response.data.message).to.equal('Pool cars can only be created by admin');
+ });
+
+ it('Should not be able to update not owned car', async () => {
+ // Update
+ const response = await testData.centralService.updateEntity(
+ testData.centralService.carApi,
+ testData.createdCars[1],
+ false
+ );
+ expect(response.status).to.equal(550);
+ });
});
});
describe('Where Super admin user', () => {
- it('Should be able to get car by ID', async () => {
- const response = await testData.centralService.carApiSuperTenant.readById(carID);
+ it('Should be able to get car catalog by ID', async () => {
+ const response = await testData.centralService.carApiSuperTenant.readCarCatalog(carID);
expect(response.status).to.equal(200);
});
@@ -118,21 +334,41 @@ describe('Car Service', function() {
expect(response.status).to.equal(200);
});
- it('Should not be able to get a detailed car without ID', async () => {
- const response = await testData.centralService.carApiSuperTenant.readById();
+ it('Should not be able to get a detailed car catalog without ID', async () => {
+ const response = await testData.centralService.carApiSuperTenant.readCarCatalog(null);
expect(response.status).to.equal(500);
});
- it('Should be able to get car debug object', async () => {
- const response = await testData.centralService.carApiSuperTenant.readById(carID);
+ it('Should be able to get car catalog debug object', async () => {
+ const response = await testData.centralService.carApiSuperTenant.readCarCatalog(carID);
expect(response.status).to.equal(200);
expect(response.data).to.have.property('hash');
});
- it('Should be able to get cars', async () => {
- const response = await testData.centralService.carApiSuperTenant.readAll({});
+ it('Should be able to get car catalogs', async () => {
+ const response = await testData.centralService.carApiSuperTenant.readCarCatalogs({});
expect(response.status).to.equal(200);
});
+
+ it('Should not be able to get cars from super tenant', async () => {
+ const response = await testData.centralService.carApiSuperTenant.readCars({});
+ expect(response.status).to.equal(560);
+ });
+
+ it('Should not be able to get car from super tenant', async () => {
+ const response = await testData.centralService.carApiSuperTenant.readCar({});
+ expect(response.status).to.equal(560);
+ });
+
+ it('Should not be able to create a car from super tenant', async () => {
+ // Create
+ const response = await testData.centralService.createEntity(
+ testData.centralService.carApiSuperTenant,
+ Factory.car.build(),
+ false
+ );
+ expect(response.status).to.equal(560);
+ });
});
});
});

MODIFY
test/api/client/CarApi.ts
test/api/client/CarApi.ts
@@ -6,11 +6,11 @@ export default class CarApi extends CrudApi {
super(authenticatedApi);
}
- public async readById(id) {
+ public async readCarCatalog(id) {
return super.read({ CarCatalogID: id }, '/client/api/CarCatalog');
}
- public async readAll(params, paging = TestConstants.DEFAULT_PAGING, ordering = TestConstants.DEFAULT_ORDERING) {
+ public async readCarCatalogs(params, paging = TestConstants.DEFAULT_PAGING, ordering = TestConstants.DEFAULT_ORDERING) {
return super.readAll(params, paging, ordering, '/client/api/CarCatalogs');
}
@@ -22,4 +22,20 @@ export default class CarApi extends CrudApi {
return super.read({ CarCatalogID: id }, '/client/api/CarCatalogImages');
}
+ public async readCar(id) {
+ return super.read({ CarID: id }, '/client/api/Car');
+ }
+
+ public async readCars(params, paging = TestConstants.DEFAULT_PAGING, ordering = TestConstants.DEFAULT_ORDERING) {
+ return super.readAll(params, paging, ordering, '/client/api/Cars');
+ }
+
+ public async update(data) {
+ return super.update(data, '/client/api/CarUpdate');
+ }
+
+ public async create(data) {
+ return super.create(data, '/client/api/CarCreate');
+ }
+
}

ADD
test/factories/CarFactory.ts
+import { Factory } from 'rosie';
+import faker from 'faker';
+
+export default Factory.define('car')
+ .attr('vin', () => faker.random.alphaNumeric(17).toUpperCase())
+ .attr('licensePlate', faker.random.alphaNumeric(12).toUpperCase())
+ .attr('carCatalogID', 1004)
+ .attr('type', 'P')
+ .attr('usersUpserted', [])
+ .attr('usersRemoved', []);

MODIFY
test/factories/Factory.ts
test/factories/Factory.ts
import AddressFactory from './AddressFactory';
import AssetFactory from './AssetFactory';
+import CarFactory from './CarFactory';
import ChargingStationFactory from './ChargingStationFactory';
import CompanyFactory from './CompanyFactory';
import OcpiEndpointsFactory from './OcpiEndpointsFactory';
@@ -20,4 +21,5 @@ export default abstract class Factory {
public static tenant = TenantFactory;
public static setting = SettingFactory;
public static ocpiEndpoint = OcpiEndpointsFactory;
+ public static car = CarFactory;
}

Add car unit test cases