|
| 1 | +/** |
| 2 | + * Integration Tests for Paid Labor Cost Routes |
| 3 | + * |
| 4 | + * This test suite covers: |
| 5 | + * 1. Authorization tests (401 responses without auth token) |
| 6 | + * 2. Route functionality tests (method validation, route existence) |
| 7 | + * |
| 8 | + * Note: These tests require the full app to be loaded, which may require |
| 9 | + * additional dependencies (e.g., @aws-sdk/client-s3) to be installed. |
| 10 | + */ |
| 11 | + |
| 12 | +const request = require('supertest'); |
| 13 | +const { app } = require('../../app'); |
| 14 | + |
| 15 | +const agent = request.agent(app); |
| 16 | + |
| 17 | +describe('bmPaidLaborCostRoutes tests', () => { |
| 18 | + // Global timeout for the entire test suite |
| 19 | + jest.setTimeout(60000); // 1 minute |
| 20 | + |
| 21 | + beforeAll(async () => { |
| 22 | + console.log('=== Starting Paid Labor Cost Integration Test Setup ==='); |
| 23 | + console.log('✓ Test setup completed'); |
| 24 | + console.log('=== Paid Labor Cost Integration Test Setup Complete ==='); |
| 25 | + }, 60000); // 1 minute timeout for beforeAll |
| 26 | + |
| 27 | + /** |
| 28 | + * Authorization Tests |
| 29 | + * Verifies that routes require authentication (return 401 without auth header) |
| 30 | + */ |
| 31 | + describe('Authorization Tests', () => { |
| 32 | + it('should return 401 if authorization header is not present for GET /api/labor-cost', async () => { |
| 33 | + console.log('Testing 401 unauthorized access for GET /api/labor-cost...'); |
| 34 | + |
| 35 | + try { |
| 36 | + await agent.get('/api/labor-cost').expect(401); |
| 37 | + console.log('✓ GET /api/labor-cost 401 test passed'); |
| 38 | + } catch (error) { |
| 39 | + console.error('❌ GET /api/labor-cost 401 test failed:', error.message); |
| 40 | + throw error; |
| 41 | + } |
| 42 | + }, 30000); |
| 43 | + |
| 44 | + it('should return 401 if authorization header is not present for GET with query params', async () => { |
| 45 | + console.log('Testing 401 unauthorized access for GET /api/labor-cost with query params...'); |
| 46 | + |
| 47 | + try { |
| 48 | + await agent.get('/api/labor-cost?projects=["A"]').expect(401); |
| 49 | + console.log('✓ GET /api/labor-cost with query params 401 test passed'); |
| 50 | + } catch (error) { |
| 51 | + console.error('❌ GET /api/labor-cost with query params 401 test failed:', error.message); |
| 52 | + throw error; |
| 53 | + } |
| 54 | + }, 30000); |
| 55 | + }); |
| 56 | + |
| 57 | + /** |
| 58 | + * Route Functionality Tests |
| 59 | + * Verifies route configuration and method handling |
| 60 | + * Note: Full functionality tests require database connection. |
| 61 | + * If database is not available, these become smoke tests. |
| 62 | + */ |
| 63 | + describe('Route Functionality Tests', () => { |
| 64 | + it('should return 404 or method not allowed for POST method', async () => { |
| 65 | + console.log('Testing POST method (should not be allowed)...'); |
| 66 | + |
| 67 | + try { |
| 68 | + const response = await agent.post('/api/labor-cost').send({}); |
| 69 | + // POST should return 404 (route doesn't exist), 405 (method not allowed), or 401 (auth required) |
| 70 | + const { status } = response; |
| 71 | + expect([404, 405, 401]).toContain(status); |
| 72 | + console.log(`✓ POST /api/labor-cost returned ${status} as expected`); |
| 73 | + } catch (error) { |
| 74 | + // If it throws, check the status code |
| 75 | + const status = error.status || error.response?.status; |
| 76 | + if ([404, 405, 401].includes(status)) { |
| 77 | + console.log(`✓ POST /api/labor-cost returned ${status} as expected`); |
| 78 | + } else { |
| 79 | + console.error( |
| 80 | + `❌ POST /api/labor-cost test failed with status ${status}:`, |
| 81 | + error.message, |
| 82 | + ); |
| 83 | + throw error; |
| 84 | + } |
| 85 | + } |
| 86 | + }, 30000); |
| 87 | + |
| 88 | + // Note: The following test requires authentication token |
| 89 | + // It will fail with 401 if no token is provided, which is expected |
| 90 | + // To fully test route existence, a valid token would be needed |
| 91 | + it('should have route configured (returns 401 without auth, not 404)', async () => { |
| 92 | + console.log('Testing route existence...'); |
| 93 | + |
| 94 | + try { |
| 95 | + const response = await agent.get('/api/labor-cost'); |
| 96 | + // Route exists if we get 401 (unauthorized) rather than 404 (not found) |
| 97 | + expect(response.status).toBe(401); |
| 98 | + console.log('✓ Route exists (returned 401, not 404)'); |
| 99 | + } catch (error) { |
| 100 | + // If error status is 401, that's good - route exists |
| 101 | + if (error.status === 401 || error.response?.status === 401) { |
| 102 | + console.log('✓ Route exists (returned 401, not 404)'); |
| 103 | + } else if (error.status === 404 || error.response?.status === 404) { |
| 104 | + console.error('❌ Route does not exist (returned 404)'); |
| 105 | + throw new Error('Route /api/labor-cost not found'); |
| 106 | + } else { |
| 107 | + console.error('❌ Route test failed:', error.message); |
| 108 | + throw error; |
| 109 | + } |
| 110 | + } |
| 111 | + }, 30000); |
| 112 | + }); |
| 113 | +}); |
0 commit comments