diff --git a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts index 7a301aa7e9..16acd14a19 100644 --- a/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts +++ b/src/app/+admin/admin-access-control/epeople-registry/eperson-form/eperson-form.component.spec.ts @@ -42,6 +42,7 @@ describe('EPersonFormComponent', () => { let mockEPeople; let ePersonDataServiceStub: any; + let authService: AuthServiceStub; beforeEach(async(() => { mockEPeople = [EPersonMock, EPersonMock2]; @@ -106,6 +107,7 @@ describe('EPersonFormComponent', () => { }; builderService = getMockFormBuilderService(); translateService = getMockTranslateService(); + authService = new AuthServiceStub(); TestBed.configureTestingModule({ imports: [CommonModule, NgbModule, FormsModule, ReactiveFormsModule, BrowserModule, TranslateModule.forRoot({ @@ -127,7 +129,7 @@ describe('EPersonFormComponent', () => { { provide: Store, useValue: {} }, { provide: RemoteDataBuildService, useValue: {} }, { provide: HALEndpointService, useValue: {} }, - { provide: AuthService, useValue: new AuthServiceStub() }, + { provide: AuthService, useValue: authService }, EPeopleRegistryComponent ], schemas: [NO_ERRORS_SCHEMA] @@ -231,4 +233,40 @@ describe('EPersonFormComponent', () => { }); }); + describe('impersonate', () => { + let ePersonId; + + beforeEach(() => { + spyOn(authService, 'impersonate').and.callThrough(); + ePersonId = 'testEPersonId'; + component.epersonInitial = Object.assign(new EPerson(), { + id: ePersonId + }); + component.impersonate(); + }); + + it('should call authService.impersonate', () => { + expect(authService.impersonate).toHaveBeenCalledWith(ePersonId); + }); + + it('should set isImpersonated to true', () => { + expect(component.isImpersonated).toBe(true); + }); + }); + + describe('stopImpersonating', () => { + beforeEach(() => { + spyOn(authService, 'stopImpersonatingAndRefresh').and.callThrough(); + component.stopImpersonating(); + }); + + it('should call authService.stopImpersonatingAndRefresh', () => { + expect(authService.stopImpersonatingAndRefresh).toHaveBeenCalled(); + }); + + it('should set isImpersonated to false', () => { + expect(component.isImpersonated).toBe(false); + }); + }); + }); diff --git a/src/app/shared/impersonate-navbar/impersonate-navbar.component.spec.ts b/src/app/shared/impersonate-navbar/impersonate-navbar.component.spec.ts index e69de29bb2..16e445f183 100644 --- a/src/app/shared/impersonate-navbar/impersonate-navbar.component.spec.ts +++ b/src/app/shared/impersonate-navbar/impersonate-navbar.component.spec.ts @@ -0,0 +1,80 @@ +import { ImpersonateNavbarComponent } from './impersonate-navbar.component'; +import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing'; +import { VarDirective } from '../utils/var.directive'; +import { RouterTestingModule } from '@angular/router/testing'; +import { TranslateModule } from '@ngx-translate/core'; +import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { AuthService } from '../../core/auth/auth.service'; +import { Store, StoreModule } from '@ngrx/store'; +import { authReducer, AuthState } from '../../core/auth/auth.reducer'; +import { AuthTokenInfo } from '../../core/auth/models/auth-token-info.model'; +import { EPersonMock } from '../testing/eperson-mock'; +import { AppState } from '../../app.reducer'; +import { By } from '@angular/platform-browser'; + +describe('ImpersonateNavbarComponent', () => { + let component: ImpersonateNavbarComponent; + let fixture: ComponentFixture; + let authService: AuthService; + let authState: AuthState; + + beforeEach(async(() => { + authService = jasmine.createSpyObj('authService', { + isImpersonating: false, + stopImpersonatingAndRefresh: {} + }); + authState = { + authenticated: true, + loaded: true, + loading: false, + authToken: new AuthTokenInfo('test_token'), + userId: EPersonMock.id + }; + + TestBed.configureTestingModule({ + declarations: [ImpersonateNavbarComponent, VarDirective], + imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), StoreModule.forRoot(authReducer)], + providers: [ + { provide: AuthService, useValue: authService } + ], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + })); + + beforeEach(inject([Store], (store: Store) => { + store + .subscribe((state) => { + (state as any).core = Object.create({}); + (state as any).core.auth = authState; + }); + + fixture = TestBed.createComponent(ImpersonateNavbarComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + })); + + describe('when the user is impersonating another user', () => { + beforeEach(() => { + component.isImpersonating = true; + fixture.detectChanges(); + }); + + it('should display a button', () => { + const button = fixture.debugElement.query(By.css('button')); + expect(button).not.toBeNull(); + }); + + it('should call authService\'s stopImpersonatingAndRefresh upon clicking the button', () => { + const button = fixture.debugElement.query(By.css('button')).nativeElement; + button.click(); + expect(authService.stopImpersonatingAndRefresh).toHaveBeenCalled(); + }); + }); + + describe('when the user is not impersonating another user', () => { + it('should not display a button', () => { + const button = fixture.debugElement.query(By.css('button')); + expect(button).toBeNull(); + }); + }); +});