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

Commit

Permalink
move deleteusers todo
Browse files Browse the repository at this point in the history
  • Loading branch information
James C. Scott committed Jul 21, 2017
1 parent 2afb68e commit da5e2be
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 88 deletions.
27 changes: 22 additions & 5 deletions static_src/actions/user_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import OrgStore from '../stores/org_store';
import SpaceStore from '../stores/space_store';

const ORG_NAME = OrgStore.cfName;
const MSG_USER_HAS_SPACE_ROLES = 'This user can\'t be removed because they still have a space ' +
'role within the organization. Please remove all space ' +
'associations before removing this user from the organization. ' +
'To review how, click the "Managing Teammates" link below.';

const userActions = {
fetchOrgUsers(orgGuid) {
Expand Down Expand Up @@ -75,11 +79,7 @@ const userActions = {
deleteUserOrDisplayNotice(spaceUsers, userGuid, orgGuid) {
const usersSpaces = spaceUsers.filter(spaceUser => spaceUser.guid === userGuid);
if (usersSpaces.length > 0) {
const description = 'This user can\'t be removed because they still have a space ' +
'role within the organization. Please remove all space ' +
'associations before removing this user from the organization. ' +
'To review how, click the "Managing Teammates" link below.';
userActions.createUserSpaceAssociationNotification(description);
userActions.createUserSpaceAssociationNotification(MSG_USER_HAS_SPACE_ROLES);
} else {
userActions.deleteUser(userGuid, orgGuid);
}
Expand All @@ -96,6 +96,23 @@ const userActions = {
userGuid,
orgGuid
});

cfApi.deleteUser(userGuid, orgGuid)
.then(() => userActions.deletedUser(userGuid, orgGuid))
// .catch((error) => {
// // Check whether we got caught on user roles in spaces
// const userHasSpaceRoles = (error &&
// error.response &&
// error.response.status === 400 &&
// error.response.data.error_code === 'CF-AssociationNotEmpty'
// );
// if (userHasSpaceRoles) {
// this.createUserSpaceAssociationNotification(MSG_USER_HAS_SPACE_ROLES);
// } else if (error.response.data) {
// // else use generic error
// this.errorRemoveUser(userGuid, error.response.data);
// }
// });
},

deletedUser(userGuid, orgGuid) {
Expand Down
2 changes: 0 additions & 2 deletions static_src/stores/user_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ export class UserStore extends BaseStore {
}

case userActionTypes.USER_DELETE: {
cfApi.deleteUser(action.userGuid, action.orgGuid);

break;
}

Expand Down
41 changes: 41 additions & 0 deletions static_src/test/unit/actions/user_actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,51 @@ describe('userActions', function() {
};

let spy = setupViewSpy(sandbox);
sandbox.stub(userActions, 'deletedUser');
sandbox.stub(cfApi, 'deleteUser').returns(Promise.resolve({}));

userActions.deleteUser(expectedUserGuid, expectedOrgGuid);

assertAction(spy, userActionTypes.USER_DELETE, expectedParams);
expect(cfApi.deleteUser.called).toEqual(true);
expect(userActions.deletedUser).toHaveBeenCalledOnce();
});

describe('error handling', function() {
it('should display a generic error if unsuccessful', function() {
var spy = sandbox.spy(userActions, 'errorRemoveUser'),
expectedUserGuid = 'aldfskjmcx',
expectedOrgGuid = 'sa09dvjakdnva';
sandbox.stub(userActions, 'deletedUser');
sandbox.stub(cfApi, 'deleteUser')
.returns(Promise.reject({ error: {response: {data: {}}}}));

userActions.deleteUser(expectedUserGuid, expectedOrgGuid);
expect(userActions.deletedUser).not.toHaveBeenCalledOnce();
//expect(userActions.deletedUser.called).toEqual(false);
expect(spy).toHaveBeenCalledOnce();
let args = spy.getCall(0).args;
expect(args[0]).toEqual(expectedUserGuid);
expect(args[1]).toEqual({});
});

it('should display a notification about user having existing roles', function() {
var spy = sandbox.spy(userActions, 'createUserSpaceAssociationNotification'),
expectedUserGuid = 'aldfskjmcx',
expectedOrgGuid = 'sa09dvjakdnva';
sandbox.spy(userActions, 'deletedUser');

const description = 'This user can\'t be removed because they still ' +
'have a space role within the organization. Please remove all ' +
'space associations before removing this user from the organization. ' +
'To review how, click the "Managing Teammates" link below.';

userActions.deleteUser(expectedUserGuid, expectedOrgGuid);
expect(userActions.deletedUser.called).toEqual(false);
expect(spy).toHaveBeenCalledOnce();
let args = spy.getCall(0).args;
expect(args[0]).toEqual(description);
});
});
});

Expand Down
60 changes: 0 additions & 60 deletions static_src/test/unit/util/cf_api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,66 +989,6 @@ describe('cfApi', function() {
done();
}).catch(done.fail);
});

it('should call org deleted action with guid', function(done) {
var stub = sandbox.stub(http, 'delete'),
spy = sandbox.spy(userActions, 'deletedUser'),
expectedUserGuid = 'aldfskjmcx',
expectedOrgGuid = 'sa09dvjakdnva';

let testPromise = createPromise({status: true});
stub.returns(testPromise);

cfApi.deleteUser(expectedUserGuid, expectedOrgGuid).then(() => {
expect(spy).toHaveBeenCalledOnce();
let args = spy.getCall(0).args;
expect(args[0]).toEqual(expectedUserGuid);
expect(args[1]).toEqual(expectedOrgGuid);
done();
}).catch(done.fail);
});

describe('error handling', function() {
it('should display a generic error if unsuccessful', function(done) {
var spy = sandbox.spy(userActions, 'errorRemoveUser'),
expectedUserGuid = 'aldfskjmcx',
expectedOrgGuid = 'sa09dvjakdnva';
moxios.stubOnce('DELETE', `/v2/organizations/${expectedOrgGuid}/users/${expectedUserGuid}`, {
status: 500,
response: {},
});

cfApi.deleteUser(expectedUserGuid, expectedOrgGuid).then(() => {
expect(spy).toHaveBeenCalledOnce();
let args = spy.getCall(0).args;
expect(args[0]).toEqual(expectedUserGuid);
expect(args[1]).toEqual({});
done();
}).catch(done.fail);
});

it('should display a notification about user having existing roles', function(done) {
var spy = sandbox.spy(userActions, 'createUserSpaceAssociationNotification'),
expectedUserGuid = 'aldfskjmcx',
expectedOrgGuid = 'sa09dvjakdnva';
moxios.stubOnce('DELETE', `/v2/organizations/${expectedOrgGuid}/users/${expectedUserGuid}`, {
status: 400,
response: {error_code: 'CF-AssociationNotEmpty'},
});

const description = 'This user can\'t be removed because they still ' +
'have a space role within the organization. Please remove all ' +
'space associations before removing this user from the organization. ' +
'To review how, click the "Managing Teammates" link below.';

cfApi.deleteUser(expectedUserGuid, expectedOrgGuid).then(() => {
expect(spy).toHaveBeenCalledOnce();
let args = spy.getCall(0).args;
expect(args[0]).toEqual(description);
done();
}).catch(done.fail);
});
})
});

describe('deleteOrgUserCategory()', function() {
Expand Down
27 changes: 6 additions & 21 deletions static_src/util/cf_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,29 +388,14 @@ export default {

deleteUser(userGuid, orgGuid) {
return http.delete(`${APIV}/organizations/${orgGuid}/users/${userGuid}`)
.then(() => {
userActions.deletedUser(userGuid, orgGuid);
}).catch((err) => {
// Check whether we got caught on user roles in spaces
.catch((err) => {
const error = parseError(err);
const userHasSpaceRoles = (error &&
error.response &&
error.response.status === 400 &&
error.response.data.error_code === 'CF-AssociationNotEmpty'
);
if (userHasSpaceRoles) {
const description = 'This user can\'t be removed because they still have a space ' +
'role within the organization. Please remove all space ' +
'associations before removing this user from the organization. ' +
'To review how, click the "Managing Teammates" link below.';
userActions.createUserSpaceAssociationNotification(description);
} else if (error.response.data) {
// else use generic error
userActions.errorRemoveUser(userGuid, error.response.data);
} else {
// fall back to logging.
handleError(err);
// if we can handle it, throw it.
if (error && error.response && error.response.data) {
return Promise.reject(error);
}
// else handle the unknown api call.
return handleError(err);
});
},

Expand Down

0 comments on commit da5e2be

Please sign in to comment.