2020-12-25 16:45:28 -05:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import aprsd
|
2021-01-11 12:09:29 -05:00
|
|
|
from aprsd import messaging
|
2020-12-25 16:45:28 -05:00
|
|
|
from aprsd.fuzzyclock import fuzzy
|
2021-01-09 09:58:56 -05:00
|
|
|
from aprsd.plugins import fortune as fortune_plugin
|
|
|
|
from aprsd.plugins import ping as ping_plugin
|
2021-01-11 12:09:29 -05:00
|
|
|
from aprsd.plugins import query as query_plugin
|
2021-01-09 09:58:56 -05:00
|
|
|
from aprsd.plugins import time as time_plugin
|
|
|
|
from aprsd.plugins import version as version_plugin
|
2020-12-25 16:45:28 -05:00
|
|
|
|
|
|
|
|
2021-01-06 17:50:02 -05:00
|
|
|
class TestPlugin(unittest.TestCase):
|
2020-12-25 16:45:28 -05:00
|
|
|
def setUp(self):
|
2020-12-25 18:13:52 -05:00
|
|
|
self.fromcall = "KFART"
|
|
|
|
self.ack = 1
|
2021-01-11 12:09:29 -05:00
|
|
|
self.config = {"ham": {"callsign": self.fromcall}}
|
2020-12-25 16:45:28 -05:00
|
|
|
|
2020-12-25 18:13:52 -05:00
|
|
|
@mock.patch("shutil.which")
|
|
|
|
def test_fortune_fail(self, mock_which):
|
2021-01-09 09:58:56 -05:00
|
|
|
fortune = fortune_plugin.FortunePlugin(self.config)
|
2020-12-25 18:13:52 -05:00
|
|
|
mock_which.return_value = None
|
|
|
|
message = "fortune"
|
|
|
|
expected = "Fortune command not installed"
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = fortune.run(self.fromcall, message, self.ack)
|
2020-12-25 18:13:52 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-01-11 14:29:02 -05:00
|
|
|
@mock.patch("subprocess.check_output")
|
2020-12-25 18:13:52 -05:00
|
|
|
@mock.patch("shutil.which")
|
2021-01-11 14:29:02 -05:00
|
|
|
def test_fortune_success(self, mock_which, mock_output):
|
2021-01-09 09:58:56 -05:00
|
|
|
fortune = fortune_plugin.FortunePlugin(self.config)
|
2020-12-25 18:13:52 -05:00
|
|
|
mock_which.return_value = "/usr/bin/games"
|
|
|
|
|
2021-01-11 14:29:02 -05:00
|
|
|
mock_output.return_value = "Funny fortune"
|
2020-12-25 18:13:52 -05:00
|
|
|
|
|
|
|
message = "fortune"
|
|
|
|
expected = "Funny fortune"
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = fortune.run(self.fromcall, message, self.ack)
|
2020-12-25 18:13:52 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2021-01-11 12:09:29 -05:00
|
|
|
@mock.patch("aprsd.messaging.MsgTrack.flush")
|
|
|
|
def test_query_flush(self, mock_flush):
|
2021-01-17 10:55:59 -05:00
|
|
|
message = "!delete"
|
2021-01-11 12:09:29 -05:00
|
|
|
query = query_plugin.QueryPlugin(self.config)
|
|
|
|
|
2021-01-14 14:28:59 -05:00
|
|
|
expected = "Deleted ALL pending msgs."
|
2021-01-11 12:09:29 -05:00
|
|
|
actual = query.run(self.fromcall, message, self.ack)
|
|
|
|
mock_flush.assert_called_once()
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
@mock.patch("aprsd.messaging.MsgTrack.restart_delayed")
|
|
|
|
def test_query_restart_delayed(self, mock_restart):
|
|
|
|
track = messaging.MsgTrack()
|
|
|
|
track.track = {}
|
2021-01-17 10:55:59 -05:00
|
|
|
message = "!4"
|
2021-01-11 12:09:29 -05:00
|
|
|
query = query_plugin.QueryPlugin(self.config)
|
|
|
|
|
2021-01-14 15:06:57 -05:00
|
|
|
expected = "No pending msgs to resend"
|
2021-01-11 12:09:29 -05:00
|
|
|
actual = query.run(self.fromcall, message, self.ack)
|
|
|
|
mock_restart.assert_not_called()
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
mock_restart.reset_mock()
|
|
|
|
|
|
|
|
# add a message
|
|
|
|
msg = messaging.TextMessage(self.fromcall, "testing", self.ack)
|
|
|
|
track.add(msg)
|
|
|
|
actual = query.run(self.fromcall, message, self.ack)
|
|
|
|
mock_restart.assert_called_once()
|
|
|
|
|
2020-12-25 16:45:28 -05:00
|
|
|
@mock.patch("time.localtime")
|
2021-01-06 17:50:02 -05:00
|
|
|
def test_time(self, mock_time):
|
2020-12-25 16:45:28 -05:00
|
|
|
fake_time = mock.MagicMock()
|
|
|
|
h = fake_time.tm_hour = 16
|
|
|
|
m = fake_time.tm_min = 12
|
2021-01-06 17:50:02 -05:00
|
|
|
fake_time.tm_sec = 55
|
2020-12-25 16:45:28 -05:00
|
|
|
mock_time.return_value = fake_time
|
2021-01-09 09:58:56 -05:00
|
|
|
time = time_plugin.TimePlugin(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
|
|
|
fromcall = "KFART"
|
|
|
|
message = "location"
|
|
|
|
ack = 1
|
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = time.run(fromcall, message, ack)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(None, actual)
|
|
|
|
|
|
|
|
cur_time = fuzzy(h, m, 1)
|
|
|
|
|
|
|
|
message = "time"
|
|
|
|
expected = "{} ({}:{} PDT) ({})".format(
|
2021-01-08 15:47:30 -05:00
|
|
|
cur_time,
|
|
|
|
str(h),
|
|
|
|
str(m).rjust(2, "0"),
|
|
|
|
message.rstrip(),
|
2020-12-25 16:45:28 -05:00
|
|
|
)
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = time.run(fromcall, message, ack)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
@mock.patch("time.localtime")
|
2021-01-06 17:50:02 -05:00
|
|
|
def test_ping(self, mock_time):
|
2020-12-25 16:45:28 -05:00
|
|
|
fake_time = mock.MagicMock()
|
|
|
|
h = fake_time.tm_hour = 16
|
|
|
|
m = fake_time.tm_min = 12
|
|
|
|
s = fake_time.tm_sec = 55
|
|
|
|
mock_time.return_value = fake_time
|
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
ping = ping_plugin.PingPlugin(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
|
|
|
fromcall = "KFART"
|
|
|
|
message = "location"
|
|
|
|
ack = 1
|
|
|
|
|
|
|
|
result = ping.run(fromcall, message, ack)
|
|
|
|
self.assertEqual(None, result)
|
|
|
|
|
|
|
|
def ping_str(h, m, s):
|
|
|
|
return (
|
|
|
|
"Pong! "
|
|
|
|
+ str(h).zfill(2)
|
|
|
|
+ ":"
|
|
|
|
+ str(m).zfill(2)
|
|
|
|
+ ":"
|
|
|
|
+ str(s).zfill(2)
|
|
|
|
)
|
|
|
|
|
|
|
|
message = "Ping"
|
|
|
|
actual = ping.run(fromcall, message, ack)
|
|
|
|
expected = ping_str(h, m, s)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
message = "ping"
|
|
|
|
actual = ping.run(fromcall, message, ack)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
def test_version(self):
|
|
|
|
expected = "APRSD version '{}'".format(aprsd.__version__)
|
2021-01-09 09:58:56 -05:00
|
|
|
version = version_plugin.VersionPlugin(self.config)
|
2020-12-25 16:45:28 -05:00
|
|
|
|
|
|
|
fromcall = "KFART"
|
|
|
|
message = "No"
|
|
|
|
ack = 1
|
|
|
|
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = version.run(fromcall, message, ack)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(None, actual)
|
|
|
|
|
|
|
|
message = "version"
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = version.run(fromcall, message, ack)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
message = "Version"
|
2021-01-09 09:58:56 -05:00
|
|
|
actual = version.run(fromcall, message, ack)
|
2020-12-25 16:45:28 -05:00
|
|
|
self.assertEqual(expected, actual)
|