Skip to content

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

Testing Valid Input:

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"

Testing Invalid Input:

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