re-added site+siteAdmin to authorization
added BAD_REQUEST for siteadmin token creation error handling
modified unit tests even they will be refactored
/!\ unit test don't check for siteAdmin (yet)
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -412,9 +412,11 @@ export default class Authorizations {
});
}
- public static canReadRegistrationToken(loggedUser: UserToken): boolean {
- return Authorizations.canPerformAction(loggedUser, Entity.TOKEN, Action.READ
- );
+ public static canReadRegistrationToken(loggedUser: UserToken, siteID: string): boolean {
+ return Authorizations.canPerformAction(loggedUser, Entity.TOKEN, Action.READ, {
+ site: siteID,
+ sites: loggedUser.sitesAdmin
+ });
}
public static canDeleteRegistrationToken(loggedUser: UserToken, siteID: string): boolean {

MODIFY
src/types/HTTPError.ts
src/types/HTTPError.ts
@@ -58,6 +58,8 @@ export enum HTTPError {
CANNOT_ACQUIRE_LOCK = 510,
INVALID_FILE_FORMAT = 583,
+
+ BAD_REQUEST = 400,
}
export enum HTTPAuthError {

MODIFY
test/api/RegistrationTokenTest.ts
test/api/RegistrationTokenTest.ts
@@ -45,7 +45,7 @@ describe('Registration token tests', function() {
testData.adminCentralService.registrationApi,
registrationTokenToCreate
);
- testData.createdRegistrationTokens.push(registrationTokenToCreate);
+ testData.createdRegistrationTokens.push(testData.newRegistrationToken);
});
// Check creation readById
@@ -65,11 +65,9 @@ describe('Registration token tests', function() {
);
});
- // Update / revoke
+ // Update
it('Should be able to update a registration token', async () => {
- testData.newRegistrationToken.description = faker.random.word();
- testData.newRegistrationToken.expirationDate = faker.date.future();
-
+ testData.newRegistrationToken.expirationDate = faker.date.past();
await testData.adminCentralService.updateEntity(
testData.adminCentralService.registrationApi,
testData.newRegistrationToken,
@@ -84,10 +82,37 @@ describe('Registration token tests', function() {
testData.newRegistrationToken,
false
);
- expect(updatedRegistrationToken.data.description).to.equal(testData.newRegistrationToken.description);
+ // Expect(updatedRegistrationToken.data.description).to.equal(testData.newRegistrationToken.description);
expect(updatedRegistrationToken.data.expirationDate).to.equal(moment.utc(testData.newRegistrationToken.expirationDate).format('yyyy-MM-DD[T]HH:mm:ss.SSS[Z]'));
});
+ // Check revoke expired
+ it('Should not be able to revoke expired registration token', async () => {
+ // TestData.newRegistrationToken = Factory.registrationToken.build();
+ testData.newRegistrationToken.description = faker.random.word();
+ const response = await testData.adminCentralService.revokeEntity(
+ testData.adminCentralService.registrationApi,
+ testData.newRegistrationToken,
+ false
+ );
+ expect(response.status).to.be.eq(HTTPError.GENERAL_ERROR);
+ });
+
+ // Revoke
+ it('Should be able to revoke a registration token', async () => {
+ // Update expiration date before testing revocation
+ testData.newRegistrationToken.expirationDate = faker.date.future();
+ await testData.adminCentralService.updateEntity(
+ testData.adminCentralService.registrationApi,
+ testData.newRegistrationToken,
+ );
+ // TestData.newRegistrationToken.revocationDate = new Date();
+ await testData.adminCentralService.revokeEntity(
+ testData.adminCentralService.registrationApi,
+ testData.newRegistrationToken,
+ );
+ });
+
// Delete
it('Should be able to delete the created registration token', async () => {
// Delete the created entity
@@ -99,6 +124,15 @@ describe('Registration token tests', function() {
// Verify delete readById
it('Should not find the deleted asset with its id', async () => {
+ const registrationTokenToCreate = Factory.registrationToken.build();
+ testData.newRegistrationToken = await testData.adminCentralService.createEntity(
+ testData.adminCentralService.registrationApi,
+ registrationTokenToCreate
+ );
+ await testData.adminCentralService.deleteEntity(
+ testData.adminCentralService.registrationApi,
+ testData.newRegistrationToken
+ );
// Check the deleted entity cannot be retrieved with its id
await testData.adminCentralService.checkDeletedEntityById(
testData.adminCentralService.registrationApi,
@@ -107,11 +141,33 @@ describe('Registration token tests', function() {
});
});
- describe('Where basic user', () => {
+ describe('Where basic user assigned', () => {
before(async () => {
testData.tenantContext = await ContextProvider.defaultInstance.getTenantContext(ContextDefinition.TENANT_CONTEXTS.TENANT_WITH_ALL_COMPONENTS);
testData.basicUserContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
testData.basicCentralService = new CentralServerService(ContextDefinition.TENANT_CONTEXTS.TENANT_WITH_ALL_COMPONENTS, testData.basicUserContext);
+ // // Create the entity
+ // testData.newSiteArea = await testData.userService.createEntity(
+ // testData.userService.siteAreaApi,
+ // Factory.siteArea.build({ siteID: testData.siteContext.getSite().id })
+ // );
+ // testData.userContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ // if (testData.userContext === testData.centralUserContext) {
+ // // Reuse the central user service (to avoid double login)
+ // testData.userService = testData.centralUserService;
+ // } else {
+ // testData.userService = new CentralServerService(
+ // testData.tenantContext.getTenant().subdomain,
+ // testData.userContext
+ // );
+ // }
+ const registrationTokenToCreate = Factory.registrationToken.build();
+ testData.newRegistrationToken = await testData.adminCentralService.createEntity(
+ testData.adminCentralService.registrationApi,
+ registrationTokenToCreate
+ );
+ testData.createdRegistrationTokens.push(testData.newRegistrationToken);
+
});
// Create
it('Should not be able to create a new registration token', async () => {
@@ -127,10 +183,11 @@ describe('Registration token tests', function() {
// Check readById
it('Should not be able to get registration token by id', async () => {
+ // Add a token user should
// Check cannot access
const response = await testData.basicCentralService.getEntityById(
testData.basicCentralService.registrationApi,
- testData.newRegistrationToken,
+ testData.createdRegistrationTokens[1],
false
);
expect(response.status).to.be.eq(HTTPAuthError.FORBIDDEN);
@@ -141,7 +198,7 @@ describe('Registration token tests', function() {
// Check cannot access
const response = await testData.basicCentralService.checkEntityInList(
testData.basicCentralService.registrationApi,
- testData.newRegistrationToken,
+ testData.createdRegistrationTokens[1],
false
);
expect(response.status).to.equal(HTTPAuthError.FORBIDDEN);
@@ -155,21 +212,40 @@ describe('Registration token tests', function() {
const response = await testData.basicCentralService.updateEntity(
testData.basicCentralService.registrationApi,
- testData.newRegistrationToken,
+ testData.createdRegistrationTokens[1],
false
);
expect(response.status).to.equal(HTTPAuthError.FORBIDDEN);
});
// Delete
- it('Should not be able to delete the created registration token', async () => {
+ it('Should not be able to delete a registration token', async () => {
// Check cannot delete
const response = await testData.basicCentralService.deleteEntity(
+ testData.basicCentralService.registrationApi,
+ testData.createdRegistrationTokens[1],
+ false
+ );
+ expect(response.status).to.equal(HTTPAuthError.FORBIDDEN);
+ });
+ });
+
+ describe('Where basic user unnassigned', () => {
+ before(async () => {
+ testData.tenantContext = await ContextProvider.defaultInstance.getTenantContext(ContextDefinition.TENANT_CONTEXTS.TENANT_WITH_ALL_COMPONENTS);
+ testData.basicUserContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER_UNASSIGNED);
+ testData.basicCentralService = new CentralServerService(ContextDefinition.TENANT_CONTEXTS.TENANT_WITH_ALL_COMPONENTS, testData.basicUserContext);
+ });
+ // Create
+ it('Should not be able to create a new registration token', async () => {
+ // Check cannot create
+ testData.newRegistrationToken = Factory.registrationToken.build();
+ const response = await testData.basicCentralService.createEntity(
testData.basicCentralService.registrationApi,
testData.newRegistrationToken,
false
);
- expect(response.status).to.equal(HTTPError.GENERAL_ERROR);
+ expect(response.status).to.be.eq(HTTPAuthError.FORBIDDEN);
});
});
});

MODIFY
test/api/client/CentralServerService.ts
test/api/client/CentralServerService.ts
@@ -261,7 +261,6 @@ export default class CentralServerService {
}
// Let the caller to handle response
return response;
-
}
public async checkDeletedEntityById(entityApi, entity, performCheck = true) {
@@ -279,6 +278,22 @@ export default class CentralServerService {
}
}
+ public async revokeEntity(entityApi, entity, performCheck = true) {
+ // Check
+ expect(entity).to.not.be.null;
+ // Delete it in the backend
+ const response = await entityApi.revoke(entity.id);
+ // Check
+ if (performCheck) {
+ // Check
+ expect(response.status).to.equal(200);
+ expect(response.data.status).to.eql('Success');
+ return response;
+ }
+ // Let the caller to handle response
+ return response;
+ }
+
public async reconnect() {
await this.authenticatedApi.authenticate(true);
}

MODIFY
test/api/client/RegistrationTokenApi.ts
test/api/client/RegistrationTokenApi.ts
@@ -19,10 +19,14 @@ export default class RegistrationTokenApi extends CrudApi {
}
public async update(data) {
- return super.update(data, 'client/api/RegistrationTokenUpdate');
+ return super.update(data, '/client/api/RegistrationTokenUpdate');
}
public async delete(id) {
return super.delete(id, '/client/api/RegistrationTokenDelete');
}
+
+ public async revoke(id) {
+ return super.delete(id, '/client/api/RegistrationTokenRevoke');
+ }
}

MODIFY
test/factories/RegistrationTokenFactory.ts
test/factories/RegistrationTokenFactory.ts
@@ -2,7 +2,6 @@ import { Factory } from 'rosie';
import faker from 'faker';
export default Factory.define('registrationToken')
- .attr('id', () => faker.random.alphaNumeric(20).toString().toUpperCase())
.attr('description', () => 'Registration token for unit test')
.attr('revocationDate', null)
.attr('siteAreaID', null);

re-added site+siteAdmin to authorization
added BAD_REQUEST for siteadmin token creation error handling
modified unit tests even they will be refactored
/!\ unit test don't check for siteAdmin (yet)