70373: EPerson impersonate tests

This commit is contained in:
Kristof De Langhe
2020-04-15 16:23:41 +02:00
parent 4cd11bcbca
commit 7df5025aa5
2 changed files with 119 additions and 1 deletions

View File

@@ -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);
});
});
});

View File

@@ -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<ImpersonateNavbarComponent>;
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<AppState>) => {
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();
});
});
});