wifi_spool/test_web_app.py

138 lines
5.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import unittest
from unittest.mock import patch
from pathlib import Path
from settings import Settings
from web_app import create_app, database_text, parse_bssid_ids, parse_positive_ids
class WebAppTest(unittest.TestCase):
def setUp(self):
app = create_app()
app.config["TESTING"] = True
self.client = app.test_client()
def test_dashboard_is_available_without_database_query(self):
response = self.client.get("/")
self.assertEqual(response.status_code, 200)
self.assertIn("Тепловая карта".encode(), response.data)
self.assertIn("По агентам".encode(), response.data)
self.assertIn("По точкам доступа".encode(), response.data)
self.assertIn('data-map-min-x="-150"'.encode(), response.data)
self.assertIn('data-map-max-x="-70"'.encode(), response.data)
self.assertIn('data-map-min-y="40"'.encode(), response.data)
self.assertIn('data-map-max-y="110"'.encode(), response.data)
self.assertIn('< −79'.encode(), response.data)
self.assertIn('≥ −70 dBm'.encode(), response.data)
self.assertNotIn('id="show-access-points"'.encode(), response.data)
self.assertIn('id="background-x"'.encode(), response.data)
self.assertIn('id="background-y"'.encode(), response.data)
self.assertIn('id="background-scale"'.encode(), response.data)
def test_map_objects_are_available(self):
response = self.client.get("/api/map-objects")
self.assertEqual(response.status_code, 200)
objects = response.get_json()
self.assertGreater(len(objects), 1000)
rectangle = next(item for item in objects if item["shape"] == "rectangle")
circle = next(item for item in objects if item["shape"] == "circle")
self.assertIn("width", rectangle)
self.assertIn("height", rectangle)
self.assertIn("name", circle)
def test_installer_copies_map_objects(self):
installer = Path("deploy/install.sh").read_text(encoding="utf-8")
self.assertIn("object_blue_crossed_rectangles.csv", installer)
self.assertIn("object_red_crossed_circles_labels.csv", installer)
def test_web_server_listens_on_all_interfaces_by_default(self):
self.assertEqual(Settings().web_host, "0.0.0.0")
def test_web_date_range_defaults_to_last_day(self):
self.assertEqual(Settings().web_default_date_range_minutes, 24 * 60)
def test_converts_legacy_byte_values_for_json(self):
self.assertEqual(database_text(b"aa:bb:cc"), "aa:bb:cc")
self.assertEqual(database_text("agent-1"), "agent-1")
def test_rejects_invalid_period_before_database_query(self):
response = self.client.get(
"/api/heatmap",
query_string={
"from": "2026-08-29T12:00:00+05:00",
"to": "2026-08-29T11:00:00+05:00",
},
)
self.assertEqual(response.status_code, 400)
self.assertIn("earlier than", response.get_json()["error"])
def test_rejects_invalid_map_bounds_before_database_query(self):
response = self.client.get(
"/api/heatmap",
query_string={
"from": "2026-08-29T11:00:00+05:00",
"to": "2026-08-29T12:00:00+05:00",
"min_x": "10",
"max_x": "5",
},
)
self.assertEqual(response.status_code, 400)
self.assertIn("minimum bounds", response.get_json()["error"])
def test_parses_unique_bssid_filters(self):
self.assertEqual(parse_bssid_ids(["1,2", "2", "3"]), [1, 2, 3])
def test_rejects_invalid_bssid_filter(self):
with self.assertRaises(ValueError):
parse_bssid_ids(["not-an-id"])
def test_parses_agent_filters(self):
self.assertEqual(parse_positive_ids(["1,2", "2"], "agent", 100), [1, 2])
def test_heatmap_returns_filtered_access_point_coordinates(self):
class Result:
def __init__(self, rows):
self.rows = rows
def fetchall(self):
return self.rows
class Connection:
def __init__(self):
self.calls = []
def __enter__(self):
return self
def __exit__(self, *_args):
return None
def execute(self, query, parameters):
self.calls.append((query, parameters))
if "access_point_coordinates" in query:
return Result([(7, "aa:bb:cc:dd:ee:ff", -72.93, 29.17)])
return Result([])
connection = Connection()
with patch("web_app.connect", return_value=connection):
response = self.client.get(
"/api/heatmap",
query_string={
"from": "2026-08-29T11:00:00+05:00",
"to": "2026-08-29T12:00:00+05:00",
"bssid": "7",
},
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get_json()["access_points"],
[{"id": 7, "bssid": "aa:bb:cc:dd:ee:ff", "x": -72.93, "y": 29.17}],
)
coordinate_call = connection.calls[1]
self.assertIn("coordinates.bssid_id = ANY(%s)", coordinate_call[0])
self.assertEqual(coordinate_call[1][-1], [7])
if __name__ == "__main__":
unittest.main()