Skip to content

Commit

Permalink
test: add extra test for detecting non-admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Feb 21, 2022
1 parent 561c225 commit fe45f91
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/authentication/protected-routes.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ export const isAdminRoute = (url: string, adminRootPath: string): boolean => {
.map((route) => convertToExpressRoute(route.path))
.filter((route) => route !== "");

let urlWithoutRootPath = url;
if (adminRootPath !== '/') {
urlWithoutRootPath = url.replace(adminRootPath, '');
if (!urlWithoutRootPath.startsWith('/')) {
urlWithoutRootPath = `/${urlWithoutRootPath}`
let urlWithoutAdminRootPath = url;
if (adminRootPath !== "/") {
urlWithoutAdminRootPath = url.replace(adminRootPath, "");
if (!urlWithoutAdminRootPath.startsWith("/")) {
urlWithoutAdminRootPath = `/${urlWithoutAdminRootPath}`;
}
}

const isAdminRootUrl = url === adminRootPath || urlWithoutRootPath === '/';
const isAdminRootUrl = url === adminRootPath;

return (
isAdminRootUrl ||
!!adminRoutes.find((route) => pathToRegexp(route).test(urlWithoutRootPath))
!!adminRoutes.find((route) =>
pathToRegexp(route).test(urlWithoutAdminRootPath)
)
);
};

Expand Down
6 changes: 5 additions & 1 deletion test/protected-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ describe("Protected routes", () => {
});
});

it("should detect non-admin routes", () => {
it("should detect non-admin routes when root path is /", () => {
expect(isAdminRoute("/api/my-endpoint", "/")).toBeFalsy();
});

it("should detect non-admin routes when root path is not /", () => {
expect(isAdminRoute("/admin/api/my-endpoint", "/admin")).toBeFalsy();
});
});
});

0 comments on commit fe45f91

Please sign in to comment.