Add tests and typing annotation for Credential
This commit is contained in:
parent
0528686903
commit
79fb5f1b27
@ -1,11 +1,14 @@
|
||||
import bitstring
|
||||
from typing import Tuple, Optional
|
||||
|
||||
# Reference for H10301 card format:
|
||||
# https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf
|
||||
|
||||
|
||||
class Credential:
|
||||
def __init__(self, code=None, hex=None):
|
||||
def __init__(
|
||||
self, code: Optional[Tuple[int, int]] = None, hex: Optional[str] = None
|
||||
) -> None:
|
||||
if code is None and hex is None:
|
||||
raise TypeError("Must set either code or hex for a Credential")
|
||||
elif code is not None and hex is not None:
|
||||
@ -21,21 +24,24 @@ class Credential:
|
||||
elif hex is not None:
|
||||
self.bits = bitstring.Bits(hex=hex)
|
||||
|
||||
def __repr__(self):
|
||||
def __repr__(self) -> str:
|
||||
return f"Credential({self.code})"
|
||||
|
||||
def __eq__(self, other):
|
||||
def __eq__(self, other: object) -> bool:
|
||||
if isinstance(other, Credential):
|
||||
return self.bits == other.bits
|
||||
else:
|
||||
return False
|
||||
|
||||
def __hash__(self):
|
||||
def __hash__(self) -> int:
|
||||
return self.bits.int
|
||||
|
||||
@property
|
||||
def code(self):
|
||||
def code(self) -> Tuple[int, int]:
|
||||
facility = self.bits[7:15].uint
|
||||
code = self.bits[15:31].uint
|
||||
return (facility, code)
|
||||
|
||||
@property
|
||||
def hex(self):
|
||||
def hex(self) -> str:
|
||||
return self.bits.hex.upper()
|
||||
|
0
memberPlumbing/hid/tests/__init__.py
Normal file
0
memberPlumbing/hid/tests/__init__.py
Normal file
13
memberPlumbing/hid/tests/test_Credential.py
Normal file
13
memberPlumbing/hid/tests/test_Credential.py
Normal file
@ -0,0 +1,13 @@
|
||||
import pytest
|
||||
|
||||
from ..Credential import Credential
|
||||
|
||||
|
||||
def test_code_to_hex() -> None:
|
||||
cred = Credential(code=(123, 45678))
|
||||
assert cred.hex == "02F764DD"
|
||||
|
||||
|
||||
def test_hex_to_code() -> None:
|
||||
cred = Credential(hex="02F764DD")
|
||||
assert cred.code == (123, 45678)
|
Reference in New Issue
Block a user