Skip to content

Commit

Permalink
fix(core): generate valid regex in guard also for wildcard routes (#2231
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sleidig authored Feb 16, 2024
1 parent da2066d commit 592b534
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TestBed } from "@angular/core/testing";
import { Router, Routes } from "@angular/router";
import { AbstractPermissionGuard } from "./abstract-permission.guard";
import { Injectable } from "@angular/core";
import { DynamicComponentConfig } from "../../config/dynamic-components/dynamic-component-config.interface";

@Injectable()
class TestPermissionGuard extends AbstractPermissionGuard {
constructor(router: Router) {
super(router);
}

protected async canAccessRoute(
routeData: DynamicComponentConfig,
): Promise<boolean> {
return routeData?.config;
}
}

describe("EntityPermissionGuard", () => {
let guard: TestPermissionGuard;

let testRoutes: Routes;

beforeEach(() => {
testRoutes = [{ path: "**", data: { config: true } }];

TestBed.configureTestingModule({
providers: [
TestPermissionGuard,
{ provide: Router, useValue: { config: testRoutes } },
],
});
guard = TestBed.inject(TestPermissionGuard);
});

it("should be created", () => {
expect(guard).toBeTruthy();
});

it("should get route config also for '**' path", async () => {
const result = await guard.checkRoutePermissions("url");

expect(result).toBeTrue();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export abstract class AbstractPermissionGuard implements CanActivate {

function isPathMatch(genericPath: string, path: string) {
const routeRegex = genericPath
.replace(/\*/g, ".*") // allow for wildcard routes in regex
.split("/")
// replace params with wildcard regex
.map((part) => (part.startsWith(":") ? "[^/]*" : part))
Expand Down

0 comments on commit 592b534

Please sign in to comment.