55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import unittest
|
|
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|