[ADF-4275] - Fix preselect user validation for User Id (#4474)

* [ADF-4274] - Fix preselect user validation for User Id

* [ADF-4275] - change search user response format

* [ADF-4275] - add unit test to search user by id on single mode

* [ADF-4275] - fix PeopleCloud unit tests and refractor validate User funcitonality

* [ADF-4275] - fix unit tests

* [ADF-4272] - reset spec file

* [ADF-4275] - rebase and fix unit tests

* Update people-cloud.component.ts
This commit is contained in:
Silviu Popa
2019-04-08 16:25:16 +03:00
committed by Eugenio Romano
parent 78daf68777
commit 2c9fdf0d4d
2 changed files with 142 additions and 78 deletions

View File

@@ -16,7 +16,7 @@
*/ */
import { PeopleCloudComponent } from './people-cloud.component'; import { PeopleCloudComponent } from './people-cloud.component';
import { ComponentFixture, TestBed, async, tick, fakeAsync } from '@angular/core/testing'; import { ComponentFixture, TestBed, async } from '@angular/core/testing';
import { IdentityUserService, AlfrescoApiService, AlfrescoApiServiceMock, CoreModule, IdentityUserModel } from '@alfresco/adf-core'; import { IdentityUserService, AlfrescoApiService, AlfrescoApiServiceMock, CoreModule, IdentityUserModel } from '@alfresco/adf-core';
import { ProcessServiceCloudTestingModule } from '../../../../testing/process-service-cloud.testing.module'; import { ProcessServiceCloudTestingModule } from '../../../../testing/process-service-cloud.testing.module';
import { of } from 'rxjs'; import { of } from 'rxjs';
@@ -363,8 +363,6 @@ describe('PeopleCloudComponent', () => {
describe('Single Mode and Pre-selected users with no validate flag', () => { describe('Single Mode and Pre-selected users with no validate flag', () => {
const change = new SimpleChange(null, mockPreselectedUsers, false);
beforeEach(async(() => { beforeEach(async(() => {
component.mode = 'single'; component.mode = 'single';
component.preSelectUsers = <any> mockPreselectedUsers; component.preSelectUsers = <any> mockPreselectedUsers;
@@ -385,14 +383,6 @@ describe('PeopleCloudComponent', () => {
}); });
})); }));
it('should pre-select preSelectUsers[0] when mode=single', async(() => {
component.ngOnChanges({ 'preSelectUsers': change });
fixture.detectChanges();
fixture.whenStable().then(() => {
const selectedUser = component.searchUserCtrl.value;
expect(selectedUser.id).toBe(mockUsers[1].id);
});
}));
it('should not pre-select any user when preSelectUsers is empty and mode=single', async(() => { it('should not pre-select any user when preSelectUsers is empty and mode=single', async(() => {
component.preSelectUsers = []; component.preSelectUsers = [];
fixture.detectChanges(); fixture.detectChanges();
@@ -428,12 +418,13 @@ describe('PeopleCloudComponent', () => {
}); });
})); }));
it('should pre-select preSelectUsers[0] when mode=single', fakeAsync(() => { it('should pre-select preSelectUsers[0] when mode=single', async(() => {
component.mode = 'single';
component.validate = false;
fixture.detectChanges(); fixture.detectChanges();
spyOn(component, 'searchUser').and.returnValue(Promise.resolve(mockPreselectedUsers)); spyOn(component, 'searchUser').and.returnValue(Promise.resolve(mockPreselectedUsers));
component.ngOnChanges({ 'preSelectUsers': change }); component.ngOnChanges({ 'preSelectUsers': change });
fixture.detectChanges(); fixture.detectChanges();
tick();
const selectedUser = component.searchUserCtrl.value; const selectedUser = component.searchUserCtrl.value;
expect(selectedUser.id).toBe(mockUsers[1].id); expect(selectedUser.id).toBe(mockUsers[1].id);
})); }));
@@ -528,63 +519,122 @@ describe('PeopleCloudComponent', () => {
expect(removeUserSpy).toHaveBeenCalled(); expect(removeUserSpy).toHaveBeenCalled();
}); });
})); }));
});
it('should emit warning if are invalid users', (done) => { it('should emit warning if are invalid users', (done) => {
spyOn(identityService, 'findUserByUsername').and.returnValue(Promise.resolve([])); spyOn(identityService, 'findUserByUsername').and.returnValue(Promise.resolve([]));
const warnMessage = { message: 'INVALID_PRESELECTED_USERS', users: [{ username: 'invalidUsername' }] }; const warnMessage = { message: 'INVALID_PRESELECTED_USERS', users: [{ username: 'invalidUsername' }] };
component.validate = true; component.validate = true;
component.preSelectUsers = <any> [{ username: 'invalidUsername' }]; component.preSelectUsers = <any> [{ username: 'invalidUsername' }];
fixture.detectChanges(); fixture.detectChanges();
component.loadSinglePreselectUser(); component.loadSinglePreselectUser();
component.warning.subscribe((response) => { component.warning.subscribe((response) => {
expect(response).toEqual(warnMessage); expect(response).toEqual(warnMessage);
expect(response.message).toEqual(warnMessage.message); expect(response.message).toEqual(warnMessage.message);
expect(response.users).toEqual(warnMessage.users); expect(response.users).toEqual(warnMessage.users);
expect(response.users[0].username).toEqual('invalidUsername'); expect(response.users[0].username).toEqual('invalidUsername');
done(); done();
});
}); });
});
it('should filter user by id if validate true', async(() => { it('should filter user by id if validate true', async(() => {
const findByIdSpy = spyOn(identityService, 'findUserById').and.returnValue(Promise.resolve(mockUsers)); const findByIdSpy = spyOn(identityService, 'findUserById').and.returnValue(of(mockUsers[0]));
component.mode = 'multiple'; component.mode = 'multiple';
component.validate = true; component.validate = true;
component.preSelectUsers = <any> [{ id: mockUsers[1].id }, { id: mockUsers[2].id }]; component.preSelectUsers = <any> [{ id: mockUsers[0].id }, { id: mockUsers[1].id }];
fixture.detectChanges(); component.ngOnChanges({ 'preSelectUsers': change });
fixture.whenStable().then(() => { fixture.detectChanges();
component.filterPreselectUsers().then((result) => { component.filterPreselectUsers().then((result) => {
fixture.detectChanges();
expect(findByIdSpy).toHaveBeenCalled(); expect(findByIdSpy).toHaveBeenCalled();
expect(component.userExists(result)).toEqual(true); expect(component.userExists(result[0])).toEqual(true);
expect(result[1].id).toBe(mockUsers[0].id);
}); });
}); }));
}));
it('should filter user by username if validate true', async(() => { it('should filter user by username if validate true', async(() => {
const findUserByUsernameSpy = spyOn(identityService, 'findUserByUsername').and.returnValue(Promise.resolve(mockUsers)); const findUserByUsernameSpy = spyOn(identityService, 'findUserByUsername').and.returnValue(of(mockUsers));
component.mode = 'multiple'; component.mode = 'multiple';
component.validate = true; component.validate = true;
component.preSelectUsers = <any> [{ username: mockUsers[1].username }, { username: mockUsers[2].username }]; component.preSelectUsers = <any> [{ username: mockUsers[1].username }, { username: mockUsers[2].username }];
fixture.detectChanges(); fixture.detectChanges();
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
component.filterPreselectUsers().then((result) => { component.filterPreselectUsers().then((result) => {
expect(findUserByUsernameSpy).toHaveBeenCalled(); expect(findUserByUsernameSpy).toHaveBeenCalled();
expect(component.userExists(result)).toEqual(true); expect(component.userExists(result[0])).toEqual(true);
expect(component.userExists(result[1])).toEqual(true);
});
}); });
}); }));
}));
it('should filter user by email if validate true', async(() => { it('should filter user by email if validate true', async(() => {
const findUserByEmailSpy = spyOn(identityService, 'findUserByEmail').and.returnValue(Promise.resolve(mockUsers)); const findUserByEmailSpy = spyOn(identityService, 'findUserByEmail').and.returnValue(of(mockUsers));
component.mode = 'multiple'; component.mode = 'multiple';
component.validate = true; component.validate = true;
component.preSelectUsers = <any> [{ email: mockUsers[1].email }, { email: mockUsers[2].email }]; component.preSelectUsers = <any> [{ email: mockUsers[1].email }, { email: mockUsers[2].email }];
fixture.detectChanges(); fixture.detectChanges();
fixture.whenStable().then(() => { fixture.whenStable().then(() => {
component.filterPreselectUsers().then((result) => { component.filterPreselectUsers().then((result) => {
expect(findUserByEmailSpy).toHaveBeenCalled(); expect(findUserByEmailSpy).toHaveBeenCalled();
expect(component.userExists(result)).toEqual(true); expect(component.userExists(result[0])).toEqual(true);
expect(component.userExists(result[1])).toEqual(true);
});
}); });
}); }));
}));
it('should search user by id on single selection mode', async(() => {
const findUserByIdSpy = spyOn(identityService, 'findUserById').and.returnValue(of(mockUsers[0]));
component.mode = 'single';
component.validate = true;
component.preSelectUsers = <any> [{ id: mockUsers[0].id }];
fixture.detectChanges();
fixture.whenStable().then(() => {
component.validatePreselectUsers().then((result) => {
expect(findUserByIdSpy).toHaveBeenCalled();
expect(result.length).toEqual(1);
});
});
}));
it('should not preselect any user if email is invalid and validation enable', async(() => {
const findUserByEmailSpy = spyOn(identityService, 'findUserByEmail').and.returnValue(of([]));
component.mode = 'single';
component.validate = true;
component.preSelectUsers = <any> [{ email: 'invalid email' }];
fixture.detectChanges();
fixture.whenStable().then(() => {
component.validatePreselectUsers().then((result) => {
expect(findUserByEmailSpy).toHaveBeenCalled();
expect(result.length).toEqual(0);
});
});
}));
it('should not preselect any user if id is invalid and validation enable', async(() => {
const findUserByIdSpy = spyOn(identityService, 'findUserById').and.returnValue(of([]));
component.mode = 'single';
component.validate = true;
component.preSelectUsers = <any> [{ id: 'invalid id' }];
fixture.detectChanges();
fixture.whenStable().then(() => {
component.validatePreselectUsers().then((result) => {
expect(findUserByIdSpy).toHaveBeenCalled();
expect(result.length).toEqual(0);
});
});
}));
it('should not preselect any user if username is invalid and validation enable', async(() => {
const findUserByUsernameSpy = spyOn(identityService, 'findUserByUsername').and.returnValue(of([]));
component.mode = 'single';
component.validate = true;
component.preSelectUsers = <any> [{ username: 'invalid user' }];
fixture.detectChanges();
fixture.whenStable().then(() => {
component.validatePreselectUsers().then((result) => {
expect(findUserByUsernameSpy).toHaveBeenCalled();
expect(result.length).toEqual(0);
});
});
}));
});
}); });

View File

@@ -100,7 +100,7 @@ export class PeopleCloudComponent implements OnInit, OnChanges {
isFocused: boolean; isFocused: boolean;
invalidUsers: IdentityUserModel[]; invalidUsers: IdentityUserModel[] = [];
constructor(private identityUserService: IdentityUserService, private logService: LogService) { constructor(private identityUserService: IdentityUserService, private logService: LogService) {
} }
@@ -161,24 +161,26 @@ export class PeopleCloudComponent implements OnInit, OnChanges {
} }
async validatePreselectUsers(): Promise<any> { async validatePreselectUsers(): Promise<any> {
this.invalidUsers = []; let filteredPreselectUsers: IdentityUserModel[];
let filteredPreSelectUsers: { isValid: boolean, user: IdentityUserModel } []; let validUsers: IdentityUserModel[] = [];
try { try {
filteredPreSelectUsers = await this.filterPreselectUsers(); filteredPreselectUsers = await this.filterPreselectUsers();
} catch (error) { } catch (error) {
filteredPreSelectUsers = []; validUsers = [];
this.logService.error(error); this.logService.error(error);
} }
return filteredPreSelectUsers.reduce((validUsers, validatedUser: any) => { await this.preSelectUsers.map((user: IdentityUserModel) => {
if (validatedUser.isValid) { const validUser = this.isValidUser(filteredPreselectUsers, user);
validUsers.push(validatedUser.user);
if (validUser) {
validUsers.push(validUser);
} else { } else {
this.invalidUsers.push(validatedUser.user); this.invalidUsers.push(user);
} }
return validUsers; });
}, []); return validUsers;
} }
async filterPreselectUsers() { async filterPreselectUsers() {
@@ -191,7 +193,7 @@ export class PeopleCloudComponent implements OnInit, OnChanges {
this.logService.error(error); this.logService.error(error);
} }
const isUserValid: boolean = this.userExists(result); const isUserValid: boolean = this.userExists(result);
return isUserValid ? { isValid: isUserValid, user: new IdentityUserModel(user) } : { isValid: isUserValid, user: user }; return isUserValid ? new IdentityUserModel(result) : null;
}); });
return await Promise.all(promiseBatch); return await Promise.all(promiseBatch);
} }
@@ -199,15 +201,27 @@ export class PeopleCloudComponent implements OnInit, OnChanges {
async searchUser(user: IdentityUserModel) { async searchUser(user: IdentityUserModel) {
const key: string = Object.keys(user)[0]; const key: string = Object.keys(user)[0];
switch (key) { switch (key) {
case 'id': return this.identityUserService.findUserById(user[key]).toPromise(); case 'id': return await this.identityUserService.findUserById(user[key]).toPromise();
case 'username': return this.identityUserService.findUserByUsername(user[key]).toPromise(); case 'username': return (await this.identityUserService.findUserByUsername(user[key]).toPromise())[0];
case 'email': return this.identityUserService.findUserByEmail(user[key]).toPromise(); case 'email': return (await this.identityUserService.findUserByEmail(user[key]).toPromise())[0];
default: return of([]); default: return of([]);
} }
} }
private isValidUser(filteredUsers: IdentityUserModel[], user: IdentityUserModel) {
return filteredUsers.find((filteredUser: IdentityUserModel) => {
return filteredUser &&
(filteredUser.id === user.id ||
filteredUser.username === user.username ||
filteredUser.email === user.email);
});
}
public userExists(result: any): boolean { public userExists(result: any): boolean {
return result && result.length > 0; return result
&& (result.id !== undefined
|| result.username !== undefined
|| result.amil !== undefined);
} }
private initSearch() { private initSearch() {