Skip to content

Commit

Permalink
fix(platform-middlewares): improve inheritance support of UseAuth/Aut…
Browse files Browse the repository at this point in the history
…hOptions/Authorize decorator
  • Loading branch information
Romakita committed Nov 1, 2024
1 parent a1c8a1e commit f296328
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/platform/platform-cache/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default defineConfig(
...presets.test.coverage,
thresholds: {
statements: 100,
branches: 98.92,
branches: 98.91,
functions: 100,
lines: 100
}
}
}
}
);
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DecoratorParameters,
decoratorTypeOf,
DecoratorTypes,
Store,
Type,
UnsupportedDecoratorType
} from "@tsed/core";
Expand Down Expand Up @@ -41,7 +42,12 @@ export function AuthOptions(guardAuth: Type<any>, options: Record<string, unknow
})(...(args as DecoratorMethodParameters));

case DecoratorTypes.CLASS:
decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
decorateMethodsOf(args[0], (_: any, property: string) => {
Store.fromMethod(args[0], property).merge(guardAuth, options, true);
});
// methodsOf(args[0]).forEach(({propertyKey}) => {
//
// });
break;

default:
Expand Down
11 changes: 10 additions & 1 deletion packages/platform/platform-middlewares/src/decorators/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ export function UseAuth(guardAuth: Type<any>, options: Record<string, unknown> =
)(...args);

case DecoratorTypes.CLASS:
decorateMethodsOf(args[0], UseAuth(guardAuth, options));
decorateMethodsOf(
args[0],
StoreFn((store: Store) => {
if (!store.has(guardAuth)) {
return UseBefore(guardAuth);
}
})
);

AuthOptions(guardAuth, options)(...args);
break;

default:
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/platform-router/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default defineConfig(
...presets.test.coverage,
thresholds: {
statements: 100,
branches: 97.47,
branches: 97.45,
functions: 100,
lines: 100
}
}
}
}
);
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`@Authorize > should support inheritance 1`] = `
{
"paths": {
"/admin/allEntries": {
"get": {
"operationId": "abstractAdminControllerGetAllEntries",
"parameters": [],
"responses": {
"200": {
"description": "Success",
},
},
"tags": [
"AdminController",
],
},
},
},
"tags": [
{
"name": "AdminController",
},
],
}
`;

exports[`@Authorize > should support inheritance 2`] = `
{
"paths": {
"/admin/bucket/allEntries": {
"get": {
"operationId": "abstractAdminControllerGetAllEntries",
"parameters": [],
"responses": {
"200": {
"description": "Success",
},
},
"tags": [
"BucketAdminController",
],
},
},
},
"tags": [
{
"name": "BucketAdminController",
},
],
}
`;
42 changes: 42 additions & 0 deletions packages/security/passport/src/decorators/authorize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import {Store} from "@tsed/core";
import {Controller, inject} from "@tsed/di";
import {Middleware, UseBefore} from "@tsed/platform-middlewares";
import {Get, getSpec} from "@tsed/schema";

import {Authorize, PassportMiddleware} from "../index.js";

@Middleware()
class AuthoriseBucket {
use() {}
}

describe("@Authorize", () => {
it("should store data", () => {
class Test {
Expand Down Expand Up @@ -51,4 +59,38 @@ describe("@Authorize", () => {
protocol: "local"
});
});
it("should support inheritance", async () => {
abstract class AbstractAdminController {
@Get("/allEntries")
getAllEntries(): Promise<unknown> {
return Promise.resolve({});
}
}

@Authorize("loginAuthProvider")
@Controller("/admin")
class AdminController extends AbstractAdminController {}

@Controller("/admin/bucket")
@UseBefore(AuthoriseBucket)
class BucketAdminController extends AbstractAdminController {}

expect(getSpec(AdminController)).toMatchSnapshot();
expect(getSpec(BucketAdminController)).toMatchSnapshot();
expect(Reflect.getOwnPropertyDescriptor(AdminController.prototype, "getAllEntries")).toBeDefined();
expect(Reflect.getOwnPropertyDescriptor(BucketAdminController.prototype, "getAllEntries")).not.toBeDefined();

const adminController = inject(AdminController);

expect(await adminController.getAllEntries()).toEqual({});

expect(Store.fromMethod(BucketAdminController, "getAllEntries").get(PassportMiddleware)).toEqual(undefined);
expect(Store.fromMethod(AbstractAdminController, "getAllEntries").get(PassportMiddleware)).toEqual(undefined);
expect(Store.fromMethod(AdminController, "getAllEntries").get(PassportMiddleware)).toEqual({
method: "authorize",
options: {},
originalUrl: true,
protocol: "loginAuthProvider"
});
});
});

0 comments on commit f296328

Please sign in to comment.