pull/18058/head^2
Sinan997 1 year ago
parent 0d18e84d33
commit 20b561dfcd

@ -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]);
};
}
}

@ -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<PermissionGuard>;
@ -18,6 +29,10 @@ describe('PermissionGuard', () => {
let httpErrorReporter: SpyObject<HttpErrorReporterService>;
let permissionService: SpyObject<PermissionService>;
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<PermissionService>;
let httpErrorReporter: SpyObject<HttpErrorReporterService>;
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<true> if RoutesService does not have requiredPolicy for given URL', async () => {
await RouterTestingHarness.create('/zibzib');
expect(TestBed.inject(Router).url).toEqual('/zibzib');
});
});

@ -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<AbpOAuthGuard>;
let guard : AbpOAuthGuard;
const route = createSpyObject<ActivatedRouteSnapshot>(ActivatedRouteSnapshot)
const state = createSpyObject<RouterStateSnapshot>(RouterStateSnapshot)
let guard: AbpOAuthGuard;
const route = createSpyObject<ActivatedRouteSnapshot>(ActivatedRouteSnapshot);
const state = createSpyObject<RouterStateSnapshot>(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<OAuthService>;
let authService: SpyObject<AuthService>;
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('/');
});
});

Loading…
Cancel
Save