diff --git a/tests/test_scanner.py b/tests/test_scanner.py index 7ab4ec2..61395c8 100644 --- a/tests/test_scanner.py +++ b/tests/test_scanner.py @@ -1,13 +1,13 @@ """Scanner specific tests""" from __future__ import annotations -from unittest import TestCase +from unittest import TestCase, mock from unittest.mock import NonCallableMock, patch from faker import Faker from faker.providers import internet as internet_provider, python as python_provider -from glocaltokens.scanner import NetworkDevice +from glocaltokens.scanner import CastListener, NetworkDevice faker = Faker() # type: ignore faker.add_provider(internet_provider) @@ -41,13 +41,52 @@ def test_initialization(self) -> None: ) @patch("glocaltokens.scanner.LOGGER.error") - def test_initialization__valid(self, mock: NonCallableMock) -> None: - """Valid initialization tests""" - NetworkDevice( - faker.word(), - faker.ipv4_private(), - faker.port_number(), - faker.word(), - faker.word(), - ) - self.assertEqual(mock.call_count, 0) + def test_service_info__valid(self, m_error: NonCallableMock) -> None: + """Valid service_info tests""" + service = mock.Mock(name="Service") + service.parsed_addresses.return_value = None + service.server = faker.ipv4_private() + service.port = faker.port_number() + + zc = mock.Mock(name="Zeroconf") + zc.get_service_info.return_value = service + + listener = CastListener() + type_ = faker.word() + name = faker.word() + listener.add_service(zc, type_, name) + self.assertEqual(m_error.call_count, 0) + + @patch("glocaltokens.scanner.LOGGER.error") + def test_service_info__invalid(self, m_error: NonCallableMock) -> None: + """Invalid service_info tests""" + service = mock.Mock(name="Service") + service.parsed_addresses.return_value = None + + zc = mock.Mock(name="Zeroconf") + zc.get_service_info.return_value = service + + listener = CastListener() + type_ = faker.word() + name = faker.word() + + # With invalid IP + service.server = faker.word() + service.port = faker.port_number() + listener.add_service(zc, type_, name) + self.assertEqual(m_error.call_count, 1) + + # With negative port + service.server = faker.ipv4_private() + service.port = faker.pyint(min_value=-9999, max_value=-1) + listener.add_service(zc, type_, name) + self.assertEqual(m_error.call_count, 2) + + # With greater port + service.server = faker.ipv4_private() + service.port = faker.pyint(min_value=65535, max_value=999999) + listener.add_service(zc, type_, name) + self.assertEqual(m_error.call_count, 3) + + # No devices should be added + self.assertEqual(listener.count, 0)