From 08e6b4ee348b8d73b1e3c0ebf56d01661ad6edf3 Mon Sep 17 00:00:00 2001 From: RehanY147 Date: Wed, 10 Jul 2024 18:58:10 +0500 Subject: [PATCH] NAS-128920: Adding empty class for missing `ws.service.ts` and `auth.service.ts` (#10305) --- src/app/services/auth/auth.service.ts | 2 +- src/app/services/ws.service.ts | 2 +- src/setup-jest.ts | 16 +++++++++++++++- src/test-utils/empty-auth.service.ts | 18 ++++++++++++++++++ src/test-utils/empty-ws.service.ts | 13 +++++++++++++ src/test-utils/missing-injection-factories.ts | 11 +++++++++++ 6 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/test-utils/empty-auth.service.ts create mode 100644 src/test-utils/empty-ws.service.ts create mode 100644 src/test-utils/missing-injection-factories.ts diff --git a/src/app/services/auth/auth.service.ts b/src/app/services/auth/auth.service.ts index 748efb925ac..1d210ca95b8 100644 --- a/src/app/services/auth/auth.service.ts +++ b/src/app/services/auth/auth.service.ts @@ -47,7 +47,7 @@ export class AuthService { * time of a token generated with auth.generate_token. The 10 seconds * difference is to allow for delays in request send/receive */ - readonly tokenRegenerationTimeMillis = 290 * 1000; + private readonly tokenRegenerationTimeMillis = 290 * 1000; private latestTokenGenerated$ = new ReplaySubject(1); get authToken$(): Observable { diff --git a/src/app/services/ws.service.ts b/src/app/services/ws.service.ts index c1422bdda4d..d5c4c63aaac 100644 --- a/src/app/services/ws.service.ts +++ b/src/app/services/ws.service.ts @@ -36,7 +36,7 @@ import { WebSocketConnectionService } from 'app/services/websocket-connection.se }) export class WebSocketService { private readonly eventSubscribers = new Map>(); - clearSubscriptions$ = new Subject(); + readonly clearSubscriptions$ = new Subject(); constructor( protected router: Router, diff --git a/src/setup-jest.ts b/src/setup-jest.ts index 33d9607fc48..186db1bd3c8 100644 --- a/src/setup-jest.ts +++ b/src/setup-jest.ts @@ -30,7 +30,11 @@ import { import failOnConsole from 'jest-fail-on-console'; import { MockProvider } from 'ng-mocks'; import { TranslateMessageFormatCompiler } from 'ngx-translate-messageformat-compiler'; -import { Observable } from 'rxjs'; +import { + Observable, +} from 'rxjs'; +import { EmptyAuthService } from 'test-utils/empty-auth.service'; +import { EmptyWebsocketService } from 'test-utils/empty-ws.service'; import { IcuMissingTranslationHandler } from 'app/core/classes/icu-missing-translation-handler'; import { CommonDirectivesModule } from 'app/directives/common/common-directives.module'; import { WINDOW } from 'app/helpers/window.helper'; @@ -40,7 +44,9 @@ import { AppLoaderModule } from 'app/modules/loader/app-loader.module'; import { AppLoaderService } from 'app/modules/loader/app-loader.service'; import { SnackbarModule } from 'app/modules/snackbar/snackbar.module'; import { TestIdModule } from 'app/modules/test-id/test-id.module'; +import { AuthService } from 'app/services/auth/auth.service'; import { ErrorHandlerService } from 'app/services/error-handler.service'; +import { WebSocketService } from 'app/services/ws.service'; failOnConsole(); @@ -113,6 +119,14 @@ defineGlobalsInjections({ mockProvider(ErrorHandlerService, { catchError: () => (source$: Observable) => source$, }), + { + provide: AuthService, + useClass: EmptyAuthService, + }, + { + provide: WebSocketService, + useClass: EmptyWebsocketService, + }, ], }); diff --git a/src/test-utils/empty-auth.service.ts b/src/test-utils/empty-auth.service.ts new file mode 100644 index 00000000000..920f4b17caf --- /dev/null +++ b/src/test-utils/empty-auth.service.ts @@ -0,0 +1,18 @@ +import { getMissingInjectionErrorFactory, getMissingInjectionErrorObservable } from 'test-utils/missing-injection-factories'; +import { AuthService } from 'app/services/auth/auth.service'; + +export class EmptyAuthService { + readonly authToken$ = getMissingInjectionErrorObservable(AuthService.name); + readonly isAuthenticated$ = getMissingInjectionErrorObservable(AuthService.name); + readonly user$ = getMissingInjectionErrorObservable(AuthService.name); + readonly isSysAdmin$ = getMissingInjectionErrorObservable(AuthService.name); + readonly userTwoFactorConfig$ = getMissingInjectionErrorObservable(AuthService.name); + getGlobalTwoFactorConfig = getMissingInjectionErrorFactory(AuthService.name); + globalTwoFactorConfigUpdated = getMissingInjectionErrorFactory(AuthService.name); + clearAuthToken = getMissingInjectionErrorFactory(AuthService.name); + login = getMissingInjectionErrorFactory(AuthService.name); + hasRole = getMissingInjectionErrorFactory(AuthService.name); + logout = getMissingInjectionErrorFactory(AuthService.name); + refreshUser = getMissingInjectionErrorFactory(AuthService.name); + loginWithToken = getMissingInjectionErrorFactory(AuthService.name); +} diff --git a/src/test-utils/empty-ws.service.ts b/src/test-utils/empty-ws.service.ts new file mode 100644 index 00000000000..62bc64b9ef2 --- /dev/null +++ b/src/test-utils/empty-ws.service.ts @@ -0,0 +1,13 @@ +import { getMissingInjectionErrorFactory, getMissingInjectionErrorObservable } from 'test-utils/missing-injection-factories'; +import { WebSocketService } from 'app/services/ws.service'; + +export class EmptyWebsocketService { + readonly clearSubscriptions$ = getMissingInjectionErrorObservable(WebSocketService.name); + call = getMissingInjectionErrorFactory(WebSocketService.name); + job = getMissingInjectionErrorFactory(WebSocketService.name); + callAndSubscribe = getMissingInjectionErrorFactory(WebSocketService.name); + startJob = getMissingInjectionErrorFactory(WebSocketService.name); + subscribe = getMissingInjectionErrorFactory(WebSocketService.name); + subscribeToLogs = getMissingInjectionErrorFactory(WebSocketService.name); + clearSubscriptions = getMissingInjectionErrorFactory(WebSocketService.name); +} diff --git a/src/test-utils/missing-injection-factories.ts b/src/test-utils/missing-injection-factories.ts new file mode 100644 index 00000000000..4a5b27b49fd --- /dev/null +++ b/src/test-utils/missing-injection-factories.ts @@ -0,0 +1,11 @@ +import { Observable, throwError } from 'rxjs'; + +export function getMissingInjectionErrorFactory(className: string): () => Observable { + return function getMissingInjectionError(): Observable { + return getMissingInjectionErrorObservable(className); + }; +} + +export function getMissingInjectionErrorObservable(className: string): Observable { + return throwError(() => new Error(`${className} injection not provided`)); +}