Skip to content
Snippets Groups Projects
Commit 055eaa6f authored by Martin's avatar Martin
Browse files

OZG-6986 impl MR comments

parent 6a95d4ea
Branches
Tags
1 merge request!25Ozg 6986 adjust routing
...@@ -208,13 +208,13 @@ describe('AppComponent', () => { ...@@ -208,13 +208,13 @@ describe('AppComponent', () => {
expect(component.evaluateNavigationByConfiguration).toHaveBeenCalled(); expect(component.evaluateNavigationByConfiguration).toHaveBeenCalled();
}); });
it('should do navigation by api root if link is missing', () => { it('should navigate by api root if link is missing', () => {
component.doNavigationByApiRoot = jest.fn(); component.navigateByApiRoot = jest.fn();
const apiRootResource: ApiRootResource = createApiRootResource(); const apiRootResource: ApiRootResource = createApiRootResource();
component.evaluateNavigationByApiRoot(apiRootResource); component.evaluateNavigationByApiRoot(apiRootResource);
expect(component.doNavigationByApiRoot).toHaveBeenCalledWith(apiRootResource); expect(component.navigateByApiRoot).toHaveBeenCalledWith(apiRootResource);
}); });
}); });
...@@ -223,7 +223,7 @@ describe('AppComponent', () => { ...@@ -223,7 +223,7 @@ describe('AppComponent', () => {
beforeEach(() => { beforeEach(() => {
configurationService.get.mockReturnValue(of(createStateResource(configurationResource))); configurationService.get.mockReturnValue(of(createStateResource(configurationResource)));
component.doNavigationByConfiguration = jest.fn(); component.navigateByConfiguration = jest.fn();
}); });
it('should call configuration service to get resource', () => { it('should call configuration service to get resource', () => {
...@@ -232,52 +232,52 @@ describe('AppComponent', () => { ...@@ -232,52 +232,52 @@ describe('AppComponent', () => {
expect(configurationService.get).toHaveBeenCalled(); expect(configurationService.get).toHaveBeenCalled();
}); });
it('should call do navigation by configuration', () => { it('should call navigate by configuration', () => {
component.evaluateNavigationByConfiguration(); component.evaluateNavigationByConfiguration();
expect(component.doNavigationByConfiguration).toHaveBeenCalledWith(configurationResource); expect(component.navigateByConfiguration).toHaveBeenCalledWith(configurationResource);
}); });
it('should NOT call do navigation by configuration if resource is loading', () => { it('should NOT call navigate by configuration if resource is loading', () => {
configurationService.get.mockReturnValue(of(createEmptyStateResource<ConfigurationResource>(true))); configurationService.get.mockReturnValue(of(createEmptyStateResource<ConfigurationResource>(true)));
component.evaluateNavigationByConfiguration(); component.evaluateNavigationByConfiguration();
expect(component.doNavigationByConfiguration).not.toHaveBeenCalled(); expect(component.navigateByConfiguration).not.toHaveBeenCalled();
}); });
}); });
describe('do navigation by configuration', () => { describe('navigate by configuration', () => {
beforeEach(() => { beforeEach(() => {
component.unsubscribe = jest.fn(); component.unsubscribe = jest.fn();
}); });
it('should navigate to postfach if settings link exists', () => { it('should navigate to postfach if settings link exists', () => {
component.doNavigationByConfiguration(createConfigurationResource([ConfigurationLinkRel.SETTING])); component.navigateByConfiguration(createConfigurationResource([ConfigurationLinkRel.SETTING]));
expect(router.navigate).toHaveBeenCalledWith(['/postfach']); expect(router.navigate).toHaveBeenCalledWith(['/postfach']);
}); });
it('should navigate to statistik if aggregation mapping link exists', () => { it('should navigate to statistik if aggregation mapping link exists', () => {
component.doNavigationByConfiguration(createConfigurationResource([ConfigurationLinkRel.AGGREGATION_MAPPINGS])); component.navigateByConfiguration(createConfigurationResource([ConfigurationLinkRel.AGGREGATION_MAPPINGS]));
expect(router.navigate).toHaveBeenCalledWith(['/statistik']); expect(router.navigate).toHaveBeenCalledWith(['/statistik']);
}); });
it('should navigate to unavailable page if no link exists', () => { it('should navigate to unavailable page if no link exists', () => {
component.doNavigationByConfiguration(createConfigurationResource()); component.navigateByConfiguration(createConfigurationResource());
expect(router.navigate).toHaveBeenCalledWith(['/unavailable']); expect(router.navigate).toHaveBeenCalledWith(['/unavailable']);
}); });
it('should call unsubscribe', () => { it('should call unsubscribe', () => {
component.doNavigationByConfiguration(createConfigurationResource()); component.navigateByConfiguration(createConfigurationResource());
expect(component.unsubscribe).toHaveBeenCalled(); expect(component.unsubscribe).toHaveBeenCalled();
}); });
}); });
describe('do navigation by api root', () => { describe('navigate by api root', () => {
beforeEach(() => { beforeEach(() => {
component.unsubscribe = jest.fn(); component.unsubscribe = jest.fn();
}); });
...@@ -285,19 +285,19 @@ describe('AppComponent', () => { ...@@ -285,19 +285,19 @@ describe('AppComponent', () => {
it('should navigate to benutzer und rollen if users link exists', () => { it('should navigate to benutzer und rollen if users link exists', () => {
const apiRootResource: ApiRootResource = createApiRootResource([ApiRootLinkRel.USERS]); const apiRootResource: ApiRootResource = createApiRootResource([ApiRootLinkRel.USERS]);
component.doNavigationByApiRoot(apiRootResource); component.navigateByApiRoot(apiRootResource);
expect(router.navigate).toHaveBeenCalledWith(['/benutzer_und_rollen']); expect(router.navigate).toHaveBeenCalledWith(['/benutzer_und_rollen']);
}); });
it('should navigate to unavailable page if no link exists', () => { it('should navigate to unavailable page if no link exists', () => {
component.doNavigationByApiRoot(createApiRootResource()); component.navigateByApiRoot(createApiRootResource());
expect(router.navigate).toHaveBeenCalledWith(['/unavailable']); expect(router.navigate).toHaveBeenCalledWith(['/unavailable']);
}); });
it('should call unsubscribe', () => { it('should call unsubscribe', () => {
component.doNavigationByApiRoot(createApiRootResource()); component.navigateByApiRoot(createApiRootResource());
expect(component.unsubscribe).toHaveBeenCalled(); expect(component.unsubscribe).toHaveBeenCalled();
}); });
......
...@@ -65,12 +65,12 @@ import { UnavailablePageComponent } from '../pages/unavailable/unavailable-page/ ...@@ -65,12 +65,12 @@ import { UnavailablePageComponent } from '../pages/unavailable/unavailable-page/
], ],
}) })
export class AppComponent implements OnInit { export class AppComponent implements OnInit {
readonly title = 'admin'; readonly title: string = 'admin';
private readonly authenticationService: AuthenticationService = inject(AuthenticationService); private readonly authenticationService = inject(AuthenticationService);
private readonly apiRootService: ApiRootService = inject(ApiRootService); private readonly apiRootService = inject(ApiRootService);
private readonly router: Router = inject(Router); private readonly router = inject(Router);
private readonly configurationService: ConfigurationService = inject(ConfigurationService); private readonly configurationService = inject(ConfigurationService);
public apiRootStateResource$: Observable<StateResource<ApiRootResource>>; public apiRootStateResource$: Observable<StateResource<ApiRootResource>>;
...@@ -98,7 +98,7 @@ export class AppComponent implements OnInit { ...@@ -98,7 +98,7 @@ export class AppComponent implements OnInit {
if (hasLink(apiRootResource, ApiRootLinkRel.CONFIGURATION)) { if (hasLink(apiRootResource, ApiRootLinkRel.CONFIGURATION)) {
this.evaluateNavigationByConfiguration(); this.evaluateNavigationByConfiguration();
} else { } else {
this.doNavigationByApiRoot(apiRootResource); this.navigateByApiRoot(apiRootResource);
} }
} }
...@@ -106,30 +106,30 @@ export class AppComponent implements OnInit { ...@@ -106,30 +106,30 @@ export class AppComponent implements OnInit {
this.configurationSubscription = this.configurationService this.configurationSubscription = this.configurationService
.get() .get()
.pipe(filter(isLoaded), mapToResource<ApiRootResource>()) .pipe(filter(isLoaded), mapToResource<ApiRootResource>())
.subscribe((configurationResource: ConfigurationResource) => this.doNavigationByConfiguration(configurationResource)); .subscribe((configurationResource: ConfigurationResource) => this.navigateByConfiguration(configurationResource));
} }
doNavigationByConfiguration(configurationResource: ConfigurationResource): void { navigateByConfiguration(configurationResource: ConfigurationResource): void {
if (hasLink(configurationResource, ConfigurationLinkRel.SETTING)) { if (hasLink(configurationResource, ConfigurationLinkRel.SETTING)) {
this.doInitialNavigation(ROUTES.POSTFACH); this.navigate(ROUTES.POSTFACH);
} else if (hasLink(configurationResource, ConfigurationLinkRel.AGGREGATION_MAPPINGS)) { } else if (hasLink(configurationResource, ConfigurationLinkRel.AGGREGATION_MAPPINGS)) {
this.doInitialNavigation(ROUTES.STATISTIK); this.navigate(ROUTES.STATISTIK);
} else { } else {
this.doInitialNavigation(ROUTES.UNAVAILABLE); this.navigate(ROUTES.UNAVAILABLE);
} }
this.unsubscribe(); this.unsubscribe();
} }
doNavigationByApiRoot(apiRootResource: ApiRootResource): void { navigateByApiRoot(apiRootResource: ApiRootResource): void {
if (hasLink(apiRootResource, ApiRootLinkRel.USERS)) { if (hasLink(apiRootResource, ApiRootLinkRel.USERS)) {
this.doInitialNavigation(ROUTES.BENUTZER_UND_ROLLEN); this.navigate(ROUTES.BENUTZER_UND_ROLLEN);
} else { } else {
this.doInitialNavigation(ROUTES.UNAVAILABLE); this.navigate(ROUTES.UNAVAILABLE);
} }
this.unsubscribe(); this.unsubscribe();
} }
private doInitialNavigation(routePath: string): void { private navigate(routePath: string): void {
this.router.navigate(['/' + routePath]); this.router.navigate(['/' + routePath]);
} }
......
...@@ -109,16 +109,16 @@ describe('AuthenticationService', () => { ...@@ -109,16 +109,16 @@ describe('AuthenticationService', () => {
const event: OAuthEvent = createOAuthEvent(); const event: OAuthEvent = createOAuthEvent();
beforeEach(() => { beforeEach(() => {
service.shouldProceed = jest.fn().mockReturnValue(true); service.shouldProceedByType = jest.fn().mockReturnValue(true);
service.setCurrentUser = jest.fn(); service.setCurrentUser = jest.fn();
service.unsubscribeEvents = jest.fn(); service.unsubscribeEvents = jest.fn();
}); });
it('should call shouldProceed on event trigger', () => { it('should call shouldProceedByType on event trigger', () => {
service.buildEventPromise(); service.buildEventPromise();
eventsSubject.next(event); eventsSubject.next(event);
expect(service.shouldProceed).toHaveBeenCalledWith(event); expect(service.shouldProceedByType).toHaveBeenCalledWith(event);
}); });
describe('on next', () => { describe('on next', () => {
...@@ -165,13 +165,13 @@ describe('AuthenticationService', () => { ...@@ -165,13 +165,13 @@ describe('AuthenticationService', () => {
}); });
}); });
describe('should proceed', () => { describe('should proceed by type', () => {
const event: OAuthEvent = createOAuthEvent(); const event: OAuthEvent = createOAuthEvent();
it('should call considered as login event', () => { it('should call considered as login event', () => {
service.consideredAsLoginEvent = jest.fn(); service.consideredAsLoginEvent = jest.fn();
service.shouldProceed(event); service.shouldProceedByType(event);
expect(service.consideredAsLoginEvent).toHaveBeenCalledWith(event.type); expect(service.consideredAsLoginEvent).toHaveBeenCalledWith(event.type);
}); });
...@@ -179,7 +179,7 @@ describe('AuthenticationService', () => { ...@@ -179,7 +179,7 @@ describe('AuthenticationService', () => {
it('should return true on login event', () => { it('should return true on login event', () => {
service.consideredAsLoginEvent = jest.fn().mockReturnValue(true); service.consideredAsLoginEvent = jest.fn().mockReturnValue(true);
const proceed: boolean = service.shouldProceed(event); const proceed: boolean = service.shouldProceedByType(event);
expect(proceed).toBeTruthy(); expect(proceed).toBeTruthy();
}); });
...@@ -188,7 +188,7 @@ describe('AuthenticationService', () => { ...@@ -188,7 +188,7 @@ describe('AuthenticationService', () => {
service.consideredAsLoginEvent = jest.fn().mockReturnValue(false); service.consideredAsLoginEvent = jest.fn().mockReturnValue(false);
service.consideredAsPageReloadEvent = jest.fn(); service.consideredAsPageReloadEvent = jest.fn();
service.shouldProceed(event); service.shouldProceedByType(event);
expect(service.consideredAsPageReloadEvent).toHaveBeenCalledWith(event.type); expect(service.consideredAsPageReloadEvent).toHaveBeenCalledWith(event.type);
}); });
...@@ -197,7 +197,7 @@ describe('AuthenticationService', () => { ...@@ -197,7 +197,7 @@ describe('AuthenticationService', () => {
service.consideredAsLoginEvent = jest.fn().mockReturnValue(false); service.consideredAsLoginEvent = jest.fn().mockReturnValue(false);
service.consideredAsPageReloadEvent = jest.fn().mockReturnValue(true); service.consideredAsPageReloadEvent = jest.fn().mockReturnValue(true);
const proceed: boolean = service.shouldProceed(event); const proceed: boolean = service.shouldProceedByType(event);
expect(proceed).toBeTruthy(); expect(proceed).toBeTruthy();
}); });
...@@ -206,7 +206,7 @@ describe('AuthenticationService', () => { ...@@ -206,7 +206,7 @@ describe('AuthenticationService', () => {
service.consideredAsLoginEvent = jest.fn().mockReturnValue(false); service.consideredAsLoginEvent = jest.fn().mockReturnValue(false);
service.consideredAsPageReloadEvent = jest.fn().mockReturnValue(false); service.consideredAsPageReloadEvent = jest.fn().mockReturnValue(false);
const proceed: boolean = service.shouldProceed(event); const proceed: boolean = service.shouldProceedByType(event);
expect(proceed).toBeFalsy(); expect(proceed).toBeFalsy();
}); });
......
...@@ -51,8 +51,13 @@ export class AuthenticationService { ...@@ -51,8 +51,13 @@ export class AuthenticationService {
} }
buildEventPromise(): Promise<void> { buildEventPromise(): Promise<void> {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => this.handleOAuthEventsForPromise(resolve, reject));
this.eventSubscription = this.oAuthService.events.pipe(filter((event: OAuthEvent) => this.shouldProceed(event))).subscribe({ }
private handleOAuthEventsForPromise(resolve: (value: void | PromiseLike<void>) => void, reject: (reason?: any) => void): void {
this.eventSubscription = this.oAuthService.events
.pipe(filter((event: OAuthEvent) => this.shouldProceedByType(event)))
.subscribe({
next: () => { next: () => {
this.setCurrentUser(); this.setCurrentUser();
this.unsubscribeEvents(); this.unsubscribeEvents();
...@@ -63,10 +68,9 @@ export class AuthenticationService { ...@@ -63,10 +68,9 @@ export class AuthenticationService {
reject(error); reject(error);
}, },
}); });
});
} }
shouldProceed(event: OAuthEvent): boolean { shouldProceedByType(event: OAuthEvent): boolean {
return this.consideredAsLoginEvent(event.type) || this.consideredAsPageReloadEvent(event.type); return this.consideredAsLoginEvent(event.type) || this.consideredAsPageReloadEvent(event.type);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment