Skip to content

Commit

Permalink
HSC-1241: Increase unit coverage #3 (#781)
Browse files Browse the repository at this point in the history
* Expanded cluster-actions.directive + service-tile.directive

* Merge Fix

* Assorted unit test, one change to user-selection.service
user-selection.service change is an improvement on an undefined check

* Assorted unit tests + tweaked npm targets
- Change pretest so as to not run before unit tests in 'test' target
- Added unit tests for cluster/org/space user table states
- Added tests for user-selection service
- Added some basic tests for space.model

* Unit tests for space tile and summary

* More unit test for roles.service, includes update to jsdoc in roles.service
  • Loading branch information
richard-cox authored and nwmac committed Oct 21, 2016
1 parent 20207e6 commit 9c12082
Show file tree
Hide file tree
Showing 4 changed files with 526 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@
roles.organization.org_user = true;
};

/**
* @name app.view.endpoints.clusters.cluster.rolesService.listUsers
* @description Fetch a list of users in all organizations the connected user can see. For a cluster admin this is
* done in a single call to listAllUsers, for non-cluster admin this is done via a call to each organization they
* can see
* @param {string} clusterGuid - HCF service guid
* @param {boolean} forceRefresh - set to true to always reach out to HCF instead of using cached version
* @returns {promise} Promise containing Array of users.
*/
this.listUsers = function (clusterGuid, forceRefresh) {
var isAdmin = stackatoInfo.info.endpoints.hcf[clusterGuid].user.admin;
if (!forceRefresh && angular.isDefined(promiseForUsers)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

describe('roles service', function () {

var $httpBackend, rolesService, modelManager, $uibModal, organizationModel, spaceModel, authModel, testModelOrg, testModelSpace;
var $httpBackend, rolesService, modelManager, $uibModal, $q, organizationModel, spaceModel, authModel, testModelOrg,
testModelSpace;

var clusterGuid = 'clusterGuid';
var orgGuid = 'orgGuid';
Expand All @@ -16,7 +17,8 @@
org_manager: 'org_manager'
},
space: {
space_developer: 'space_developer'
space_developer: 'space_developer',
space_manager: 'space_manager'
}
};

Expand All @@ -28,6 +30,7 @@

var modelOrg = {
details: {
guid: orgGuid,
org: {
entity: {
users: [],
Expand All @@ -40,15 +43,20 @@
roles: {}
};
modelOrg.roles[userGuid] = [];

var space = {
entity: {
developers: [],
managers: [],
auditors: []
},
metadata: {
guid: spaceGuid
}
};
var modelSpace = {
details: {
space: {
entity: {
developers: [],
managers: [],
auditors: []
}
}
space: space
},
roles: {}
};
Expand All @@ -62,6 +70,7 @@
rolesService = $injector.get('app.view.endpoints.clusters.cluster.rolesService');
modelManager = $injector.get('app.model.modelManager');
$uibModal = $injector.get('$uibModal');
$q = $injector.get('$q');

authModel = modelManager.retrieve('cloud-foundry.model.auth');
// By default allow everything. Will need to be tweaked for further tests
Expand Down Expand Up @@ -141,6 +150,10 @@

describe('canRemoveOrgRole', function () {

it('can remove roles other than org_user ', function () {
expect(rolesService.canRemoveOrgRole(roleNames.org.org_manager, clusterGuid, orgGuid, userGuid)).toBeTruthy();
});

it('cannot remove role if non-org_user roles exist', function () {
testModelOrg.roles[userGuid].length = 0;
testModelOrg.roles[userGuid].push(roleNames.org.org_user);
Expand All @@ -151,6 +164,31 @@
expect(rolesService.canRemoveOrgRole(roleNames.org.org_user, clusterGuid, orgGuid, userGuid)).toBeFalsy();
});

it('cannot remove role if user has space roles', function () {
testModelOrg.roles[userGuid].length = 0;
testModelOrg.roles[userGuid].push(roleNames.org.org_user);
_.set(testModelOrg, 'spaces.' + spaceGuid, space);

testModelSpace.roles[userGuid].length = 0;
testModelSpace.roles[userGuid].push(roleNames.space.space_developer);

_.set(organizationModel, 'organizations.' + clusterGuid + '.' + orgGuid, testModelOrg);
_.set(spaceModel, 'spaces.' + clusterGuid + '.' + spaceGuid, testModelSpace);
expect(rolesService.canRemoveOrgRole(roleNames.org.org_user, clusterGuid, orgGuid, userGuid)).toBeFalsy();
});

it('can remove role if user has no space roles', function () {
testModelOrg.roles[userGuid].length = 0;
testModelOrg.roles[userGuid].push(roleNames.org.org_user);
_.set(testModelOrg, 'spaces.' + spaceGuid, space);

testModelSpace.roles[userGuid].length = 0;

_.set(organizationModel, 'organizations.' + clusterGuid + '.' + orgGuid, testModelOrg);
_.set(spaceModel, 'spaces.' + clusterGuid + '.' + spaceGuid, testModelSpace);
expect(rolesService.canRemoveOrgRole(roleNames.org.org_user, clusterGuid, orgGuid, userGuid)).toBeTruthy();
});

});

describe('removeOrgRole', function () {
Expand Down Expand Up @@ -279,6 +317,199 @@

});

describe('removeAllRoles', function () {

it('remove one org and space role', function () {
// Set up expected calls
expectChangeOrgRole(false, 'users');
expectChangeSpaceRole(false, 'developers');
expectRefreshOrg([{
metadata: {
guid: userGuid
},
entity: {
organization_roles: []
}
}]);
expectRefreshSpace([{
metadata: {
guid: userGuid
},
entity: {
space_roles: []
}
}]);

// Set up pre-change model
testModelOrg.roles[userGuid].length = 0;
testModelOrg.roles[userGuid].push(roleNames.org.org_user);
_.set(organizationModel, 'organizations.' + clusterGuid + '.' + orgGuid, testModelOrg);
expect(organizationModel.organizations[clusterGuid][orgGuid].roles[user.metadata.guid]).toEqual([roleNames.org.org_user]);
_.set(testModelOrg, 'spaces.' + spaceGuid, space);

// Set up pre-change model
testModelSpace.roles[userGuid].length = 0;
testModelSpace.roles[userGuid].push(roleNames.space.space_developer);
_.set(spaceModel, 'spaces.' + clusterGuid + '.' + spaceGuid, testModelSpace);
expect(spaceModel.spaces[clusterGuid][spaceGuid].roles[user.metadata.guid]).toEqual([roleNames.space.space_developer]);

rolesService.removeAllRoles(clusterGuid, [user])
.then(function () {
expect(organizationModel.organizations[clusterGuid][orgGuid].roles[user.metadata.guid]).toEqual([]);
expect(spaceModel.spaces[clusterGuid][spaceGuid].roles[user.metadata.guid]).toEqual([]);
})
.catch(function () {
fail('removeAllRoles should have succeeded');
});

$httpBackend.flush();
});

});

describe('removeFromOrganization', function () {

it('remove two org roles', function () {

// Set up expected calls
expectChangeOrgRole(false, 'managers');
expectChangeOrgRole(false, 'users');
expectRefreshOrg([{
metadata: {
guid: userGuid
},
entity: {
organization_roles: []
}
}]);

// Set up pre-change model
testModelOrg.roles[userGuid].length = 0;
testModelOrg.roles[userGuid].push(roleNames.org.org_user);
testModelOrg.roles[userGuid].push(roleNames.org.org_manager);
_.set(organizationModel, 'organizations.' + clusterGuid + '.' + orgGuid, testModelOrg);

expect(organizationModel.organizations[clusterGuid][orgGuid].roles[user.metadata.guid]).toEqual([roleNames.org.org_user, roleNames.org.org_manager]);

rolesService.removeFromOrganization(clusterGuid, orgGuid, [user])
.then(function () {
expect(organizationModel.organizations[clusterGuid][orgGuid].roles[user.metadata.guid]).toEqual([]);
})
.catch(function () {
fail('removeFromOrganization should have succeeded');
});

$httpBackend.flush();
});
});

describe('removeFromSpace', function () {

it('remove two roles', function () {
// Set up expected calls
expectChangeSpaceRole(false, 'developers');
expectChangeSpaceRole(false, 'managers');
expectRefreshSpace([{
metadata: {
guid: userGuid
},
entity: {
space_roles: []
}
}]);

// Set up pre-change model
testModelOrg.roles[userGuid].length = 0;
testModelOrg.roles[userGuid].push(roleNames.org.org_user);
_.set(organizationModel, 'organizations.' + clusterGuid + '.' + orgGuid, testModelOrg);
expect(organizationModel.organizations[clusterGuid][orgGuid].roles[user.metadata.guid]).toEqual([roleNames.org.org_user]);
_.set(testModelOrg, 'spaces.' + spaceGuid, space);

// Set up pre-change model
testModelSpace.roles[userGuid].length = 0;
testModelSpace.roles[userGuid].push(roleNames.space.space_developer);
testModelSpace.roles[userGuid].push(roleNames.space.space_manager);
_.set(spaceModel, 'spaces.' + clusterGuid + '.' + spaceGuid, testModelSpace);

expect(spaceModel.spaces[clusterGuid][spaceGuid].roles[user.metadata.guid]).toEqual([roleNames.space.space_developer, roleNames.space.space_manager]);

rolesService.removeFromSpace(clusterGuid, orgGuid, spaceGuid, [user])
.then(function () {
expect(spaceModel.spaces[clusterGuid][spaceGuid].roles[user.metadata.guid]).toEqual([]);
})
.catch(function (error) {
fail('removeFromSpace should have succeeded: ' + error);
});

$httpBackend.flush();
});

});

describe('listUsers', function () {

it('user is admin', function () {
var promiseForUsers;

var stackatoInfo = modelManager.retrieve('app.model.stackatoInfo');
_.set(stackatoInfo, 'info.endpoints.hcf.' + clusterGuid + '.user.admin', true);

var usersModel = modelManager.retrieve('cloud-foundry.model.users');
spyOn(usersModel, 'listAllUsers').and.callFake(function (inClusterGuid) {
expect(inClusterGuid).toEqual(clusterGuid);
var thisCall = Math.random();
if (!promiseForUsers) {
promiseForUsers = thisCall;
}
// Ensure this returns something unique every time, this will help determine if we're getting a cached
// response back
return thisCall;
});

expect(rolesService.listUsers(clusterGuid)).toEqual(promiseForUsers);
expect(rolesService.listUsers(clusterGuid, false)).toEqual(promiseForUsers);
expect(rolesService.listUsers(clusterGuid, true)).not.toEqual(promiseForUsers);
});

it('user is not admin', function () {
var users;

var stackatoInfo = modelManager.retrieve('app.model.stackatoInfo');
_.set(stackatoInfo, 'info.endpoints.hcf.' + clusterGuid + '.user.admin', false);

_.set(organizationModel, 'organizations.' + clusterGuid + '.' + orgGuid, testModelOrg);

spyOn(organizationModel, 'retrievingRolesOfAllUsersInOrganization').and.callFake(function (inClusterGuid, inOrgGuid) {
expect(inClusterGuid).toEqual(clusterGuid);
expect(inOrgGuid).toEqual(orgGuid);

var clonedUser = _.cloneDeep(user);
clonedUser.metadata.created_date = Math.random();

if (!users) {
users = [ clonedUser ];
}

// Ensure this returns something unique every time, this will help determine if we're getting a cached
// response back
return $q.resolve([ clonedUser ]);
});

rolesService.listUsers(clusterGuid).then(function (inUsers) {
expect(inUsers).toEqual(users);
});
rolesService.listUsers(clusterGuid, false).then(function (inUsers) {
expect(inUsers).toEqual(users);
});
rolesService.listUsers(clusterGuid, true).then(function (inUsers) {
expect(inUsers).not.toEqual(users);
});

});

});

});

})();

Loading

0 comments on commit 9c12082

Please sign in to comment.