[ADF-5465] Not being redirected to login page when Kerberos is enabled (#7272)

* Revert "[MNT-22334] ADW - User information not displayed when Kerberos is in use (#7172)"

This reverts commit 4befb779

* [ADF-5465] Not being redirected to login page when Kerberos is enabled

* * fix user info

* * add providers

* * fix test
This commit is contained in:
Dharan 2021-10-05 13:54:38 +05:30 committed by GitHub
parent 6f968f7e4a
commit 8a3c49ebf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 147 additions and 170 deletions

View File

@ -73,6 +73,16 @@ if [[ -n "${APP_CONFIG_ECM_HOST}" ]]; then
-i "${NGINX_ENVSUBST_OUTPUT_DIR}/app.config.json" -i "${NGINX_ENVSUBST_OUTPUT_DIR}/app.config.json"
fi fi
if [[ -n "${APP_CONFIG_KERBEROS_ENABLED}" ]]; then
sed -e "s/\"withCredentials\": [^,]*/\"withCredentials\": ${APP_CONFIG_KERBEROS_ENABLED}/g" \
-i "${NGINX_ENVSUBST_OUTPUT_DIR}/app.config.json"
fi
if [[ -n "${APP_CONFIG_PROVIDERS}" ]]; then
sed -e "s/\"providers\": [^,]*/\"providers\": \"${APP_CONFIG_PROVIDERS}\"/g" \
-i "${NGINX_ENVSUBST_OUTPUT_DIR}/app.config.json"
fi
if [ -n "${APP_CONFIG_APPS_DEPLOYED}" ]; then if [ -n "${APP_CONFIG_APPS_DEPLOYED}" ]; then
sed -e "s/\"alfresco-deployed-apps\": \[.*\]/\"alfresco-deployed-apps\": ${APP_CONFIG_APPS_DEPLOYED}/g" \ sed -e "s/\"alfresco-deployed-apps\": \[.*\]/\"alfresco-deployed-apps\": ${APP_CONFIG_APPS_DEPLOYED}/g" \
-i "${NGINX_ENVSUBST_OUTPUT_DIR}/app.config.json" -i "${NGINX_ENVSUBST_OUTPUT_DIR}/app.config.json"

View File

@ -20,4 +20,6 @@ docker run --rm -it \
--env APP_CONFIG_OAUTH2_REDIRECT_LOGOUT=$APP_CONFIG_OAUTH2_REDIRECT_LOGOUT \ --env APP_CONFIG_OAUTH2_REDIRECT_LOGOUT=$APP_CONFIG_OAUTH2_REDIRECT_LOGOUT \
--env APP_CONFIG_BPM_HOST=$APP_CONFIG_BPM_HOST \ --env APP_CONFIG_BPM_HOST=$APP_CONFIG_BPM_HOST \
--env APP_CONFIG_ECM_HOST=$APP_CONFIG_ECM_HOST \ --env APP_CONFIG_ECM_HOST=$APP_CONFIG_ECM_HOST \
--env APP_CONFIG_PROVIDERS=$APP_CONFIG_PROVIDERS \
--env APP_CONFIG_KERBEROS_ENABLED=$APP_CONFIG_KERBEROS_ENABLED \
--user 1000:1000 --publish $HOST_PORT:$CONTAINER_PORT $DOCKER_IMAGE_REPO --user 1000:1000 --publish $HOST_PORT:$CONTAINER_PORT $DOCKER_IMAGE_REPO

View File

@ -60,11 +60,29 @@ describe('AuthenticationService', () => {
jasmine.Ajax.uninstall(); jasmine.Ajax.uninstall();
}); });
describe('kerberos', () => {
beforeEach(() => {
appConfigService.config.providers = 'ALL';
appConfigService.config.auth = { withCredentials: true };
});
it('should emit login event for kerberos', (done) => {
spyOn(authService.peopleApi, 'getPerson').and.returnValue(Promise.resolve({}));
spyOn(authService.profileApi, 'getProfile').and.returnValue(Promise.resolve({}));
const disposableLogin = authService.onLogin.subscribe(() => {
expect(authService.profileApi.getProfile).toHaveBeenCalledTimes(1);
expect(authService.peopleApi.getPerson).toHaveBeenCalledTimes(1);
disposableLogin.unsubscribe();
done();
});
appConfigService.load();
});
});
describe('when the setting is ECM', () => { describe('when the setting is ECM', () => {
beforeEach(() => { beforeEach(() => {
appConfigService.config.providers = 'ECM'; appConfigService.config.providers = 'ECM';
appConfigService.config.auth = { withCredentials: false };
appConfigService.load(); appConfigService.load();
apiService.reset(); apiService.reset();
}); });
@ -186,20 +204,12 @@ describe('AuthenticationService', () => {
it('[ECM] should return isBpmLoggedIn false', () => { it('[ECM] should return isBpmLoggedIn false', () => {
expect(authService.isBpmLoggedIn()).toBe(false); expect(authService.isBpmLoggedIn()).toBe(false);
}); });
it('[ECM] should return true if kerberos configured', () => {
appConfigService.config.auth.withCredentials = true;
expect(authService.isLoggedInWith('ECM')).toBe(true);
expect(authService.isLoggedIn()).toBe(true);
});
}); });
describe('when the setting is BPM', () => { describe('when the setting is BPM', () => {
beforeEach(() => { beforeEach(() => {
appConfigService.config.providers = 'BPM'; appConfigService.config.providers = 'BPM';
appConfigService.config.auth = { withCredentials: false };
appConfigService.load(); appConfigService.load();
apiService.reset(); apiService.reset();
}); });
@ -311,14 +321,12 @@ describe('AuthenticationService', () => {
it('[BPM] should return isALLProvider false', () => { it('[BPM] should return isALLProvider false', () => {
expect(authService.isALLProvider()).toBe(false); expect(authService.isALLProvider()).toBe(false);
}); });
}); });
describe('remember me', () => { describe('remember me', () => {
beforeEach(() => { beforeEach(() => {
appConfigService.config.providers = 'ECM'; appConfigService.config.providers = 'ECM';
appConfigService.config.auth = { withCredentials: false };
appConfigService.load(); appConfigService.load();
apiService.reset(); apiService.reset();
}); });
@ -356,8 +364,7 @@ describe('AuthenticationService', () => {
it('[ECM] should not save the remember me cookie after failed login', (done) => { it('[ECM] should not save the remember me cookie after failed login', (done) => {
const disposableLogin = authService.login('fake-username', 'fake-password').subscribe( const disposableLogin = authService.login('fake-username', 'fake-password').subscribe(
() => { () => {},
},
() => { () => {
expect(cookie['ALFRESCO_REMEMBER_ME']).toBeUndefined(); expect(cookie['ALFRESCO_REMEMBER_ME']).toBeUndefined();
disposableLogin.unsubscribe(); disposableLogin.unsubscribe();
@ -384,7 +391,6 @@ describe('AuthenticationService', () => {
beforeEach(() => { beforeEach(() => {
appConfigService.config.providers = 'ALL'; appConfigService.config.providers = 'ALL';
appConfigService.config.auth = { withCredentials: false };
appConfigService.load(); appConfigService.load();
apiService.reset(); apiService.reset();
}); });
@ -414,8 +420,7 @@ describe('AuthenticationService', () => {
it('[ALL] should return login fail if only ECM call fail', (done) => { it('[ALL] should return login fail if only ECM call fail', (done) => {
const disposableLogin = authService.login('fake-username', 'fake-password').subscribe( const disposableLogin = authService.login('fake-username', 'fake-password').subscribe(
() => { () => {},
},
() => { () => {
expect(authService.isLoggedIn()).toBe(false, 'isLoggedIn'); expect(authService.isLoggedIn()).toBe(false, 'isLoggedIn');
expect(authService.getTicketEcm()).toBe(null, 'getTicketEcm'); expect(authService.getTicketEcm()).toBe(null, 'getTicketEcm');
@ -437,8 +442,7 @@ describe('AuthenticationService', () => {
it('[ALL] should return login fail if only BPM call fail', (done) => { it('[ALL] should return login fail if only BPM call fail', (done) => {
const disposableLogin = authService.login('fake-username', 'fake-password').subscribe( const disposableLogin = authService.login('fake-username', 'fake-password').subscribe(
() => { () => {},
},
() => { () => {
expect(authService.isLoggedIn()).toBe(false); expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null); expect(authService.getTicketEcm()).toBe(null);
@ -461,8 +465,7 @@ describe('AuthenticationService', () => {
it('[ALL] should return ticket undefined when the credentials are wrong', (done) => { it('[ALL] should return ticket undefined when the credentials are wrong', (done) => {
const disposableLogin = authService.login('fake-username', 'fake-password').subscribe( const disposableLogin = authService.login('fake-username', 'fake-password').subscribe(
() => { () => {},
},
() => { () => {
expect(authService.isLoggedIn()).toBe(false); expect(authService.isLoggedIn()).toBe(false);
expect(authService.getTicketEcm()).toBe(null); expect(authService.getTicketEcm()).toBe(null);

View File

@ -16,13 +16,13 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable, from, throwError, Observer, ReplaySubject } from 'rxjs'; import { Observable, from, throwError, Observer, ReplaySubject, forkJoin } from 'rxjs';
import { AlfrescoApiService } from './alfresco-api.service'; import { AlfrescoApiService } from './alfresco-api.service';
import { CookieService } from './cookie.service'; import { CookieService } from './cookie.service';
import { LogService } from './log.service'; import { LogService } from './log.service';
import { RedirectionModel } from '../models/redirection.model'; import { RedirectionModel } from '../models/redirection.model';
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service'; import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
import { UserProfileApi, UserRepresentation } from '@alfresco/js-api'; import { PeopleApi, UserProfileApi, UserRepresentation } from '@alfresco/js-api';
import { map, catchError, tap } from 'rxjs/operators'; import { map, catchError, tap } from 'rxjs/operators';
import { HttpHeaders } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http';
import { JwtHelperService } from './jwt-helper.service'; import { JwtHelperService } from './jwt-helper.service';
@ -39,7 +39,7 @@ export class AuthenticationService {
private bearerExcludedUrls: string[] = ['auth/realms', 'resources/', 'assets/']; private bearerExcludedUrls: string[] = ['auth/realms', 'resources/', 'assets/'];
/** /**
* Emits Basic auth login event * Emits login event
*/ */
onLogin: ReplaySubject<any> = new ReplaySubject<any>(1); onLogin: ReplaySubject<any> = new ReplaySubject<any>(1);
@ -48,6 +48,12 @@ export class AuthenticationService {
*/ */
onLogout: ReplaySubject<any> = new ReplaySubject<any>(1); onLogout: ReplaySubject<any> = new ReplaySubject<any>(1);
_peopleApi: PeopleApi;
get peopleApi(): PeopleApi {
this._peopleApi = this._peopleApi ?? new PeopleApi(this.alfrescoApi.getInstance());
return this._peopleApi;
}
_profileApi: UserProfileApi; _profileApi: UserProfileApi;
get profileApi(): UserProfileApi { get profileApi(): UserProfileApi {
this._profileApi = this._profileApi ?? new UserProfileApi(this.alfrescoApi.getInstance()); this._profileApi = this._profileApi ?? new UserProfileApi(this.alfrescoApi.getInstance());
@ -64,18 +70,31 @@ export class AuthenticationService {
this.alfrescoApi.getInstance().reply('logged-in', () => { this.alfrescoApi.getInstance().reply('logged-in', () => {
this.onLogin.next(); this.onLogin.next();
}); });
if (this.isKerberosEnabled()) {
this.loadUserDetails();
}
}); });
} }
private loadUserDetails() {
const ecmUser$ = from(this.peopleApi.getPerson('-me-'));
const bpmUser$ = this.getBpmLoggedUser();
if (this.isALLProvider()) {
forkJoin([ecmUser$, bpmUser$]).subscribe(() => this.onLogin.next());
} else if (this.isECMProvider()) {
ecmUser$.subscribe(() => this.onLogin.next());
} else {
bpmUser$.subscribe(() => this.onLogin.next());
}
}
/** /**
* Checks if the user logged in. * Checks if the user logged in.
* @returns True if logged in, false otherwise * @returns True if logged in, false otherwise
*/ */
isLoggedIn(): boolean { isLoggedIn(): boolean {
if (this.isKerberosConfigured()) {
return true;
}
if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) { if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) {
return false; return false;
} }
@ -92,6 +111,14 @@ export class AuthenticationService {
} }
} }
/**
* Does kerberos enabled?
* @returns True if enabled, false otherwise
*/
isKerberosEnabled(): boolean {
return this.appConfig.get<boolean>(AppConfigValues.AUTH_WITH_CREDENTIALS, false);
}
/** /**
* Does the provider support OAuth? * Does the provider support OAuth?
* @returns True if supported, false otherwise * @returns True if supported, false otherwise
@ -236,10 +263,6 @@ export class AuthenticationService {
* @returns True if logged in, false otherwise * @returns True if logged in, false otherwise
*/ */
isEcmLoggedIn(): boolean { isEcmLoggedIn(): boolean {
if (this.isKerberosConfigured()) {
return true;
}
if (this.isECMProvider() || this.isALLProvider()) { if (this.isECMProvider() || this.isALLProvider()) {
if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) { if (!this.isOauth() && this.cookie.isEnabled() && !this.isRememberMeSet()) {
return false; return false;
@ -263,10 +286,6 @@ export class AuthenticationService {
return false; return false;
} }
isKerberosConfigured(): boolean {
return this.appConfig.get<boolean>(AppConfigValues.AUTH_WITH_CREDENTIALS, false);
}
/** /**
* Gets the ECM username. * Gets the ECM username.
* @returns The ECM username * @returns The ECM username

View File

@ -27,7 +27,7 @@ import { IdentityUserService } from '../../services/identity-user.service';
import { BpmUserModel } from '../../models/bpm-user.model'; import { BpmUserModel } from '../../models/bpm-user.model';
import { EcmUserModel } from '../../models/ecm-user.model'; import { EcmUserModel } from '../../models/ecm-user.model';
import { UserInfoComponent } from './user-info.component'; import { UserInfoComponent } from './user-info.component';
import { of } from 'rxjs'; import { of, timer } from 'rxjs';
import { setupTestBed } from '../../testing/setup-test-bed'; import { setupTestBed } from '../../testing/setup-test-bed';
import { CoreTestingModule } from '../../testing/core.testing.module'; import { CoreTestingModule } from '../../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
@ -86,6 +86,12 @@ describe('User info component', () => {
fixture.detectChanges(); fixture.detectChanges();
} }
async function whenFixtureReady() {
fixture.detectChanges();
await timer(500).toPromise();
fixture.detectChanges();
}
setupTestBed({ setupTestBed({
imports: [ imports: [
TranslateModule.forRoot(), TranslateModule.forRoot(),
@ -160,11 +166,8 @@ describe('User info component', () => {
it('should show ecm only last name when user first name is null ', async () => { it('should show ecm only last name when user first name is null ', async () => {
getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmEditedUser)); getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmEditedUser));
component.getUserInfo(); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
openUserInfo(); openUserInfo();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
const ecmUsername = fixture.debugElement.query(By.css('#ecm-username')); const ecmUsername = fixture.debugElement.query(By.css('#ecm-username'));
@ -175,51 +178,39 @@ describe('User info component', () => {
}); });
it('should show the username when showName attribute is true', async () => { it('should show the username when showName attribute is true', async () => {
await fixture.whenStable().then(() => { await whenFixtureReady();
fixture.detectChanges(); expect(component.showName).toBeTruthy();
expect(component.showName).toBeTruthy(); expect(element.querySelector('#adf-userinfo-ecm-name-display')).not.toBeNull();
expect(element.querySelector('#adf-userinfo-ecm-name-display')).not.toBeNull();
});
}); });
it('should hide the username when showName attribute is false', async () => { it('should hide the username when showName attribute is false', async () => {
component.showName = false; component.showName = false;
await whenFixtureReady();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#adf-userinfo-ecm-name-display')).toBeNull(); expect(element.querySelector('#adf-userinfo-ecm-name-display')).toBeNull();
}); });
it('should have the defined class to show the name on the right side', async () => { it('should have the defined class to show the name on the right side', async () => {
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container').classList).toContain('adf-userinfo-name-right'); expect(element.querySelector('#userinfo_container').classList).toContain('adf-userinfo-name-right');
}); });
it('should not have the defined class to show the name on the left side', async () => { it('should not have the defined class to show the name on the left side', async () => {
component.namePosition = 'left'; component.namePosition = 'left';
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container').classList).not.toContain('adf-userinfo-name-right'); expect(element.querySelector('#userinfo_container').classList).not.toContain('adf-userinfo-name-right');
}); });
describe('and has image', () => { describe('and has image', () => {
beforeEach(() => { beforeEach(async () => {
isOauthStub.and.returnValue(false); isOauthStub.and.returnValue(false);
isEcmLoggedInStub.and.returnValue(true); isEcmLoggedInStub.and.returnValue(true);
isLoggedInStub.and.returnValue(true); isLoggedInStub.and.returnValue(true);
getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUser)); getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUser));
fixture.detectChanges(); await whenFixtureReady();
}); });
it('should get the ecm current user image from the service', async () => { it('should get the ecm current user image from the service', async () => {
await fixture.whenStable();
fixture.detectChanges();
openUserInfo(); openUserInfo();
const loggedImage = fixture.debugElement.query(By.css('#logged-user-img')); const loggedImage = fixture.debugElement.query(By.css('#logged-user-img'));
@ -229,26 +220,19 @@ describe('User info component', () => {
}); });
it('should display the current user image if user has avatarId', (done) => { it('should display the current user image if user has avatarId', (done) => {
fixture.detectChanges(); openUserInfo();
fixture.whenStable().then(() => { const loggedImage = fixture.debugElement.query(By.css('#logged-user-img'));
fixture.detectChanges(); component.ecmUser$.subscribe((response: EcmUserModel) => {
openUserInfo(); expect(response).toBeDefined();
const loggedImage = fixture.debugElement.query(By.css('#logged-user-img')); expect(response.avatarId).toBe('fake-avatar-id');
component.ecmUser$.subscribe((response: EcmUserModel) => { done();
expect(response).toBeDefined();
expect(response.avatarId).toBe('fake-avatar-id');
done();
});
expect(element.querySelector('#userinfo_container')).not.toBeNull();
expect(loggedImage).not.toBeNull();
expect(loggedImage.properties.src).toContain('alfresco-logo.svg');
}); });
expect(element.querySelector('#userinfo_container')).not.toBeNull();
expect(loggedImage).not.toBeNull();
expect(loggedImage.properties.src).toContain('alfresco-logo.svg');
}); });
it('should get the ecm user information from the service', async () => { it('should get the ecm user information from the service', async () => {
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
openUserInfo(); openUserInfo();
const ecmImage = fixture.debugElement.query(By.css('#ecm-user-detail-image')); const ecmImage = fixture.debugElement.query(By.css('#ecm-user-detail-image'));
const ecmFullName = fixture.debugElement.query(By.css('#ecm-full-name')); const ecmFullName = fixture.debugElement.query(By.css('#ecm-full-name'));
@ -271,8 +255,7 @@ describe('User info component', () => {
isLoggedInStub.and.returnValue(true); isLoggedInStub.and.returnValue(true);
isBpmLoggedInStub.and.returnValue(false); isBpmLoggedInStub.and.returnValue(false);
getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUserNoImage)); getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUserNoImage));
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
}); });
it('should show N/A when the job title is null', () => { it('should show N/A when the job title is null', () => {
@ -295,20 +278,19 @@ describe('User info component', () => {
}); });
it('should display the current user Initials if the user dose not have avatarId', (done) => { it('should display the current user Initials if the user dose not have avatarId', (done) => {
fixture.whenStable().then(() => { fixture.detectChanges();
fixture.detectChanges(); const pipe = new InitialUsernamePipe(new FakeSanitizer());
const pipe = new InitialUsernamePipe(new FakeSanitizer()); const expected = pipe.transform({
component.ecmUser$.subscribe((response: EcmUserModel) => { id: 13,
expect(response).toBeDefined(); firstName: 'Wilbur',
expect(response.avatarId).toBeNull(); lastName: 'Adams',
done(); email: 'wilbur@app.com'
}); });
expect(pipe.transform({ expect(expected).toBe('<div id="user-initials-image" class="">WA</div>');
id: 13, component.ecmUser$.subscribe((response: EcmUserModel) => {
firstName: 'Wilbur', expect(response).toBeDefined();
lastName: 'Adams', expect(response.avatarId).toBeNull();
email: 'wilbur@app.com' done();
})).toBe('<div id="user-initials-image" class="">WA</div>');
}); });
}); });
}); });
@ -341,11 +323,7 @@ describe('User info component', () => {
}); });
it('should show full name next the user image', async () => { it('should show full name next the user image', async () => {
getCurrentUserInfoStub.and.returnValue(of(fakeBpmUser)); await whenFixtureReady();
component.getUserInfo();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
openUserInfo(); openUserInfo();
const bpmUserName = fixture.debugElement.query(By.css('#bpm-username')); const bpmUserName = fixture.debugElement.query(By.css('#bpm-username'));
expect(element.querySelector('#userinfo_container')).not.toBeNull(); expect(element.querySelector('#userinfo_container')).not.toBeNull();
@ -355,10 +333,7 @@ describe('User info component', () => {
}); });
it('should get the bpm current user image from the service', async () => { it('should get the bpm current user image from the service', async () => {
getCurrentUserInfoStub.and.returnValue(of(fakeBpmUser)); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).not.toBeNull(); expect(element.querySelector('#userinfo_container')).not.toBeNull();
expect(element.querySelector('#logged-user-img')).not.toBeNull(); expect(element.querySelector('#logged-user-img')).not.toBeNull();
expect(element.querySelector('#logged-user-img').getAttribute('src')) expect(element.querySelector('#logged-user-img').getAttribute('src'))
@ -366,15 +341,12 @@ describe('User info component', () => {
}); });
it('should show last name if first name is null', async () => { it('should show last name if first name is null', async () => {
fixture.detectChanges();
const wrongBpmUser: BpmUserModel = new BpmUserModel({ const wrongBpmUser: BpmUserModel = new BpmUserModel({
firstName: null, firstName: null,
lastName: 'fake-last-name' lastName: 'fake-last-name'
}); });
getCurrentUserInfoStub.and.returnValue(of(wrongBpmUser)); getCurrentUserInfoStub.and.returnValue(of(wrongBpmUser));
component.getUserInfo(); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
const fullNameElement = (element.querySelector('#adf-userinfo-bpm-name-display')); const fullNameElement = (element.querySelector('#adf-userinfo-bpm-name-display'));
fixture.detectChanges(); fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
@ -389,10 +361,8 @@ describe('User info component', () => {
lastName: 'fake-last-name' lastName: 'fake-last-name'
}); });
getCurrentUserInfoStub.and.returnValue(of(wrongFirstNameBpmUser)); getCurrentUserInfoStub.and.returnValue(of(wrongFirstNameBpmUser));
await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
expect(element.querySelector('#adf-userinfo-bpm-name-display')).toBeDefined(); expect(element.querySelector('#adf-userinfo-bpm-name-display')).toBeDefined();
expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).toContain('fake-last-name'); expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).toContain('fake-last-name');
@ -405,11 +375,8 @@ describe('User info component', () => {
lastName: 'null' lastName: 'null'
}); });
getCurrentUserInfoStub.and.returnValue(of(wrongLastNameBpmUser)); getCurrentUserInfoStub.and.returnValue(of(wrongLastNameBpmUser));
component.getUserInfo(); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
expect(element.querySelector('#adf-userinfo-bpm-name-display')).toBeDefined(); expect(element.querySelector('#adf-userinfo-bpm-name-display')).toBeDefined();
expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).toContain('fake-first-name'); expect(element.querySelector('#adf-userinfo-bpm-name-display').textContent).toContain('fake-first-name');
@ -417,7 +384,7 @@ describe('User info component', () => {
}); });
it('should not show the tabs', async () => { it('should not show the tabs', async () => {
fixture.detectChanges(); await whenFixtureReady();
openUserInfo(); openUserInfo();
await fixture.whenStable(); await fixture.whenStable();
fixture.detectChanges(); fixture.detectChanges();
@ -437,35 +404,8 @@ describe('User info component', () => {
spyOn(bpmUserService, 'getCurrentUserInfo').and.returnValue(of(fakeBpmUser)); spyOn(bpmUserService, 'getCurrentUserInfo').and.returnValue(of(fakeBpmUser));
}); });
it('should able to fetch ecm userInfo', (done) => {
fixture.detectChanges();
fixture.whenStable().then(() => {
component.ecmUser$.subscribe((response: EcmUserModel) => {
expect(response).toBeDefined();
expect(response.firstName).toBe('fake-ecm-first-name');
expect(response.lastName).toBe('fake-ecm-last-name');
expect(response.email).toBe('fakeEcm@ecmUser.com');
done();
});
});
});
it('should able to fetch bpm userInfo', (done) => {
fixture.detectChanges();
fixture.whenStable().then(() => {
component.bpmUser$.subscribe((response: BpmUserModel) => {
expect(response).toBeDefined();
expect(response.firstName).toBe('fake-bpm-first-name');
expect(response.lastName).toBe('fake-bpm-last-name');
expect(response.email).toBe('fakeBpm@fake.com');
done();
});
});
});
it('should get the bpm user information from the service', async () => { it('should get the bpm user information from the service', async () => {
await whenFixtureReady();
openUserInfo(); openUserInfo();
const bpmTab = fixture.debugElement.queryAll(By.css('#tab-group-env .mat-tab-labels .mat-tab-label'))[1]; const bpmTab = fixture.debugElement.queryAll(By.css('#tab-group-env .mat-tab-labels .mat-tab-label'))[1];
bpmTab.triggerEventHandler('click', null); bpmTab.triggerEventHandler('click', null);
@ -482,6 +422,7 @@ describe('User info component', () => {
}); });
it('should get the ecm user information from the service', async () => { it('should get the ecm user information from the service', async () => {
await whenFixtureReady();
openUserInfo(); openUserInfo();
const ecmUsername = fixture.debugElement.query(By.css('#ecm-username')); const ecmUsername = fixture.debugElement.query(By.css('#ecm-username'));
const ecmImage = fixture.debugElement.query(By.css('#ecm-user-detail-image')); const ecmImage = fixture.debugElement.query(By.css('#ecm-user-detail-image'));
@ -496,7 +437,8 @@ describe('User info component', () => {
expect(fixture.debugElement.query(By.css('#ecm-job-title')).nativeElement.textContent).toContain('job-ecm-test'); expect(fixture.debugElement.query(By.css('#ecm-job-title')).nativeElement.textContent).toContain('job-ecm-test');
}); });
it('should show the ecm image if exists', () => { it('should show the ecm image if exists', async () => {
await whenFixtureReady();
openUserInfo(); openUserInfo();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
expect(element.querySelector('#logged-user-img')).toBeDefined(); expect(element.querySelector('#logged-user-img')).toBeDefined();
@ -505,15 +447,14 @@ describe('User info component', () => {
it('should show the ecm initials if the ecm user has no image', async () => { it('should show the ecm initials if the ecm user has no image', async () => {
getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUserNoImage)); getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUserNoImage));
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
expect(element.querySelector('#user-initials-image').textContent).toContain('ff'); expect(element.querySelector('#user-initials-image').textContent).toContain('ff');
}); });
it('should show the tabs for the env', () => { it('should show the tabs for the env', async () => {
await whenFixtureReady();
openUserInfo(); openUserInfo();
const tabGroup = fixture.debugElement.query(By.css('#tab-group-env')); const tabGroup = fixture.debugElement.query(By.css('#tab-group-env'));
const tabs = fixture.debugElement.queryAll(By.css('#tab-group-env .mat-tab-labels .mat-tab-label')); const tabs = fixture.debugElement.queryAll(By.css('#tab-group-env .mat-tab-labels .mat-tab-label'));
@ -523,7 +464,8 @@ describe('User info component', () => {
expect(tabs.length).toBe(2); expect(tabs.length).toBe(2);
}); });
it('should not close the menu when a tab is clicked', () => { it('should not close the menu when a tab is clicked', async () => {
await whenFixtureReady();
openUserInfo(); openUserInfo();
const tabGroup = fixture.debugElement.query(By.css('#tab-group-env')); const tabGroup = fixture.debugElement.query(By.css('#tab-group-env'));
const tabs = fixture.debugElement.queryAll(By.css('#tab-group-env .mat-tab-labels .mat-tab-label')); const tabs = fixture.debugElement.queryAll(By.css('#tab-group-env .mat-tab-labels .mat-tab-label'));
@ -547,9 +489,7 @@ describe('User info component', () => {
}); });
it('should show the identity user initials if is not ecm user', async () => { it('should show the identity user initials if is not ecm user', async () => {
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
expect(element.querySelector('#user-initials-image').textContent).toContain('ff'); expect(element.querySelector('#user-initials-image').textContent).toContain('ff');
}); });
@ -569,8 +509,8 @@ describe('User info component', () => {
}); });
it('should show full name next the user image', async () => { it('should show full name next the user image', async () => {
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
const imageButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#identity-user-image'); const imageButton: HTMLButtonElement = <HTMLButtonElement> element.querySelector('#identity-user-image');
imageButton.click(); imageButton.click();
fixture.detectChanges(); fixture.detectChanges();
@ -582,13 +522,9 @@ describe('User info component', () => {
}); });
it('should show last name if first name is null', async () => { it('should show last name if first name is null', async () => {
fixture.detectChanges();
getCurrentUserInfoStub.and.returnValue(identityUserWithOutFirstNameMock); getCurrentUserInfoStub.and.returnValue(identityUserWithOutFirstNameMock);
component.getUserInfo(); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
const fullNameElement = element.querySelector('#adf-userinfo-identity-name-display'); const fullNameElement = element.querySelector('#adf-userinfo-identity-name-display');
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
expect(element.querySelector('#adf-userinfo-identity-name-display')).not.toBeNull(); expect(element.querySelector('#adf-userinfo-identity-name-display')).not.toBeNull();
@ -598,9 +534,8 @@ describe('User info component', () => {
it('should not show first name if it is null string', async () => { it('should not show first name if it is null string', async () => {
getCurrentUserInfoStub.and.returnValue(identityUserWithOutFirstNameMock); getCurrentUserInfoStub.and.returnValue(identityUserWithOutFirstNameMock);
component.getUserInfo(); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
const fullNameElement = element.querySelector('#adf-userinfo-identity-name-display'); const fullNameElement = element.querySelector('#adf-userinfo-identity-name-display');
fixture.detectChanges(); fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
@ -611,10 +546,8 @@ describe('User info component', () => {
it('should not show last name if it is null string', async () => { it('should not show last name if it is null string', async () => {
getCurrentUserInfoStub.and.returnValue(identityUserWithOutLastNameMock); getCurrentUserInfoStub.and.returnValue(identityUserWithOutLastNameMock);
component.getUserInfo(); await whenFixtureReady();
fixture.detectChanges();
await fixture.whenStable();
const fullNameElement = element.querySelector('#adf-userinfo-identity-name-display'); const fullNameElement = element.querySelector('#adf-userinfo-identity-name-display');
fixture.detectChanges(); fixture.detectChanges();
expect(element.querySelector('#userinfo_container')).toBeDefined(); expect(element.querySelector('#userinfo_container')).toBeDefined();
@ -627,10 +560,8 @@ describe('User info component', () => {
getCurrentUserInfoStub.and.returnValue(identityUserWithOutLastNameMock); getCurrentUserInfoStub.and.returnValue(identityUserWithOutLastNameMock);
getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUser)); getCurrenEcmtUserInfoStub.and.returnValue(of(fakeEcmUser));
isEcmLoggedInStub.and.returnValue(true); isEcmLoggedInStub.and.returnValue(true);
fixture.detectChanges(); await whenFixtureReady();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('.adf-userinfo-pic')).toBeNull(); expect(element.querySelector('.adf-userinfo-pic')).toBeNull();
expect(element.querySelector('.adf-userinfo-profile-image')).toBeDefined(); expect(element.querySelector('.adf-userinfo-profile-image')).toBeDefined();
expect(element.querySelector('.adf-userinfo-profile-image')).not.toBeNull(); expect(element.querySelector('.adf-userinfo-profile-image')).not.toBeNull();

View File

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { Component, Input, OnInit, ViewEncapsulation, ViewChild } from '@angular/core'; import { Component, Input, OnInit, ViewEncapsulation, ViewChild, OnDestroy } from '@angular/core';
import { AuthenticationService } from '../../services/authentication.service'; import { AuthenticationService } from '../../services/authentication.service';
import { BpmUserModel } from '../../models/bpm-user.model'; import { BpmUserModel } from '../../models/bpm-user.model';
import { EcmUserModel } from '../../models/ecm-user.model'; import { EcmUserModel } from '../../models/ecm-user.model';
@ -23,8 +23,9 @@ import { IdentityUserModel } from '../../models/identity-user.model';
import { BpmUserService } from '../../services/bpm-user.service'; import { BpmUserService } from '../../services/bpm-user.service';
import { EcmUserService } from '../../services/ecm-user.service'; import { EcmUserService } from '../../services/ecm-user.service';
import { IdentityUserService } from '../../services/identity-user.service'; import { IdentityUserService } from '../../services/identity-user.service';
import { of, Observable } from 'rxjs'; import { of, Observable, Subject } from 'rxjs';
import { MatMenuTrigger, MenuPositionX, MenuPositionY } from '@angular/material/menu'; import { MatMenuTrigger, MenuPositionX, MenuPositionY } from '@angular/material/menu';
import { debounceTime, startWith, takeUntil } from 'rxjs/operators';
@Component({ @Component({
selector: 'adf-userinfo', selector: 'adf-userinfo',
@ -32,7 +33,7 @@ import { MatMenuTrigger, MenuPositionX, MenuPositionY } from '@angular/material/
styleUrls: ['./user-info.component.scss'], styleUrls: ['./user-info.component.scss'],
encapsulation: ViewEncapsulation.None encapsulation: ViewEncapsulation.None
}) })
export class UserInfoComponent implements OnInit { export class UserInfoComponent implements OnInit, OnDestroy {
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger; @ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;
@ -68,6 +69,7 @@ export class UserInfoComponent implements OnInit {
bpmUser$: Observable<BpmUserModel>; bpmUser$: Observable<BpmUserModel>;
identityUser$: Observable<IdentityUserModel>; identityUser$: Observable<IdentityUserModel>;
selectedIndex: number; selectedIndex: number;
private destroy$ = new Subject();
constructor(private ecmUserService: EcmUserService, constructor(private ecmUserService: EcmUserService,
private bpmUserService: BpmUserService, private bpmUserService: BpmUserService,
@ -76,7 +78,17 @@ export class UserInfoComponent implements OnInit {
} }
ngOnInit() { ngOnInit() {
this.getUserInfo(); this.authService.onLogin
.pipe(
startWith(this.authService.isLoggedIn()),
debounceTime(500),
takeUntil(this.destroy$)
).subscribe(() => this.getUserInfo());
}
ngOnDestroy(): void {
this.destroy$.next(true);
this.destroy$.complete();
} }
getUserInfo() { getUserInfo() {