vendor refactoring
This commit is contained in:
@@ -16,7 +16,7 @@ class TestErrorHandling:
|
||||
def test_invalid_json_request(self, client, auth_headers):
|
||||
"""Test handling of malformed JSON requests"""
|
||||
response = client.post(
|
||||
"/api/v1/vendor ",
|
||||
"/api/v1/vendor",
|
||||
headers=auth_headers,
|
||||
content="{ invalid json syntax"
|
||||
)
|
||||
@@ -31,9 +31,9 @@ class TestErrorHandling:
|
||||
"""Test validation errors for missing required fields"""
|
||||
# Missing vendor_name
|
||||
response = client.post(
|
||||
"/api/v1/vendor ",
|
||||
"/api/v1/vendor",
|
||||
headers=auth_headers,
|
||||
json={"vendor_code": "TESTSHOP"}
|
||||
json={"vendor_code": "TESTVENDOR"}
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
@@ -46,11 +46,11 @@ class TestErrorHandling:
|
||||
"""Test validation errors for invalid field formats"""
|
||||
# Invalid vendor_code format (contains special characters)
|
||||
response = client.post(
|
||||
"/api/v1/vendor ",
|
||||
"/api/v1/vendor",
|
||||
headers=auth_headers,
|
||||
json={
|
||||
"vendor_code": "INVALID@SHOP!",
|
||||
"vendor_name": "Test Shop"
|
||||
"vendor_code": "INVALID@VENDOR!",
|
||||
"vendor_name": "Test Vendor"
|
||||
}
|
||||
)
|
||||
|
||||
@@ -63,7 +63,7 @@ class TestErrorHandling:
|
||||
|
||||
def test_missing_authentication_token(self, client):
|
||||
"""Test authentication required endpoints without token"""
|
||||
response = client.get("/api/v1/vendor ")
|
||||
response = client.get("/api/v1/vendor")
|
||||
|
||||
assert response.status_code == 401
|
||||
data = response.json()
|
||||
@@ -73,7 +73,7 @@ class TestErrorHandling:
|
||||
def test_invalid_authentication_token(self, client):
|
||||
"""Test endpoints with invalid JWT token"""
|
||||
headers = {"Authorization": "Bearer invalid_token_here"}
|
||||
response = client.get("/api/v1/vendor ", headers=headers)
|
||||
response = client.get("/api/v1/vendor", headers=headers)
|
||||
|
||||
assert response.status_code == 401
|
||||
data = response.json()
|
||||
@@ -85,7 +85,7 @@ class TestErrorHandling:
|
||||
# This would require creating an expired token for testing
|
||||
expired_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.expired.token"
|
||||
headers = {"Authorization": f"Bearer {expired_token}"}
|
||||
response = client.get("/api/v1/vendor ", headers=headers)
|
||||
response = client.get("/api/v1/vendor", headers=headers)
|
||||
|
||||
assert response.status_code == 401
|
||||
data = response.json()
|
||||
@@ -93,13 +93,13 @@ class TestErrorHandling:
|
||||
|
||||
def test_vendor_not_found(self, client, auth_headers):
|
||||
"""Test accessing non-existent vendor """
|
||||
response = client.get("/api/v1/vendor /NONEXISTENT", headers=auth_headers)
|
||||
response = client.get("/api/v1/vendor/NONEXISTENT", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 404
|
||||
data = response.json()
|
||||
assert data["error_code"] == "VENDOR_NOT_FOUND"
|
||||
assert data["status_code"] == 404
|
||||
assert data["details"]["resource_type"] == "Shop"
|
||||
assert data["details"]["resource_type"] == "Vendor"
|
||||
assert data["details"]["identifier"] == "NONEXISTENT"
|
||||
|
||||
def test_product_not_found(self, client, auth_headers):
|
||||
@@ -117,10 +117,10 @@ class TestErrorHandling:
|
||||
"""Test creating vendor with duplicate vendor code"""
|
||||
vendor_data = {
|
||||
"vendor_code": test_vendor.vendor_code,
|
||||
"vendor_name": "Duplicate Shop"
|
||||
"vendor_name": "Duplicate Vendor"
|
||||
}
|
||||
|
||||
response = client.post("/api/v1/vendor ", headers=auth_headers, json=vendor_data)
|
||||
response = client.post("/api/v1/vendor", headers=auth_headers, json=vendor_data)
|
||||
|
||||
assert response.status_code == 409
|
||||
data = response.json()
|
||||
@@ -146,7 +146,7 @@ class TestErrorHandling:
|
||||
|
||||
def test_unauthorized_vendor_access(self, client, auth_headers, inactive_vendor):
|
||||
"""Test accessing vendor without proper permissions"""
|
||||
response = client.get(f"/api/v1/vendor /{inactive_vendor.vendor_code}", headers=auth_headers)
|
||||
response = client.get(f"/api/v1/vendor/{inactive_vendor.vendor_code}", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 403
|
||||
data = response.json()
|
||||
@@ -171,10 +171,10 @@ class TestErrorHandling:
|
||||
vendors_created = []
|
||||
for i in range(6): # Assume limit is 5
|
||||
vendor_data = {
|
||||
"vendor_code": f"SHOP{i:03d}",
|
||||
"vendor_code": f"VENDOR{i:03d}",
|
||||
"vendor_name": f"Test Vendor {i}"
|
||||
}
|
||||
response = client.post("/api/v1/vendor ", headers=auth_headers, json=vendor_data)
|
||||
response = client.post("/api/v1/vendor", headers=auth_headers, json=vendor_data)
|
||||
vendors_created.append(response)
|
||||
|
||||
# At least one should succeed, and if limit is enforced, later ones should fail
|
||||
@@ -246,7 +246,7 @@ class TestErrorHandling:
|
||||
def test_method_not_allowed(self, client, auth_headers):
|
||||
"""Test 405 for wrong HTTP method on existing endpoints"""
|
||||
# Try DELETE on an endpoint that only supports GET
|
||||
response = client.delete("/api/v1/vendor ", headers=auth_headers)
|
||||
response = client.delete("/api/v1/vendor", headers=auth_headers)
|
||||
|
||||
assert response.status_code == 405
|
||||
# FastAPI automatically handles 405 errors
|
||||
@@ -255,7 +255,7 @@ class TestErrorHandling:
|
||||
"""Test handling of unsupported content types"""
|
||||
headers = {**auth_headers, "Content-Type": "application/xml"}
|
||||
response = client.post(
|
||||
"/api/v1/vendor ",
|
||||
"/api/v1/vendor",
|
||||
headers=headers,
|
||||
content="<vendor ><code>TEST</code></vendor >"
|
||||
)
|
||||
@@ -266,12 +266,12 @@ class TestErrorHandling:
|
||||
"""Test handling of unusually large payloads"""
|
||||
large_description = "x" * 100000 # Very long description
|
||||
vendor_data = {
|
||||
"vendor_code": "LARGESHOP",
|
||||
"vendor_name": "Large Shop",
|
||||
"vendor_code": "LARGEVENDOR",
|
||||
"vendor_name": "Large Vendor",
|
||||
"description": large_description
|
||||
}
|
||||
|
||||
response = client.post("/api/v1/vendor ", headers=auth_headers, json=vendor_data)
|
||||
response = client.post("/api/v1/vendor", headers=auth_headers, json=vendor_data)
|
||||
|
||||
# Should either accept it or reject with appropriate error
|
||||
assert response.status_code in [200, 201, 413, 422]
|
||||
@@ -285,7 +285,7 @@ class TestErrorHandling:
|
||||
# Make rapid requests to potentially trigger rate limiting
|
||||
responses = []
|
||||
for _ in range(50): # Aggressive request count
|
||||
response = client.get("/api/v1/vendor ", headers=auth_headers)
|
||||
response = client.get("/api/v1/vendor", headers=auth_headers)
|
||||
responses.append(response)
|
||||
|
||||
# Check if any rate limiting occurred and verify error structure
|
||||
@@ -344,7 +344,7 @@ class TestErrorHandling:
|
||||
def test_error_response_consistency(self, client, auth_headers):
|
||||
"""Test that all error responses follow consistent structure"""
|
||||
test_cases = [
|
||||
("/api/v1/vendor /NONEXISTENT", 404),
|
||||
("/api/v1/vendor/NONEXISTENT", 404),
|
||||
("/api/v1/marketplace/product/NONEXISTENT", 404),
|
||||
]
|
||||
|
||||
@@ -365,7 +365,7 @@ class TestErrorHandling:
|
||||
def test_cors_error_handling(self, client):
|
||||
"""Test CORS errors are handled properly"""
|
||||
# Test preflight request
|
||||
response = client.options("/api/v1/vendor ")
|
||||
response = client.options("/api/v1/vendor")
|
||||
|
||||
# Should either succeed or be handled gracefully
|
||||
assert response.status_code in [200, 204, 405]
|
||||
@@ -373,7 +373,7 @@ class TestErrorHandling:
|
||||
def test_authentication_error_details(self, client):
|
||||
"""Test authentication error provides helpful details"""
|
||||
# Test missing Authorization header
|
||||
response = client.get("/api/v1/vendor ")
|
||||
response = client.get("/api/v1/vendor")
|
||||
|
||||
assert response.status_code == 401
|
||||
data = response.json()
|
||||
@@ -406,7 +406,7 @@ class TestErrorRecovery:
|
||||
assert health_response.status_code == 200
|
||||
|
||||
# API endpoints may or may not work depending on system state
|
||||
api_response = client.get("/api/v1/vendor ", headers=auth_headers)
|
||||
api_response = client.get("/api/v1/vendor", headers=auth_headers)
|
||||
# Should get either data or a proper error, not a crash
|
||||
assert api_response.status_code in [200, 401, 403, 500, 503]
|
||||
|
||||
@@ -416,7 +416,7 @@ class TestErrorRecovery:
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
# Trigger an error
|
||||
client.get("/api/v1/vendor /NONEXISTENT", headers=auth_headers)
|
||||
client.get("/api/v1/vendor/NONEXISTENT", headers=auth_headers)
|
||||
|
||||
# Check that error was logged (if your app logs 404s as errors)
|
||||
# Adjust based on your logging configuration
|
||||
|
||||
Reference in New Issue
Block a user