109 lines
4.4 KiB
Python
109 lines
4.4 KiB
Python
import json
|
|
import tempfile
|
|
import unittest
|
|
from decimal import Decimal
|
|
from pathlib import Path
|
|
|
|
from settings import SETTINGS, Settings
|
|
from wifi_spool import (
|
|
SCHEMA,
|
|
RawScanArchive,
|
|
is_scan_results_message,
|
|
load_access_point_coordinates,
|
|
mosquitto_command,
|
|
normalize_bssid,
|
|
parse_message,
|
|
)
|
|
|
|
|
|
class WifiSpoolTest(unittest.TestCase):
|
|
def test_raw_scan_archive_is_enabled_by_default(self):
|
|
self.assertTrue(Settings().save_raw_scan_results)
|
|
|
|
def test_filters_and_normalizes_scan_results(self):
|
|
data = {
|
|
"message_kind": "scan_results",
|
|
"agent_id": "agent-1",
|
|
"payload": {
|
|
"location": {"x": -110.2, "y": 93.7},
|
|
"scan_results": [
|
|
{"bssid": "F0:61:C0:87:20:B0(on", "signal_level": "-67", "ssid": "RONAVI_MCNS5"},
|
|
{"bssid": "cc:dd", "signal_level": "-71", "ssid": "OTHER"},
|
|
],
|
|
},
|
|
}
|
|
line = "wifi/vostok/44/scan_results " + json.dumps({"data": json.dumps(data)})
|
|
|
|
results = parse_message(line, SETTINGS.target_ssid)
|
|
self.assertEqual(len(results), 1)
|
|
self.assertEqual((results[0].location_x, results[0].location_y), (-110, 94))
|
|
self.assertEqual(results[0].signal_level, -67)
|
|
self.assertEqual(results[0].agent_id, "agent-1")
|
|
self.assertEqual(results[0].bssid, "f0:61:c0:87:20:b0")
|
|
|
|
def test_normalizes_only_a_valid_bssid_with_on_suffix(self):
|
|
self.assertEqual(
|
|
normalize_bssid("94:64:24:6c:2a:91(on)"), "94:64:24:6c:2a:91"
|
|
)
|
|
self.assertEqual(normalize_bssid("not-a-bssid(on"), "not-a-bssid(on")
|
|
|
|
def test_ignores_other_message_kind(self):
|
|
line = 'topic {"message_kind":"heartbeat"}'
|
|
self.assertEqual(parse_message(line, SETTINGS.target_ssid), [])
|
|
|
|
def test_archives_each_raw_scan_result_in_a_separate_file(self):
|
|
line = 'wifi/topic {"message_kind":"scan_results","payload":{}}'
|
|
self.assertTrue(is_scan_results_message(line))
|
|
self.assertFalse(is_scan_results_message('wifi/topic {"message_kind":"heartbeat"}'))
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
archive = RawScanArchive(directory)
|
|
first = archive.save(line)
|
|
second = archive.save(line)
|
|
self.assertNotEqual(first, second)
|
|
self.assertEqual(len(list(Path(directory).glob("scan_results-*.txt"))), 2)
|
|
self.assertEqual(first.read_text(encoding="utf-8"), line + "\n")
|
|
|
|
def test_schema_contains_collection_timestamp(self):
|
|
self.assertIn("collected_at TIMESTAMPTZ", SCHEMA)
|
|
self.assertIn("DEFAULT CURRENT_TIMESTAMP", SCHEMA)
|
|
|
|
def test_schema_normalizes_agents(self):
|
|
self.assertIn("CREATE TABLE IF NOT EXISTS agents", SCHEMA)
|
|
self.assertIn("agent_id BIGINT NOT NULL", SCHEMA)
|
|
self.assertIn("FOREIGN KEY (agent_id) REFERENCES agents(id)", SCHEMA)
|
|
|
|
def test_schema_contains_access_point_coordinates(self):
|
|
self.assertIn("CREATE TABLE IF NOT EXISTS access_point_coordinates", SCHEMA)
|
|
self.assertIn("location_x NUMERIC", SCHEMA)
|
|
self.assertIn("FOREIGN KEY (bssid_id) REFERENCES bssids(id)", SCHEMA)
|
|
|
|
def test_loads_access_point_coordinates_with_empty_pair(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "coordinates.csv"
|
|
path.write_text(
|
|
"AA:BB:CC:DD:EE:FF;-72.93;-27\n"
|
|
"11:22:33:44:55:66;;\n",
|
|
encoding="utf-8",
|
|
)
|
|
coordinates = load_access_point_coordinates(path)
|
|
self.assertEqual(coordinates[0].bssid, "aa:bb:cc:dd:ee:ff")
|
|
self.assertEqual(coordinates[0].location_x, Decimal("-72.93"))
|
|
self.assertEqual(coordinates[0].location_y, Decimal("-27"))
|
|
self.assertIsNone(coordinates[1].location_x)
|
|
self.assertIsNone(coordinates[1].location_y)
|
|
|
|
def test_schema_merges_existing_bssids_with_on_suffix(self):
|
|
self.assertIn("SET bssid_id = duplicates.canonical_id", SCHEMA)
|
|
self.assertIn("regexp_replace(bssid, '\\(on\\)?$'", SCHEMA)
|
|
|
|
def test_mqtt_command_uses_keepalive(self):
|
|
command = mosquitto_command(SETTINGS)
|
|
keepalive_index = command.index("-k")
|
|
self.assertEqual(
|
|
command[keepalive_index + 1], str(SETTINGS.mqtt_keepalive_seconds)
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|