Refactoring code for modular approach
This commit is contained in:
45
tests/test_error_handling.py
Normal file
45
tests/test_error_handling.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# tests/test_error_handling.py
|
||||
import pytest
|
||||
|
||||
|
||||
class TestErrorHandling:
|
||||
def test_invalid_json(self, client, auth_headers):
|
||||
"""Test handling of invalid JSON"""
|
||||
response = client.post("/api/v1/products",
|
||||
headers=auth_headers,
|
||||
data="invalid json")
|
||||
|
||||
assert response.status_code == 422 # Validation error
|
||||
|
||||
def test_missing_required_fields(self, client, auth_headers):
|
||||
"""Test handling of missing required fields"""
|
||||
response = client.post("/api/v1/products",
|
||||
headers=auth_headers,
|
||||
json={"title": "Test"}) # Missing product_id
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
def test_invalid_authentication(self, client):
|
||||
"""Test handling of invalid authentication"""
|
||||
response = client.get("/api/v1/products",
|
||||
headers={"Authorization": "Bearer invalid_token"})
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
def test_nonexistent_resource(self, client, auth_headers):
|
||||
"""Test handling of nonexistent resource access"""
|
||||
response = client.get("/api/v1/products/NONEXISTENT", headers=auth_headers)
|
||||
assert response.status_code == 404
|
||||
|
||||
response = client.get("/api/v1/shops/NONEXISTENT", headers=auth_headers)
|
||||
assert response.status_code == 404
|
||||
|
||||
def test_duplicate_resource_creation(self, client, auth_headers, test_product):
|
||||
"""Test handling of duplicate resource creation"""
|
||||
product_data = {
|
||||
"product_id": test_product.product_id, # Duplicate ID
|
||||
"title": "Another Product"
|
||||
}
|
||||
|
||||
response = client.post("/api/v1/products", headers=auth_headers, json=product_data)
|
||||
assert response.status_code == 400
|
||||
Reference in New Issue
Block a user