diff --git a/npm/ng-packs/packages/core/src/lib/tests/initial-utils.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/initial-utils.spec.ts index 12d27544f8..b28447be99 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/initial-utils.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/initial-utils.spec.ts @@ -1,18 +1,17 @@ import { Component, Injector } from '@angular/core'; import { createComponentFactory, Spectator } from '@ngneat/spectator/jest'; -import { OAuthService } from 'angular-oauth2-oidc'; import { of } from 'rxjs'; import { AbpApplicationConfigurationService } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/abp-application-configuration.service'; import { ApplicationConfigurationDto } from '../proxy/volo/abp/asp-net-core/mvc/application-configurations/models'; import { SessionStateService } from '../services/session-state.service'; import { EnvironmentService } from '../services/environment.service'; -import { AuthService } from '../services/auth.service'; +import { AuthService } from '../abstracts/auth.service'; import { ConfigStateService } from '../services/config-state.service'; -import * as AuthFlowStrategy from '../strategies/auth-flow.strategy'; import { CORE_OPTIONS } from '../tokens/options.token'; -import { checkAccessToken, getInitialData, localeInitializer } from '../utils/initial-utils'; +import { getInitialData, localeInitializer } from '../utils/initial-utils'; import * as environmentUtils from '../utils/environment-utils'; import * as multiTenancyUtils from '../utils/multi-tenancy-utils'; +import { RestService } from '../services/rest.service'; const environment = { oAuthConfig: { issuer: 'test' } }; @@ -31,8 +30,8 @@ describe('InitialUtils', () => { ConfigStateService, AbpApplicationConfigurationService, AuthService, - OAuthService, SessionStateService, + RestService, ], providers: [ { @@ -52,6 +51,7 @@ describe('InitialUtils', () => { const environmentService = spectator.inject(EnvironmentService); const configStateService = spectator.inject(ConfigStateService); const sessionStateService = spectator.inject(SessionStateService); + const parseTenantFromUrlSpy = jest.spyOn(multiTenancyUtils, 'parseTenantFromUrl'); const getRemoteEnvSpy = jest.spyOn(environmentUtils, 'getRemoteEnv'); parseTenantFromUrlSpy.mockReturnValue(Promise.resolve()); @@ -82,20 +82,6 @@ describe('InitialUtils', () => { }); }); - describe('#checkAccessToken', () => { - test('should call logOut fn of OAuthService when token is valid and current user not found', async () => { - const injector = spectator.inject(Injector); - const injectorSpy = jest.spyOn(injector, 'get'); - const clearOAuthStorageSpy = jest.spyOn(AuthFlowStrategy, 'clearOAuthStorage'); - - injectorSpy.mockReturnValueOnce({ getDeep: () => false }); - injectorSpy.mockReturnValueOnce({ hasValidAccessToken: () => true }); - - checkAccessToken(injector); - expect(clearOAuthStorageSpy).toHaveBeenCalled(); - }); - }); - describe('#localeInitializer', () => { test('should resolve registerLocale', async () => { const injector = spectator.inject(Injector); diff --git a/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts b/npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts similarity index 81% rename from npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts rename to npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts index 151e0ec639..62c42ae6aa 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts +++ b/npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts @@ -1,13 +1,13 @@ import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { OAuthService } from 'angular-oauth2-oidc'; -import { AuthGuard } from '../guards/auth.guard'; -import { AuthService } from '../services/auth.service'; +import { AbpOAuthGuard } from '../guards/oauth.guard'; +import { AuthService } from '@Abp/ng.core'; describe('AuthGuard', () => { - let spectator: SpectatorService; - let guard: AuthGuard; + let spectator: SpectatorService; + let guard: AbpOAuthGuard; const createService = createServiceFactory({ - service: AuthGuard, + service: AbpOAuthGuard, mocks: [OAuthService, AuthService], }); diff --git a/npm/ng-packs/packages/oauth/src/lib/tests/initial-utils.spec.ts b/npm/ng-packs/packages/oauth/src/lib/tests/initial-utils.spec.ts new file mode 100644 index 0000000000..31fa3dfcf7 --- /dev/null +++ b/npm/ng-packs/packages/oauth/src/lib/tests/initial-utils.spec.ts @@ -0,0 +1,110 @@ +import { Component, Injector } from '@angular/core'; +import { createComponentFactory, Spectator } from '@ngneat/spectator/jest'; +import { OAuthService } from 'angular-oauth2-oidc'; + +import { + CORE_OPTIONS, + EnvironmentService, + AuthService, + ConfigStateService, + AbpApplicationConfigurationService, + SessionStateService, + ApplicationConfigurationDto, +} from '@abp/ng.core'; +import * as clearOAuthStorageDefault from '../utils/clear-o-auth-storage'; +import { checkAccessToken, initFactory } from '../utils/init-factory'; +import { of } from 'rxjs'; + +const environment = { oAuthConfig: { issuer: 'test' } }; + +@Component({ + selector: 'abp-dummy', + template: '', +}) +export class DummyComponent {} + +describe('InitialUtils', () => { + let spectator: Spectator; + const createComponent = createComponentFactory({ + component: DummyComponent, + mocks: [ + EnvironmentService, + ConfigStateService, + AbpApplicationConfigurationService, + AuthService, + OAuthService, + SessionStateService, + ], + providers: [ + { + provide: CORE_OPTIONS, + useValue: { + environment, + registerLocaleFn: () => Promise.resolve(), + skipGetAppConfiguration: false, + }, + }, + ], + }); + + beforeEach(() => (spectator = createComponent())); + + describe('#getInitialData', () => { + let mockInjector; + let configStateService; + let authService; + beforeEach(() => { + mockInjector = { + get: spectator.inject, + }; + configStateService = spectator.inject(ConfigStateService); + authService = spectator.inject(AuthService); + }); + + test('should called configStateService.refreshAppState', async () => { + const configRefreshAppStateSpy = jest.spyOn(configStateService, 'refreshAppState'); + const appConfigRes = { + currentTenant: { id: 'test', name: 'testing' }, + } as ApplicationConfigurationDto; + + configRefreshAppStateSpy.mockReturnValue(of(appConfigRes)); + + await initFactory(mockInjector)(); + + expect(configRefreshAppStateSpy).toHaveBeenCalled(); + }); + }); + + describe('#checkAccessToken', () => { + let injector; + let injectorSpy; + let clearOAuthStorageSpy; + beforeEach(() => { + injector = spectator.inject(Injector); + injectorSpy = jest.spyOn(injector, 'get'); + clearOAuthStorageSpy = jest.spyOn(clearOAuthStorageDefault, 'clearOAuthStorage'); + clearOAuthStorageSpy.mockReset(); + }); + + test('should call logOut fn of OAuthService when token is valid and current user not found', async () => { + injectorSpy.mockReturnValueOnce({ getDeep: () => false }); + injectorSpy.mockReturnValueOnce({ hasValidAccessToken: () => true }); + checkAccessToken(injector); + expect(clearOAuthStorageSpy).toHaveBeenCalled(); + }); + + test('should not call logOut fn of OAuthService when token is invalid', async () => { + injectorSpy.mockReturnValueOnce({ getDeep: () => true }); + injectorSpy.mockReturnValueOnce({ hasValidAccessToken: () => false }); + checkAccessToken(injector); + expect(clearOAuthStorageSpy).not.toHaveBeenCalled(); + }); + + test('should not call logOut fn of OAuthService when token is valid but user is not found', async () => { + injectorSpy.mockReturnValueOnce({ getDeep: () => true }); + injectorSpy.mockReturnValueOnce({ hasValidAccessToken: () => true }); + checkAccessToken(injector); + expect(clearOAuthStorageSpy).not.toHaveBeenCalled(); + }); + }); +});