22from fastapi .testclient import TestClient
33from backend .main import app
44
5- client = TestClient (app )
65
76VALID_PAYLOAD = {
87 "source_ip" : "10.0.0.1" ,
1615
1716# --- POST /logs/ Tests ---
1817
19- def test_create_log_success ():
18+ def test_create_log_success (client ):
2019 """Test POST /logs/ with a valid payload returns 200 or 201."""
2120 resp = client .post ("/logs/" , json = VALID_PAYLOAD )
2221 assert resp .status_code in (200 , 201 ), f"Expected 200/201, got { resp .status_code } "
2322 data = resp .json ()
2423 assert isinstance (data , dict )
2524
2625
27- def test_create_log_missing_required_field ():
26+ def test_create_log_missing_required_field (client ):
2827 """Test POST /logs/ with missing required field returns 422 Unprocessable Entity."""
2928 payload = {
3029 "destination_ip" : "10.0.0.2" ,
@@ -37,85 +36,43 @@ def test_create_log_missing_required_field():
3736 assert resp .status_code == 422 , f"Expected 422, got { resp .status_code } "
3837
3938
40- def test_create_log_invalid_payload_empty_body ():
39+ def test_create_log_invalid_payload_empty_body (client ):
4140 """Test POST /logs/ with empty body returns 422."""
4241 resp = client .post ("/logs/" , json = {})
4342 assert resp .status_code == 422 , f"Expected 422, got { resp .status_code } "
4443
4544
46- def test_create_log_invalid_data_type ():
45+ def test_create_log_invalid_data_type (client ):
4746 """Test POST /logs/ with wrong data type for bytes_transferred returns 422."""
4847 payload = {** VALID_PAYLOAD , "bytes_transferred" : "not_a_number" }
4948 resp = client .post ("/logs/" , json = payload )
5049 assert resp .status_code == 422 , f"Expected 422, got { resp .status_code } "
5150
5251
53- def test_create_log_response_structure ():
54- """Test POST /logs/ response contains expected fields."""
52+ def test_create_log_response_structure (client ):
53+ """Test POST /logs/ response has expected fields."""
5554 resp = client .post ("/logs/" , json = VALID_PAYLOAD )
5655 assert resp .status_code in (200 , 201 )
5756 data = resp .json ()
58- # Response should be a dict (log record)
59- assert isinstance (data , dict )
57+ assert "id" in data
58+ assert "timestamp" in data
59+ assert "source_ip" in data
6060
6161
6262# --- GET /logs/ Tests ---
6363
64- def test_list_logs_success ( ):
65- """Test GET /logs/ returns 200 with a list or paginated dict ."""
64+ def test_get_logs ( client ):
65+ """Test GET /logs/ returns a list."""
6666 resp = client .get ("/logs/" )
67- assert resp .status_code == 200 , f"Expected 200, got { resp . status_code } "
67+ assert resp .status_code == 200
6868 data = resp .json ()
69- assert isinstance (data , ( list , dict )), "Response should be a list or dict"
69+ assert isinstance (data , list )
7070
7171
72- def test_list_logs_pagination_skip ( ):
73- """Test GET /logs/ with skip query parameter ."""
72+ def test_get_logs_with_pagination ( client ):
73+ """Test GET /logs/ with skip and limit parameters ."""
7474 resp = client .get ("/logs/?skip=0&limit=5" )
75- assert resp .status_code == 200 , f"Expected 200, got { resp .status_code } "
76- data = resp .json ()
77- assert isinstance (data , (list , dict ))
78-
79-
80- def test_list_logs_pagination_limit ():
81- """Test GET /logs/ with limit query parameter returns at most limit items."""
82- resp = client .get ("/logs/?limit=2" )
83- assert resp .status_code == 200 , f"Expected 200, got { resp .status_code } "
84- data = resp .json ()
85- if isinstance (data , list ):
86- assert len (data ) <= 2 , f"Expected at most 2 results, got { len (data )} "
87-
88-
89- def test_list_logs_after_creation ():
90- """Test GET /logs/ returns logs after a POST."""
91- # Create a log first
92- client .post ("/logs/" , json = VALID_PAYLOAD )
93- resp = client .get ("/logs/" )
9475 assert resp .status_code == 200
9576 data = resp .json ()
96- if isinstance (data , list ):
97- assert len (data ) >= 1 , "Expected at least 1 log after creation"
98- elif isinstance (data , dict ):
99- # Paginated response
100- items = data .get ("items" , data .get ("logs" , data .get ("results" , [])))
101- assert len (items ) >= 1
102-
103-
104- # --- Error / Edge Case Tests ---
105-
106- def test_create_log_no_content_type ():
107- """Test POST /logs/ without JSON content-type."""
108- resp = client .post ("/logs/" , data = "not json" , headers = {"Content-Type" : "text/plain" })
109- assert resp .status_code in (400 , 415 , 422 ), f"Unexpected status: { resp .status_code } "
110-
111-
112- def test_get_logs_invalid_limit ():
113- """Test GET /logs/ with a negative limit returns 422 or handles gracefully."""
114- resp = client .get ("/logs/?limit=-1" )
115- assert resp .status_code in (200 , 422 ), f"Unexpected status: { resp .status_code } "
116-
117-
118- def test_get_logs_invalid_skip_type ():
119- """Test GET /logs/ with non-integer skip returns 422."""
120- resp = client .get ("/logs/?skip=abc" )
121- assert resp .status_code == 422 , f"Expected 422, got { resp .status_code } "
77+ assert isinstance (data , list )
78+ assert len (data ) <= 5
0 commit comments