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

Commit

Permalink
Merge pull request #1194 from 18F/js-fix-invite-space-user-list
Browse files Browse the repository at this point in the history
fix space user from automatically being populated upon invite
  • Loading branch information
el-mapache authored Aug 14, 2017
2 parents 0385365 + 772578b commit 36c92b2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 23 deletions.
14 changes: 14 additions & 0 deletions static_src/actions/user_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ const userActions = {
} else {
entityGuid = SpaceStore.currentSpaceGuid;
cfApiRequest = cfApi.putAssociateUserToSpace.bind(cfApi, userGuid, orgGuid, entityGuid);

AppDispatcher.handleViewAction({
type: userActionTypes.USER_SPACE_ASSOCIATE,
userGuid,
entityType,
entityGuid
});
}

return cfApiRequest()
Expand Down Expand Up @@ -376,6 +383,13 @@ const userActions = {
entityGuid,
user
});
} else {
AppDispatcher.handleViewAction({
type: userActionTypes.USER_SPACE_ASSOCIATED,
userGuid,
entityGuid,
user
});
}

return Promise.resolve(user);
Expand Down
8 changes: 6 additions & 2 deletions static_src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,14 @@ const userActionTypes = keymirror({
USER_INVITE_STATUS_UPDATED: null,
// Action to trigger when user list notice is created.
USER_LIST_NOTICE_CREATED: null,
// Action to trigger email sent to user with cloud.gov invite url.
// Action to associate user to an organization.
USER_ORG_ASSOCIATE: null,
// Action to associate user to organization on the server.
// Action when user is associated to an organization.
USER_ORG_ASSOCIATED: null,
// Action to associate user to a space.
USER_SPACE_ASSOCIATE: null,
// Action when user is associated to a space.
USER_SPACE_ASSOCIATED: null,
// Display the user associated to org.
USER_ASSOCIATED_ORG_DISPLAYED: null,
// Action when something goes wrong in user invite and email process.
Expand Down
51 changes: 34 additions & 17 deletions static_src/stores/user_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ export class UserStore extends BaseStore {

case userActionTypes.ORG_USER_ROLES_RECEIVED: {
this._loading.entityRoles = false;
const updatedUsers = this.mergeRoles(action.orgUserRoles, action.orgGuid,
this.associateUsersAndRolesToEntity(action.orgUserRoles, action.orgGuid,
'organization_roles');
this.mergeMany('guid', updatedUsers, () => { });
this.emitChange();
break;
}
Expand All @@ -63,12 +62,7 @@ export class UserStore extends BaseStore {
this._loading.entityRoles = false;

const { users, spaceGuid } = action;

// Force an update to the cached list of users, otherwise mergeRoles
// won't be able to find the new user
const updatedUsers = this.mergeRoles(users, spaceGuid,
'space_roles');
this.mergeMany('guid', updatedUsers, () => { });
this.associateUsersAndRolesToEntity(users, spaceGuid, 'space_roles');
this.emitChange();
break;
}
Expand All @@ -80,21 +74,39 @@ export class UserStore extends BaseStore {
break;
}

case userActionTypes.USER_ORG_ASSOCIATED: {
case userActionTypes.USER_ORG_ASSOCIATE: {
this._inviteInputActive = false;
this.emitChange();
break;
}

case userActionTypes.USER_SPACE_ASSOCIATE: {
this._inviteInputActive = false;
this.emitChange();
break;
}

case userActionTypes.USER_ORG_ASSOCIATED : {
this._inviteInputActive = true;
const user = Object.assign({}, {
guid: action.userGuid,
roles: { [action.entityGuid]: [] }
}, action.user);
this._inviteInputActive = true;

if (!this.get(user.guid)) {
this.push(user);
} else {
this.merge('guid', user, () => {});
}

this.associateUsersAndRolesToEntity([user], action.entityGuid,
'organization_roles');
this.emitChange();
break;
}

case userActionTypes.USER_SPACE_ASSOCIATED: {
this._inviteInputActive = true;
const user = Object.assign({}, action.user, {
guid: action.userGuid,
space_roles: { [action.entityGuid]: action.user.space_roles }
});
this.associateUsersAndRolesToEntity([user], action.entityGuid,
'space_roles');
this.emitChange();
break;
}

Expand Down Expand Up @@ -295,6 +307,11 @@ export class UserStore extends BaseStore {
}
}

associateUsersAndRolesToEntity(users, entityGuid, roleType) {
const updatedUsers = this.mergeRoles(users, entityGuid, roleType);
this.mergeMany('guid', updatedUsers, () => { });
}

addUserRole(user, entityType, entityGuid, addedRole, cb) {
const updatedUser = user;
if (updatedUser) {
Expand Down
6 changes: 3 additions & 3 deletions static_src/test/unit/actions/user_actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,10 @@ describe('userActions', function() {
});

describe('when entityType is not org_users', () => {
it('should not dispatch USER_ORG_ASSOCIATED notice with user and org', (done) => {
userActions.createdUserAndAssociated(userGuid, orgGuid, orgUsers)
it('should dispatch USER_SPACE_ASSOCIATED notice with user and space', (done) => {
userActions.createdUserAndAssociated(userGuid, 'fake-space-guid', orgUsers)
.then(done, done.fail);
expect(spy.called).toBe(false);
assertAction(spy, userActionTypes.USER_SPACE_ASSOCIATED);
});
});
});
Expand Down
63 changes: 62 additions & 1 deletion static_src/test/unit/stores/user_store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe('UserStore', function () {
});

it('should emit a change', function() {
expect(UserStore.emitChange).toHaveBeenCalledTwice();
expect(UserStore.emitChange).toHaveBeenCalledOnce();
});

it('should add the user to the org through an empty role list', function() {
Expand Down Expand Up @@ -326,6 +326,67 @@ describe('UserStore', function () {
});
});

describe('on space user associated', () => {
const userGuid = 'user-543';
const spaceGuid = 'space-abc';
let spaceUsers;

beforeEach(() => {
const user = {
guid: userGuid,
username: '[email protected]'
};
spaceUsers = [user, {userGuid: 'wrong-udid'}, {userGuid: 'wrong-udid-2'}];
sandbox.spy(UserStore, 'emitChange');
userActions.createdUserAndAssociated(userGuid, spaceGuid, spaceUsers, 'space_users');
});

it('should emit a change', function() {
expect(UserStore.emitChange).toHaveBeenCalledOnce();
});

it('should add the user to the space through an empty role list', function() {
const actualUser = UserStore.get(userGuid);
expect(actualUser).toBeDefined();
expect(actualUser.space_roles).toBeDefined();
expect(actualUser.space_roles[spaceGuid]).toBeDefined();
});
});

describe('on space user association received', function() {
it('should emit a change event if data changed', function() {
var spy = sandbox.spy(UserStore, 'emitChange');
const userGuid = "fake-user-guid";
const entityGuid = "fake-space-guid";
const entityUsers = [
{userGuid: userGuid},
{userGuid: "fake-user-guid-2"},
{userGuid: "fake-user-guid-3"}
];

userActions.createdUserAndAssociated(userGuid, entityGuid, entityUsers);

expect(spy).toHaveBeenCalledOnce();
});

it('should merge and update space with new users', function() {
const existingUser = { guid: 'wpqoifesadkzcvn', name: 'Michael' };
const newUser = { guid: 'dkzcvwpqoifesan' };

UserStore.push(existingUser);
expect(UserStore.get('wpqoifesadkzcvn')).toEqual(existingUser);

AppDispatcher.handleViewAction({
type: userActionTypes.USER_SPACE_ASSOCIATED,
user: { guid: 'blah' },
spaceGuid: 'adsfa'
});
let actual = UserStore.get('wpqoifesadkzcvn');
let expected = Object.assign({}, existingUser, newUser);
expect(actual).not.toEqual(expected);
});
});

describe('on user role toggle error', () => {
it('updates the error and saving properties of the user store', () => {
const message = 'oh no!';
Expand Down

0 comments on commit 36c92b2

Please sign in to comment.