76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
import json
|
|
from unittest import TestCase
|
|
from unittest.mock import patch
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from ip_listener.cli import DEFAULT_API_URL, run
|
|
|
|
|
|
class RunTestCase(TestCase):
|
|
def setUp(self):
|
|
patcher = patch("ip_listener.main.subprocess.check_output")
|
|
self.mocked_dns = patcher.start()
|
|
|
|
patcher = patch("ip_listener.main.requests.get")
|
|
self.mocked_get = patcher.start()
|
|
|
|
patcher = patch("ip_listener.main.requests.put")
|
|
self.mocked_put = patcher.start()
|
|
|
|
def test_simple(self):
|
|
self.mocked_dns.return_value = b"111.420\n"
|
|
self.mocked_get.return_value.json.return_value = {
|
|
"dnsEntries": [
|
|
{
|
|
"name": "@",
|
|
"expire": 60,
|
|
"type": "A",
|
|
"content": "111.421",
|
|
}
|
|
],
|
|
"_links": [
|
|
{
|
|
"rel": "self",
|
|
"link": "https://api.transip.nl/v6/domains/foobar.com/dns",
|
|
},
|
|
{
|
|
"rel": "domain",
|
|
"link": "https://api.transip.nl/v6/domains/foobar.com",
|
|
},
|
|
],
|
|
}
|
|
|
|
with self.assertLogs("ip_listener.main", level="INFO") as logger:
|
|
runner = CliRunner()
|
|
result = runner.invoke(run, ["foobar.com", "TOKEN"])
|
|
|
|
self.assertEqual(
|
|
logger.output, ["INFO:ip_listener.main:Updated domain foobar.com"]
|
|
)
|
|
|
|
self.assertEqual(result.exit_code, 0)
|
|
|
|
self.mocked_get.assert_called_with(
|
|
f"{DEFAULT_API_URL}/domains/foobar.com/dns",
|
|
headers={"Authorization": "Bearer TOKEN"},
|
|
)
|
|
|
|
expected_json = json.dumps(
|
|
{
|
|
"dnsEntries": [
|
|
{
|
|
"name": "@",
|
|
"expire": 60,
|
|
"type": "A",
|
|
"content": "111.420",
|
|
}
|
|
]
|
|
}
|
|
)
|
|
|
|
self.mocked_put.assert_called_with(
|
|
f"{DEFAULT_API_URL}/domains/foobar.com/dns",
|
|
data=expected_json,
|
|
headers={"Authorization": "Bearer TOKEN"},
|
|
)
|