72541: End-User-Agreement redirect via store

This commit is contained in:
Kristof De Langhe
2020-08-28 13:05:02 +02:00
parent 76b9fd4702
commit cb3ef1dde4
4 changed files with 30 additions and 17 deletions

View File

@@ -2,23 +2,26 @@ import { EndUserAgreementGuard } from './end-user-agreement.guard';
import { EndUserAgreementService } from './end-user-agreement.service'; import { EndUserAgreementService } from './end-user-agreement.service';
import { Router, UrlTree } from '@angular/router'; import { Router, UrlTree } from '@angular/router';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
import { AuthService } from '../auth/auth.service';
describe('EndUserAgreementGuard', () => { describe('EndUserAgreementGuard', () => {
let guard: EndUserAgreementGuard; let guard: EndUserAgreementGuard;
let endUserAgreementService: EndUserAgreementService; let endUserAgreementService: EndUserAgreementService;
let authService: AuthService;
let router: Router; let router: Router;
beforeEach(() => { beforeEach(() => {
endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', { endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', {
hasCurrentUserAcceptedAgreement: observableOf(true) hasCurrentUserAcceptedAgreement: observableOf(true)
}); });
authService = jasmine.createSpyObj('authService', ['setRedirectUrl']);
router = jasmine.createSpyObj('router', { router = jasmine.createSpyObj('router', {
navigateByUrl: {}, navigateByUrl: {},
parseUrl: new UrlTree() parseUrl: new UrlTree()
}); });
guard = new EndUserAgreementGuard(endUserAgreementService, router); guard = new EndUserAgreementGuard(endUserAgreementService, authService, router);
}); });
describe('canActivate', () => { describe('canActivate', () => {
@@ -39,7 +42,8 @@ describe('EndUserAgreementGuard', () => {
it('should navigate the user with a redirect url', (done) => { it('should navigate the user with a redirect url', (done) => {
const redirect = 'redirect/url'; const redirect = 'redirect/url';
guard.canActivate(undefined, Object.assign({ url: redirect })).subscribe(() => { guard.canActivate(undefined, Object.assign({ url: redirect })).subscribe(() => {
expect(router.navigateByUrl).toHaveBeenCalledWith(jasmine.anything(), { state: { redirect } }); expect(authService.setRedirectUrl).toHaveBeenCalledWith(redirect);
expect(router.navigateByUrl).toHaveBeenCalled();
done(); done();
}); });
}); });

View File

@@ -4,6 +4,7 @@ import { Observable } from 'rxjs/internal/Observable';
import { returnEndUserAgreementUrlTreeOnFalse } from '../shared/operators'; import { returnEndUserAgreementUrlTreeOnFalse } from '../shared/operators';
import { EndUserAgreementService } from './end-user-agreement.service'; import { EndUserAgreementService } from './end-user-agreement.service';
import { tap } from 'rxjs/operators'; import { tap } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service';
/** /**
* A guard redirecting users to the end agreement page when they haven't accepted the latest user agreement * A guard redirecting users to the end agreement page when they haven't accepted the latest user agreement
@@ -12,6 +13,7 @@ import { tap } from 'rxjs/operators';
export class EndUserAgreementGuard implements CanActivate { export class EndUserAgreementGuard implements CanActivate {
constructor(protected endUserAgreementService: EndUserAgreementService, constructor(protected endUserAgreementService: EndUserAgreementService,
protected authService: AuthService,
protected router: Router) { protected router: Router) {
} }
@@ -26,7 +28,8 @@ export class EndUserAgreementGuard implements CanActivate {
returnEndUserAgreementUrlTreeOnFalse(this.router), returnEndUserAgreementUrlTreeOnFalse(this.router),
tap((result) => { tap((result) => {
if (result instanceof UrlTree) { if (result instanceof UrlTree) {
this.router.navigateByUrl(result, { state: { redirect: state.url } }) this.authService.setRedirectUrl(state.url);
this.router.navigateByUrl(result);
} }
}) })
); );

View File

@@ -24,19 +24,19 @@ describe('EndUserAgreementComponent', () => {
let redirectUrl; let redirectUrl;
function init() { function init() {
redirectUrl = 'redirect/url';
endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', { endUserAgreementService = jasmine.createSpyObj('endUserAgreementService', {
hasCurrentUserAcceptedAgreement: observableOf(false), hasCurrentUserAcceptedAgreement: observableOf(false),
setUserAcceptedAgreement: observableOf(true) setUserAcceptedAgreement: observableOf(true)
}); });
notificationsService = jasmine.createSpyObj('notificationsService', ['success', 'error']); notificationsService = jasmine.createSpyObj('notificationsService', ['success', 'error']);
authService = jasmine.createSpyObj('authService', { authService = jasmine.createSpyObj('authService', {
isAuthenticated: observableOf(true) isAuthenticated: observableOf(true),
getRedirectUrl: observableOf(redirectUrl)
}); });
store = jasmine.createSpyObj('store', ['dispatch']); store = jasmine.createSpyObj('store', ['dispatch']);
router = jasmine.createSpyObj('router', ['navigate', 'navigateByUrl']); router = jasmine.createSpyObj('router', ['navigate', 'navigateByUrl']);
redirectUrl = 'redirect/url';
window.history.pushState({ redirect: redirectUrl }, '');
} }
beforeEach(async(() => { beforeEach(async(() => {

View File

@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../core/auth/auth.service'; import { AuthService } from '../../core/auth/auth.service';
import { take } from 'rxjs/operators'; import { switchMap, take } from 'rxjs/operators';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppState } from '../../app.reducer'; import { AppState } from '../../app.reducer';
@@ -8,7 +8,8 @@ import { LogOutAction } from '../../core/auth/auth.actions';
import { EndUserAgreementService } from '../../core/end-user-agreement/end-user-agreement.service'; import { EndUserAgreementService } from '../../core/end-user-agreement/end-user-agreement.service';
import { NotificationsService } from '../../shared/notifications/notifications.service'; import { NotificationsService } from '../../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { hasValue } from '../../shared/empty.util'; import { of as observableOf } from 'rxjs';
import { isNotEmpty } from '../../shared/empty.util';
@Component({ @Component({
selector: 'ds-end-user-agreement', selector: 'ds-end-user-agreement',
@@ -54,15 +55,20 @@ export class EndUserAgreementComponent implements OnInit {
* Set the End User Agreement, display a notification and (optionally) redirect the user back to their original destination * Set the End User Agreement, display a notification and (optionally) redirect the user back to their original destination
*/ */
submit() { submit() {
this.endUserAgreementService.setUserAcceptedAgreement(this.accepted).subscribe((success) => { this.endUserAgreementService.setUserAcceptedAgreement(this.accepted).pipe(
if (success) { switchMap((success) => {
this.notificationsService.success(this.translate.instant('info.end-user-agreement.accept.success')); if (success) {
const redirect = window.history.state.redirect; this.notificationsService.success(this.translate.instant('info.end-user-agreement.accept.success'));
if (hasValue(redirect)) { return this.authService.getRedirectUrl();
this.router.navigateByUrl(redirect); } else {
this.notificationsService.error(this.translate.instant('info.end-user-agreement.accept.error'));
return observableOf(undefined);
} }
} else { }),
this.notificationsService.error(this.translate.instant('info.end-user-agreement.accept.error')); take(1)
).subscribe((redirectUrl) => {
if (isNotEmpty(redirectUrl)) {
this.router.navigateByUrl(redirectUrl);
} }
}); });
} }