From 85fa1c5a9c03ec51b31cfd5bbbdd8dca0c13c275 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Wed, 21 Jul 2021 09:46:15 +0200 Subject: [PATCH] move effect for cleaning cache to auth.effects --- src/app/core/auth/auth.effects.spec.ts | 16 ++++++++++++++++ src/app/core/auth/auth.effects.ts | 13 +++++++++++++ src/app/core/cache/object-cache.effects.spec.ts | 5 ----- src/app/core/cache/object-cache.effects.ts | 8 +++----- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/app/core/auth/auth.effects.spec.ts b/src/app/core/auth/auth.effects.spec.ts index 5d530f39a9..ed91eb3eea 100644 --- a/src/app/core/auth/auth.effects.spec.ts +++ b/src/app/core/auth/auth.effects.spec.ts @@ -35,6 +35,7 @@ import { EPersonMock } from '../../shared/testing/eperson.mock'; import { AppState, storeModuleConfig } from '../../app.reducer'; import { StoreActionTypes } from '../../store.actions'; import { isAuthenticated, isAuthenticatedLoaded } from './selectors'; +import { AuthorizationDataService } from '../data/feature-authorization/authorization-data.service'; describe('AuthEffects', () => { let authEffects: AuthEffects; @@ -44,6 +45,8 @@ describe('AuthEffects', () => { let token; let store: MockStore; + const authorizationService = jasmine.createSpyObj(['invalidateAuthorizationsRequestCache']); + function init() { authServiceStub = new AuthServiceStub(); token = authServiceStub.getToken(); @@ -68,6 +71,7 @@ describe('AuthEffects', () => { providers: [ AuthEffects, provideMockStore({ initialState }), + { provide: AuthorizationDataService, useValue: authorizationService }, { provide: AuthService, useValue: authServiceStub }, provideMockActions(() => actions), // other providers @@ -417,4 +421,16 @@ describe('AuthEffects', () => { })); }); }); + + describe('invalidateAuthorizationsRequestCache$', () => { + it('should call invalidateAuthorizationsRequestCache method in response to a REHYDRATE action', (done) => { + actions = hot('--a-|', { a: { type: StoreActionTypes.REHYDRATE } }); + + authEffects.invalidateAuthorizationsRequestCache$.subscribe(() => { + expect((authEffects as any).authorizationsService.invalidateAuthorizationsRequestCache).toHaveBeenCalled(); + }); + + done(); + }); + }); }); diff --git a/src/app/core/auth/auth.effects.ts b/src/app/core/auth/auth.effects.ts index c142e8873f..1477a1832e 100644 --- a/src/app/core/auth/auth.effects.ts +++ b/src/app/core/auth/auth.effects.ts @@ -53,6 +53,7 @@ import { RequestActionTypes } from '../data/request.actions'; import { NotificationsActionTypes } from '../../shared/notifications/notifications.actions'; import { LeaveZoneScheduler } from '../utilities/leave-zone.scheduler'; import { EnterZoneScheduler } from '../utilities/enter-zone.scheduler'; +import { AuthorizationDataService } from '../data/feature-authorization/authorization-data.service'; // Action Types that do not break/prevent the user from an idle state const IDLE_TIMER_IGNORE_TYPES: string[] @@ -218,6 +219,16 @@ export class AuthEffects { ); })); + /** + * When the store is rehydrated in the browser, invalidate all cache hits regarding the + * authorizations endpoint, to be sure to have consistent responses after a login with external idp + * + */ + @Effect({ dispatch: false }) invalidateAuthorizationsRequestCache$ = this.actions$ + .pipe(ofType(StoreActionTypes.REHYDRATE), + tap(() => this.authorizationsService.invalidateAuthorizationsRequestCache()) + ); + @Effect() public logOut$: Observable = this.actions$ .pipe( @@ -284,11 +295,13 @@ export class AuthEffects { * @constructor * @param {Actions} actions$ * @param {NgZone} zone + * @param {AuthorizationDataService} authorizationsService * @param {AuthService} authService * @param {Store} store */ constructor(private actions$: Actions, private zone: NgZone, + private authorizationsService: AuthorizationDataService, private authService: AuthService, private store: Store) { } diff --git a/src/app/core/cache/object-cache.effects.spec.ts b/src/app/core/cache/object-cache.effects.spec.ts index cc518c0bf2..3a50a5dbc7 100644 --- a/src/app/core/cache/object-cache.effects.spec.ts +++ b/src/app/core/cache/object-cache.effects.spec.ts @@ -5,20 +5,16 @@ import { cold, hot } from 'jasmine-marbles'; import { ObjectCacheEffects } from './object-cache.effects'; import { ResetObjectCacheTimestampsAction } from './object-cache.actions'; import { StoreActionTypes } from '../../store.actions'; -import { AuthorizationDataService } from '../data/feature-authorization/authorization-data.service'; describe('ObjectCacheEffects', () => { let cacheEffects: ObjectCacheEffects; let actions: Observable; const timestamp = 10000; - const authorizationService = jasmine.createSpyObj(['invalidateAuthorizationsRequestCache']); - beforeEach(() => { TestBed.configureTestingModule({ providers: [ ObjectCacheEffects, provideMockActions(() => actions), - { provide: AuthorizationDataService, useValue: authorizationService }, // other providers ], }); @@ -37,7 +33,6 @@ describe('ObjectCacheEffects', () => { const expected = cold('--b-', { b: new ResetObjectCacheTimestampsAction(new Date().getTime()) }); expect(cacheEffects.fixTimestampsOnRehydrate).toBeObservable(expected); - expect((cacheEffects as any).authorizationsService.invalidateAuthorizationsRequestCache).toHaveBeenCalled(); }); }); }); diff --git a/src/app/core/cache/object-cache.effects.ts b/src/app/core/cache/object-cache.effects.ts index 79b0680406..2bd8ad0e3c 100644 --- a/src/app/core/cache/object-cache.effects.ts +++ b/src/app/core/cache/object-cache.effects.ts @@ -1,10 +1,9 @@ -import { map, tap } from 'rxjs/operators'; +import { map } from 'rxjs/operators'; import { Injectable } from '@angular/core'; import { Actions, Effect, ofType } from '@ngrx/effects'; import { StoreActionTypes } from '../../store.actions'; import { ResetObjectCacheTimestampsAction } from './object-cache.actions'; -import { AuthorizationDataService } from '../data/feature-authorization/authorization-data.service'; @Injectable() export class ObjectCacheEffects { @@ -19,11 +18,10 @@ export class ObjectCacheEffects { */ @Effect() fixTimestampsOnRehydrate = this.actions$ .pipe(ofType(StoreActionTypes.REHYDRATE), - map(() => new ResetObjectCacheTimestampsAction(new Date().getTime())), - tap(() => this.authorizationsService.invalidateAuthorizationsRequestCache()) + map(() => new ResetObjectCacheTimestampsAction(new Date().getTime())) ); - constructor(private actions$: Actions, private authorizationsService: AuthorizationDataService) { + constructor(private actions$: Actions) { } }