Files
flyer-crawler.projectium.com/src/routes/user.integration.test.ts
Torben Sorensen f43db7f82e
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 1m21s
frontend work !
2025-11-22 13:36:04 -08:00

87 lines
3.2 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import * as apiClient from '../services/apiClient';
import type { User, Profile } from '../types';
/**
* @vitest-environment node
*/
/**
* A helper function to create a new user and log them in, returning the user object and auth token.
* This provides an authenticated context for testing protected API endpoints.
*/
const createAndLoginUser = async (email: string, password = 'password123') => {
// Register the new user.
await apiClient.registerUser(email, password, 'Test User');
// Log in to get the auth token.
const { user, token } = await apiClient.loginUser(email, password, false);
return { user, token };
};
describe('User API Routes Integration Tests', () => {
let testUser: User;
let authToken: string;
// Before any tests run, create a new user and log them in.
// The token will be used for all subsequent API calls in this test suite.
beforeAll(async () => {
const email = `user-test-${Date.now()}@example.com`;
const { user, token } = await createAndLoginUser(email);
testUser = user;
authToken = token;
});
// After all tests, clean up by deleting the created user.
afterAll(async () => {
if (testUser) {
// This requires an authenticated call to delete the account.
await apiClient.deleteUserAccount('password123', authToken);
}
});
it('should fetch the authenticated user profile via GET /api/users/profile', async () => {
// Act: Call the API endpoint using the authenticated token.
const profile = await apiClient.getAuthenticatedUserProfile(authToken);
// Assert: Verify the profile data matches the created user.
expect(profile).toBeDefined();
expect(profile.id).toBe(testUser.id);
expect(profile.user.email).toBe(testUser.email);
expect(profile.full_name).toBe('Test User');
expect(profile.role).toBe('user');
});
it('should update the user profile via PUT /api/users/profile', async () => {
// Arrange: Define the profile updates.
const profileUpdates = {
full_name: 'Updated Test User',
};
// Act: Call the update endpoint with the new data and the auth token.
const updatedProfile = await apiClient.updateUserProfile(profileUpdates, authToken);
// Assert: Check that the returned profile reflects the changes.
expect(updatedProfile).toBeDefined();
expect(updatedProfile.full_name).toBe('Updated Test User');
// Also, fetch the profile again to ensure the change was persisted.
const refetchedProfile = await apiClient.getAuthenticatedUserProfile(authToken);
expect(refetchedProfile.full_name).toBe('Updated Test User');
});
it('should update user preferences via PUT /api/users/profile/preferences', async () => {
// Arrange: Define the preference updates.
const preferenceUpdates = {
darkMode: true,
};
// Act: Call the update endpoint.
const updatedProfile = await apiClient.updateUserPreferences(preferenceUpdates, authToken);
// Assert: Check that the preferences object in the returned profile is updated.
expect(updatedProfile).toBeDefined();
expect(updatedProfile.preferences).toBeDefined();
expect(updatedProfile.preferences?.darkMode).toBe(true);
});
});