unicheckbot/apps/tgbot/test/test_port_parsers.py

75 lines
2.3 KiB
Python
Raw Normal View History

2021-02-20 18:24:39 +03:00
from unittest import TestCase
from tgbot.handlers.default.tcp import TCPCheckerHandler
from tgbot.handlers.base import process_args_for_host_port,\
2021-02-20 18:24:39 +03:00
NotEnoughArgs, InvalidPort
class TestArgsProc(TestCase):
def test_exceptions(self):
"""Test exceptions being raised
on invalid commands
"""
cases = [
('/cmd', NotEnoughArgs),
('/cmd example.com testsarenice', InvalidPort)
]
for cmd, exc in cases:
with self.subTest(command=cmd):
self.assertRaises(
exc,
2021-02-20 22:56:20 +03:00
process_args_for_host_port, cmd, 443
2021-02-20 18:24:39 +03:00
)
def test_host_port(self):
"""Test that host and port are parsed correctly
"""
cases = [
('/cmd example.com', 'example.com', 443),
('/cmd example.com 42', 'example.com', 42),
('/cmd example.com:42', 'example.com', 42)
]
for cmd, host, port in cases:
with self.subTest(cmd=cmd, host=host, port=port):
test_host, test_port = process_args_for_host_port(cmd, 443)
self.assertEqual(test_host, host)
self.assertEqual(test_port, port)
class TestTCPCheckerHandler(TestCase):
def setUp(self) -> None:
self.method = TCPCheckerHandler().process_args
return super().setUp()
def test_exceptions(self):
"""Test all appropriate excpetions are raised.
"""
cases = [
('/cmd', NotEnoughArgs),
('/cmd example.com', NotEnoughArgs),
('/cmd example.com jdbnjsbndjsd', InvalidPort)
]
for cmd, exc in cases:
with self.subTest(cmd=cmd):
self.assertRaises(
exc,
2021-02-20 22:56:20 +03:00
self.method, cmd
2021-02-20 18:24:39 +03:00
)
def test_host_port(self):
"""Test that host and port are parsed correctly
"""
cases = [
('/cmd example.com 42', 'example.com', 42),
('/cmd example.com:65', 'example.com', 65)
]
for cmd, host, port in cases:
with self.subTest(cmd=cmd, host=host, port=port):
test_host, test_port = self.method(cmd)
self.assertEqual(test_host, host)
self.assertEqual(test_port, port)