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

180 lines (172 loc) 6.68 kB
import { InjectionToken, Injectable } from '@angular/core'; import { __extends, __decorate, __read } 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'; var ESCALATION_WAIT_TIME = new InjectionToken('wait time'); var RetryWithEscalationMock = /** @class */ (function (_super) { __extends(RetryWithEscalationMock, _super); function RetryWithEscalationMock() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.tokenEscalationsRequested = []; _this.escalationSuccessful = true; return _this; } RetryWithEscalationMock.forTestBed = function () { return { provide: RetryWithEscalation, useFactory: retryWithEscalationFactory, }; }; RetryWithEscalationMock.prototype.retryWithEscalation = function (tokenName) { var _this = this; return function (source) { return source.pipe(retryWhen(function (errors) { var tries = 0; return errors.pipe(mergeMap(function (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 RetryWithEscalationMock; }(RetryWithEscalation)); /** * 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 var SCHEDULER = new InjectionToken('scheduler'); var TokenEscalatorMock = /** @class */ (function () { function TokenEscalatorMock(actions$, store, http, tokenExtractor) { this.actions$ = actions$; this.store = store; this.http = http; this.tokenExtractor = tokenExtractor; this.escalators = {}; this.requestsForToken = {}; } TokenEscalatorMock.forTestBed = function () { return { provide: TokenEscalator, useFactory: tokenEscalatorFactory, }; }; /** * deprecated Please use the correctly spelled function `simulateEscalationRequest` * * @param tokenName */ TokenEscalatorMock.prototype.simulateEsclationRequest = function (tokenName) { if (this.escalators[tokenName]) { this.escalators[tokenName].next({}); } else { throw new Error("No escalator for " + tokenName + " setup"); } }; TokenEscalatorMock.prototype.simulateEscalationRequest = function (tokenName) { if (this.escalators[tokenName]) { this.escalators[tokenName].next({}); } else { throw new Error("No escalator for " + tokenName + " setup"); } }; TokenEscalatorMock.prototype.escalateToken = function (_a) { var _this = this; var tokenName = _a.tokenName, authorizeUrl = _a.authorizeUrl, isDefaultToken = _a.isDefaultToken; var observ = new Observable(function (observer) { _this.escalators[tokenName] = observer; }).pipe(takeUntil(timer(10000)), withLatestFrom(authorizeUrl), switchMap(function (_a) { var _b = __read(_a, 2), action = _b[0], url = _b[1]; if (!_this.requestsForToken[tokenName]) { _this.requestsForToken[tokenName] = []; } _this.requestsForToken[tokenName].push(url); return of({ type: 'null op' }); })); observ.subscribe(function () { }); return observ; }; TokenEscalatorMock.ctorParameters = function () { return [ { type: Actions }, { type: Store }, { type: HttpClient }, { type: TokenExtractor } ]; }; TokenEscalatorMock = __decorate([ Injectable() ], TokenEscalatorMock); return TokenEscalatorMock; }()); /** * Return mock */ // eslint-disable-next-line prefer-arrow/prefer-arrow-functions function tokenEscalatorFactory() { return new TokenEscalatorMock(undefined, undefined, undefined, undefined); } var TOKEN_NOT_FOUND = new Error('Token Not found in response'); var TokenExtractorMock = /** @class */ (function (_super) { __extends(TokenExtractorMock, _super); function TokenExtractorMock() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.extractedTokens = []; return _this; } TokenExtractorMock.forTestBed = function () { return { provide: TokenExtractor, useFactory: tokenExtractorMockFactory, }; }; TokenExtractorMock.prototype.extractJwtToken = function (_a) { var _this = this; var tokenName = _a.tokenName, isDefaultToken = _a.isDefaultToken; return function (source) { return source.pipe(tap(function (request) { var token = _this.extractTokenFromResponse(request); if (token === '') { throw TOKEN_NOT_FOUND; } else { _this.extractedTokens.push(token); } })); }; }; TokenExtractorMock = __decorate([ Injectable() ], TokenExtractorMock); return TokenExtractorMock; }(TokenExtractor)); /** * 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