Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Refactor delete org/space permission function to allow errorChangeUserRole to be called #1140

Merged
merged 2 commits into from Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions static_src/actions/user_actions.js
Expand Up @@ -132,9 +132,14 @@ const userActions = {
return api(
userGuid,
entityGuid,
roles,
apiKey
).catch(error => this.errorChangeUserRole(error));
).then(() => {
userActions.deletedUserRoles(
roles,
userGuid,
entityGuid,
entityType);
}).catch(error => this.errorChangeUserRole(error));
},

deletedUserRoles(roles, userGuid, entityGuid, entityType) {
Expand Down
1 change: 0 additions & 1 deletion static_src/stores/user_store.js
Expand Up @@ -156,7 +156,6 @@ export class UserStore extends BaseStore {
const orgPermissionsReq = cfApi.deleteOrgUserPermissions(
action.userGuid,
action.orgGuid,
'users',
'users');

orgPermissionsReq.then(() => {
Expand Down
34 changes: 14 additions & 20 deletions static_src/test/unit/actions/user_actions.spec.js
Expand Up @@ -587,7 +587,7 @@ describe('userActions', function() {

beforeEach(function(done) {
sandbox.spy(cfApi, 'deleteOrgUserPermissions');
sandbox.spy(userActions, 'errorRemoveUser');
sandbox.spy(userActions, 'errorChangeUserRole');
sandbox.stub(userActions, 'deletedUserRoles').returns(Promise.resolve());
roles = ['org_manager'];
apiKey = 'managers';
Expand Down Expand Up @@ -616,7 +616,6 @@ describe('userActions', function() {
expect(cfApi.deleteOrgUserPermissions).toHaveBeenCalledWith(sinon.match(
userGuid,
orgGuid,
roles,
apiKey
));
});
Expand All @@ -631,8 +630,8 @@ describe('userActions', function() {
));
});

it('should not call errorRemoveUser', function() {
expect(userActions.errorRemoveUser.called).toEqual(false);
it('should not call errorChangeUserRole', function() {
expect(userActions.errorChangeUserRole.called).toEqual(false);
});
});

Expand All @@ -645,7 +644,7 @@ describe('userActions', function() {
beforeEach(function(done) {
sandbox.spy(cfApi, 'deleteOrgUserPermissions');
sandbox.spy(userActions, 'deletedUserRoles');
sandbox.stub(userActions, 'errorRemoveUser').returns(Promise.resolve());
sandbox.spy(userActions, 'errorChangeUserRole');
roles = ['org_manager'];
apiKey = 'managers';
userGuid = 'user-123';
Expand Down Expand Up @@ -674,15 +673,13 @@ describe('userActions', function() {
expect(cfApi.deleteOrgUserPermissions).toHaveBeenCalledWith(sinon.match(
userGuid,
orgGuid,
roles,
apiKey
));
});

it('should call errorRemoveUser with the userGuid and the error response', function() {
expect(userActions.errorRemoveUser).toHaveBeenCalledOnce();
expect(userActions.errorRemoveUser).toHaveBeenCalledWith(sinon.match(
userGuid,
it('should call errorChangeUserRole with the userGuid and the error response', function() {
expect(userActions.errorChangeUserRole).toHaveBeenCalledOnce();
expect(userActions.errorChangeUserRole).toHaveBeenCalledWith(sinon.match(
{}
));
});
Expand All @@ -700,7 +697,7 @@ describe('userActions', function() {

beforeEach(function(done) {
sandbox.spy(cfApi, 'deleteSpaceUserPermissions');
sandbox.spy(userActions, 'errorRemoveUser');
sandbox.spy(userActions, 'errorChangeUserRole');
sandbox.stub(userActions, 'deletedUserRoles').returns(Promise.resolve());
roles = ['space_manager'];
apiKey = 'managers';
Expand Down Expand Up @@ -729,7 +726,6 @@ describe('userActions', function() {
expect(cfApi.deleteSpaceUserPermissions).toHaveBeenCalledWith(sinon.match(
userGuid,
spaceGuid,
roles,
apiKey
));
});
Expand All @@ -744,8 +740,8 @@ describe('userActions', function() {
));
});

it('should not call errorRemoveUser', function() {
expect(userActions.errorRemoveUser.called).toEqual(false);
it('should not call errorChangeUserRole', function() {
expect(userActions.errorChangeUserRole.called).toEqual(false);
});
});
});
Expand All @@ -759,7 +755,7 @@ describe('userActions', function() {
beforeEach(function(done) {
sandbox.spy(cfApi, 'deleteSpaceUserPermissions');
sandbox.spy(userActions, 'deletedUserRoles');
sandbox.stub(userActions, 'errorRemoveUser').returns(Promise.resolve());
sandbox.spy(userActions, 'errorChangeUserRole');
roles = ['space_manager'];
apiKey = 'managers';
userGuid = 'user-123';
Expand Down Expand Up @@ -788,15 +784,13 @@ describe('userActions', function() {
expect(cfApi.deleteSpaceUserPermissions).toHaveBeenCalledWith(sinon.match(
userGuid,
spaceGuid,
roles,
apiKey
));
});

it('should call errorRemoveUser action with all information', function() {
expect(userActions.errorRemoveUser).toHaveBeenCalledOnce();
expect(userActions.errorRemoveUser).toHaveBeenCalledWith(sinon.match(
userGuid,
it('should call errorChangeUserRole action with all information', function() {
expect(userActions.errorChangeUserRole).toHaveBeenCalledOnce();
expect(userActions.errorChangeUserRole).toHaveBeenCalledWith(sinon.match(
{}
));
});
Expand Down
25 changes: 12 additions & 13 deletions static_src/test/unit/util/cf_api.spec.js
Expand Up @@ -1037,13 +1037,11 @@ describe('cfApi', function() {
var spy = sandbox.stub(http, 'delete').returns(Promise.resolve({})),
expectedUserGuid = 'zvmxncznv-9u8qwphu',
expectedOrgGuid = '0291kdvakjbdfvhp',
expectedPermission = 'manager',
expectedApiKey = 'managers';

cfApi.deleteOrgUserPermissions(
expectedUserGuid,
expectedOrgGuid,
expectedPermission,
expectedApiKey).then(() => {
expect(spy).toHaveBeenCalledOnce();
let actual = spy.getCall(0).args[0];
Expand All @@ -1056,26 +1054,27 @@ describe('cfApi', function() {

it(`should call user action on a 400 response that has code 10006 with
message about the error from cf`, function(done) {
var spy = sandbox.spy(userActions, 'errorRemoveUser'),
var spy = sandbox.spy(http, 'delete'),
expectedUserGuid = 'zcvmzxncbvpafd',
expectedOrgGuid = '0291kdvakjbdfvhp',
expectedApiKey = 'managers',
expectedResponse = {
code: 10006,
description: 'Please delete the user associations for your spaces',
error_code: 'CF-AssociationNotEmpty'
};
moxios.stubOnce('DELETE', `/v2/organizations/asdf/apiKey/${expectedUserGuid}`, {
moxios.stubOnce('DELETE', `/v2/organizations/${expectedOrgGuid}/${expectedApiKey}/${expectedUserGuid}`, {
status: 400,
response: expectedResponse
});

cfApi.deleteOrgUserPermissions(expectedUserGuid, 'asdf', 'role', 'apiKey').then(
() => {
expect(spy).toHaveBeenCalledOnce();
let args = spy.getCall(0).args;
expect(args[0]).toEqual(expectedUserGuid);
expect(args[1]).toEqual(expectedResponse);
done();
}).catch(done.fail);
cfApi.deleteOrgUserPermissions(expectedUserGuid, expectedOrgGuid, expectedApiKey);
expect(spy).toHaveBeenCalledOnce();
let actual = spy.getCall(0).args[0];
expect(actual).toMatch(new RegExp(expectedUserGuid));
expect(actual).toMatch(new RegExp(expectedOrgGuid));
expect(actual).toMatch(new RegExp(expectedApiKey));
done();
});
});

Expand All @@ -1084,7 +1083,7 @@ describe('cfApi', function() {
var spy = sandbox.spy(http, 'put'),
expectedUserGuid = 'zvmxncznv-9u8qwphu',
expectedOrgGuid = '0291kdvakjbdfvhp',
expectedPermission = 'manager';
expectedPermission = 'managers';

cfApi.putOrgUserPermissions(
expectedUserGuid,
Expand Down
19 changes: 6 additions & 13 deletions static_src/util/cf_api.js
Expand Up @@ -400,13 +400,10 @@ export default {
/${userGuid}`);
},

deleteOrgUserPermissions(userGuid, orgGuid, permissions, apiKey) {
deleteOrgUserPermissions(userGuid, orgGuid, apiKey) {
return http.delete(`${APIV}/organizations/${orgGuid}/${apiKey}/${userGuid}`)
.then(() => {
userActions.deletedUserRoles(permissions, userGuid, orgGuid, 'organizations');
}, (err) => {
userActions.errorRemoveUser(userGuid, err.response.data);
});
.then((res) => res.response
);
},

putOrgUserPermissions(userGuid, orgGuid, permissions) {
Expand Down Expand Up @@ -458,14 +455,10 @@ export default {
.then((res) => this.formatSplitResponse(res.data));
},

// TODO refactor with org user permissions
deleteSpaceUserPermissions(userGuid, spaceGuid, role, apiKey) {
deleteSpaceUserPermissions(userGuid, spaceGuid, apiKey) {
return http.delete(`${APIV}/spaces/${spaceGuid}/${apiKey}/${userGuid}`)
.then(() => {
userActions.deletedUserRoles(role, userGuid, spaceGuid, 'spaces');
}, (err) => {
userActions.errorRemoveUser(userGuid, err.response.data);
});
.then((res) => res.response
);
},

fetchServicePlan(servicePlanGuid) {
Expand Down