Add tests for extraction API endpoints and enhance existing tests
- Implement tests for admin extraction API endpoints including status retrieval, deletion of extractions, and permission checks. - Add tests for user extraction deletion, ensuring proper handling of permissions and non-existent extractions. - Enhance sound endpoint tests to include duplicate handling in responses. - Refactor favorite service tests to utilize mock dependencies for better maintainability and clarity. - Update sound scanner tests to improve file handling and ensure proper deletion of associated files.
This commit is contained in:
@@ -229,3 +229,73 @@ class TestExtractionEndpoints:
|
||||
break
|
||||
|
||||
assert processing_found, "Processing extraction not found in results"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_extraction_success(self, authenticated_client, test_user, test_session):
|
||||
"""Test successful deletion of user's own extraction."""
|
||||
# Create test extraction
|
||||
extraction = Extraction(
|
||||
url="https://example.com/video",
|
||||
user_id=test_user.id,
|
||||
status="completed",
|
||||
)
|
||||
test_session.add(extraction)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(extraction)
|
||||
|
||||
# Delete the extraction
|
||||
response = await authenticated_client.delete(f"/api/v1/extractions/{extraction.id}")
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["message"] == f"Extraction {extraction.id} deleted successfully"
|
||||
|
||||
# Verify extraction was deleted from database
|
||||
deleted_extraction = await test_session.get(Extraction, extraction.id)
|
||||
assert deleted_extraction is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_extraction_not_found(self, authenticated_client):
|
||||
"""Test deleting non-existent extraction."""
|
||||
response = await authenticated_client.delete("/api/v1/extractions/999")
|
||||
|
||||
assert response.status_code == 404
|
||||
data = response.json()
|
||||
assert "not found" in data["detail"].lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_extraction_permission_denied(self, authenticated_client, test_session, test_plan):
|
||||
"""Test deleting another user's extraction."""
|
||||
# Create extraction owned by different user
|
||||
other_user = User(
|
||||
name="Other User",
|
||||
email="other@example.com",
|
||||
is_active=True,
|
||||
plan_id=test_plan.id,
|
||||
)
|
||||
test_session.add(other_user)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(other_user)
|
||||
|
||||
extraction = Extraction(
|
||||
url="https://example.com/video",
|
||||
user_id=other_user.id,
|
||||
status="completed",
|
||||
)
|
||||
test_session.add(extraction)
|
||||
await test_session.commit()
|
||||
await test_session.refresh(extraction)
|
||||
|
||||
# Try to delete other user's extraction
|
||||
response = await authenticated_client.delete(f"/api/v1/extractions/{extraction.id}")
|
||||
|
||||
assert response.status_code == 403
|
||||
data = response.json()
|
||||
assert "permission" in data["detail"].lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_extraction_unauthenticated(self, client):
|
||||
"""Test deleting extraction without authentication."""
|
||||
response = await client.delete("/api/v1/extractions/1")
|
||||
|
||||
assert response.status_code == 401
|
||||
|
||||
Reference in New Issue
Block a user