fix issues
This commit is contained in:
@@ -14,14 +14,30 @@ import type { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
|
||||
// Mock SessionViewModel factory
|
||||
function createMockSession(overrides: Partial<SessionViewModel> = {}): SessionViewModel {
|
||||
return {
|
||||
const baseSession = {
|
||||
isAuthenticated: true,
|
||||
user: {
|
||||
userId: 'user-123',
|
||||
email: 'test@example.com',
|
||||
displayName: 'Test User',
|
||||
...overrides.user,
|
||||
},
|
||||
userId: 'user-123',
|
||||
email: 'test@example.com',
|
||||
displayName: 'Test User',
|
||||
role: undefined,
|
||||
};
|
||||
|
||||
// Handle the case where overrides might have a user object
|
||||
// (for backward compatibility with existing test patterns)
|
||||
if (overrides.user) {
|
||||
const { user, ...rest } = overrides;
|
||||
return {
|
||||
...baseSession,
|
||||
...rest,
|
||||
userId: user.userId || baseSession.userId,
|
||||
email: user.email || baseSession.email,
|
||||
displayName: user.displayName || baseSession.displayName,
|
||||
role: user.role,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...baseSession,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
@@ -78,34 +94,41 @@ describe('AuthGateway', () => {
|
||||
// Note: AuthorizationBlocker currently returns 'enabled' for all authenticated users
|
||||
// in demo mode. These tests document the intended behavior for when role-based
|
||||
// access control is fully implemented.
|
||||
it('should allow access when user has required role (current: always allows for authenticated)', () => {
|
||||
it('should allow access when user has required role', () => {
|
||||
const authContext = createMockAuthContext({
|
||||
session: createMockSession(),
|
||||
session: createMockSession({
|
||||
user: {
|
||||
userId: 'user-123',
|
||||
email: 'admin@example.com',
|
||||
displayName: 'Admin User',
|
||||
role: 'admin',
|
||||
},
|
||||
}),
|
||||
});
|
||||
const gateway = new AuthGateway(authContext, {
|
||||
requiredRoles: ['admin'],
|
||||
});
|
||||
|
||||
// Current behavior: always allows for authenticated users
|
||||
expect(gateway.canAccess()).toBe(true);
|
||||
});
|
||||
|
||||
it('should deny access when user lacks required role (future behavior)', () => {
|
||||
// This test documents what should happen when role system is implemented
|
||||
// For now, it demonstrates the current limitation
|
||||
it('should deny access when user lacks required role', () => {
|
||||
const authContext = createMockAuthContext({
|
||||
session: createMockSession(),
|
||||
session: createMockSession({
|
||||
user: {
|
||||
userId: 'user-123',
|
||||
email: 'user@example.com',
|
||||
displayName: 'Regular User',
|
||||
role: 'user',
|
||||
},
|
||||
}),
|
||||
});
|
||||
const gateway = new AuthGateway(authContext, {
|
||||
requiredRoles: ['admin'],
|
||||
});
|
||||
|
||||
// Current: allows access
|
||||
expect(gateway.canAccess()).toBe(true);
|
||||
|
||||
// Future: should be false
|
||||
// expect(gateway.canAccess()).toBe(false);
|
||||
// expect(gateway.getBlockMessage()).toContain('admin');
|
||||
expect(gateway.canAccess()).toBe(false);
|
||||
expect(gateway.getBlockMessage()).toContain('admin');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -252,9 +275,9 @@ describe('AuthGateway', () => {
|
||||
requiredRoles: ['admin'], // lowercase
|
||||
});
|
||||
|
||||
// Current behavior: AuthorizationBlocker always returns 'enabled' for authenticated users
|
||||
// So access is granted regardless of role matching
|
||||
expect(gateway.canAccess()).toBe(true);
|
||||
// Role matching is case-sensitive
|
||||
expect(gateway.canAccess()).toBe(false);
|
||||
expect(gateway.getBlockMessage()).toContain('admin');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -292,20 +315,24 @@ describe('AuthGateway', () => {
|
||||
|
||||
it('should provide appropriate block message for missing roles', () => {
|
||||
const authContext = createMockAuthContext({
|
||||
session: createMockSession(),
|
||||
session: createMockSession({
|
||||
user: {
|
||||
userId: 'user-123',
|
||||
email: 'user@example.com',
|
||||
displayName: 'Regular User',
|
||||
role: 'user',
|
||||
},
|
||||
}),
|
||||
});
|
||||
const gateway = new AuthGateway(authContext, {
|
||||
requiredRoles: ['admin'],
|
||||
});
|
||||
|
||||
// First check what the gateway actually returns
|
||||
const canAccess = gateway.canAccess();
|
||||
const state = gateway.getAccessState();
|
||||
|
||||
// Current behavior: AuthorizationBlocker always returns 'enabled' for authenticated users
|
||||
// So access is granted and message is "Access granted"
|
||||
expect(canAccess).toBe(true);
|
||||
expect(state.reason).toBe('Access granted');
|
||||
expect(canAccess).toBe(false);
|
||||
expect(state.reason).toContain('admin');
|
||||
});
|
||||
|
||||
it('should provide appropriate block message when loading', () => {
|
||||
|
||||
8
apps/website/lib/gateways/AuthGuard.test.tsx
Normal file
8
apps/website/lib/gateways/AuthGuard.test.tsx
Normal file
@@ -0,0 +1,8 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { AuthGuard } from './AuthGuard';
|
||||
|
||||
describe('AuthGuard', () => {
|
||||
it('should be defined', () => {
|
||||
expect(AuthGuard).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -19,14 +19,30 @@ vi.mock('next/navigation');
|
||||
|
||||
// Mock SessionViewModel factory
|
||||
function createMockSession(overrides: Partial<SessionViewModel> = {}): SessionViewModel {
|
||||
return {
|
||||
const baseSession = {
|
||||
isAuthenticated: true,
|
||||
user: {
|
||||
userId: 'user-123',
|
||||
email: 'test@example.com',
|
||||
displayName: 'Test User',
|
||||
...overrides.user,
|
||||
},
|
||||
userId: 'user-123',
|
||||
email: 'test@example.com',
|
||||
displayName: 'Test User',
|
||||
role: undefined,
|
||||
};
|
||||
|
||||
// Handle the case where overrides might have a user object
|
||||
// (for backward compatibility with existing test patterns)
|
||||
if (overrides.user) {
|
||||
const { user, ...rest } = overrides;
|
||||
return {
|
||||
...baseSession,
|
||||
...rest,
|
||||
userId: user.userId || baseSession.userId,
|
||||
email: user.email || baseSession.email,
|
||||
displayName: user.displayName || baseSession.displayName,
|
||||
role: user.role,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...baseSession,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
8
apps/website/lib/gateways/index.test.ts
Normal file
8
apps/website/lib/gateways/index.test.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
describe('gateways index', () => {
|
||||
it('should export gateways', async () => {
|
||||
const module = await import('./index');
|
||||
expect(Object.keys(module).length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user