Skip to content

Commit

Permalink
Restore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI committed Sep 4, 2021
1 parent cfad359 commit 3293179
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 3293179

Please sign in to comment.