nitin
2025-03-10 19:02:40 +05:30
committed by GitHub
parent a1e0d7b7d7
commit 77574594f2
29 changed files with 496 additions and 212 deletions

View File

@ -0,0 +1,52 @@
import { ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AdminPanelGuard } from 'src/engine/guards/admin-panel-guard';
describe('AdminPanelGuard', () => {
const guard = new AdminPanelGuard();
it('should return true if user can access full admin panel', async () => {
const mockContext = {
getContext: jest.fn(() => ({
req: {
user: {
canAccessFullAdminPanel: true,
},
},
})),
};
jest
.spyOn(GqlExecutionContext, 'create')
.mockReturnValue(mockContext as any);
const mockExecutionContext = {} as ExecutionContext;
const result = await guard.canActivate(mockExecutionContext);
expect(result).toBe(true);
});
it('should return false if user cannot access full admin panel', async () => {
const mockContext = {
getContext: jest.fn(() => ({
req: {
user: {
canAccessFullAdminPanel: false,
},
},
})),
};
jest
.spyOn(GqlExecutionContext, 'create')
.mockReturnValue(mockContext as any);
const mockExecutionContext = {} as ExecutionContext;
const result = await guard.canActivate(mockExecutionContext);
expect(result).toBe(false);
});
});

View File

@ -0,0 +1,15 @@
import { CanActivate, ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { Observable } from 'rxjs';
export class AdminPanelGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const ctx = GqlExecutionContext.create(context);
const request = ctx.getContext().req;
return request.user.canAccessFullAdminPanel === true;
}
}