This repository was archived by the owner on Jun 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_functionality.py
More file actions
215 lines (176 loc) · 7.05 KB
/
Copy pathverify_functionality.py
File metadata and controls
215 lines (176 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
"""
Verification script to ensure the application is working correctly after abuse prevention implementation.
"""
import sys
import os
import asyncio
import logging
# Add project root to path
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_imports():
"""Test that all modules can be imported."""
logger.info("Testing imports...")
try:
# Core application modules
from app.main import app
from app.trial_service import TrialService
from app.user_service import UserService
from app.styles_service import StyleService
# Protection modules (should work even if not fully configured)
from app.fingerprint_service import DeviceFingerprintService
from app.vpn_detection_service import VPNDetectionService
from app.captcha_service import CAPTCHAService
from app.risk_scoring_service import RiskScoringService
from app.rate_limiting_service import AdvancedRateLimitingService
from app.abuse_monitoring_service import AbuseMonitoringService
from app.protection_middleware import AbuseProtectionMiddleware
from app.config import security_config
logger.info("✅ All imports successful")
return True
except Exception as e:
logger.error(f"❌ Import failed: {e}")
return False
def test_fastapi_app():
"""Test FastAPI app creation."""
logger.info("Testing FastAPI app...")
try:
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
# Test health endpoint
response = client.get("/health")
assert response.status_code == 200
# Test trial status (should work without auth)
response = client.get("/trial/status")
assert response.status_code == 200
logger.info("✅ FastAPI app working correctly")
return True
except Exception as e:
logger.error(f"❌ FastAPI app test failed: {e}")
return False
async def test_trial_service():
"""Test trial service functionality."""
logger.info("Testing trial service...")
try:
from app.trial_service import TrialService
service = TrialService()
# Test session creation
session = await service.get_or_create_trial_session("192.168.1.1", "test-agent")
assert session.session_id.startswith("trial-")
assert session.ip_address == "192.168.1.1"
assert session.images_used == 0
assert session.max_images == 5
# Test usage check
can_use, usage_response = await service.check_trial_usage(session.session_id)
assert can_use is True
assert usage_response.images_remaining == 5
logger.info("✅ Trial service working correctly")
return True
except Exception as e:
logger.error(f"❌ Trial service test failed: {e}")
return False
def test_protection_services():
"""Test protection services initialization."""
logger.info("Testing protection services...")
try:
from app.fingerprint_service import DeviceFingerprintService
from app.vpn_detection_service import VPNDetectionService
from app.captcha_service import CAPTCHAService
from app.models import SecurityConfig
# Test fingerprint service
fp_service = DeviceFingerprintService()
hash_result = fp_service._hash_string("test")
assert len(hash_result) == 64 # SHA-256 length
# Test VPN service
vpn_service = VPNDetectionService()
default_assessment = vpn_service._create_default_assessment("192.168.1.1")
assert default_assessment.ip_address == "192.168.1.1"
# Test CAPTCHA service
captcha_service = CAPTCHAService()
challenge_level = captcha_service.determine_challenge_level(0.1)
assert challenge_level.value in ["none", "simple_math", "recaptcha_v2", "recaptcha_v3", "hcaptcha"]
logger.info("✅ Protection services working correctly")
return True
except Exception as e:
logger.error(f"❌ Protection services test failed: {e}")
return False
def test_models():
"""Test data models."""
logger.info("Testing data models...")
try:
from app.models import TrialSession, SecurityConfig, IPRiskAssessment
import time
# Test TrialSession model
session = TrialSession(
session_id="test-123",
ip_address="192.168.1.1",
user_agent="test-agent",
images_used=0,
max_images=5,
created_at="2024-01-01T00:00:00Z",
creation_timestamp=time.time()
)
assert session.session_id == "test-123"
# Test SecurityConfig
config = SecurityConfig()
assert hasattr(config, 'fingerprinting_enabled')
assert hasattr(config, 'high_risk_threshold')
# Test IPRiskAssessment
assessment = IPRiskAssessment(
ip_address="192.168.1.1",
risk_score=0.5,
confidence=0.8
)
assert assessment.ip_address == "192.168.1.1"
logger.info("✅ Data models working correctly")
return True
except Exception as e:
logger.error(f"❌ Data models test failed: {e}")
return False
async def main():
"""Run all verification tests."""
logger.info("🔍 Starting application functionality verification...")
tests = [
("Imports", test_imports),
("FastAPI App", test_fastapi_app),
("Trial Service", test_trial_service),
("Protection Services", test_protection_services),
("Data Models", test_models),
]
results = []
for test_name, test_func in tests:
logger.info(f"\n--- Testing {test_name} ---")
try:
if asyncio.iscoroutinefunction(test_func):
result = await test_func()
else:
result = test_func()
results.append((test_name, result))
except Exception as e:
logger.error(f"❌ {test_name} test crashed: {e}")
results.append((test_name, False))
# Summary
logger.info("\n" + "="*50)
logger.info("VERIFICATION SUMMARY")
logger.info("="*50)
passed = 0
for test_name, result in results:
status = "✅ PASS" if result else "❌ FAIL"
logger.info(f"{test_name}: {status}")
if result:
passed += 1
total = len(results)
logger.info(f"\nPassed: {passed}/{total}")
if passed == total:
logger.info("🎉 ALL TESTS PASSED - Application is working correctly!")
return True
else:
logger.error(f"⚠️ {total - passed} test(s) failed - Some issues detected")
return False
if __name__ == "__main__":
success = asyncio.run(main())
sys.exit(0 if success else 1)