feat(fusion_clock): NFC card UID normalization helper
Add _normalize_uid static method to FusionClockNfcKiosk that strips whitespace, uppercases, removes separators, validates hex-only content, and reformats to canonical colon-separated pairs; returns None for empty/invalid input. Covered by 7 new TransactionCase unit tests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,15 +3,32 @@
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
|
||||
import logging
|
||||
import re
|
||||
from odoo import http
|
||||
from odoo.http import request
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
_UID_HEX_PATTERN = re.compile(r'^[0-9A-F]+$')
|
||||
|
||||
|
||||
class FusionClockNfcKiosk(http.Controller):
|
||||
"""NFC tap-to-clock kiosk controller. Reuses FusionClockAPI helpers."""
|
||||
|
||||
@staticmethod
|
||||
def _normalize_uid(uid):
|
||||
"""Normalize an NFC card UID to canonical hex (uppercase, colon-separated).
|
||||
|
||||
Returns None if the input is empty or not valid hex.
|
||||
"""
|
||||
if not uid:
|
||||
return None
|
||||
cleaned = uid.strip().upper().replace('-', '').replace(':', '').replace(' ', '')
|
||||
if not cleaned or not _UID_HEX_PATTERN.match(cleaned):
|
||||
return None
|
||||
if len(cleaned) % 2 != 0:
|
||||
return None
|
||||
return ':'.join(cleaned[i:i+2] for i in range(0, len(cleaned), 2))
|
||||
|
||||
@http.route('/fusion_clock/kiosk/nfc', type='http', auth='user', website=True)
|
||||
def nfc_kiosk_page(self, **kw):
|
||||
"""Render the NFC kiosk page for a wall-mounted tablet."""
|
||||
|
||||
@@ -36,3 +36,46 @@ class TestNfcKioskController(HttpCase):
|
||||
response = self.url_open('/fusion_clock/kiosk/nfc')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn('NFC Clock Kiosk', response.text)
|
||||
|
||||
|
||||
from odoo.tests.common import TransactionCase
|
||||
from odoo.addons.fusion_clock.controllers.clock_nfc_kiosk import FusionClockNfcKiosk
|
||||
|
||||
|
||||
@tagged('-at_install', 'post_install', 'fusion_clock')
|
||||
class TestUidNormalization(TransactionCase):
|
||||
|
||||
def test_lowercase_input_uppercased(self):
|
||||
self.assertEqual(
|
||||
FusionClockNfcKiosk._normalize_uid('04:a2:b5:62:c1:80'),
|
||||
'04:A2:B5:62:C1:80',
|
||||
)
|
||||
|
||||
def test_no_separator_input_gets_colons(self):
|
||||
self.assertEqual(
|
||||
FusionClockNfcKiosk._normalize_uid('04A2B562C180'),
|
||||
'04:A2:B5:62:C1:80',
|
||||
)
|
||||
|
||||
def test_dash_separator_replaced(self):
|
||||
self.assertEqual(
|
||||
FusionClockNfcKiosk._normalize_uid('04-A2-B5-62-C1-80'),
|
||||
'04:A2:B5:62:C1:80',
|
||||
)
|
||||
|
||||
def test_whitespace_stripped(self):
|
||||
self.assertEqual(
|
||||
FusionClockNfcKiosk._normalize_uid(' 04:A2:B5:62:C1:80 '),
|
||||
'04:A2:B5:62:C1:80',
|
||||
)
|
||||
|
||||
def test_empty_input_returns_none(self):
|
||||
self.assertIsNone(FusionClockNfcKiosk._normalize_uid(''))
|
||||
self.assertIsNone(FusionClockNfcKiosk._normalize_uid(None))
|
||||
|
||||
def test_invalid_chars_returns_none(self):
|
||||
self.assertIsNone(FusionClockNfcKiosk._normalize_uid('not-a-uid'))
|
||||
self.assertIsNone(FusionClockNfcKiosk._normalize_uid('04:A2:ZZ:62:C1:80'))
|
||||
|
||||
def test_odd_length_returns_none(self):
|
||||
self.assertIsNone(FusionClockNfcKiosk._normalize_uid('04A2B562C18'))
|
||||
|
||||
Reference in New Issue
Block a user