import axios from 'axios'; const API_URL = 'https://api.filamenteka.rs/api'; const TEST_TIMEOUT = 30000; describe('Color Management Tests', () => { let authToken: string; let createdColorId: string; beforeAll(async () => { // Login to get auth token const loginResponse = await axios.post(`${API_URL}/login`, { username: 'admin', password: 'admin123' }); authToken = loginResponse.data.token; }, TEST_TIMEOUT); afterAll(async () => { // Clean up any test colors that might have been left behind if (createdColorId) { try { await axios.delete( `${API_URL}/colors/${createdColorId}`, { headers: { 'Authorization': `Bearer ${authToken}` } } ); } catch (error) { // Ignore errors - color might already be deleted } } }, TEST_TIMEOUT); describe('Color CRUD Operations', () => { it('should create a new color with "Test" prefix', async () => { const testColor = { name: 'Test Color ' + Date.now(), // Unique name to avoid conflicts hex: '#FF00FF' }; const response = await axios.post( `${API_URL}/colors`, testColor, { headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } } ); expect(response.status).toBe(200); expect(response.data).toHaveProperty('id'); expect(response.data.name).toBe(testColor.name); expect(response.data.hex).toBe(testColor.hex); // Store ID for cleanup createdColorId = response.data.id; }, TEST_TIMEOUT); it('should retrieve the created test color', async () => { const response = await axios.get(`${API_URL}/colors`); expect(response.status).toBe(200); expect(Array.isArray(response.data)).toBe(true); // Find our created color const ourColor = response.data.find((c: any) => c.id === createdColorId); expect(ourColor).toBeDefined(); expect(ourColor.name).toContain('Test Color'); }, TEST_TIMEOUT); it('should update the test color', async () => { const updateData = { name: 'Test Color Updated ' + Date.now(), hex: '#00FF00' }; const response = await axios.put( `${API_URL}/colors/${createdColorId}`, updateData, { headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } } ); expect(response.status).toBe(200); expect(response.data.name).toBe(updateData.name); expect(response.data.hex).toBe(updateData.hex); }, TEST_TIMEOUT); it('should delete the test color', async () => { const response = await axios.delete( `${API_URL}/colors/${createdColorId}`, { headers: { 'Authorization': `Bearer ${authToken}` } } ); expect(response.status).toBe(200); // Verify it's deleted const getResponse = await axios.get(`${API_URL}/colors`); const deletedColor = getResponse.data.find((c: any) => c.id === createdColorId); expect(deletedColor).toBeUndefined(); // Clear the ID since it's deleted createdColorId = ''; }, TEST_TIMEOUT); }); describe('Cleanup Test Colors', () => { it('should clean up any colors starting with "Test"', async () => { // Get all colors const response = await axios.get(`${API_URL}/colors`); const testColors = response.data.filter((c: any) => c.name.startsWith('Test') || c.name.toLowerCase().includes('test') ); // Delete each test color for (const color of testColors) { try { await axios.delete( `${API_URL}/colors/${color.id}`, { headers: { 'Authorization': `Bearer ${authToken}` } } ); console.log(`Cleaned up test color: ${color.name}`); } catch (error) { console.error(`Failed to clean up color ${color.name}:`, error.message); } } // Verify cleanup const verifyResponse = await axios.get(`${API_URL}/colors`); const remainingTestColors = verifyResponse.data.filter((c: any) => c.name.startsWith('Test') || c.name.toLowerCase().includes('test') ); expect(remainingTestColors.length).toBe(0); }, TEST_TIMEOUT); }); });