def test_reconfigure():
    dm = DoorManager()
    dm.configure([Door(name="100", pin_id=21, readers=[]), Door(name="101", pin_id=22, readers=[])])
    assert len(dm._doors) == 2
    assert dm._doors["100"].pin_id == 21

    dm.configure([Door(name="102", pin_id=23, readers=[])])
    assert len(dm._doors) == 1
    assert dm._doors["102"].pin_id == 23
Пример #2
0
def test_clean(config, door_manager):
    door_manager.configure([
        Door(name="lol1", pin_id=22, readers=[]),
        Door(name="lol2", pin_id=23, readers=[]),
        Door(name="lol3", pin_id=24, readers=[]),
    ])
    door_manager.all()
    assert len(door_manager.all()) == 3

    config.clean()
    assert len(door_manager.all()) == 0
def test_reconfigure(nfc_reader_manager):
    nfc_reader_manager.configure([
        Door(name="100", pin_id=21, readers=["D8", "D10"]),
        Door(name="101", pin_id=22, readers=["D9"])
    ])
    assert len(nfc_reader_manager._nfc_readers) == 3
    assert nfc_reader_manager._nfc_readers["D10"].door == "100"

    nfc_reader_manager.configure(
        [Door(name="102", pin_id=23, readers=["D9", "D20"])])
    assert len(nfc_reader_manager._nfc_readers) == 2
    assert nfc_reader_manager._nfc_readers["D9"].door == "102"
def test_all_by_door_name(nfc_reader_manager):
    nfc_reader_manager.configure([
        Door(name="lol", pin_id=21, readers=["D9", "D19"]),
        Door(name="lol1", pin_id=22, readers=["D10", "D21"]),
        Door(name="lol2", pin_id=23, readers=["D1", "D2"]),
    ])
    nfc_readers = nfc_reader_manager.all_by_door_name(door_name="lol1")
    assert len(nfc_readers) == 2
    assert nfc_readers[0].door == "lol1"
    assert nfc_readers[0].pin_number == "D10"
    assert nfc_readers[1].door == "lol1"
    assert nfc_readers[1].pin_number == "D21"
def test_all_by_not_opened(door_manager):
    door_manager.configure([
        Door(name="lol", pin_id=21, readers=[]),
        Door(name="lol2", pin_id=22, readers=[]),
    ])
    doors = door_manager.all()
    assert len(doors) == 2

    doors[0]._opened = True

    doors_not_opened = door_manager.all_by_not_opened()
    assert len(doors_not_opened) == 1
    assert doors_not_opened[0].name == "lol2"
    assert doors_not_opened[0].pin_id == 22
Пример #6
0
async def test_api_door_open(cli, mocker, gpio, door_manager):
    door_manager.configure([Door(name="103", pin_id=20, readers=[])])
    mocker.patch("nfcclient.doors.model.Door.open")
    response = await cli.get("/doors/103/open/?seconds=1")
    assert response.status == 200
    door_manager.get("103").open.assert_called_once_with(seconds=1,
                                                         remote=True)
Пример #7
0
async def test_api_refresh(config, cli, monkeypatch, door_manager):
    door_manager.configure([Door(name="103", pin_id=22, readers=[])])
    assert door_manager.get("103").pin_id == 22
    assert door_manager.get("100") is None

    with aioresponses(passthrough=[f"http://{cli.host}:{cli.port}"]) as mocked:
        mocked.get("http://localhost:8123/config/1",
                   payload={
                       "master_keys": [
                           "0x2b0x150x270xc", "0xda0x130x640x1a",
                           "0xca0xbf0x570x1a", "0xa0x720xa90x15"
                       ],
                       "doors": [
                           {
                               "name": "100",
                               "pin_id": 21,
                               "readers": ["D23", "D24"]
                           },
                       ],
                       "reader_timeout":
                       0.5,
                       "door_open_seconds":
                       1,
                   })

        response = await cli.get("/refresh/")
        assert response.status == 200
        assert door_manager.get("100").pin_id == 21
        assert door_manager.get("103") is None
Пример #8
0
def test_doors():
    d = Door.from_dict({
        "name": "a",
        "pin_id": 1,
        "readers": ["b"],
    })
    assert d.name == "a"
    assert d.pin_id == 1
    assert d.readers == ["b"]
def test_all_immutable(nfc_reader_manager):
    door = Door(name="lol", pin_id=21, readers=["D9"])
    nfc_reader_manager.configure([door])
    nfc_readers = nfc_reader_manager.all()
    assert len(nfc_readers) == 1
    assert nfc_readers[0].door == "lol"

    nfc_readers.append(NFCReader(pin="22", door="101", reader_timeout=1))
    assert len(nfc_readers) == 2
    assert len(nfc_reader_manager.all()) == 1
def test_all_immutable(door_manager):
    door = Door(name="lol", pin_id=21, readers=[])
    door_manager.configure([door])
    doors = door_manager.all()
    assert len(doors) == 1
    assert doors[0].name == "lol"
    assert doors[0].pin_id == 21

    doors.append(door)
    assert len(doors) == 2
    assert len(door_manager.all()) == 1
Пример #11
0
def test_card_read(event_loop, mocker, config, gpio, door_manager):
    door_name = "103"
    pin = 1
    door_manager.configure([Door(name=door_name, pin_id=pin, readers=[])])
    mocker.patch("nfcclient.nfc_reader.nfc_reader_mock.NFCReaderMock.read_card", return_value=[43, 21, 39, 12])
    mocker.patch("nfcclient.card_reader.CardReaderFacade.authorize", return_value=True)
    mocker.patch("nfcclient.doors.model.Door._open")
    nfc_reader_mock = NFCReaderMock(pin="1", door=door_name, reader_timeout=0.5)
    reader = CardReaderFacade(config)
    event_loop.run_until_complete(reader.read_card(reader=nfc_reader_mock))

    door_manager.get(door_name)._open.assert_called_once()
Пример #12
0
def door_manager():
    dm.configure([Door(name="100", pin_id=21, readers=[])])
    return dm
def test_configure_fails(nfc_reader_manager, mocker, caplog):
    mocker.patch(
        "nfcclient.nfc_reader.nfc_reader_factory.NFCReaderFactory.create"
    ).side_effect = Exception("LOL")
    nfc_reader_manager.configure([Door(name="100", pin_id=21, readers=["D8"])])
    assert "NFC Reader D8 for door 100 failed: LOL" in caplog.text
def test_all(nfc_reader_manager):
    nfc_reader_manager.configure([Door(name="lol", pin_id=21, readers=["D9"])])
    nfc_readers = nfc_reader_manager.all()
    assert len(nfc_readers) == 1
    assert nfc_readers[0].door == "lol"
def test_configure(nfc_reader_manager):
    nfc_reader_manager.configure([Door(name="100", pin_id=21, readers=["D8"])])
    assert len(nfc_reader_manager._nfc_readers) == 1
    assert nfc_reader_manager._nfc_readers["D8"].door == "100"
def test_get(door_manager):
    door_manager.configure([Door(name="lol", pin_id=21, readers=[])])
    door = door_manager.get("lol")
    assert door.name == "lol"
    assert door.pin_id == 21
def test_all(door_manager):
    door_manager.configure([Door(name="lol", pin_id=21, readers=[])])
    doors = door_manager.all()
    assert len(doors) == 1
    assert doors[0].name == "lol"
    assert doors[0].pin_id == 21