70373: AuthService impersonate tests

This commit is contained in:
Kristof De Langhe
2020-04-15 16:47:18 +02:00
parent 7df5025aa5
commit 5e94cf379c

View File

@@ -8,7 +8,7 @@ import { of as observableOf } from 'rxjs';
import { authReducer, AuthState } from './auth.reducer';
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
import { AuthService } from './auth.service';
import { AuthService, IMPERSONATING_COOKIE } from './auth.service';
import { RouterStub } from '../../shared/testing/router-stub';
import { ActivatedRouteStub } from '../../shared/testing/active-router-stub';
import { CookieService } from '../services/cookie.service';
@@ -316,5 +316,120 @@ describe('AuthService test', () => {
expect(routeServiceMock.getHistory).toHaveBeenCalled();
expect(routerStub.navigateByUrl).toHaveBeenCalledWith('/');
});
describe('impersonate', () => {
const userId = 'testUserId';
beforeEach(() => {
spyOn(authService, 'refreshAfterLogout');
authService.impersonate(userId);
});
it('should impersonate user', () => {
expect(storage.set).toHaveBeenCalledWith(IMPERSONATING_COOKIE, userId);
});
it('should call refreshAfterLogout', () => {
expect(authService.refreshAfterLogout).toHaveBeenCalled();
});
});
describe('stopImpersonating', () => {
beforeEach(() => {
authService.stopImpersonating();
});
it('should impersonate user', () => {
expect(storage.remove).toHaveBeenCalledWith(IMPERSONATING_COOKIE);
});
});
describe('stopImpersonatingAndRefresh', () => {
beforeEach(() => {
spyOn(authService, 'refreshAfterLogout');
authService.stopImpersonatingAndRefresh();
});
it('should impersonate user', () => {
expect(storage.remove).toHaveBeenCalledWith(IMPERSONATING_COOKIE);
});
it('should call refreshAfterLogout', () => {
expect(authService.refreshAfterLogout).toHaveBeenCalled();
});
});
describe('getImpersonateID', () => {
beforeEach(() => {
authService.getImpersonateID();
});
it('should impersonate user', () => {
expect(storage.get).toHaveBeenCalledWith(IMPERSONATING_COOKIE);
});
});
describe('isImpersonating', () => {
const userId = 'testUserId';
let result: boolean;
describe('when the cookie doesn\'t contain a value', () => {
beforeEach(() => {
result = authService.isImpersonating();
});
it('should return false', () => {
expect(result).toBe(false);
});
});
describe('when the cookie contains a value', () => {
beforeEach(() => {
storage.get = jasmine.createSpy().and.returnValue(userId);
result = authService.isImpersonating();
});
it('should return true', () => {
expect(result).toBe(true);
});
});
});
describe('isImpersonatingUser', () => {
const userId = 'testUserId';
let result: boolean;
describe('when the cookie doesn\'t contain a value', () => {
beforeEach(() => {
result = authService.isImpersonatingUser(userId);
});
it('should return false', () => {
expect(result).toBe(false);
});
});
describe('when the cookie contains the right value', () => {
beforeEach(() => {
storage.get = jasmine.createSpy().and.returnValue(userId);
result = authService.isImpersonatingUser(userId);
});
it('should return true', () => {
expect(result).toBe(true);
});
});
describe('when the cookie contains the wrong value', () => {
beforeEach(() => {
storage.get = jasmine.createSpy().and.returnValue('wrongValue');
result = authService.isImpersonatingUser(userId);
});
it('should return false', () => {
expect(result).toBe(false);
});
});
});
});
});