mirror of
https://github.com/trustedsec/hate_crack.git
synced 2026-07-04 11:47:24 -07:00
3.0 KiB
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.mockimports (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
- test_list_customers_success - Validates customer listing with multiple customers
- test_list_customers_returns_valid_data - Validates customer data structure
- test_connection_and_auth - Tests successful authentication
- test_invalid_api_key_fails - Tests authentication failure handling
- test_upload_hashfile - Tests hashfile upload functionality
- test_create_job_workflow - Tests complete end-to-end job creation workflow
Benefits
- No Dependencies: Tests run without needing a Hashview server or API credentials
- Fast Execution: Mocked tests complete in milliseconds
- Reliable: Tests won't fail due to network issues or server downtime
- CI/CD Ready: Can run in GitHub Actions and other CI environments
- 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.