Skip to content

Commit

Permalink
Merge pull request #151 from infinityofspace/unit_tests
Browse files Browse the repository at this point in the history
added proper unit tests closes #149
  • Loading branch information
infinityofspace authored Nov 12, 2024
2 parents b5499bc + cc0a4ad commit 2ff93ed
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 290 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ setuptools>=41.6.0
requests>=2.20.0,<3.0
certbot>=1.18.0,<4.0
dnspython>=2.0.0,<3.0
responses~=0.25
132 changes: 132 additions & 0 deletions tests/cert_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import unittest
from argparse import Namespace

import responses
from certbot.configuration import NamespaceConfig
from certbot.errors import PluginError

from certbot_dns_duckdns.cert.client import Authenticator


class TestCertClient(unittest.TestCase):
@responses.activate
def test_valid_auth(self):
api_token = "token"
domain = "example.duckdns.org"
txt_value = "ABCDEF"

responses.get(
url=f"https://www.duckdns.org/update?token={api_token}&domains={domain}&txt={txt_value}",
body="OK",
)

namespace = Namespace(
duckdns_token=api_token,
duckdns_token_token_env="DUCKDNS_TOKEN",
duckdns_no_txt_restore=False,
config_dir="config_dir",
work_dir="work_dir",
logs_dir="logs_dir",
http01_port=80,
https_port=443,
domains=["example.duckdns.org"],
)
config = NamespaceConfig(namespace)

authenticator = Authenticator(config, name="duckdns")

authenticator._perform(
domain=domain, validation_name="", validation=txt_value
)

@responses.activate
def test_invalid_auth(self):
api_token = "token"
domain = "example.duckdns.org"
txt_value = "ABCDEF"

responses.get(
url=f"https://www.duckdns.org/update?token={api_token}&domains={domain}&txt={txt_value}",
body="OK",
)

namespace = Namespace(
duckdns_token=api_token + "invalid",
duckdns_token_token_env="DUCKDNS_TOKEN",
duckdns_no_txt_restore=False,
config_dir="config_dir",
work_dir="work_dir",
logs_dir="logs_dir",
http01_port=80,
https_port=443,
domains=["example.duckdns.org"],
)
config = NamespaceConfig(namespace)

authenticator = Authenticator(config, name="duckdns")

with self.assertRaises(PluginError):
authenticator._perform(
domain=domain, validation_name="", validation=txt_value
)

@responses.activate
def test_invalid_duckdns_domain(self):
api_token = "token"
domain = "example.org"
txt_value = "ABCDEF"

responses.get(
url=f"https://www.duckdns.org/update?token={api_token}&domains={domain}&txt={txt_value}",
body="OK",
)

namespace = Namespace(
duckdns_token=api_token,
duckdns_token_token_env="DUCKDNS_TOKEN",
duckdns_no_txt_restore=False,
config_dir="config_dir",
work_dir="work_dir",
logs_dir="logs_dir",
http01_port=80,
https_port=443,
domains=[domain],
)
config = NamespaceConfig(namespace)

authenticator = Authenticator(config, name="duckdns")

with self.assertRaises(PluginError):
authenticator._perform(
domain=domain, validation_name="", validation=txt_value
)

@responses.activate
def test_cleanup(self):
api_token = "token"
domain = "example.duckdns.org"
txt_value = "ABCDEF"

responses.get(
url=f"https://www.duckdns.org/update?token={api_token}&domains={domain}&txt=&clear=true",
body="OK",
)

namespace = Namespace(
duckdns_token=api_token,
duckdns_token_token_env="DUCKDNS_TOKEN",
duckdns_no_txt_restore=False,
config_dir="config_dir",
work_dir="work_dir",
logs_dir="logs_dir",
http01_port=80,
https_port=443,
domains=["example.duckdns.org"],
)
config = NamespaceConfig(namespace)

authenticator = Authenticator(config, name="duckdns")

authenticator._cleanup(
domain=domain, validation_name="", validation=txt_value
)
165 changes: 0 additions & 165 deletions tests/cert_test.py

This file was deleted.

Loading

0 comments on commit 2ff93ed

Please sign in to comment.