Adding asset test cases for basic user
MODIFY
test/api/AssetTest.ts
test/api/AssetTest.ts
@@ -59,6 +59,33 @@ describe('Asset', function() {
testData.tenantContext.getTenant().subdomain,
testData.centralUserContext
);
+ testData.userService = testData.centralUserService;
+ // Create a new Company
+ testData.newCompany = await testData.userService.createEntity(
+ testData.userService.companyApi,
+ Factory.company.build()
+ );
+ testData.createdCompanies.push(testData.newCompany);
+ expect(testData.newCompany).to.not.be.null;
+ // Create a new Site
+ testData.newSite = await testData.userService.createEntity(
+ testData.userService.siteApi,
+ Factory.site.build({
+ companyID: testData.newCompany.id
+ })
+ );
+ testData.createdSites.push(testData.newSite);
+ expect(testData.newSite).to.not.be.null;
+ // Create a new Site Area
+ testData.newSiteArea = await testData.userService.createEntity(
+ testData.userService.siteAreaApi,
+ Factory.siteArea.build({
+ siteID: testData.newSite.id
+ })
+ );
+ testData.createdSiteAreas.push(testData.newSiteArea);
+ expect(testData.newSiteArea).to.not.be.null;
+
});
after(async () => {
@@ -102,7 +129,7 @@ describe('Asset', function() {
describe('Where admin user', () => {
- before(async () => {
+ before(() => {
testData.userContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
if (testData.userContext === testData.centralUserContext) {
// Reuse the central user service (to avoid double login)
@@ -113,33 +140,6 @@ describe('Asset', function() {
testData.userContext
);
}
- // Create a new Company
- testData.newCompany = await testData.userService.createEntity(
- testData.userService.companyApi,
- Factory.company.build()
- );
- testData.createdCompanies.push(testData.newCompany);
- // Check
- expect(testData.newCompany).to.not.be.null;
- // Create a new Site
- testData.newSite = await testData.userService.createEntity(
- testData.userService.siteApi,
- Factory.site.build({
- companyID: testData.newCompany.id
- })
- );
- testData.createdSites.push(testData.newSite);
- // Check
- expect(testData.newSite).to.not.be.null;
- // Create a new Site Area
- testData.newSiteArea = await testData.userService.createEntity(
- testData.userService.siteAreaApi,
- Factory.siteArea.build({
- siteID: testData.newSite.id
- })
- );
- testData.createdSiteAreas.push(testData.newSiteArea);
- expect(testData.newSiteArea).to.not.be.null;
});
it('Should be able to create a new Asset', async () => {
@@ -240,6 +240,86 @@ describe('Asset', function() {
expect(response.data.count).to.be.eq(1);
});
});
+
+ describe('Where basic user', () => {
+ before(async () => {
+ const adminContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ const basicUserContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ if (adminContext === testData.centralUserContext) {
+ // Reuse the central user service (to avoid double login)
+ testData.userService = testData.centralUserService;
+ } else {
+ testData.userService = new CentralServerService(
+ testData.tenantContext.getTenant().subdomain,
+ adminContext
+ );
+ }
+ // Create a new Asset
+ testData.newAsset = await testData.userService.createEntity(
+ testData.userService.assetApi,
+ Factory.asset.build({
+ siteAreaID: testData.createdSiteAreas[0].id,
+ assetType: 'PR'
+ })
+ );
+ testData.createdAssets.push(testData.newAsset);
+ expect(testData.newAsset).to.not.be.null;
+ // Assign basic user to site
+ let response = await testData.userService.siteApi.addUsersToSite(testData.newSite.id, [basicUserContext.id]);
+ expect(response.status).to.be.eq(StatusCodes.OK);
+ response = await testData.userService.siteApi.assignSiteAdmin(testData.newSite.id, basicUserContext.id);
+ expect(response.status).to.be.eq(StatusCodes.OK);
+ // Login as basic user
+ testData.userContext = basicUserContext;
+ testData.userService = new CentralServerService(
+ testData.tenantContext.getTenant().subdomain,
+ testData.userContext
+ );
+ });
+
+ // Should not be able to create an asset
+ it('Should not be able to create an asset', async () => {
+ const newAssetToCreateResponse = await testData.userService.createEntity(
+ testData.userService.assetApi,
+ Factory.asset.build({
+ siteAreaID: testData.createdSiteAreas[0].id,
+ assetType: 'CO'
+ }),
+ false
+ );
+ expect(newAssetToCreateResponse.status).to.equal(StatusCodes.FORBIDDEN);
+ });
+
+ // Should not be able to update an asset
+ it('Should not be able to update an asset', async () => {
+ testData.newAsset.name = 'New Name';
+ const assetToUpdateResponse = await testData.userService.updateEntity(
+ testData.userService.assetApi,
+ testData.newAsset,
+ false
+ );
+ expect(assetToUpdateResponse.status).to.equal(StatusCodes.FORBIDDEN);
+ });
+
+ // Should not be able to delete an asset
+ it('Should not be able to delete an asset', async () => {
+ const assetToDeleteResponse = await testData.userService.deleteEntity(
+ testData.userService.assetApi,
+ testData.newAsset,
+ false
+ );
+ expect(assetToDeleteResponse.status).to.equal(StatusCodes.FORBIDDEN);
+ });
+
+ // Should be able to view the assets
+ it('Should be able to view existing assets of sites user is admin of', async () => {
+ // Check if the created entity is in the list
+ await testData.userService.checkEntityInList(
+ testData.userService.assetApi,
+ testData.newAsset
+ );
+ });
+ });
});
});

Adding asset test cases for basic user