From 20b561dfcd06a83560cc2152c4df6157d730829f Mon Sep 17 00:00:00 2001 From: Sinan997 Date: Fri, 3 Nov 2023 17:01:33 +0300 Subject: [PATCH] add tests --- .../services/http-error-reporter.service.ts | 4 +- .../src/lib/tests/permission.guard.spec.ts | 117 +++++++++++++++--- .../oauth/src/lib/tests/auth.guard.spec.ts | 65 +++++++++- 3 files changed, 165 insertions(+), 21 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/http-error-reporter.service.ts b/npm/ng-packs/packages/core/src/lib/services/http-error-reporter.service.ts index 65d063f82d..f43c114c8b 100644 --- a/npm/ng-packs/packages/core/src/lib/services/http-error-reporter.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/http-error-reporter.service.ts @@ -19,8 +19,8 @@ export class HttpErrorReporterService { return this._errors$.value; } - reportError = (error: HttpErrorResponse) => { + reportError(error: HttpErrorResponse) { this._reporter$.next(error); this._errors$.next([...this.errors, error]); - }; + } } diff --git a/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts index 372f3434f3..6464f76a66 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts @@ -1,15 +1,26 @@ import { APP_BASE_HREF } from '@angular/common'; import { HttpClientTestingModule } from '@angular/common/http/testing'; import { Component } from '@angular/core'; -import { RouterModule } from '@angular/router'; -import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; +import { provideRouter, Route, Router, RouterModule } from '@angular/router'; +import { + createServiceFactory, + createSpyObject, + SpectatorService, + SpyObject, +} from '@ngneat/spectator/jest'; import { of } from 'rxjs'; -import { PermissionGuard } from '../guards/permission.guard'; +import { permissionGuard, PermissionGuard } from '../guards/permission.guard'; import { HttpErrorReporterService } from '../services/http-error-reporter.service'; import { PermissionService } from '../services/permission.service'; import { RoutesService } from '../services/routes.service'; import { CORE_OPTIONS } from '../tokens/options.token'; import { IncludeLocalizationResourcesProvider } from '../providers'; +import { TestBed } from '@angular/core/testing'; +import { RouterTestingHarness } from '@angular/router/testing'; +import { OAuthService } from 'angular-oauth2-oidc'; +import { OTHERS_GROUP } from '../tokens'; +import { SORT_COMPARE_FUNC, compareFuncFactory } from '../tokens/compare-func.token'; +import { CoreModule } from '../core.module'; describe('PermissionGuard', () => { let spectator: SpectatorService; @@ -18,6 +29,10 @@ describe('PermissionGuard', () => { let httpErrorReporter: SpyObject; let permissionService: SpyObject; + const mockOAuthService = { + hasValidAccessToken: jest.fn().mockReturnValue(true), + }; + @Component({ template: '' }) class DummyComponent {} @@ -27,25 +42,25 @@ describe('PermissionGuard', () => { declarations: [DummyComponent], imports: [ HttpClientTestingModule, - RouterModule.forRoot( - [ - { - path: 'test', - component: DummyComponent, - data: { - requiredPolicy: 'TestPolicy', - }, + RouterModule.forRoot([ + { + path: 'test', + component: DummyComponent, + data: { + requiredPolicy: 'TestPolicy', }, - ], - {}, - ), + }, + ]), ], providers: [ { provide: APP_BASE_HREF, useValue: '/', }, + { provide: OAuthService, useValue: mockOAuthService }, { provide: CORE_OPTIONS, useValue: { skipGetAppConfiguration: true } }, + { provide: OTHERS_GROUP, useValue: 'AbpUi::OthersGroup' }, + { provide: SORT_COMPARE_FUNC, useValue: compareFuncFactory }, IncludeLocalizationResourcesProvider, ], }); @@ -108,3 +123,77 @@ describe('PermissionGuard', () => { }); }); }); + +@Component({ standalone: true, template: '' }) +class DummyComponent {} +describe('authGuard', () => { + let permissionService: SpyObject; + let httpErrorReporter: SpyObject; + + const mockOAuthService = { + hasValidAccessToken: jest.fn().mockReturnValue(true), + }; + + let routes: Route[] = [ + { + path: 'dummy', + component: DummyComponent, + canActivate: [permissionGuard], + data: { + requiredPolicy: 'TestPolicy', + }, + }, + { + path: 'zibzib', + component: DummyComponent, + canActivate: [permissionGuard], + }, + ]; + + beforeEach(() => { + httpErrorReporter = createSpyObject(HttpErrorReporterService); + permissionService = createSpyObject(PermissionService); + + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule, CoreModule.forRoot()], + providers: [ + { provide: OAuthService, useValue: mockOAuthService }, + { provide: PermissionService, useValue: permissionService }, + { provide: HttpErrorReporterService, useValue: httpErrorReporter }, + provideRouter(routes), + ], + }); + }); + + it('should return true when the grantedPolicy is true', async () => { + permissionService.getGrantedPolicy$.andReturn(of(true)); + await RouterTestingHarness.create('/dummy'); + + expect(TestBed.inject(Router).url).toEqual('/dummy'); + expect(httpErrorReporter.reportError).not.toHaveBeenCalled(); + }); + + it('should return false and report an error when the grantedPolicy is false', async () => { + permissionService.getGrantedPolicy$.andReturn(of(false)); + await RouterTestingHarness.create('/dummy'); + + expect(TestBed.inject(Router).url).not.toEqual('/dummy'); + expect(httpErrorReporter.reportError).toHaveBeenCalled(); + expect(httpErrorReporter.reportError).toBeCalledWith({ status: 403 }); + }); + + it('should check the requiredPolicy from RoutesService', async () => { + permissionService.getGrantedPolicy$.mockImplementation(policy => { + return of(policy === 'TestPolicy'); + }); + await RouterTestingHarness.create('/dummy'); + + expect(TestBed.inject(Router).url).toEqual('/dummy'); + expect(httpErrorReporter.reportError).not.toHaveBeenCalled(); + }); + + it('should return Observable if RoutesService does not have requiredPolicy for given URL', async () => { + await RouterTestingHarness.create('/zibzib'); + expect(TestBed.inject(Router).url).toEqual('/zibzib'); + }); +}); diff --git a/npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts b/npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts index feca76666e..47f57bc50c 100644 --- a/npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts +++ b/npm/ng-packs/packages/oauth/src/lib/tests/auth.guard.spec.ts @@ -1,14 +1,25 @@ import { createServiceFactory, SpectatorService, createSpyObject } from '@ngneat/spectator/jest'; import { OAuthService } from 'angular-oauth2-oidc'; -import { AbpOAuthGuard } from '../guards/oauth.guard'; +import { AbpOAuthGuard, abpOAuthGuard } from '../guards/oauth.guard'; import { AuthService } from '@abp/ng.core'; -import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; +import { + ActivatedRouteSnapshot, + Route, + Router, + RouterStateSnapshot, + provideRouter, +} from '@angular/router'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { Component } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { RouterTestingHarness } from '@angular/router/testing'; +import { SpyObject } from '@ngneat/spectator'; describe('AuthGuard', () => { let spectator: SpectatorService; - let guard : AbpOAuthGuard; - const route = createSpyObject(ActivatedRouteSnapshot) - const state = createSpyObject(RouterStateSnapshot) + let guard: AbpOAuthGuard; + const route = createSpyObject(ActivatedRouteSnapshot); + const state = createSpyObject(RouterStateSnapshot); const createService = createServiceFactory({ service: AbpOAuthGuard, @@ -34,3 +45,47 @@ describe('AuthGuard', () => { expect(navigateToLoginSpy).toHaveBeenCalled(); }); }); + +@Component({ standalone: true, template: '' }) +class DummyComponent {} +describe('authGuard', () => { + let oAuthService: SpyObject; + let authService: SpyObject; + let routes: Route[] = [ + { + path: 'dummy', + canActivate: [abpOAuthGuard], + component: DummyComponent, + }, + ]; + + beforeEach(() => { + authService = createSpyObject(AuthService); + oAuthService = createSpyObject(OAuthService); + + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + providers: [ + { provide: AuthService, useValue: authService }, + { provide: OAuthService, useValue: oAuthService }, + provideRouter(routes), + ], + }); + }); + + it('should move to the dummy route', async () => { + oAuthService.hasValidAccessToken.andReturn(true); + await RouterTestingHarness.create('/dummy'); + + expect(TestBed.inject(Router).url).toEqual('/dummy'); + }); + + it("should'nt move to the dummy route", async () => { + oAuthService.hasValidAccessToken.andReturn(false); + await RouterTestingHarness.create('/dummy'); + + expect(authService.navigateToLogin).toHaveBeenCalled(); + expect(TestBed.inject(Router).url).not.toEqual('/dummy'); + expect(TestBed.inject(Router).url).toEqual('/'); + }); +});