Fraud Detection Test Suite - Quick Reference
๐ Test Suite Overview
| Metric |
Value |
| Test File |
backend/tests/test_14_fraud_detection.py |
| Total Lines |
~650 |
| Test Classes |
5 |
| Test Methods |
40+ |
| Endpoints Covered |
14 |
| New Fixtures |
6 |
๐งช Test Breakdown
Fraud Rule Management (13 tests)
โ Create rule - identity category
โ Create rule - behavior category
โ Create rule - document category
โ Create rule - financial category
โ Create rule - with conditions
โ Fetch all rules
โ Fetch rules - filter by active
โ Fetch rules - filter by category
โ Get single rule by ID
โ Edit existing rule
โ Toggle rule - deactivate
โ Toggle rule - activate
โ Delete rule
+ 3 validation error tests
Fraud Detection Check (3 tests)
โ Check application - no fraud
โ Check application - with fraud flag
โ Check application - invalid ID error
Fraud Flag Management (11 tests)
โ Fetch flags - no filters
โ Fetch flags - by status (open)
โ Fetch flags - by status (escalated)
โ Fetch flags - by risk level
โ Get single flag
โ Get flag - with customer details
โ Mark false positive
โ Confirm fraud
โ Escalate flag
โ Mark false positive - with empty notes
+ 2 error handling tests
Integration Tests (6 tests)
โ Fraud workflow - detection to escalation
โ Fraud workflow - rule-based detection
โ Fraud detection - high risk score
โ Fraud detection - multiple rules
โ Flag lifecycle - complete workflow
โ Fraud detection - caching consistency
Error Handling (4 tests)
โ Duplicate rule name
โ Edit non-existent rule
โ Get non-existent flag
โ Missing required fields
๐ง Fixtures Available
In conftest.py:
| Fixture Name |
Purpose |
Scope |
create_fraud_rule_data |
Basic rule creation template |
function |
create_fraud_rule_data_with_conditions |
Rule with conditions |
function |
test_fraud_rule |
Persistent test rule |
session |
test_fraud_flag |
Persistent test flag |
session |
test_fraud_flag_with_customer |
Flag with customer info |
function |
test_loan_application_with_fraud_flag |
App with flag |
function |
Usage Example:
def test_my_fraud_feature(
client: TestClient,
superuser_auth_headers: Dict[str, str],
test_fraud_rule: LoanFraudRule,
create_fraud_rule_data: Dict[str, any],
):
# Use fixtures in your test
pass
๐ Tested Fraud Categories
โโโโโโโโโโโโโโโ
โ identity โ Identity fraud detection
โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
โ behavior โ Behavioral anomalies
โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
โ document โ Document-based fraud
โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโ
โ financial โ Financial irregularities
โโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโ
โ application โ Application-level fraud
โโโโโโโโโโโโโโโโ
๐ฏ Expected Responses
Successful Rule Creation (201)
{
"id": 1,
"name": "Identity Fraud Rule",
"category": "identity",
"risk_score": 75,
"action": "flag",
"severity": "high",
"is_active": true,
"priority": 1
}
List Response (200)
{
"rules": [...],
"pagination": {
"page": 1,
"page_size": 10,
"total": 25
}
}
Fraud Flag Response (200)
{
"id": 1,
"risk_score": 80.0,
"risk_level": "high",
"status": "open",
"flag_reason": "Suspicious activity",
"fraudster": { ... }
}
Error Response (404)
{
"detail": "Rule not found"
}
๐ Running Tests
All fraud detection tests:
pytest backend/tests/test_14_fraud_detection.py -v
Specific test class:
pytest backend/tests/test_14_fraud_detection.py::TestFraudRuleManagement -v
Single test:
pytest backend/tests/test_14_fraud_detection.py::TestFraudRuleManagement::test_create_fraud_rule_identity_category -v
With coverage:
pytest backend/tests/test_14_fraud_detection.py --cov=routers.loan --cov-report=term-missing
Watch mode (with pytest-watch):
ptw backend/tests/test_14_fraud_detection.py -- -v
๐ Test Pattern Examples
def test_create_fraud_rule_identity_category(
client: TestClient,
superuser_auth_headers: Dict[str, str],
create_fraud_rule_data: Dict[str, any],
):
rule_data = create_fraud_rule_data.copy()
rule_data["name"] = f"Rule {random.randint(1000, 9999)}"
rule_data["category"] = "identity"
response = client.post(
"/api/v1/loan/fraudrule/add",
headers=superuser_auth_headers,
json=rule_data,
)
assert response.status_code == 201
data = response.json()
assert data["category"] == "identity"
def test_create_fraud_rule_invalid_category(
client: TestClient,
superuser_auth_headers: Dict[str, str],
create_fraud_rule_data: Dict[str, any],
):
rule_data = create_fraud_rule_data.copy()
rule_data["category"] = "invalid_category"
response = client.post(
"/api/v1/loan/fraudrule/add",
headers=superuser_auth_headers,
json=rule_data,
)
assert response.status_code in [400, 422]
Testing Integration Workflow:
def test_fraud_workflow_detection_to_escalation(
client: TestClient,
superuser_auth_headers: Dict[str, str],
test_loan_application: LoanApplication,
):
# Step 1: Run fraud check
response = client.post(
"/api/v1/loan/fraud/check",
headers=superuser_auth_headers,
json={"application_id": test_loan_application.id},
)
assert response.status_code in [200, 201]
๐ Fraud Flag Lifecycle
โโโโโโโโโโโ
โ CREATE โ System creates flag after rule match
โโโโโโฌโโโโโ
โ
โโโโโโโโโโโโโโโ
โ OPEN โ Flag awaits review
โโโโโโฌโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโ
โ ESCALATED/UNDER โ Manual review or escalation
โ REVIEW โ
โโโโโโฌโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ โ โ
v v v
RESOLVED FALSE_POS AUTO_BLOCKED
(Fraud) (Error) (System Action)
๐ Additional Documentation
- Full details: FRAUD_DETECTION_TESTS.md
- API Reference: See
routers/loan.py endpoints
- Models: See
models/loan.py
- Test Data:
tests/data/add_fraud_rule_data.json
โจ Key Features Tested
โ
Multiple fraud categories
โ
Complex rule conditions
โ
Filtering and pagination
โ
Rule activation/deactivation
โ
Fraud flag state transitions
โ
False positive handling
โ
Escalation workflows
โ
Error scenarios
โ
Integration workflows
โ
Data validation
๐ Troubleshooting
Tests Won't Run
- Issue: Import error with
socket.py
- Cause: Module name conflicts with stdlib
- Status: Pre-existing issue, not related to fraud tests
- Solution: Rename
routers/socket.py to routers/ws_socket.py
Fixture Not Found
- Issue:
test_fraud_rule fixture not available
- Solution: Ensure
conftest.py is updated with new fixtures
- Check: Verify import of
LoanFraudRule in conftest
Test Data Missing
- Issue:
add_fraud_rule_data.json not found
- Location:
backend/tests/data/add_fraud_rule_data.json
- Status: File exists and has been updated with proper schema
๐ฆ Files Modified/Created
| File |
Action |
Purpose |
test_14_fraud_detection.py |
โ
Created |
Main test suite |
conftest.py |
โ
Updated |
Added 6 fixtures |
add_fraud_rule_data.json |
โ
Updated |
Updated schema |
FRAUD_DETECTION_TESTS.md |
โ
Created |
Full documentation |