41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { SetMetadata } from '@nestjs/common';
|
|
import { RequireAuthenticatedUser, REQUIRE_AUTHENTICATED_USER_METADATA_KEY } from './RequireAuthenticatedUser';
|
|
|
|
// Mock SetMetadata
|
|
vi.mock('@nestjs/common', () => ({
|
|
SetMetadata: vi.fn(() => (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) => descriptor),
|
|
}));
|
|
|
|
describe('RequireAuthenticatedUser', () => {
|
|
it('should return a method decorator', () => {
|
|
const decorator = RequireAuthenticatedUser();
|
|
expect(typeof decorator).toBe('function');
|
|
});
|
|
|
|
it('should call SetMetadata with correct key and value', () => {
|
|
RequireAuthenticatedUser();
|
|
|
|
expect(SetMetadata).toHaveBeenCalledWith(REQUIRE_AUTHENTICATED_USER_METADATA_KEY, {
|
|
required: true,
|
|
});
|
|
});
|
|
|
|
it('should return a decorator that can be used as both method and class decorator', () => {
|
|
const decorator = RequireAuthenticatedUser();
|
|
|
|
// Test as method decorator
|
|
const mockTarget = {};
|
|
const mockPropertyKey = 'testMethod';
|
|
const mockDescriptor = { value: () => {} };
|
|
|
|
const result = decorator(mockTarget, mockPropertyKey, mockDescriptor);
|
|
|
|
// The decorator should return the descriptor
|
|
expect(result).toBe(mockDescriptor);
|
|
});
|
|
|
|
it('should have correct metadata key', () => {
|
|
expect(REQUIRE_AUTHENTICATED_USER_METADATA_KEY).toBe('gridpilot:requireAuthenticatedUser');
|
|
});
|
|
});
|