Add GET tests for basic user.
MODIFY
test/api/SiteOrgTest.ts
test/api/SiteOrgTest.ts
@@ -26,6 +26,35 @@ class TestData {
const testData: TestData = new TestData();
+function login(userRole) {
+ testData.userContext = testData.tenantContext.getUserContext(userRole);
+ 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
+ );
+ }
+}
+
+async function createSite() {
+ // Create a site
+ testData.newSite = await testData.userService.createEntity(
+ testData.userService.siteApi,
+ Factory.site.build({ companyID: testData.tenantContext.getContext().companies[0].id })
+ );
+ testData.createdSites.push(testData.newSite);
+}
+
+async function assignUserToSite(userRole, site) {
+ // Assign the user to the site
+ const userContext = await testData.tenantContext.getUserContext(userRole);
+ await testData.userService.siteApi.addUsersToSite(site.id, [userContext.id]);
+}
+
+
describe('Site tests', function() {
this.timeout(1000000); // Will automatically stop the unit test after that period of time
@@ -34,10 +63,6 @@ describe('Site tests', function() {
await ContextProvider.defaultInstance.prepareContexts();
});
- afterEach(() => {
- // Can be called after each UT to clean up created data
- });
-
after(async () => {
// Final clean up at the end
await ContextProvider.defaultInstance.cleanUpCreatedContent();
@@ -54,6 +79,12 @@ describe('Site tests', function() {
);
});
+ describe('Where admin user', () => {
+
+ before(async () => {
+ login(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ });
+
after(async () => {
// Delete any created site
testData.createdSites.forEach(async (site) => {
@@ -75,21 +106,6 @@ describe('Site tests', function() {
testData.createdUsers = [];
});
- 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
- );
- }
- });
-
it('Should be able to create a new site', async () => {
// Create the entity
testData.newSite = await testData.userService.createEntity(
@@ -166,44 +182,71 @@ describe('Site tests', function() {
describe('Where basic user', () => {
before(async () => {
- 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
+ login(ContextDefinition.USER_CONTEXTS.DEFAULT_ADMIN);
+ await createSite();
+ await assignUserToSite(ContextDefinition.USER_CONTEXTS.BASIC_USER, testData.newSite);
+ await createSite();
+ login(ContextDefinition.USER_CONTEXTS.BASIC_USER);
+ });
+
+ after(async () => {
+ // Delete any created site
+ testData.createdSites.forEach(async (site) => {
+ await testData.centralUserService.deleteEntity(
+ testData.centralUserService.siteApi,
+ site,
+ false
);
- }
+ });
+ testData.createdSites = [];
+ // Delete any created user
+ testData.createdUsers.forEach(async (user) => {
+ await testData.centralUserService.deleteEntity(
+ testData.centralUserService.userApi,
+ user,
+ false
+ );
+ });
+ testData.createdUsers = [];
});
- // it('Should find the created site by id', async () => {
- // // Check if the created entity can be retrieved with its id
- // await testData.userService.getEntityById(
- // testData.userService.siteApi,
- // testData.newSite
- // );
- // });
-
- // it('Should find the created site in the site list', async () => {
- // // Check if the created entity is in the list
- // await testData.userService.checkEntityInList(
- // testData.userService.siteApi,
- // testData.newSite
- // );
- // });
-
- // it('Should find the updated site by id', async () => {
- // // Check if the updated entity can be retrieved with its id
- // const updatedSite = await testData.userService.getEntityById(
- // testData.userService.siteApi,
- // testData.newSite
- // );
- // // Check
- // expect(updatedSite.name).to.equal(testData.newSite.name);
- // });
+ it('Should find a site he is assigned to', async () => {
+ await testData.userService.getEntityById(
+ testData.userService.siteApi,
+ testData.createdSites[0]
+ );
+ });
+ it('Should find a site he is assigned to in the site list', async () => {
+ // Check if the created entity is in the list
+ await testData.userService.checkEntityInList(
+ testData.userService.siteApi,
+ testData.createdSites[0]
+ );
+ });
+
+ it('Should not find a site he is not assigned to', async () => {
+ try {
+ await testData.userService.getEntityById(
+ testData.userService.siteApi,
+ testData.createdSites[1]
+ );
+ } catch (error) {
+ expect(error.actual).to.eq(403);
+ }
+ });
+
+ it('Should not find a site he is not assigned to in the site list', async () => {
+ try {
+ // Check if the created entity is in the list
+ await testData.userService.checkEntityInList(
+ testData.userService.siteApi,
+ testData.createdSites[1]
+ );
+ } catch (error) {
+ expect(error.actual.result).to.not.containSubset([{ id: testData.createdSites[1].id }]);
+ }
+ });
it('Should not be able to create a new site', async () => {
try {
@@ -243,7 +286,5 @@ describe('Site tests', function() {
});
});
-
});
-
});

MODIFY
test/api/client/SiteApi.ts
test/api/client/SiteApi.ts
@@ -41,4 +41,21 @@ export default class SiteApi extends CrudApi {
public async delete(id) {
return super.delete(id, '/client/api/SiteDelete');
}
+
+ public async assignSiteAdmin(siteId, userId) {
+ return super.update({
+ siteID: siteId,
+ userID: userId,
+ siteAdmin: true
+ }, '/client/api/SiteUserAdmin');
+ }
+
+ public async assignSiteOwner(siteId, userId) {
+ return super.update({
+ siteID: siteId,
+ userID: userId,
+ siteOwner: true
+ }, '/client/api/SiteOwner');
+ }
+
}

Add GET tests for basic user.