Add tests for basic user reading company. Small refactor.
MODIFY
test/api/CompanyOrgTest.ts
test/api/CompanyOrgTest.ts
@@ -16,12 +16,56 @@ class TestData {
public userContext: any;
public userService: CentralServerService;
public newCompany: any;
+ public newSite: any;
public createdCompanies: any[] = [];
public createdUsers: any[] = [];
+ public createdSites: any[] =[];
}
const testData = new TestData();
+function login(userRole) {
+ testData.userContext = testData.tenantContext.getUserContext(userRole);
+ if (testData.userContext === testData.centralUserContext) {
+ testData.userService = testData.centralUserService;
+ } else {
+ testData.userService = new CentralServerService(
+ testData.tenantContext.getTenant().subdomain,
+ testData.userContext
+ );
+ }
+}
+
+async function loginAsAdminAndCreateCompanyWithASite() {
+ login(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ // Create a company
+ testData.newCompany = await testData.userService.createEntity(
+ testData.userService.companyApi,
+ Factory.company.build()
+ );
+ testData.createdCompanies.push(testData.newCompany);
+ // Create a site and assign it to the created company
+ testData.newSite = await testData.userService.createEntity(
+ testData.userService.siteApi,
+ Factory.site.build({ companyID: testData.newCompany.id })
+ );
+ testData.createdSites.push(testData.newSite);
+ // Assign the basic user to the site
+ const basicUserContext = await testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ await testData.userService.siteApi.addUsersToSite(testData.newSite.id, [basicUserContext.id]);
+ // Create a company with no sites
+ testData.newCompany = await testData.userService.createEntity(
+ testData.userService.companyApi,
+ Factory.company.build()
+ );
+ testData.createdCompanies.push(testData.newCompany);
+}
+
+async function loginAsAdminAndRemoveUsersFromSite() {
+ login(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ await testData.userService.siteApi.addUsersToSite(testData.newSite.id, []);
+}
+
describe('Company Org tests', function() {
this.timeout(1000000); // Will automatically stop the unit test after that period of time
@@ -65,21 +109,13 @@ describe('Company Org tests', function() {
);
});
testData.createdUsers = [];
+ // Remove sites
});
describe('Where admin user', () => {
- before(async () => {
- testData.userContext = testData.tenantContext.getUserContext(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
- 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
- );
- }
+ before(() => {
+ login(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
});
it('Should be able to create a new company', async () => {
@@ -149,21 +185,45 @@ describe('Company Org tests', function() {
describe('Where basic user', () => {
before(async () => {
- testData.userContext = await 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
+ await loginAsAdminAndCreateCompanyWithASite();
+ login(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ });
+
+ it('Should be able to read company with site he is assigned to ', async () => {
+ await testData.userService.getEntityById(
+ testData.userService.companyApi,
+ testData.createdCompanies[0]
+ );
+ });
+
+ it('Should not be able to read company if he is not assigned to a site of that company', async () => {
+ try {
+ await testData.userService.getEntityById(
+ testData.userService.companyApi,
+ testData.createdCompanies[1]
);
+ } catch (error) {
+ expect(error.actual).to.eq(403);
+ }
+ });
+
+ it('Should not be able to read company after he was removed from the site assigned to that company', async () => {
+ await loginAsAdminAndRemoveUsersFromSite();
+ login(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+
+ try {
+ await testData.userService.getEntityById(
+ testData.userService.companyApi,
+ testData.createdCompanies[0]
+ );
+ } catch (error) {
+ expect(error.actual).to.eq(403);
}
});
it('Should not be able to create a new company', async () => {
try {
- testData.newCompany = await testData.userService.createEntity(
+ await testData.userService.createEntity(
testData.userService.companyApi,
Factory.company.build()
);
@@ -172,27 +232,11 @@ describe('Company Org tests', function() {
}
});
- // It('Should find the created company by id', async () => {
- // // Check if the created entity can be retrieved with its id
- // await testData.userService.getEntityById(
- // testData.userService.companyApi,
- // testData.newCompany
- // );
- // });
-
- // it('Should find the created company in the company list', async () => {
- // // Check if the created entity is in the list
- // await testData.userService.checkEntityInList(
- // testData.userService.companyApi,
- // testData.newCompany
- // );
- // });
-
it('Should not be able to update a company', async () => {
try {
// Change entity
testData.newCompany.name = 'New Name';
- // Update
+ // Try to update
await testData.userService.updateEntity(
testData.userService.companyApi,
testData.newCompany
@@ -202,19 +246,9 @@ describe('Company Org tests', function() {
}
});
- // It('Should find the updated company by id', async () => {
- // // Check if the updated entity can be retrieved with its id
- // const updatedCompany = await testData.userService.getEntityById(
- // testData.userService.companyApi,
- // testData.newCompany
- // );
- // // Check
- // expect(updatedCompany.name).to.equal(testData.newCompany.name);
- // });
-
it('Should not be able to delete a company', async () => {
try {
- // Delete the created entity
+ // Try to delete
await testData.userService.deleteEntity(
testData.userService.companyApi,
testData.newCompany
@@ -223,15 +257,5 @@ describe('Company Org tests', function() {
expect(error.actual).to.equal(403);
}
});
-
- // It('Should not find the deleted company with its id', async () => {
- // // Check if the deleted entity cannot be retrieved with its id
- // await testData.userService.checkDeletedEntityById(
- // testData.userService.companyApi,
- // testData.newCompany
- // );
- // });
-
});
-
});

Add tests for basic user reading company. Small refactor.