sync
This commit is contained in:
+3
-3
@@ -136,16 +136,16 @@ def get_rating_distribution(
|
||||
browser = playwright.chromium.launch(**launch)
|
||||
context = browser.new_context(locale="pl-PL", viewport={"width": 1100, "height": 800})
|
||||
try:
|
||||
report(50, "Otwarto Chromium")
|
||||
report(50, "Trwa łączenie z Google Maps")
|
||||
page = context.pages[0] if context.pages else context.new_page()
|
||||
context.add_init_script(
|
||||
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
||||
)
|
||||
page.goto(url, wait_until="domcontentloaded", timeout=30_000)
|
||||
_accept_cookies(page)
|
||||
report(75, "Załadowano Google Maps")
|
||||
report(75, "Ładowanie opinii")
|
||||
_open_reviews(page)
|
||||
report(90, "Otwarto panel opinii")
|
||||
report(90, "Obliczanie statystyk")
|
||||
page.wait_for_timeout(700)
|
||||
labels = page.locator("[aria-label]").evaluate_all(
|
||||
"els => els.map(el => el.getAttribute('aria-label')).filter(Boolean)"
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Rozkład opinii Google</title>
|
||||
<title>Licznik opinii Google</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<main><section class="card">
|
||||
<p class="eyebrow">Google Maps</p>
|
||||
<h1>Policz opinie według oceny</h1>
|
||||
<p class="intro">Wklej link do firmy lub miejsca. Aplikacja niczego nie zapisuje.</p>
|
||||
<h1>Extra licznik opinii 9000</h1>
|
||||
<p class="intro">Wklej link do firmy lub miejsca z Google Maps.</p>
|
||||
<form id="review-form">
|
||||
<label for="maps-url">Link Google Maps</label>
|
||||
<div class="input-row">
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user