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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import pytest
import aiohttp
from unittest.mock import AsyncMock, patch, MagicMock
from app.validator import ProxyValidator, ValidationResult


class TestProxyValidator:
    """Comprehensive test suite for ProxyValidator"""

    @pytest.fixture
    def validator(self):
        return ProxyValidator(timeout=5, max_concurrent=10)

    # ==================== FORMAT VALIDATION ====================

    @pytest.mark.asyncio
    async def test_validate_format_valid_http(self, validator):
        """Test format validation for valid HTTP proxy"""
        assert await validator.validate_format("http://192.168.1.1:8080")
        assert await validator.validate_format("192.168.1.1:8080")

    @pytest.mark.asyncio
    async def test_validate_format_valid_https(self, validator):
        """Test format validation for valid HTTPS proxy"""
        assert await validator.validate_format("https://10.0.0.1:3128")

    @pytest.mark.asyncio
    async def test_validate_format_valid_socks4(self, validator):
        """Test format validation for valid SOCKS4 proxy"""
        assert await validator.validate_format("socks4://127.0.0.1:1080")

    @pytest.mark.asyncio
    async def test_validate_format_valid_socks5(self, validator):
        """Test format validation for valid SOCKS5 proxy"""
        assert await validator.validate_format("socks5://172.16.0.1:9050")

    @pytest.mark.asyncio
    async def test_validate_format_invalid_ip(self, validator):
        """Test format validation rejects invalid IP"""
        assert not await validator.validate_format("999.999.999.999:8080")
        assert not await validator.validate_format("192.168.1:8080")
        assert not await validator.validate_format("192.168.1.256:8080")

    @pytest.mark.asyncio
    async def test_validate_format_invalid_port(self, validator):
        """Test format validation rejects invalid port"""
        assert not await validator.validate_format("192.168.1.1:99999")
        assert not await validator.validate_format("192.168.1.1:")
        assert not await validator.validate_format("192.168.1.1")

    @pytest.mark.asyncio
    async def test_validate_format_edge_cases(self, validator):
        """Test format validation edge cases"""
        assert not await validator.validate_format("")
        assert not await validator.validate_format("not-a-proxy")
        assert not await validator.validate_format("http://")
        assert not await validator.validate_format("://192.168.1.1:8080")

    # ==================== CONNECTIVITY VALIDATION ====================

    @pytest.mark.asyncio
    async def test_validate_connectivity_success(self, validator):
        """Test successful proxy connectivity validation"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            is_valid, latency, error = await validator.validate_connectivity(
                "http://192.168.1.1:8080"
            )

        assert is_valid is True
        assert latency is not None
        assert latency >= 0
        assert error is None

    @pytest.mark.asyncio
    async def test_validate_connectivity_timeout(self, validator):
        """Test proxy connectivity timeout"""
        mock_session = MagicMock()
        mock_session.get.side_effect = TimeoutError()
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            is_valid, latency, error = await validator.validate_connectivity(
                "http://192.168.1.1:8080"
            )

        assert is_valid is False
        assert latency is None
        assert "timeout" in error.lower()

    @pytest.mark.asyncio
    async def test_validate_connectivity_proxy_error(self, validator):
        """Test proxy connection error"""
        mock_session = MagicMock()
        mock_session.get.side_effect = aiohttp.ClientProxyConnectionError(
            MagicMock(), MagicMock()
        )
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            is_valid, latency, error = await validator.validate_connectivity(
                "http://192.168.1.1:8080"
            )

        assert is_valid is False
        assert latency is None
        assert "connection failed" in error.lower()

    @pytest.mark.asyncio
    async def test_validate_connectivity_http_error(self, validator):
        """Test HTTP error status codes"""
        test_cases = [
            (403, "HTTP 403"),
            (404, "HTTP 404"),
            (500, "HTTP 500"),
            (502, "HTTP 502"),
        ]

        for status_code, expected_error in test_cases:
            mock_resp = AsyncMock()
            mock_resp.status = status_code
            mock_resp.__aenter__.return_value = mock_resp
            mock_resp.__aexit__.return_value = None

            mock_session = MagicMock()
            mock_session.get.return_value.__aenter__.return_value = mock_resp
            mock_session.__aenter__.return_value = mock_session
            mock_session.__aexit__.return_value = None

            with patch("aiohttp.ClientSession", return_value=mock_session):
                is_valid, latency, error = await validator.validate_connectivity(
                    "http://192.168.1.1:8080"
                )

            assert is_valid is False, f"Status {status_code} should fail validation"
            assert latency is None
            assert expected_error in error

    # ==================== ANONYMITY CHECK ====================

    @pytest.mark.asyncio
    async def test_check_anonymity_elite(self, validator):
        """Test elite proxy anonymity detection"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.json.return_value = {"headers": {}}
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            anonymity = await validator.check_anonymity("http://192.168.1.1:8080")

        assert anonymity == "elite"

    @pytest.mark.asyncio
    async def test_check_anonymity_transparent(self, validator):
        """Test transparent proxy detection"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.json.return_value = {
            "headers": {"X-Forwarded-For": "1.2.3.4", "Via": "proxy"}
        }
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            anonymity = await validator.check_anonymity("http://192.168.1.1:8080")

        assert anonymity == "transparent"

    @pytest.mark.asyncio
    async def test_check_anonymity_anonymous(self, validator):
        """Test anonymous proxy detection"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.json.return_value = {"headers": {"Proxy-Connection": "keep-alive"}}
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            anonymity = await validator.check_anonymity("http://192.168.1.1:8080")

        assert anonymity == "anonymous"

    @pytest.mark.asyncio
    async def test_check_anonymity_error(self, validator):
        """Test anonymity check handles errors gracefully"""
        mock_session = MagicMock()
        mock_session.get.side_effect = Exception("Network error")
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            anonymity = await validator.check_anonymity("http://192.168.1.1:8080")

        assert anonymity is None

    # ==================== GOOGLE ACCESS TEST ====================

    @pytest.mark.asyncio
    async def test_google_access_success(self, validator):
        """Test successful Google access through proxy"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            can_access = await validator.test_google_access("http://192.168.1.1:8080")

        assert can_access is True

    @pytest.mark.asyncio
    async def test_google_access_failure(self, validator):
        """Test failed Google access through proxy"""
        mock_session = MagicMock()
        mock_session.get.side_effect = Exception("Connection failed")
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            can_access = await validator.test_google_access("http://192.168.1.1:8080")

        assert can_access is False

    # ==================== GEO INFO ====================

    @pytest.mark.asyncio
    async def test_get_geo_info_success(self, validator):
        """Test successful geo info retrieval"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.json.return_value = {
            "country_code": "US",
            "country_name": "United States",
            "region": "California",
            "city": "San Francisco",
        }
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            geo_info = await validator.get_geo_info("8.8.8.8")

        assert geo_info["country_code"] == "US"
        assert geo_info["country_name"] == "United States"
        assert geo_info["state"] == "California"
        assert geo_info["city"] == "San Francisco"

    @pytest.mark.asyncio
    async def test_get_geo_info_error(self, validator):
        """Test geo info retrieval error handling"""
        mock_session = MagicMock()
        mock_session.get.side_effect = Exception("API error")
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            geo_info = await validator.get_geo_info("8.8.8.8")

        assert geo_info["country_code"] is None
        assert geo_info["country_name"] is None

    # ==================== PROXY TYPE DETECTION ====================

    @pytest.mark.asyncio
    async def test_detect_proxy_type_datacenter(self, validator):
        """Test datacenter proxy detection"""
        test_cases = [
            {"org": "Amazon AWS"},
            {"org": "Google Cloud"},
            {"org": "Microsoft Azure"},
            {"org": "DigitalOcean LLC"},
            {"org": "Linode Hosting"},
            {"org": "OVH Datacenter"},
        ]

        for data in test_cases:
            mock_resp = AsyncMock()
            mock_resp.status = 200
            mock_resp.json.return_value = data
            mock_resp.__aenter__.return_value = mock_resp
            mock_resp.__aexit__.return_value = None

            mock_session = MagicMock()
            mock_session.get.return_value.__aenter__.return_value = mock_resp
            mock_session.__aenter__.return_value = mock_session
            mock_session.__aexit__.return_value = None

            with patch("aiohttp.ClientSession", return_value=mock_session):
                proxy_type = await validator.detect_proxy_type("1.2.3.4")

            assert proxy_type == "datacenter", (
                f"Should detect datacenter for {data['org']}"
            )

    @pytest.mark.asyncio
    async def test_detect_proxy_type_residential(self, validator):
        """Test residential proxy detection"""
        mock_resp = AsyncMock()
        mock_resp.status = 200
        mock_resp.json.return_value = {"org": "Comcast Cable"}
        mock_resp.__aenter__.return_value = mock_resp
        mock_resp.__aexit__.return_value = None

        mock_session = MagicMock()
        mock_session.get.return_value.__aenter__.return_value = mock_resp
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            proxy_type = await validator.detect_proxy_type("1.2.3.4")

        assert proxy_type == "residential"

    @pytest.mark.asyncio
    async def test_detect_proxy_type_error(self, validator):
        """Test proxy type detection error handling"""
        mock_session = MagicMock()
        mock_session.get.side_effect = Exception("API error")
        mock_session.__aenter__.return_value = mock_session
        mock_session.__aexit__.return_value = None

        with patch("aiohttp.ClientSession", return_value=mock_session):
            proxy_type = await validator.detect_proxy_type("1.2.3.4")

        assert proxy_type == "unknown"

    # ==================== QUALITY SCORE CALCULATION ====================

    @pytest.mark.asyncio
    async def test_calculate_quality_score_perfect(self, validator):
        """Test quality score for perfect proxy"""
        score = await validator.calculate_quality_score(
            latency_ms=50,
            anonymity="elite",
            can_access_google=True,
            proxy_type="residential",
        )
        assert score == 100  # 40 + 30 + 15 + 15 = 100

    @pytest.mark.asyncio
    async def test_calculate_quality_score_good(self, validator):
        """Test quality score for good proxy"""
        score = await validator.calculate_quality_score(
            latency_ms=300,
            anonymity="anonymous",
            can_access_google=True,
            proxy_type="datacenter",
        )
        assert score == 70  # 30 + 20 + 15 + 5 = 70

    @pytest.mark.asyncio
    async def test_calculate_quality_score_poor(self, validator):
        """Test quality score for poor proxy"""
        score = await validator.calculate_quality_score(
            latency_ms=1500,
            anonymity="transparent",
            can_access_google=False,
            proxy_type="unknown",
        )
        assert score == 15  # 10 + 5 + 0 + 0 = 15

    @pytest.mark.asyncio
    async def test_calculate_quality_score_minimal(self, validator):
        """Test quality score with minimal data"""
        score = await validator.calculate_quality_score(
            latency_ms=None, anonymity=None, can_access_google=None, proxy_type=None
        )
        assert score == 0

    @pytest.mark.asyncio
    async def test_calculate_quality_score_caps_at_100(self, validator):
        """Test quality score never exceeds 100"""
        score = await validator.calculate_quality_score(
            latency_ms=10,
            anonymity="elite",
            can_access_google=True,
            proxy_type="residential",
        )
        assert score <= 100

    # ==================== COMPREHENSIVE VALIDATION ====================

    @pytest.mark.asyncio
    async def test_validate_comprehensive_success(self, validator):
        """Test comprehensive validation with all checks"""
        # Mock connectivity check
        with patch.object(
            validator,
            "validate_connectivity",
            return_value=(True, 150, None),
        ):
            # Mock other checks
            with patch.object(validator, "check_anonymity", return_value="elite"):
                with patch.object(validator, "test_google_access", return_value=True):
                    with patch.object(
                        validator,
                        "get_geo_info",
                        return_value={
                            "country_code": "US",
                            "country_name": "United States",
                            "state": "CA",
                            "city": "SF",
                        },
                    ):
                        with patch.object(
                            validator, "detect_proxy_type", return_value="datacenter"
                        ):
                            result = await validator.validate_comprehensive(
                                "http://1.2.3.4:8080", "1.2.3.4"
                            )

        assert result.success is True
        assert result.latency_ms == 150
        assert result.anonymity == "elite"
        assert result.can_access_google is True
        assert result.country_code == "US"
        assert result.proxy_type == "datacenter"

    @pytest.mark.asyncio
    async def test_validate_comprehensive_connectivity_failure(self, validator):
        """Test comprehensive validation when connectivity fails"""
        with patch.object(
            validator,
            "validate_connectivity",
            return_value=(False, None, "Connection timeout"),
        ):
            result = await validator.validate_comprehensive(
                "http://1.2.3.4:8080", "1.2.3.4"
            )

        assert result.success is False
        assert result.error_message == "Connection timeout"
        assert result.latency_ms is None

    # ==================== BATCH VALIDATION ====================

    @pytest.mark.asyncio
    async def test_validate_batch_success(self, validator):
        """Test batch validation of multiple proxies"""
        proxies = [
            ("http://1.2.3.4:8080", "1.2.3.4"),
            ("http://5.6.7.8:3128", "5.6.7.8"),
        ]

        mock_result = ValidationResult(success=True, latency_ms=100, anonymity="elite")

        with patch.object(
            validator, "validate_comprehensive", return_value=mock_result
        ):
            results = await validator.validate_batch(proxies)

        assert len(results) == 2
        assert all(result[1].success for result in results)

    @pytest.mark.asyncio
    async def test_validate_batch_mixed_results(self, validator):
        """Test batch validation with mixed success/failure"""

        async def mock_validate(proxy_url, ip):
            if "1.2.3.4" in proxy_url:
                return ValidationResult(success=True, latency_ms=100)
            else:
                return ValidationResult(success=False, error_message="Failed")

        proxies = [
            ("http://1.2.3.4:8080", "1.2.3.4"),
            ("http://9.9.9.9:8080", "9.9.9.9"),
        ]

        with patch.object(
            validator, "validate_comprehensive", side_effect=mock_validate
        ):
            results = await validator.validate_batch(proxies)

        assert len(results) == 2
        assert results[0][1].success is True
        assert results[1][1].success is False

    @pytest.mark.asyncio
    async def test_validate_batch_exception_handling(self, validator):
        """Test batch validation handles exceptions gracefully"""
        proxies = [
            ("http://1.2.3.4:8080", "1.2.3.4"),
        ]

        with patch.object(
            validator, "validate_comprehensive", side_effect=Exception("Test error")
        ):
            results = await validator.validate_batch(proxies)

        assert len(results) == 1
        assert results[0][1].success is False
        assert "Test error" in results[0][1].error_message