78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
import unittest
|
|
import time
|
|
from unittest.mock import patch
|
|
|
|
from app import app
|
|
from scraper import parse_distribution_labels, validate_maps_url
|
|
|
|
|
|
class ValidationTests(unittest.TestCase):
|
|
def test_accepts_google_maps_links(self):
|
|
urls = [
|
|
"https://maps.app.goo.gl/USRCto35jhoVtmj4A",
|
|
"https://www.google.com/maps/place/Test/@50,20,15z",
|
|
"https://maps.google.pl/maps?q=test",
|
|
]
|
|
for url in urls:
|
|
with self.subTest(url=url):
|
|
self.assertTrue(validate_maps_url(url).startswith("https://"))
|
|
|
|
def test_rejects_unsafe_links(self):
|
|
urls = [
|
|
"http://maps.app.goo.gl/USRCto35jhoVtmj4A",
|
|
"https://evil.example/maps/x",
|
|
"https://google.com.evil.example/maps/x",
|
|
"https://user:pass@www.google.com/maps/x",
|
|
"https://www.google.com:444/maps/x",
|
|
"https://www.google.com/search?q=x",
|
|
"file:///etc/passwd",
|
|
]
|
|
for url in urls:
|
|
with self.subTest(url=url), self.assertRaises(ValueError):
|
|
validate_maps_url(url)
|
|
|
|
def test_parses_exact_accessibility_labels(self):
|
|
labels = [
|
|
"5 gwiazdek, 1 234 opinie", "4 gwiazdki, 56 opinii",
|
|
"3 stars, 7 reviews", "2 stars, 1 review", "1 gwiazdka, 0 opinii",
|
|
]
|
|
self.assertEqual(parse_distribution_labels(labels), {5: 1234, 4: 56, 3: 7, 2: 1, 1: 0})
|
|
|
|
|
|
class ApiTests(unittest.TestCase):
|
|
def test_home_page(self):
|
|
self.assertEqual(app.test_client().get("/").status_code, 200)
|
|
|
|
def test_rejects_non_google_url(self):
|
|
response = app.test_client().post("/api/reviews", json={"url": "https://example.com/"})
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
def test_rejects_large_request(self):
|
|
response = app.test_client().post("/api/reviews", data=b"x" * 5000, content_type="application/json")
|
|
self.assertEqual(response.status_code, 413)
|
|
|
|
def test_reports_job_progress_and_result(self):
|
|
expected = {"total": 3, "average": 4.0, "ratings": {"5": 2, "4": 0, "3": 0, "2": 0, "1": 1}}
|
|
|
|
def fake_scrape(_url, progress):
|
|
progress(50, "Otwarto Chromium")
|
|
progress(99, "Pojawiły się liczniki")
|
|
return expected
|
|
|
|
client = app.test_client()
|
|
with patch("app.get_rating_distribution", side_effect=fake_scrape):
|
|
start = client.post("/api/reviews", json={"url": "https://maps.app.goo.gl/USRCto35jhoVtmj4A"})
|
|
self.assertEqual(start.status_code, 202)
|
|
job_id = start.get_json()["job_id"]
|
|
for _ in range(100):
|
|
job = client.get(f"/api/reviews/{job_id}").get_json()
|
|
if job["status"] == "complete":
|
|
break
|
|
time.sleep(0.005)
|
|
self.assertEqual(job["progress"], 100)
|
|
self.assertEqual(job["result"], expected)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|