Skip to content

Commit

Permalink
Merge pull request #1655 from benthieu/fix/SilentRenewFailed-notFired
Browse files Browse the repository at this point in the history
fix(refreshSession): fix refreshSessionWithRefreshTokens
  • Loading branch information
FabianGosebrink authored Jan 19, 2023
2 parents 36fc58d + c07eb6a commit 56726e5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TestBed, waitForAsync } from '@angular/core/testing';
import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
import { of, throwError } from 'rxjs';
import { mockClass } from '../../test/auto-mock';
import { FlowsService } from '../flows/flows.service';
Expand Down Expand Up @@ -38,36 +38,39 @@ describe('RefreshSessionRefreshTokenService', () => {
});

describe('refreshSessionWithRefreshTokens', () => {
it(
'calls flowsService.processRefreshToken()',
waitForAsync(() => {
const spy = spyOn(flowsService, 'processRefreshToken').and.returnValue(of(null));
it('calls flowsService.processRefreshToken()', waitForAsync(() => {
const spy = spyOn(flowsService, 'processRefreshToken').and.returnValue(of(null));

refreshSessionRefreshTokenService
.refreshSessionWithRefreshTokens({ configId: 'configId1' }, [{ configId: 'configId1' }])
.subscribe(() => {
expect(spy).toHaveBeenCalled();
});
})
);
refreshSessionRefreshTokenService
.refreshSessionWithRefreshTokens({ configId: 'configId1' }, [{ configId: 'configId1' }])
.subscribe(() => {
expect(spy).toHaveBeenCalled();
});
}));

it(
'resetAuthorizationData and stopPeriodicTokenCheck in case of error',
waitForAsync(() => {
spyOn(flowsService, 'processRefreshToken').and.returnValue(throwError(() => new Error('error')));
const resetSilentRenewRunningSpy = spyOn(resetAuthDataService, 'resetAuthorizationData');
const stopPeriodicallyTokenCheckSpy = spyOn(intervalService, 'stopPeriodicTokenCheck');
it('resetAuthorizationData in case of error', waitForAsync(() => {
spyOn(flowsService, 'processRefreshToken').and.returnValue(throwError(() => new Error('error')));
const resetSilentRenewRunningSpy = spyOn(resetAuthDataService, 'resetAuthorizationData');

refreshSessionRefreshTokenService
.refreshSessionWithRefreshTokens({ configId: 'configId1' }, [{ configId: 'configId1' }])
.subscribe({
error: (err) => {
expect(resetSilentRenewRunningSpy).toHaveBeenCalled();
expect(stopPeriodicallyTokenCheckSpy).toHaveBeenCalled();
expect(err).toBeTruthy();
},
});
})
);
refreshSessionRefreshTokenService.refreshSessionWithRefreshTokens({ configId: 'configId1' }, [{ configId: 'configId1' }]).subscribe({
error: (err) => {
expect(resetSilentRenewRunningSpy).toHaveBeenCalled();
expect(err).toBeTruthy();
},
});
}));

it('finalize with stopPeriodicTokenCheck in case of error', fakeAsync(() => {
spyOn(flowsService, 'processRefreshToken').and.returnValue(throwError(() => new Error('error')));
const stopPeriodicallyTokenCheckSpy = spyOn(intervalService, 'stopPeriodicTokenCheck');

refreshSessionRefreshTokenService.refreshSessionWithRefreshTokens({ configId: 'configId1' }, [{ configId: 'configId1' }]).subscribe({
error: (err) => {
expect(err).toBeTruthy();
},
});
tick();
expect(stopPeriodicallyTokenCheckSpy).toHaveBeenCalled();
}));
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { catchError, finalize } from 'rxjs/operators';
import { OpenIdConfiguration } from '../config/openid-configuration';
import { CallbackContext } from '../flows/callback-context';
import { FlowsService } from '../flows/flows.service';
Expand All @@ -23,14 +23,16 @@ export class RefreshSessionRefreshTokenService {
customParamsRefresh?: { [key: string]: string | number | boolean }
): Observable<CallbackContext> {
this.loggerService.logDebug(config, 'BEGIN refresh session Authorize');
let refreshTokenFailed = false;

return this.flowsService.processRefreshToken(config, allConfigs, customParamsRefresh).pipe(
catchError((error) => {
this.intervalService.stopPeriodicTokenCheck();
this.resetAuthDataService.resetAuthorizationData(config, allConfigs);
refreshTokenFailed = true;

return throwError(() => new Error(error));
})
}),
finalize(() => refreshTokenFailed && this.intervalService.stopPeriodicTokenCheck())
);
}
}

0 comments on commit 56726e5

Please sign in to comment.