UNPKG

@terminus/ngx-tools

Version:

[![CircleCI][circle-badge]][circle-link] [![codecov][codecov-badge]][codecov-project] [![semantic-release][semantic-release-badge]][semantic-release] [![MIT License][license-image]][license-url] <br> [![NPM version][npm-version-image]][npm-url] [![Github

167 lines (159 loc) 5.57 kB
import { InjectionToken, Injectable } from '@angular/core'; import { __decorate } from 'tslib'; import { RetryWithEscalation, TokenEscalator, TokenExtractor } from '@terminus/ngx-tools/jwt'; import { throwError, Observable, timer, of } from 'rxjs'; import { retryWhen, mergeMap, takeUntil, withLatestFrom, switchMap, tap } from 'rxjs/operators'; import { HttpClient } from '@angular/common/http'; import { Actions } from '@ngrx/effects'; import { Store } from '@ngrx/store'; const ESCALATION_WAIT_TIME = new InjectionToken('wait time'); let RetryWithEscalationMock = class RetryWithEscalationMock extends RetryWithEscalation { constructor() { super(...arguments); this.tokenEscalationsRequested = []; this.escalationSuccessful = true; } static forTestBed() { return { provide: RetryWithEscalation, useFactory: retryWithEscalationFactory, }; } retryWithEscalation(tokenName) { return (source) => source.pipe(retryWhen((errors) => { let tries = 0; return errors.pipe(mergeMap(err => { if (tries > 0 || err instanceof Error || err.status !== 403) { return throwError(err); } tries += 1; this.tokenEscalationsRequested.push(tokenName); if (this.escalationSuccessful) { return 'complete'; } return throwError(new Error('Failed to escalate token')); })); })); } }; RetryWithEscalationMock = __decorate([ Injectable() ], RetryWithEscalationMock); /** * Return mock */ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions function retryWithEscalationFactory() { return new RetryWithEscalationMock(undefined, undefined, undefined, undefined); } // TODO: Scheduler is deprecated: Scheduler is an internal implementation detail of RxJS, and should not be used // directly. Rather, create your own class and implement SchedulerLike // eslint-disable-next-line deprecation/deprecation const SCHEDULER = new InjectionToken('scheduler'); let TokenEscalatorMock = class TokenEscalatorMock { constructor(actions$, store, http, tokenExtractor) { this.actions$ = actions$; this.store = store; this.http = http; this.tokenExtractor = tokenExtractor; this.escalators = {}; this.requestsForToken = {}; } static forTestBed() { return { provide: TokenEscalator, useFactory: tokenEscalatorFactory, }; } /** * deprecated Please use the correctly spelled function `simulateEscalationRequest` * * @param tokenName */ simulateEsclationRequest(tokenName) { if (this.escalators[tokenName]) { this.escalators[tokenName].next({}); } else { throw new Error(`No escalator for ${tokenName} setup`); } } simulateEscalationRequest(tokenName) { if (this.escalators[tokenName]) { this.escalators[tokenName].next({}); } else { throw new Error(`No escalator for ${tokenName} setup`); } } escalateToken({ tokenName, authorizeUrl, isDefaultToken }) { const observ = new Observable((observer) => { this.escalators[tokenName] = observer; }).pipe(takeUntil(timer(10000)), withLatestFrom(authorizeUrl), switchMap(([action, url]) => { if (!this.requestsForToken[tokenName]) { this.requestsForToken[tokenName] = []; } this.requestsForToken[tokenName].push(url); return of({ type: 'null op' }); })); observ.subscribe(() => { }); return observ; } }; TokenEscalatorMock.ctorParameters = () => [ { type: Actions }, { type: Store }, { type: HttpClient }, { type: TokenExtractor } ]; TokenEscalatorMock = __decorate([ Injectable() ], TokenEscalatorMock); /** * Return mock */ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions function tokenEscalatorFactory() { return new TokenEscalatorMock(undefined, undefined, undefined, undefined); } const TOKEN_NOT_FOUND = new Error('Token Not found in response'); let TokenExtractorMock = class TokenExtractorMock extends TokenExtractor { constructor() { super(...arguments); this.extractedTokens = []; } static forTestBed() { return { provide: TokenExtractor, useFactory: tokenExtractorMockFactory, }; } extractJwtToken({ tokenName, isDefaultToken }) { return (source) => source.pipe(tap(request => { const token = this.extractTokenFromResponse(request); if (token === '') { throw TOKEN_NOT_FOUND; } else { this.extractedTokens.push(token); } })); } }; TokenExtractorMock = __decorate([ Injectable() ], TokenExtractorMock); /** * Return mock */ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions function tokenExtractorMockFactory() { return new TokenExtractorMock(undefined); } /** * Generated bundle index. Do not edit. */ export { ESCALATION_WAIT_TIME, RetryWithEscalationMock, SCHEDULER, TOKEN_NOT_FOUND, TokenEscalatorMock, TokenExtractorMock, retryWithEscalationFactory, tokenEscalatorFactory, tokenExtractorMockFactory }; //# sourceMappingURL=terminus-ngx-tools-jwt-testing.js.map