Spaces:
Sleeping
Sleeping
File size: 8,435 Bytes
369e64d a9f0e5a 369e64d | 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 | """Tests for URLScan.io API client."""
import pytest
import responses
from unittest.mock import patch
import sys
from pathlib import Path
from src.phising_detection.utils.urlscan import URLScanClient, URLScanError
class TestURLScanClient:
"""Tests for URLScanClient class."""
def test_init_with_api_key(self):
"""Test initialization with API key provided."""
client = URLScanClient(api_key="test_key")
assert client.api_key == "test_key"
def test_init_with_env_var(self, monkeypatch):
"""Test initialization with API key from environment."""
monkeypatch.setenv("URLSCAN_API_KEY", "env_key")
client = URLScanClient()
assert client.api_key == "env_key"
def test_init_without_api_key(self, monkeypatch):
"""Test initialization fails without API key."""
monkeypatch.delenv("URLSCAN_API_KEY", raising=False)
with pytest.raises(URLScanError) as exc_info:
URLScanClient()
assert "No API key provided" in str(exc_info.value)
@responses.activate
def test_submit_url_success(self):
"""Test successful URL submission."""
# Mock API response
responses.add(
responses.POST,
"https://urlscan.io/api/v1/scan/",
json={
"uuid": "test-uuid-123",
"result": "https://urlscan.io/result/test-uuid-123/",
"api": "https://urlscan.io/api/v1/result/test-uuid-123/"
},
status=200
)
client = URLScanClient(api_key="test_key")
result = client.submit_url("https://example.com")
assert result["uuid"] == "test-uuid-123"
assert "result" in result
assert len(responses.calls) == 1
assert responses.calls[0].request.url == "https://urlscan.io/api/v1/scan/"
@responses.activate
def test_submit_url_with_tags(self):
"""Test URL submission with tags."""
responses.add(
responses.POST,
"https://urlscan.io/api/v1/scan/",
json={"uuid": "test-uuid", "result": "url"},
status=200
)
client = URLScanClient(api_key="test_key")
client.submit_url(
"https://example.com",
visibility="unlisted",
tags=["phishing", "test"]
)
request_body = responses.calls[0].request.body
assert b"phishing" in request_body
assert b"test" in request_body
@responses.activate
def test_submit_url_rate_limit(self):
"""Test rate limit error handling."""
responses.add(
responses.POST,
"https://urlscan.io/api/v1/scan/",
status=429
)
client = URLScanClient(api_key="test_key")
with pytest.raises(URLScanError) as exc_info:
client.submit_url("https://example.com")
assert "Rate limit exceeded" in str(exc_info.value)
@responses.activate
def test_submit_url_bad_request(self):
"""Test bad request error handling."""
responses.add(
responses.POST,
"https://urlscan.io/api/v1/scan/",
body="Invalid URL",
status=400
)
client = URLScanClient(api_key="test_key")
with pytest.raises(URLScanError) as exc_info:
client.submit_url("not-a-valid-url")
assert "Bad request" in str(exc_info.value)
@responses.activate
def test_get_result_success(self):
"""Test successful result retrieval."""
responses.add(
responses.GET,
"https://urlscan.io/api/v1/result/test-uuid/",
json={
"page": {
"url": "https://example.com",
"title": "Example Domain"
},
"verdicts": {
"overall": {
"score": 0,
"malicious": False
}
},
"task": {"uuid": "test-uuid"}
},
status=200
)
client = URLScanClient(api_key="test_key")
result = client.get_result("test-uuid")
assert result["page"]["url"] == "https://example.com"
assert result["verdicts"]["overall"]["malicious"] is False
@responses.activate
def test_get_result_not_found(self):
"""Test result not found error."""
responses.add(
responses.GET,
"https://urlscan.io/api/v1/result/test-uuid/",
status=404
)
client = URLScanClient(api_key="test_key")
with pytest.raises(URLScanError) as exc_info:
client.get_result("test-uuid")
assert "not found or not ready" in str(exc_info.value)
@responses.activate
def test_search_success(self):
"""Test search functionality."""
responses.add(
responses.GET,
"https://urlscan.io/api/v1/search/",
json={
"total": 2,
"results": [
{"task": {"url": "https://example.com"}},
{"task": {"url": "https://example.org"}}
]
},
status=200
)
client = URLScanClient(api_key="test_key")
results = client.search(query="domain:example.com", size=10)
assert results["total"] == 2
assert len(results["results"]) == 2
@responses.activate
def test_get_verdict_malicious(self):
"""Test verdict extraction for malicious URL."""
responses.add(
responses.GET,
"https://urlscan.io/api/v1/result/test-uuid/",
json={
"verdicts": {
"overall": {
"score": 100,
"malicious": True
}
}
},
status=200
)
client = URLScanClient(api_key="test_key")
verdict = client.get_verdict("test-uuid")
assert verdict == "malicious"
@responses.activate
def test_get_verdict_safe(self):
"""Test verdict extraction for safe URL."""
responses.add(
responses.GET,
"https://urlscan.io/api/v1/result/test-uuid/",
json={
"verdicts": {
"overall": {
"score": 0,
"malicious": False
}
}
},
status=200
)
client = URLScanClient(api_key="test_key")
verdict = client.get_verdict("test-uuid")
assert verdict == "safe"
@responses.activate
def test_submit_and_wait_success(self):
"""Test submit and wait for results."""
# Mock submission
responses.add(
responses.POST,
"https://urlscan.io/api/v1/scan/",
json={
"uuid": "test-uuid",
"result": "https://urlscan.io/result/test-uuid/"
},
status=200
)
# Mock result retrieval
responses.add(
responses.GET,
"https://urlscan.io/api/v1/result/test-uuid/",
json={
"page": {"title": "Example"},
"task": {"uuid": "test-uuid"}
},
status=200
)
client = URLScanClient(api_key="test_key")
result = client.submit_and_wait(
"https://example.com",
max_wait=10,
poll_interval=1
)
assert result["page"]["title"] == "Example"
@responses.activate
def test_submit_and_wait_timeout(self):
"""Test timeout in submit_and_wait."""
# Mock submission
responses.add(
responses.POST,
"https://urlscan.io/api/v1/scan/",
json={"uuid": "test-uuid"},
status=200
)
# Mock result always returning 404
responses.add(
responses.GET,
"https://urlscan.io/api/v1/result/test-uuid/",
status=404
)
client = URLScanClient(api_key="test_key")
with pytest.raises(URLScanError) as exc_info:
client.submit_and_wait(
"https://example.com",
max_wait=3,
poll_interval=1
)
assert "Timeout" in str(exc_info.value)
|