-
Notifications
You must be signed in to change notification settings - Fork 1
Fix for wallet-admin switch for admin vs. engagement approver #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import { Role } from 'src/core/auth/auth.constants'; | ||
| import { AccessControlService } from './access-control.service'; | ||
| import { RoleAccessProvider } from './role-access.interface'; | ||
|
|
||
| describe('AccessControlService', () => { | ||
| let service: AccessControlService; | ||
|
|
||
| beforeEach(() => { | ||
| service = new AccessControlService(); | ||
| }); | ||
|
|
||
| it('skips engagement approver filters when payment admin role is also present', async () => { | ||
| const paymentAdminApplyFilter = jest | ||
| .fn() | ||
| .mockImplementation((_userId, req: Record<string, unknown>) => | ||
| Promise.resolve({ | ||
| ...req, | ||
| admin: true, | ||
| }), | ||
| ); | ||
| const engagementApproverApplyFilter = jest | ||
| .fn() | ||
| .mockImplementation((_userId, req: Record<string, unknown>) => | ||
| Promise.resolve({ | ||
| ...req, | ||
| category: 'ENGAGEMENT_PAYMENT', | ||
| }), | ||
| ); | ||
| const paymentAdminProvider: RoleAccessProvider<Record<string, unknown>> = { | ||
| roleName: Role.PaymentAdmin, | ||
| applyFilter: paymentAdminApplyFilter, | ||
| }; | ||
| const engagementApproverProvider: RoleAccessProvider< | ||
| Record<string, unknown> | ||
| > = { | ||
| roleName: Role.EngagementPaymentApprover, | ||
| applyFilter: engagementApproverApplyFilter, | ||
| }; | ||
|
|
||
| service.register(paymentAdminProvider); | ||
| service.register(engagementApproverProvider); | ||
|
|
||
| const result = await service.applyFilters<Record<string, unknown>>( | ||
| '88770025', | ||
| [Role.PaymentAdmin, Role.EngagementPaymentApprover], | ||
| { type: 'PAYMENT' }, | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| admin: true, | ||
| type: 'PAYMENT', | ||
| }); | ||
| expect(paymentAdminApplyFilter).toHaveBeenCalledTimes(1); | ||
| expect(engagementApproverApplyFilter).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('applies engagement approver filters when payment admin role is absent', async () => { | ||
| const engagementApproverApplyFilter = jest | ||
| .fn() | ||
| .mockImplementation((_userId, req: Record<string, unknown>) => | ||
| Promise.resolve({ | ||
| ...req, | ||
| category: 'ENGAGEMENT_PAYMENT', | ||
| }), | ||
| ); | ||
| const engagementApproverProvider: RoleAccessProvider< | ||
| Record<string, unknown> | ||
| > = { | ||
| roleName: Role.EngagementPaymentApprover, | ||
| applyFilter: engagementApproverApplyFilter, | ||
| }; | ||
|
|
||
| service.register(engagementApproverProvider); | ||
|
|
||
| const result = await service.applyFilters<Record<string, unknown>>( | ||
| '88770025', | ||
| [Role.EngagementPaymentApprover], | ||
| { type: 'PAYMENT' }, | ||
| ); | ||
|
|
||
| expect(result).toEqual({ | ||
| category: 'ENGAGEMENT_PAYMENT', | ||
| type: 'PAYMENT', | ||
| }); | ||
| expect(engagementApproverApplyFilter).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('skips engagement approver resource checks when payment admin role is also present', async () => { | ||
| const paymentAdminVerifyAccess = jest.fn().mockResolvedValue(undefined); | ||
| const engagementApproverVerifyAccess = jest | ||
| .fn() | ||
| .mockRejectedValue(new Error('should not be called')); | ||
| const paymentAdminProvider: RoleAccessProvider = { | ||
| roleName: Role.PaymentAdmin, | ||
| verifyAccessToResource: paymentAdminVerifyAccess, | ||
| }; | ||
| const engagementApproverProvider: RoleAccessProvider = { | ||
| roleName: Role.EngagementPaymentApprover, | ||
| verifyAccessToResource: engagementApproverVerifyAccess, | ||
| }; | ||
|
|
||
| service.register(paymentAdminProvider); | ||
| service.register(engagementApproverProvider); | ||
|
|
||
| await expect( | ||
| service.verifyAccess('winning-id', '88770025', [ | ||
| Role.PaymentAdmin, | ||
| Role.EngagementPaymentApprover, | ||
| ]), | ||
| ).resolves.toBeUndefined(); | ||
|
|
||
| expect(paymentAdminVerifyAccess).toHaveBeenCalledTimes(1); | ||
| expect(engagementApproverVerifyAccess).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { Role } from 'src/core/auth/auth.constants'; | ||
| import { RoleAccessProvider } from './role-access.interface'; | ||
|
|
||
| @Injectable() | ||
|
|
@@ -9,14 +10,39 @@ export class AccessControlService { | |
| this.providers.set(provider.roleName.trim().toLowerCase(), provider); | ||
| } | ||
|
|
||
| private normalizeRoles(roles: string[] = []): Set<string> { | ||
| return new Set( | ||
| (roles || []).map((role) => role?.trim().toLowerCase()).filter(Boolean), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| ); | ||
| } | ||
|
|
||
| private shouldSkipProvider( | ||
| providerRole: string, | ||
| normalizedRoles: Set<string>, | ||
| ): boolean { | ||
| return ( | ||
| providerRole === Role.EngagementPaymentApprover.trim().toLowerCase() && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| normalizedRoles.has(Role.PaymentAdmin.trim().toLowerCase()) | ||
| ); | ||
| } | ||
|
|
||
| async applyFilters<T>( | ||
| userId: string, | ||
| roles: string[] = [], | ||
| req: any, | ||
| ): Promise<T> { | ||
| let out = { ...req }; | ||
| const normalizedRoles = this.normalizeRoles(roles); | ||
| for (const r of roles || []) { | ||
| const p = this.providers.get(r?.trim().toLowerCase()); | ||
| const normalizedRole = r?.trim().toLowerCase(); | ||
| if ( | ||
| !normalizedRole || | ||
| this.shouldSkipProvider(normalizedRole, normalizedRoles) | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const p = this.providers.get(normalizedRole); | ||
| if (p?.applyFilter) { | ||
| out = await p.applyFilter(userId, out); | ||
| } | ||
|
|
@@ -25,8 +51,17 @@ export class AccessControlService { | |
| } | ||
|
|
||
| async verifyAccess(resourceId: string, userId: string, roles: string[] = []) { | ||
| const normalizedRoles = this.normalizeRoles(roles); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| for (const r of roles || []) { | ||
| const p = this.providers.get(r?.trim().toLowerCase()); | ||
| const normalizedRole = r?.trim().toLowerCase(); | ||
| if ( | ||
| !normalizedRole || | ||
| this.shouldSkipProvider(normalizedRole, normalizedRoles) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const p = this.providers.get(normalizedRole); | ||
| if (p?.verifyAccessToResource) { | ||
| await p.verifyAccessToResource(resourceId, userId); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[💡
readability]Using
mockRejectedValuewith an error message like 'should not be called' is a bit misleading. Consider using a more descriptive message or a different approach to ensure clarity in test failures.