User badge UT cases
MODIFY
test/api/UserTest.ts
test/api/UserTest.ts
import chai, { assert, expect } from 'chai';
-
+import chaiSubset from 'chai-subset';
+import moment from 'moment';
import CentralServerService from '../api/client/CentralServerService';
+import Factory from '../factories/Factory';
+import responseHelper from '../helpers/responseHelper';
+import User from '../types/User';
+import ChargingStationContext from './context/ChargingStationContext';
import ContextDefinition from './context/ContextDefinition';
import ContextProvider from './context/ContextProvider';
-import Factory from '../factories/Factory';
import SiteContext from './context/SiteContext';
import TenantContext from './context/TenantContext';
-import User from '../types/User';
-import chaiSubset from 'chai-subset';
+
chai.use(chaiSubset);
+chai.use(responseHelper);
+
class TestData {
public tenantContext: TenantContext;
@@ -20,6 +25,8 @@ class TestData {
public siteContext: SiteContext;
public newUser: User;
public createdUsers: any[] = [];
+ public siteAreaContext: any;
+ public chargingStationContext: ChargingStationContext;
}
const testData: TestData = new TestData();
@@ -51,6 +58,8 @@ describe('User tests', function() {
testData.tenantContext.getTenant().subdomain,
testData.centralUserContext
);
+ testData.siteAreaContext = testData.siteContext.getSiteAreaContext(ContextDefinition.SITE_AREA_CONTEXTS.WITH_ACL);
+ testData.chargingStationContext = testData.siteAreaContext.getChargingStationContext(ContextDefinition.CHARGING_STATION_CONTEXTS.ASSIGNED_OCPP16);
});
after(async () => {
@@ -145,6 +154,64 @@ describe('User tests', function() {
);
});
+ it('Should not be able to delete a badge that has already been used', async () => {
+ const connectorId = 1;
+ const tagId = testData.newUser.tags[0].id;
+ const meterStart = 180;
+ const startDate = moment();
+ const response = await testData.chargingStationContext.startTransaction(
+ connectorId, tagId, meterStart, startDate.toDate());
+ // eslint-disable-next-line @typescript-eslint/unbound-method
+ expect(response).to.be.transactionValid;
+ testData.newUser.tags = [testData.newUser.tags[1], testData.newUser.tags[2]];
+ // Update
+ await testData.userService.updateEntity(
+ testData.userService.userApi,
+ testData.newUser
+ );
+ testData.newUser = (await testData.userService.getEntityById(
+ testData.userService.userApi,
+ testData.newUser,
+ false
+ )).data;
+ // Check
+ const deactivatedTag = testData.newUser.tags.find(tag => tag.id === tagId);
+ expect(testData.newUser.tags).has.lengthOf(3);
+ expect(deactivatedTag).to.not.be.null;
+ expect(deactivatedTag.active).to.equal(false);
+ });
+
+ it('Should not be able to start a transaction with a deactivated badge', async () => {
+ const connectorId = 1;
+ const tagId = testData.newUser.tags[0].id;
+ const meterStart = 180;
+ const startDate = moment();
+ const response = await testData.chargingStationContext.startTransaction(
+ connectorId, tagId, meterStart, startDate.toDate());
+ // eslint-disable-next-line @typescript-eslint/unbound-method
+ expect(response).to.be.transactionStatus('Invalid');
+ });
+
+ it('Should be able to delete a badge that has not been used', async () => {
+ const tagId = testData.newUser.tags[1].id;
+ testData.newUser.tags = [testData.newUser.tags[2]];
+ // Update
+ await testData.userService.updateEntity(
+ testData.userService.userApi,
+ testData.newUser
+ );
+ testData.newUser = (await testData.userService.getEntityById(
+ testData.userService.userApi,
+ testData.newUser,
+ false
+ )).data;
+ // Check
+ const deactivatedTag = testData.newUser.tags.find(tag => tag.id === tagId);
+ expect(testData.newUser.tags).has.lengthOf(2);
+ expect(deactivatedTag).to.be.undefined;
+
+ });
+
it('Should find the updated user by id', async () => {
// Check if the updated entity can be retrieved with its id
const updatedUser = await testData.userService.getEntityById(

MODIFY
test/factories/UserFactory.ts
test/factories/UserFactory.ts
@@ -36,7 +36,20 @@ const userFactory = Factory.define('user')
.attr('status', 'A')
.attr('locale', 'en_US')
.attr('tags', () => [
- { id: faker.random.alphaNumeric(8).toUpperCase(),
+ {
+ id: faker.random.alphaNumeric(8).toUpperCase(),
+ description: '',
+ issuer: true,
+ active: true
+ },
+ {
+ id: faker.random.alphaNumeric(8).toUpperCase(),
+ description: '',
+ issuer: true,
+ active: true
+ },
+ {
+ id: faker.random.alphaNumeric(8).toUpperCase(),
description: '',
issuer: true,
active: true

User badge UT cases