Files
hate_crack/TESTING.md
T
Justin Bollinger e590f76335 Testing README
2026-01-25 15:06:08 -05:00

3.0 KiB

Test Mocking Summary

Overview

All Hashview API tests have been updated to use mocked responses instead of real API calls. This allows tests to run in CI/CD environments (like GitHub Actions) without requiring connectivity to a Hashview server or actual API credentials.

Changes Made

1. Updated Test Files

test_hashview.py (consolidated test suite)

  • Added unittest.mock imports (Mock, patch, MagicMock)
  • Removed dependency on config.json file
  • Replaced all real API calls with mocked responses
  • Mock responses match the actual API response format (e.g., 'users' field as JSON string)
  • Includes comprehensive tests for:
    • Customer listing and validation
    • Authentication and authorization
    • Hashfile upload
    • Complete job creation workflow

2. Key Mock Patterns

# Example: Mocking list_customers response
mock_response = Mock()
mock_response.json.return_value = {
    'users': json.dumps([  # Note: 'users' is a JSON string in the real API
        {'id': 1, 'name': 'Test Customer'}
    ])
}
mock_response.raise_for_status = Mock()
api.session.get.return_value = mock_response

3. GitHub Actions Workflow

Created .github/workflows/tests.yml to automatically run tests on:

  • Push to main/master/develop branches
  • Pull requests to main/master/develop branches
  • Tests run against Python 3.9, 3.10, 3.11, and 3.12

4. Documentation

Updated readme.md with:

  • Testing section explaining how to run tests locally
  • Description of test structure
  • Information about CI/CD integration

Test Results

6 tests passing
Tests run in ~0.1 seconds (vs ~20 seconds with real API calls)

Test Coverage

  1. test_list_customers_success - Validates customer listing with multiple customers
  2. test_list_customers_returns_valid_data - Validates customer data structure
  3. test_connection_and_auth - Tests successful authentication
  4. test_invalid_api_key_fails - Tests authentication failure handling
  5. test_upload_hashfile - Tests hashfile upload functionality
  6. test_create_job_workflow - Tests complete end-to-end job creation workflow

Benefits

  1. No Dependencies: Tests run without needing a Hashview server or API credentials
  2. Fast Execution: Mocked tests complete in milliseconds
  3. Reliable: Tests won't fail due to network issues or server downtime
  4. CI/CD Ready: Can run in GitHub Actions and other CI environments
  5. Portable: Tests work anywhere Python is installed

Running Tests

# Install dependencies
pip install pytest pytest-mock requests

# Run all tests
pytest -v

# Run specific test
pytest test_hashview.py -v

# Run a specific test method
pytest test_hashview.py::TestHashviewAPI::test_create_job_workflow -v

Note on Real API Testing

While these mocked tests validate the code logic, you may still want to occasionally run integration tests against a real Hashview instance to ensure the API hasn't changed. The test files can be easily modified to toggle between mocked and real API calls if needed.