feat: authentication with OAuth
Some checks failed
ci / ci (22, ubuntu-latest) (push) Failing after 6m46s
Some checks failed
ci / ci (22, ubuntu-latest) (push) Failing after 6m46s
This commit is contained in:
444
app/composables/__tests__/useAuth.test.ts
Normal file
444
app/composables/__tests__/useAuth.test.ts
Normal file
@@ -0,0 +1,444 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { mockNuxtImport } from '@nuxt/test-utils/runtime';
|
||||
import type { AuthProviderInfo, AuthModel, RecordModel } from 'pocketbase';
|
||||
|
||||
/**
|
||||
* Comprehensive tests for useAuth composable
|
||||
* Based on specs from private/specs.md section 3.1 (Authentication API)
|
||||
*
|
||||
* These tests verify actual behavior, not just API existence.
|
||||
*/
|
||||
|
||||
// Mock PocketBase
|
||||
const mockAuthStore = {
|
||||
isValid: false,
|
||||
record: null as AuthModel | null,
|
||||
clear: vi.fn(),
|
||||
onChange: vi.fn(),
|
||||
};
|
||||
|
||||
const mockCollection = vi.fn();
|
||||
const mockAuthWithOAuth2 = vi.fn();
|
||||
const mockListAuthMethods = vi.fn();
|
||||
const mockAuthRefresh = vi.fn();
|
||||
|
||||
vi.mock('../usePocketbase', () => ({
|
||||
usePocketbase: () => ({
|
||||
authStore: mockAuthStore,
|
||||
collection: mockCollection,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock router using Nuxt's test utils
|
||||
const mockRouterPush = vi.fn();
|
||||
const mockRouter = {
|
||||
push: mockRouterPush,
|
||||
};
|
||||
|
||||
mockNuxtImport('useRouter', () => {
|
||||
return () => mockRouter;
|
||||
});
|
||||
|
||||
describe('useAuth', () => {
|
||||
beforeEach(async () => {
|
||||
// Reset all mocks
|
||||
vi.clearAllMocks();
|
||||
mockAuthStore.isValid = false;
|
||||
mockAuthStore.record = null;
|
||||
|
||||
// Setup default mock implementations
|
||||
mockCollection.mockReturnValue({
|
||||
authWithOAuth2: mockAuthWithOAuth2,
|
||||
listAuthMethods: mockListAuthMethods,
|
||||
authRefresh: mockAuthRefresh,
|
||||
});
|
||||
|
||||
// Clear module cache to get fresh imports
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('Composable Export', () => {
|
||||
it('should be exported as a function', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
expect(useAuth).toBeDefined();
|
||||
expect(typeof useAuth).toBe('function');
|
||||
});
|
||||
|
||||
it('should return an object with auth methods and state', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
expect(auth).toBeDefined();
|
||||
expect(typeof auth).toBe('object');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Initial State', () => {
|
||||
it('should initialize with user as null', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
expect(auth.user.value).toBeNull();
|
||||
});
|
||||
|
||||
it('should initialize with isAuthenticated as false', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
expect(auth.isAuthenticated.value).toBe(false);
|
||||
});
|
||||
|
||||
it('should initialize with loading as false', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
expect(auth.loading.value).toBe(false);
|
||||
});
|
||||
|
||||
it('should initialize with error as null', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
expect(auth.error.value).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('initAuth', () => {
|
||||
it('should sync user from authStore', async () => {
|
||||
const mockUser: AuthModel = {
|
||||
id: 'user123',
|
||||
email: 'test@example.com',
|
||||
} as unknown as AuthModel;
|
||||
|
||||
mockAuthStore.record = mockUser;
|
||||
mockAuthStore.isValid = true;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.initAuth();
|
||||
|
||||
expect(auth.user.value).toEqual(mockUser);
|
||||
});
|
||||
|
||||
it('should register onChange listener', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.initAuth();
|
||||
|
||||
expect(mockAuthStore.onChange).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should update user when authStore changes', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.initAuth();
|
||||
|
||||
// Get the onChange callback
|
||||
const onChangeCallback = mockAuthStore.onChange.mock.calls[0]?.[0];
|
||||
|
||||
// Simulate auth change
|
||||
const newUser = {
|
||||
id: 'newUser456',
|
||||
email: 'new@example.com',
|
||||
};
|
||||
|
||||
onChangeCallback('token123', newUser);
|
||||
|
||||
expect(auth.user.value).toEqual(newUser);
|
||||
});
|
||||
});
|
||||
|
||||
describe('login', () => {
|
||||
const mockProviders: AuthProviderInfo[] = [
|
||||
{
|
||||
name: 'google',
|
||||
displayName: 'Google',
|
||||
state: 'state123',
|
||||
codeVerifier: 'verifier',
|
||||
codeChallenge: 'challenge',
|
||||
codeChallengeMethod: 'S256',
|
||||
authURL: 'https://google.com/oauth',
|
||||
},
|
||||
{
|
||||
name: 'microsoft',
|
||||
displayName: 'Microsoft',
|
||||
state: 'state456',
|
||||
codeVerifier: 'verifier2',
|
||||
codeChallenge: 'challenge2',
|
||||
codeChallengeMethod: 'S256',
|
||||
authURL: 'https://microsoft.com/oauth',
|
||||
},
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
mockListAuthMethods.mockResolvedValue({
|
||||
oauth2: {
|
||||
enabled: true,
|
||||
providers: mockProviders,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should set loading to true when login starts', async () => {
|
||||
mockAuthWithOAuth2.mockImplementation(() => new Promise(() => {})); // Never resolves
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
const loginPromise = auth.login('google');
|
||||
|
||||
expect(auth.loading.value).toBe(true);
|
||||
|
||||
// Cleanup
|
||||
await Promise.race([loginPromise, new Promise((resolve) => setTimeout(resolve, 10))]);
|
||||
});
|
||||
|
||||
it('should clear previous errors when starting new login', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
// Set an error first
|
||||
auth.error.value = new Error('Previous error');
|
||||
|
||||
mockAuthWithOAuth2.mockResolvedValue({});
|
||||
|
||||
await auth.login('google');
|
||||
|
||||
expect(auth.error.value).toBeNull();
|
||||
});
|
||||
|
||||
it('should call authWithOAuth2 with correct provider', async () => {
|
||||
mockAuthWithOAuth2.mockResolvedValue({});
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.login('google');
|
||||
|
||||
expect(mockAuthWithOAuth2).toHaveBeenCalledWith({ provider: 'google' });
|
||||
});
|
||||
|
||||
it('should set loading to false after login completes', async () => {
|
||||
mockAuthWithOAuth2.mockResolvedValue({});
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.login('google');
|
||||
|
||||
expect(auth.loading.value).toBe(false);
|
||||
});
|
||||
|
||||
it('should throw error if provider is not configured', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.login('github'); // Not in mockProviders
|
||||
|
||||
expect(auth.error.value).toBeDefined();
|
||||
expect(auth.error.value?.message).toContain('github');
|
||||
expect(auth.error.value?.message).toContain('not configured');
|
||||
});
|
||||
|
||||
it('should handle OAuth errors gracefully', async () => {
|
||||
const mockError = new Error('OAuth failed');
|
||||
mockAuthWithOAuth2.mockRejectedValue(mockError);
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.login('google');
|
||||
|
||||
expect(auth.error.value).toEqual(mockError);
|
||||
expect(auth.loading.value).toBe(false);
|
||||
});
|
||||
|
||||
it('should support multiple OAuth providers', async () => {
|
||||
mockAuthWithOAuth2.mockResolvedValue({});
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.login('google');
|
||||
expect(mockAuthWithOAuth2).toHaveBeenCalledWith({ provider: 'google' });
|
||||
|
||||
await auth.login('microsoft');
|
||||
expect(mockAuthWithOAuth2).toHaveBeenCalledWith({ provider: 'microsoft' });
|
||||
});
|
||||
|
||||
it('should return empty array when OAuth is disabled', async () => {
|
||||
mockListAuthMethods.mockResolvedValue({
|
||||
oauth2: {
|
||||
enabled: false,
|
||||
providers: [],
|
||||
},
|
||||
});
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.login('google');
|
||||
|
||||
expect(auth.error.value).toBeDefined();
|
||||
expect(auth.error.value?.message).toContain('not configured');
|
||||
});
|
||||
});
|
||||
|
||||
describe('handleOAuthCallback', () => {
|
||||
it('should sync user from authStore', async () => {
|
||||
const mockUser: AuthModel = {
|
||||
id: 'user123',
|
||||
email: 'test@example.com',
|
||||
} as unknown as AuthModel;
|
||||
|
||||
mockAuthStore.record = mockUser;
|
||||
mockAuthStore.isValid = true;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.handleOAuthCallback();
|
||||
|
||||
expect(auth.user.value).toEqual(mockUser);
|
||||
});
|
||||
|
||||
it('should redirect to dashboard when authenticated', async () => {
|
||||
mockAuthStore.record = {
|
||||
id: 'user123',
|
||||
email: 'test@example.com',
|
||||
} as unknown as RecordModel;
|
||||
mockAuthStore.isValid = true;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.handleOAuthCallback();
|
||||
|
||||
expect(mockRouterPush).toHaveBeenCalledWith('/dashboard');
|
||||
});
|
||||
|
||||
it('should redirect to home when not authenticated', async () => {
|
||||
mockAuthStore.record = null;
|
||||
mockAuthStore.isValid = false;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.handleOAuthCallback();
|
||||
|
||||
expect(mockRouterPush).toHaveBeenCalledWith('/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('refreshAuth', () => {
|
||||
it('should call authRefresh on users collection', async () => {
|
||||
mockAuthRefresh.mockResolvedValue({
|
||||
token: 'newToken',
|
||||
record: { id: 'user123', email: 'test@example.com' },
|
||||
});
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
await auth.refreshAuth();
|
||||
|
||||
expect(mockCollection).toHaveBeenCalledWith('users');
|
||||
expect(mockAuthRefresh).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return the refresh result', async () => {
|
||||
const mockResult = {
|
||||
token: 'newToken',
|
||||
record: { id: 'user123', email: 'test@example.com' },
|
||||
};
|
||||
|
||||
mockAuthRefresh.mockResolvedValue(mockResult);
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
const result = await auth.refreshAuth();
|
||||
|
||||
expect(result).toEqual(mockResult);
|
||||
});
|
||||
});
|
||||
|
||||
describe('logout', () => {
|
||||
it('should clear authStore', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
auth.logout();
|
||||
|
||||
expect(mockAuthStore.clear).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should clear authStore even when user is authenticated', async () => {
|
||||
mockAuthStore.record = {
|
||||
id: 'user123',
|
||||
email: 'test@example.com',
|
||||
} as unknown as RecordModel;
|
||||
mockAuthStore.isValid = true;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
auth.logout();
|
||||
|
||||
expect(mockAuthStore.clear).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('isAuthenticated computed', () => {
|
||||
it('should be false when authStore is invalid', async () => {
|
||||
mockAuthStore.isValid = false;
|
||||
mockAuthStore.record = { id: 'user123', email: 'test@example.com' } as unknown as RecordModel;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
await auth.initAuth();
|
||||
|
||||
expect(auth.isAuthenticated.value).toBe(false);
|
||||
});
|
||||
|
||||
it('should be false when user is null', async () => {
|
||||
mockAuthStore.isValid = true;
|
||||
mockAuthStore.record = null;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
await auth.initAuth();
|
||||
|
||||
expect(auth.isAuthenticated.value).toBe(false);
|
||||
});
|
||||
|
||||
it('should be true when authStore is valid and user exists', async () => {
|
||||
mockAuthStore.isValid = true;
|
||||
mockAuthStore.record = { id: 'user123', email: 'test@example.com' } as unknown as RecordModel;
|
||||
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
await auth.initAuth();
|
||||
|
||||
expect(auth.isAuthenticated.value).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Exposed API', () => {
|
||||
it('should expose all required methods and properties', async () => {
|
||||
const { useAuth } = await import('../useAuth');
|
||||
const auth = useAuth();
|
||||
|
||||
// State
|
||||
expect(auth.user).toBeDefined();
|
||||
expect(auth.isAuthenticated).toBeDefined();
|
||||
expect(auth.loading).toBeDefined();
|
||||
expect(auth.error).toBeDefined();
|
||||
|
||||
// Methods
|
||||
expect(typeof auth.initAuth).toBe('function');
|
||||
expect(typeof auth.login).toBe('function');
|
||||
expect(typeof auth.logout).toBe('function');
|
||||
expect(typeof auth.handleOAuthCallback).toBe('function');
|
||||
expect(typeof auth.refreshAuth).toBe('function');
|
||||
});
|
||||
});
|
||||
});
|
||||
158
app/composables/__tests__/usePageTitle.test.ts
Normal file
158
app/composables/__tests__/usePageTitle.test.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { usePageTitle } from '../usePageTitle';
|
||||
|
||||
/**
|
||||
* Unit tests for usePageTitle composable
|
||||
*
|
||||
* This composable manages page titles throughout the application.
|
||||
*/
|
||||
|
||||
describe('usePageTitle', () => {
|
||||
beforeEach(() => {
|
||||
// Reset modules to ensure clean state
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('Initialization', () => {
|
||||
it('should initialize with default title "Tímmál"', () => {
|
||||
const { title } = usePageTitle();
|
||||
expect(title.value).toBe('Tímmál');
|
||||
});
|
||||
|
||||
it('should initialize with empty page name', () => {
|
||||
const { pageName } = usePageTitle();
|
||||
expect(pageName.value).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Setting Page Name', () => {
|
||||
it('should update page name when setPageName is called', () => {
|
||||
const { pageName, setPageName } = usePageTitle();
|
||||
|
||||
setPageName('Dashboard');
|
||||
|
||||
expect(pageName.value).toBe('Dashboard');
|
||||
});
|
||||
|
||||
it('should update title to include page name', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
|
||||
setPageName('Dashboard');
|
||||
|
||||
expect(title.value).toBe('Dashboard - Tímmál');
|
||||
});
|
||||
|
||||
it('should handle empty string gracefully', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
|
||||
// First set a name
|
||||
setPageName('Dashboard');
|
||||
expect(title.value).toBe('Dashboard - Tímmál');
|
||||
|
||||
// Then clear it
|
||||
setPageName('');
|
||||
expect(title.value).toBe('Tímmál');
|
||||
});
|
||||
|
||||
it('should handle multiple page name changes', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
|
||||
setPageName('Dashboard');
|
||||
expect(title.value).toBe('Dashboard - Tímmál');
|
||||
|
||||
setPageName('Projects');
|
||||
expect(title.value).toBe('Projects - Tímmál');
|
||||
|
||||
setPageName('Settings');
|
||||
expect(title.value).toBe('Settings - Tímmál');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Title Formatting', () => {
|
||||
it('should format title as "PageName - Tímmál" when page name is set', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
|
||||
setPageName('Reports');
|
||||
|
||||
expect(title.value).toBe('Reports - Tímmál');
|
||||
});
|
||||
|
||||
it('should format title as "Tímmál" when page name is empty', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
setPageName(null);
|
||||
|
||||
expect(title.value).toBe('Tímmál');
|
||||
});
|
||||
|
||||
it('should preserve special characters in page name', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
|
||||
setPageName('Reports & Analytics');
|
||||
|
||||
expect(title.value).toBe('Reports & Analytics - Tímmál');
|
||||
});
|
||||
|
||||
it('should preserve unicode characters', () => {
|
||||
const { title, setPageName } = usePageTitle();
|
||||
|
||||
setPageName('Paramètres');
|
||||
|
||||
expect(title.value).toBe('Paramètres - Tímmál');
|
||||
});
|
||||
});
|
||||
|
||||
describe('State Sharing', () => {
|
||||
it('should share state across multiple calls', () => {
|
||||
const instance1 = usePageTitle();
|
||||
const instance2 = usePageTitle();
|
||||
|
||||
// Set via first instance
|
||||
instance1.setPageName('Dashboard');
|
||||
|
||||
// Should be visible in second instance
|
||||
expect(instance2.title.value).toBe('Dashboard - Tímmál');
|
||||
expect(instance2.pageName.value).toBe('Dashboard');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Exposed API', () => {
|
||||
it('should expose title as computed', () => {
|
||||
const { title } = usePageTitle();
|
||||
|
||||
expect(title).toBeDefined();
|
||||
expect(title.value).toBeDefined();
|
||||
});
|
||||
|
||||
it('should expose pageName as readonly', () => {
|
||||
const { pageName } = usePageTitle();
|
||||
|
||||
expect(pageName).toBeDefined();
|
||||
expect(pageName.value).toBeDefined();
|
||||
});
|
||||
|
||||
it('should expose setPageName method', () => {
|
||||
const { setPageName } = usePageTitle();
|
||||
|
||||
expect(setPageName).toBeDefined();
|
||||
expect(typeof setPageName).toBe('function');
|
||||
});
|
||||
|
||||
it('title should be readonly (TypeScript enforced)', () => {
|
||||
const { title } = usePageTitle();
|
||||
|
||||
// readonly() is enforced by TypeScript, not at runtime
|
||||
// This test just verifies the property exists
|
||||
expect(title).toBeDefined();
|
||||
expect(title.value).toBeDefined();
|
||||
});
|
||||
|
||||
it('pageName should be readonly (TypeScript enforced)', () => {
|
||||
const { pageName } = usePageTitle();
|
||||
|
||||
// readonly() is enforced by TypeScript, not at runtime
|
||||
// This test just verifies the property exists
|
||||
expect(pageName).toBeDefined();
|
||||
expect(pageName.value).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
85
app/composables/__tests__/usePocketbase.test.ts
Normal file
85
app/composables/__tests__/usePocketbase.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { usePocketbase } from '../usePocketbase';
|
||||
import PocketBase from 'pocketbase';
|
||||
|
||||
/**
|
||||
* Tests for usePocketbase composable
|
||||
*
|
||||
* This composable provides a singleton PocketBase client instance.
|
||||
*/
|
||||
|
||||
describe('usePocketbase', () => {
|
||||
beforeEach(() => {
|
||||
// Reset modules to clear singleton between test suites
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('Instance Creation', () => {
|
||||
it('should return a PocketBase instance', () => {
|
||||
const pb = usePocketbase();
|
||||
expect(pb).toBeInstanceOf(PocketBase);
|
||||
});
|
||||
|
||||
it('should return the same instance on multiple calls (singleton)', () => {
|
||||
const pb1 = usePocketbase();
|
||||
const pb2 = usePocketbase();
|
||||
expect(pb1).toBe(pb2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Configuration', () => {
|
||||
it('should initialize with URL from runtime config or fallback to default', () => {
|
||||
const pb = usePocketbase();
|
||||
|
||||
// Should have a baseURL set (either from config or default)
|
||||
expect(pb.baseURL).toBeDefined();
|
||||
expect(typeof pb.baseURL).toBe('string');
|
||||
expect(pb.baseURL).toMatch(/^https?:\/\//); // Valid URL format
|
||||
});
|
||||
|
||||
it('should use a valid URL format', () => {
|
||||
const pb = usePocketbase();
|
||||
|
||||
// URL should be a valid HTTP/HTTPS URL
|
||||
expect(pb.baseURL).toMatch(/^https?:\/\/[\w\d.:]+/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Singleton Behavior', () => {
|
||||
it('should maintain singleton across multiple imports', () => {
|
||||
const pb1 = usePocketbase();
|
||||
const pb2 = usePocketbase();
|
||||
const pb3 = usePocketbase();
|
||||
|
||||
expect(pb1).toBe(pb2);
|
||||
expect(pb2).toBe(pb3);
|
||||
});
|
||||
|
||||
it('should share auth state across all consumers', () => {
|
||||
const pb1 = usePocketbase();
|
||||
const pb2 = usePocketbase();
|
||||
|
||||
// Both should share the same authStore
|
||||
expect(pb1.authStore).toBe(pb2.authStore);
|
||||
});
|
||||
});
|
||||
|
||||
describe('PocketBase Features', () => {
|
||||
it('should have authStore available', () => {
|
||||
const pb = usePocketbase();
|
||||
expect(pb.authStore).toBeDefined();
|
||||
});
|
||||
|
||||
it('should have collection method available', () => {
|
||||
const pb = usePocketbase();
|
||||
expect(pb.collection).toBeDefined();
|
||||
expect(typeof pb.collection).toBe('function');
|
||||
});
|
||||
|
||||
it('should be able to access collections', () => {
|
||||
const pb = usePocketbase();
|
||||
const usersCollection = pb.collection('users');
|
||||
expect(usersCollection).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
56
app/composables/useAuth.ts
Normal file
56
app/composables/useAuth.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { AuthProviderInfo, AuthModel } from 'pocketbase';
|
||||
import { usePocketbase } from './usePocketbase';
|
||||
|
||||
export const useAuth = () => {
|
||||
const pb = usePocketbase();
|
||||
const router = useRouter();
|
||||
|
||||
const userCollection = 'users';
|
||||
|
||||
const user = ref<AuthModel | null>(null);
|
||||
const isAuthenticated = computed<boolean>(() => pb.authStore.isValid && !!user.value);
|
||||
const loading = ref<boolean>(false);
|
||||
const error = ref<Error | null>(null);
|
||||
|
||||
const initAuth = async () => {
|
||||
user.value = pb.authStore.record;
|
||||
pb.authStore.onChange((_token, model) => (user.value = model));
|
||||
};
|
||||
|
||||
const authProviders = async (): Promise<AuthProviderInfo[]> => {
|
||||
const authMethods = await pb.collection(userCollection).listAuthMethods();
|
||||
return authMethods.oauth2.enabled ? authMethods.oauth2.providers : [];
|
||||
};
|
||||
|
||||
const login = async (provider: string) => {
|
||||
loading.value = true;
|
||||
error.value = null;
|
||||
try {
|
||||
const providers = await authProviders();
|
||||
const providerData = providers.find((p) => p.name === provider);
|
||||
if (!providerData) {
|
||||
throw new Error(`${provider} OAuth is not configured`);
|
||||
}
|
||||
await pb.collection(userCollection).authWithOAuth2({ provider });
|
||||
} catch (pbError) {
|
||||
error.value = pbError as Error;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const refreshAuth = async () => await pb.collection(userCollection).authRefresh();
|
||||
|
||||
const handleOAuthCallback = async () => {
|
||||
user.value = pb.authStore.record;
|
||||
if (isAuthenticated.value) {
|
||||
await router.push('/dashboard');
|
||||
} else {
|
||||
await router.push('/');
|
||||
}
|
||||
};
|
||||
|
||||
const logout = () => pb.authStore.clear();
|
||||
|
||||
return { user, loading, error, isAuthenticated, login, logout, initAuth, refreshAuth, handleOAuthCallback };
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
export const usePageTitle = () => {
|
||||
const pageName = useState<string>('pageName', () => '');
|
||||
const title = computed<string>(() => (pageName.value.length > 0 ? `${pageName.value} - Tímmál` : 'Tímmál'));
|
||||
const pageName = useState<string | null>('pageName', () => null);
|
||||
const title = computed<string>(() => ((pageName.value ?? '').length > 0 ? `${pageName.value} - Tímmál` : 'Tímmál'));
|
||||
|
||||
const setPageName = (newName: string) => {
|
||||
const setPageName = (newName: string | null) => {
|
||||
pageName.value = newName;
|
||||
useHead({ title: title.value });
|
||||
};
|
||||
|
||||
15
app/composables/usePocketbase.ts
Normal file
15
app/composables/usePocketbase.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import PocketBase from 'pocketbase';
|
||||
|
||||
let pbInstance: PocketBase | null = null;
|
||||
|
||||
export const usePocketbase = () => {
|
||||
if (!pbInstance) {
|
||||
const config = useRuntimeConfig();
|
||||
pbInstance = new PocketBase(config.pocketbaseUrl || 'http://localhost:8090');
|
||||
if (import.meta.server) {
|
||||
pbInstance.autoCancellation(false);
|
||||
}
|
||||
}
|
||||
|
||||
return pbInstance;
|
||||
};
|
||||
8
cucumber.js
Normal file
8
cucumber.js
Normal file
@@ -0,0 +1,8 @@
|
||||
export default {
|
||||
default: {
|
||||
require: ['features/step_definitions/**/*.mjs'],
|
||||
format: ['progress'],
|
||||
formatOptions: { snippetInterface: 'async-await' },
|
||||
publishQuiet: true,
|
||||
},
|
||||
};
|
||||
57
features/page-title.feature
Normal file
57
features/page-title.feature
Normal file
@@ -0,0 +1,57 @@
|
||||
Feature: Page Title Management
|
||||
As a user navigating the Tímmál application
|
||||
I want page titles to update based on the current page
|
||||
So that I can identify which page I'm on from the browser tab
|
||||
|
||||
Background:
|
||||
Given the page title system is initialized
|
||||
|
||||
Scenario: Default page title on application load
|
||||
When I first load the application
|
||||
Then the page title should be "Tímmál"
|
||||
|
||||
Scenario: Setting a page name updates the title
|
||||
When I navigate to the "Dashboard" page
|
||||
Then the page title should be "Dashboard - Tímmál"
|
||||
|
||||
Scenario: Changing between different pages
|
||||
When I navigate to the "Dashboard" page
|
||||
Then the page title should be "Dashboard - Tímmál"
|
||||
When I navigate to the "Projects" page
|
||||
Then the page title should be "Projects - Tímmál"
|
||||
When I navigate to the "Reports" page
|
||||
Then the page title should be "Reports - Tímmál"
|
||||
|
||||
Scenario: Clearing page name returns to default
|
||||
Given I am on the "Settings" page
|
||||
And the page title is "Settings - Tímmál"
|
||||
When I clear the page name
|
||||
Then the page title should be "Tímmál"
|
||||
|
||||
Scenario: Page title with special characters
|
||||
When I navigate to the "Reports & Analytics" page
|
||||
Then the page title should be "Reports & Analytics - Tímmál"
|
||||
|
||||
Scenario: Page title with unicode characters
|
||||
When I navigate to the "Paramètres" page
|
||||
Then the page title should be "Paramètres - Tímmál"
|
||||
|
||||
Scenario: Multiple users share the same page title state
|
||||
Given user "Alice" sets the page name to "Dashboard"
|
||||
When user "Bob" checks the page title
|
||||
Then user "Bob" should see "Dashboard - Tímmál"
|
||||
|
||||
Scenario Outline: Various page names
|
||||
When I navigate to the "<page_name>" page
|
||||
Then the page title should be "<expected_title>"
|
||||
|
||||
Examples:
|
||||
| page_name | expected_title |
|
||||
| Dashboard | Dashboard - Tímmál |
|
||||
| Projects | Projects - Tímmál |
|
||||
| Tasks | Tasks - Tímmál |
|
||||
| Reports | Reports - Tímmál |
|
||||
| Settings | Settings - Tímmál |
|
||||
| Profile | Profile - Tímmál |
|
||||
| Time Tracking | Time Tracking - Tímmál |
|
||||
| User Management | User Management - Tímmál |
|
||||
98
features/step_definitions/page-title.steps.mjs
Normal file
98
features/step_definitions/page-title.steps.mjs
Normal file
@@ -0,0 +1,98 @@
|
||||
import { Given, When, Then, Before } from '@cucumber/cucumber';
|
||||
import { expect } from 'chai';
|
||||
|
||||
// Shared state (simulating useState behavior)
|
||||
const sharedState = { pageName: '' };
|
||||
|
||||
// Simple mock of usePageTitle behavior for Cucumber tests
|
||||
// This simulates the useState() sharing behavior
|
||||
const createPageTitleMock = () => {
|
||||
return {
|
||||
get title() {
|
||||
return {
|
||||
get value() {
|
||||
return sharedState.pageName.length > 0 ? `${sharedState.pageName} - Tímmál` : 'Tímmál';
|
||||
},
|
||||
};
|
||||
},
|
||||
get pageName() {
|
||||
return {
|
||||
get value() {
|
||||
return sharedState.pageName;
|
||||
},
|
||||
};
|
||||
},
|
||||
setPageName(newName) {
|
||||
sharedState.pageName = newName;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
// Instance for step definitions
|
||||
let pageTitleInstance;
|
||||
const userInstances = new Map();
|
||||
|
||||
Before(function () {
|
||||
// Reset shared state before each scenario
|
||||
sharedState.pageName = '';
|
||||
userInstances.clear();
|
||||
pageTitleInstance = null;
|
||||
});
|
||||
|
||||
Given('the page title system is initialized', function () {
|
||||
pageTitleInstance = createPageTitleMock();
|
||||
pageTitleInstance.setPageName('');
|
||||
});
|
||||
|
||||
When('I first load the application', function () {
|
||||
pageTitleInstance = createPageTitleMock();
|
||||
});
|
||||
|
||||
When('I navigate to the {string} page', function (pageName) {
|
||||
if (!pageTitleInstance) {
|
||||
pageTitleInstance = createPageTitleMock();
|
||||
}
|
||||
pageTitleInstance.setPageName(pageName);
|
||||
});
|
||||
|
||||
When('I clear the page name', function () {
|
||||
pageTitleInstance.setPageName('');
|
||||
});
|
||||
|
||||
Given('I am on the {string} page', function (pageName) {
|
||||
if (!pageTitleInstance) {
|
||||
pageTitleInstance = createPageTitleMock();
|
||||
}
|
||||
pageTitleInstance.setPageName(pageName);
|
||||
});
|
||||
|
||||
Given('the page title is {string}', function (expectedTitle) {
|
||||
expect(pageTitleInstance.title.value).to.equal(expectedTitle);
|
||||
});
|
||||
|
||||
Given('user {string} sets the page name to {string}', function (userName, pageName) {
|
||||
let userInstance = userInstances.get(userName);
|
||||
if (!userInstance) {
|
||||
userInstance = createPageTitleMock();
|
||||
userInstances.set(userName, userInstance);
|
||||
}
|
||||
userInstance.setPageName(pageName);
|
||||
});
|
||||
|
||||
When('user {string} checks the page title', function (userName) {
|
||||
let userInstance = userInstances.get(userName);
|
||||
if (!userInstance) {
|
||||
userInstance = createPageTitleMock();
|
||||
userInstances.set(userName, userInstance);
|
||||
}
|
||||
});
|
||||
|
||||
Then('the page title should be {string}', function (expectedTitle) {
|
||||
expect(pageTitleInstance.title.value).to.equal(expectedTitle);
|
||||
});
|
||||
|
||||
Then('user {string} should see {string}', function (userName, expectedTitle) {
|
||||
const userInstance = userInstances.get(userName);
|
||||
expect(userInstance).to.not.be.undefined;
|
||||
expect(userInstance.title.value).to.equal(expectedTitle);
|
||||
});
|
||||
72
features/step_definitions/page-title.steps.ts
Normal file
72
features/step_definitions/page-title.steps.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { Given, When, Then, Before } from '@cucumber/cucumber';
|
||||
import { expect } from 'vitest';
|
||||
import { usePageTitle } from '../../app/composables/usePageTitle';
|
||||
|
||||
// Store the composable instance
|
||||
let pageTitleInstance: ReturnType<typeof usePageTitle>;
|
||||
let userInstances: Map<string, ReturnType<typeof usePageTitle>>;
|
||||
|
||||
Before(function () {
|
||||
// Reset before each scenario
|
||||
userInstances = new Map();
|
||||
});
|
||||
|
||||
Given('the page title system is initialized', function () {
|
||||
pageTitleInstance = usePageTitle();
|
||||
// Reset to default state
|
||||
pageTitleInstance.setPageName('');
|
||||
});
|
||||
|
||||
When('I first load the application', function () {
|
||||
pageTitleInstance = usePageTitle();
|
||||
});
|
||||
|
||||
When('I navigate to the {string} page', function (pageName: string) {
|
||||
if (!pageTitleInstance) {
|
||||
pageTitleInstance = usePageTitle();
|
||||
}
|
||||
pageTitleInstance.setPageName(pageName);
|
||||
});
|
||||
|
||||
When('I clear the page name', function () {
|
||||
pageTitleInstance.setPageName('');
|
||||
});
|
||||
|
||||
Given('I am on the {string} page', function (pageName: string) {
|
||||
if (!pageTitleInstance) {
|
||||
pageTitleInstance = usePageTitle();
|
||||
}
|
||||
pageTitleInstance.setPageName(pageName);
|
||||
});
|
||||
|
||||
Given('the page title is {string}', function (expectedTitle: string) {
|
||||
expect(pageTitleInstance.title.value).toBe(expectedTitle);
|
||||
});
|
||||
|
||||
Given('user {string} sets the page name to {string}', function (userName: string, pageName: string) {
|
||||
let userInstance = userInstances.get(userName);
|
||||
if (!userInstance) {
|
||||
userInstance = usePageTitle();
|
||||
userInstances.set(userName, userInstance);
|
||||
}
|
||||
userInstance.setPageName(pageName);
|
||||
});
|
||||
|
||||
When('user {string} checks the page title', function (userName: string) {
|
||||
let userInstance = userInstances.get(userName);
|
||||
if (!userInstance) {
|
||||
userInstance = usePageTitle();
|
||||
userInstances.set(userName, userInstance);
|
||||
}
|
||||
// Just store the instance, we'll check in Then step
|
||||
});
|
||||
|
||||
Then('the page title should be {string}', function (expectedTitle: string) {
|
||||
expect(pageTitleInstance.title.value).toBe(expectedTitle);
|
||||
});
|
||||
|
||||
Then('user {string} should see {string}', function (userName: string, expectedTitle: string) {
|
||||
const userInstance = userInstances.get(userName);
|
||||
expect(userInstance).toBeDefined();
|
||||
expect(userInstance!.title.value).toBe(expectedTitle);
|
||||
});
|
||||
@@ -36,6 +36,7 @@
|
||||
"@vitest/coverage-v8": "4.0.15",
|
||||
"@vitest/ui": "^4.0.15",
|
||||
"@vue/test-utils": "^2.4.6",
|
||||
"chai": "^6.2.1",
|
||||
"eslint": "^9.39.1",
|
||||
"happy-dom": "^20.0.11",
|
||||
"playwright-core": "^1.57.0",
|
||||
|
||||
36
pb_migrations/1765135365_updated_users.js
Normal file
36
pb_migrations/1765135365_updated_users.js
Normal file
@@ -0,0 +1,36 @@
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
migrate((app) => {
|
||||
const collection = app.findCollectionByNameOrId('_pb_users_auth_')
|
||||
|
||||
// update collection data
|
||||
unmarshal({
|
||||
oauth2: {
|
||||
enabled: true
|
||||
},
|
||||
otp: {
|
||||
enabled: true
|
||||
},
|
||||
passwordAuth: {
|
||||
enabled: false
|
||||
}
|
||||
}, collection)
|
||||
|
||||
return app.save(collection)
|
||||
}, (app) => {
|
||||
const collection = app.findCollectionByNameOrId('_pb_users_auth_')
|
||||
|
||||
// update collection data
|
||||
unmarshal({
|
||||
oauth2: {
|
||||
enabled: false
|
||||
},
|
||||
otp: {
|
||||
enabled: false
|
||||
},
|
||||
passwordAuth: {
|
||||
enabled: true
|
||||
}
|
||||
}, collection)
|
||||
|
||||
return app.save(collection)
|
||||
})
|
||||
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -54,6 +54,9 @@ importers:
|
||||
'@vue/test-utils':
|
||||
specifier: ^2.4.6
|
||||
version: 2.4.6
|
||||
chai:
|
||||
specifier: ^6.2.1
|
||||
version: 6.2.1
|
||||
eslint:
|
||||
specifier: ^9.39.1
|
||||
version: 9.39.1(jiti@2.6.1)
|
||||
|
||||
Reference in New Issue
Block a user