import axios from 'axios'; const API_URL = 'https://api.filamenteka.rs/api'; const TEST_TIMEOUT = 30000; // 30 seconds describe('API Integration Tests', () => { let authToken: string; let createdFilamentId: 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 filaments that might have been left behind if (createdFilamentId) { try { await axios.delete( `${API_URL}/filaments/${createdFilamentId}`, { headers: { 'Authorization': `Bearer ${authToken}` } } ); } catch (error) { // Ignore errors - filament might already be deleted } } }, TEST_TIMEOUT); describe('Filament CRUD Operations', () => { it('should create a new filament', async () => { const newFilament = { tip: 'PLA', finish: 'Basic', boja: 'Black', boja_hex: '#000000', refill: '2', vakum: '1 vakuum', kolicina: '3', cena: '3999' }; const response = await axios.post( `${API_URL}/filaments`, newFilament, { headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } } ); expect(response.status).toBe(200); expect(response.data).toHaveProperty('id'); expect(response.data.tip).toBe('PLA'); expect(response.data.boja).toBe('Black'); // Store ID for cleanup createdFilamentId = response.data.id; }, TEST_TIMEOUT); it('should retrieve all filaments including the created one', async () => { const response = await axios.get(`${API_URL}/filaments`); expect(response.status).toBe(200); expect(Array.isArray(response.data)).toBe(true); // Find our created filament const ourFilament = response.data.find((f: any) => f.id === createdFilamentId); expect(ourFilament).toBeDefined(); expect(ourFilament.boja).toBe('Black'); }, TEST_TIMEOUT); it('should update the created filament', async () => { const updateData = { tip: 'PETG', finish: 'Silk', boja: 'Blue', boja_hex: '#1E88E5', refill: '3', vakum: '2 vakuum', kolicina: '6', cena: '4500' }; const response = await axios.put( `${API_URL}/filaments/${createdFilamentId}`, updateData, { headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } } ); expect(response.status).toBe(200); expect(response.data.tip).toBe('PETG'); expect(response.data.boja).toBe('Blue'); expect(response.data.cena).toBe('4500'); }, TEST_TIMEOUT); it('should delete the created filament', async () => { const response = await axios.delete( `${API_URL}/filaments/${createdFilamentId}`, { headers: { 'Authorization': `Bearer ${authToken}` } } ); expect(response.status).toBe(200); // Verify it's deleted const getResponse = await axios.get(`${API_URL}/filaments`); const deletedFilament = getResponse.data.find((f: any) => f.id === createdFilamentId); expect(deletedFilament).toBeUndefined(); }, TEST_TIMEOUT); }); describe('Error Handling', () => { it('should return 401 for unauthorized requests', async () => { await expect( axios.post(`${API_URL}/filaments`, {}, { headers: { 'Content-Type': 'application/json' } }) ).rejects.toMatchObject({ response: { status: 401 } }); }); it('should handle invalid data gracefully', async () => { await expect( axios.post( `${API_URL}/filaments`, { invalid: 'data' }, { headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } } ) ).rejects.toMatchObject({ response: { status: 500 } }); }); }); });