173 lines
5.0 KiB
TypeScript
173 lines
5.0 KiB
TypeScript
/**
|
|
* Tests for AuthorizationBlocker
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { AuthorizationBlocker, AuthorizationBlockReason } from './AuthorizationBlocker';
|
|
import type { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
|
|
|
describe('AuthorizationBlocker', () => {
|
|
let blocker: AuthorizationBlocker;
|
|
|
|
// Mock SessionViewModel
|
|
const createMockSession = (overrides?: Partial<SessionViewModel>): SessionViewModel => {
|
|
const base: SessionViewModel = {
|
|
userId: 'user-123',
|
|
email: 'test@example.com',
|
|
displayName: 'Test User',
|
|
isAuthenticated: true,
|
|
avatarInitials: 'TU',
|
|
greeting: 'Hello, Test User!',
|
|
hasDriverProfile: false,
|
|
authStatusDisplay: 'Logged In',
|
|
user: {
|
|
userId: 'user-123',
|
|
email: 'test@example.com',
|
|
displayName: 'Test User',
|
|
primaryDriverId: null,
|
|
avatarUrl: null,
|
|
},
|
|
};
|
|
|
|
return { ...base, ...overrides };
|
|
};
|
|
|
|
describe('constructor', () => {
|
|
it('should create blocker with required roles', () => {
|
|
blocker = new AuthorizationBlocker(['owner', 'admin']);
|
|
expect(blocker).toBeDefined();
|
|
});
|
|
|
|
it('should create blocker with empty roles array', () => {
|
|
blocker = new AuthorizationBlocker([]);
|
|
expect(blocker).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('updateSession', () => {
|
|
beforeEach(() => {
|
|
blocker = new AuthorizationBlocker(['owner']);
|
|
});
|
|
|
|
it('should update session state', () => {
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
|
|
expect(blocker.canExecute()).toBe(true);
|
|
});
|
|
|
|
it('should handle null session', () => {
|
|
blocker.updateSession(null);
|
|
|
|
expect(blocker.canExecute()).toBe(false);
|
|
expect(blocker.getReason()).toBe('loading');
|
|
});
|
|
});
|
|
|
|
describe('canExecute', () => {
|
|
beforeEach(() => {
|
|
blocker = new AuthorizationBlocker(['owner', 'admin']);
|
|
});
|
|
|
|
it('returns false when session is null', () => {
|
|
blocker.updateSession(null);
|
|
expect(blocker.canExecute()).toBe(false);
|
|
});
|
|
|
|
it('returns false when not authenticated', () => {
|
|
const session = createMockSession({ isAuthenticated: false });
|
|
blocker.updateSession(session);
|
|
expect(blocker.canExecute()).toBe(false);
|
|
});
|
|
|
|
it('returns true when authenticated (temporary workaround)', () => {
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
expect(blocker.canExecute()).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('getReason', () => {
|
|
beforeEach(() => {
|
|
blocker = new AuthorizationBlocker(['owner']);
|
|
});
|
|
|
|
it('returns loading when session is null', () => {
|
|
blocker.updateSession(null);
|
|
expect(blocker.getReason()).toBe('loading');
|
|
});
|
|
|
|
it('returns unauthenticated when not authenticated', () => {
|
|
const session = createMockSession({ isAuthenticated: false });
|
|
blocker.updateSession(session);
|
|
expect(blocker.getReason()).toBe('unauthenticated');
|
|
});
|
|
|
|
it('returns enabled when authenticated (temporary)', () => {
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
expect(blocker.getReason()).toBe('enabled');
|
|
});
|
|
});
|
|
|
|
describe('block and release', () => {
|
|
beforeEach(() => {
|
|
blocker = new AuthorizationBlocker(['owner']);
|
|
});
|
|
|
|
it('block should set session to null', () => {
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
|
|
expect(blocker.canExecute()).toBe(true);
|
|
|
|
blocker.block();
|
|
|
|
expect(blocker.canExecute()).toBe(false);
|
|
expect(blocker.getReason()).toBe('loading');
|
|
});
|
|
|
|
it('release should be no-op', () => {
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
|
|
blocker.release();
|
|
|
|
expect(blocker.canExecute()).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('getBlockMessage', () => {
|
|
beforeEach(() => {
|
|
blocker = new AuthorizationBlocker(['owner']);
|
|
});
|
|
|
|
it('returns correct message for loading', () => {
|
|
blocker.updateSession(null);
|
|
expect(blocker.getBlockMessage()).toBe('Loading user data...');
|
|
});
|
|
|
|
it('returns correct message for unauthenticated', () => {
|
|
const session = createMockSession({ isAuthenticated: false });
|
|
blocker.updateSession(session);
|
|
expect(blocker.getBlockMessage()).toBe('You must be logged in to access the admin area.');
|
|
});
|
|
|
|
it('returns correct message for enabled', () => {
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
expect(blocker.getBlockMessage()).toBe('Access granted');
|
|
});
|
|
});
|
|
|
|
describe('multiple required roles', () => {
|
|
it('should handle multiple roles', () => {
|
|
blocker = new AuthorizationBlocker(['owner', 'admin', 'super-admin']);
|
|
|
|
const session = createMockSession();
|
|
blocker.updateSession(session);
|
|
|
|
expect(blocker.canExecute()).toBe(true);
|
|
});
|
|
});
|
|
}); |