FIX: lowercase MAC normalization across project v0.1

Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
jokob-sk
2026-02-07 13:44:50 +11:00
parent 3734c43284
commit 946ad00253
22 changed files with 164 additions and 257 deletions

View File

@@ -1,70 +1,78 @@
import pytest
import random
from helper import get_setting_value
from api_server.api_server_start import app
from models.device_instance import DeviceInstance
@pytest.fixture(scope="session")
def api_token():
return get_setting_value("API_TOKEN")
@pytest.fixture
def client():
with app.test_client() as client:
yield client
@pytest.fixture
def test_mac_norm():
# Normalized MAC
return "AA:BB:CC:DD:EE:FF"
# Now normalized to lowercase
return "aa:bb:cc:dd:ee:ff"
@pytest.fixture
def test_parent_mac_input():
# Lowercase input MAC
return "aa:bb:cc:dd:ee:00"
# Input with mixed/upper case to test the trigger/normalization
return "AA:BB:CC:DD:EE:00"
@pytest.fixture
def test_parent_mac_norm():
# Normalized expected MAC
return "AA:BB:CC:DD:EE:00"
# Expected result in DB (lowercase)
return "aa:bb:cc:dd:ee:00"
def auth_headers(token):
return {"Authorization": f"Bearer {token}"}
def test_update_normalization(client, api_token, test_mac_norm, test_parent_mac_input, test_parent_mac_norm):
# 1. Create a device (using normalized MAC)
# 1. Create a device
create_payload = {
"createNew": True,
"devName": "Normalization Test Device",
"devOwner": "Unit Test",
}
# Pass the lowercase mac
resp = client.post(f"/device/{test_mac_norm}", json=create_payload, headers=auth_headers(api_token))
assert resp.status_code == 200
assert resp.json.get("success") is True
# 2. Update the device using LOWERCASE MAC in URL
# And set devParentMAC to LOWERCASE
# 2. Update the device sending UPPERCASE parent MAC
# To verify the triggers/logic flip it to lowercase
update_payload = {
"devParentMAC": test_parent_mac_input,
"devName": "Updated Device"
}
# Using lowercase MAC in URL: aa:bb:cc:dd:ee:ff
lowercase_mac = test_mac_norm.lower()
resp = client.post(f"/device/{lowercase_mac}", json=update_payload, headers=auth_headers(api_token))
resp = client.post(f"/device/{test_mac_norm}", json=update_payload, headers=auth_headers(api_token))
assert resp.status_code == 200
assert resp.json.get("success") is True
# 3. Verify in DB that devParentMAC is NORMALIZED
# 3. Verify in DB that devParentMAC is LOWERCASE
device_handler = DeviceInstance()
# Query using lowercase mac
device = device_handler.getDeviceData(test_mac_norm)
assert device is not None
assert device["devName"] == "Updated Device"
# This is the critical check:
# CRITICAL CHECKS:
# It must be lowercase now
assert device["devParentMAC"] == test_parent_mac_norm
assert device["devParentMAC"] != test_parent_mac_input # Should verify it changed from input if input was different case
# It should NOT be the uppercase input we sent
assert device["devParentMAC"] != test_parent_mac_input
# Cleanup
device_handler.deleteDeviceByMAC(test_mac_norm)
device_handler.deleteDeviceByMAC(test_mac_norm)