Skip to content

Commit

Permalink
Add tests for the ClouDNS Authenticator.
Browse files Browse the repository at this point in the history
Adds testing for the Authenticator that follow the conventions of the upstream
certbot repository.  This should ensure compatibility if this was ever merged
upstream.
  • Loading branch information
tedski authored and dalbothek committed Sep 25, 2024
1 parent 45ee81a commit 40f52b0
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 207 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ jobs:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -93,4 +94,5 @@ jobs:
- name: Build package
run: poetry build

# TODO: Run some tests
- name: Run tests
run: poetry run pytest
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ dmypy.json
# Cython debug symbols
cython_debug/

### VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### JetBrains
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"certbot_dns_cloudns"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
1 change: 1 addition & 0 deletions certbot_dns_cloudns/_internal/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""certbot-dns-cloudns tests"""
127 changes: 127 additions & 0 deletions certbot_dns_cloudns/_internal/tests/authenticator_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""Tests for certbot_dns_cloudns._internal.authenticator."""

import sys
from unittest import mock

import pytest

from certbot import errors
from certbot.compat import os
from certbot.plugins import dns_test_common
from certbot.plugins.dns_test_common import DOMAIN
from certbot.tests import util as test_util

AUTH_ID = "1234"
SUB_AUTH_ID = "5678"
SUB_AUTH_USER = "tester"
AUTH_PASSWORD = "foobar"


class AuthenticatorTest(
test_util.TempDirTestCase, dns_test_common.BaseAuthenticatorTest
):
def setUp(self):
from certbot_dns_cloudns._internal.authenticator import Authenticator

super().setUp()

path = os.path.join(self.tempdir, "file.ini")
dns_test_common.write(
{
"cloudns_auth_id": AUTH_ID,
"cloudns_auth_password": AUTH_PASSWORD,
},
path,
)

self.config = mock.MagicMock(
cloudns_credentials=path,
cloudns_propagation_seconds=0, # don't wait during tests
cloudns_nameserver="1.1.1.1", # nameserver is required for parsing
)
self.auth = Authenticator(self.config, "cloudns")
self.mock_client = mock.MagicMock()
self.auth._get_client = mock.MagicMock(return_value=self.mock_client)

@test_util.patch_display_util()
def test_perform(self, unused_mock_get_utility):
self.auth._change_txt_record = mock.MagicMock()
self.auth._wait_for_change = mock.MagicMock()
self.auth.perform([self.achall])

expected = [
mock.call.add_txt_record(
DOMAIN, "_acme-challenge." + DOMAIN, mock.ANY, mock.ANY
)
]
assert expected == self.mock_client.mock_calls

def test_cleanup(self):
self.auth._attempt_cleanup = True
self.auth.cleanup([self.achall])

expected = [
mock.call.del_txt_record(
DOMAIN, "_acme-challenge." + DOMAIN, mock.ANY
)
]
assert expected == self.mock_client.mock_calls

@test_util.patch_display_util()
def test_sub_auth_user(self, unused_mock_get_utility):
dns_test_common.write(
{
"cloudns_sub_auth_user": SUB_AUTH_USER,
"cloudns_auth_password": AUTH_PASSWORD,
},
self.config.cloudns_credentials,
)
self.auth.perform([self.achall])

expected = [
mock.call.add_txt_record(
DOMAIN, "_acme-challenge." + DOMAIN, mock.ANY, mock.ANY
)
]
assert expected == self.mock_client.mock_calls

@test_util.patch_display_util()
def test_sub_auth_id(self, unused_mock_get_utility):
dns_test_common.write(
{
"cloudns_sub_auth_id": SUB_AUTH_ID,
"cloudns_auth_password": AUTH_PASSWORD,
},
self.config.cloudns_credentials,
)
self.auth.perform([self.achall])

expected = [
mock.call.add_txt_record(
DOMAIN, "_acme-challenge." + DOMAIN, mock.ANY, mock.ANY
)
]
assert expected == self.mock_client.mock_calls

def test_no_creds(self):
dns_test_common.write({}, self.config.cloudns_credentials)
with pytest.raises(errors.PluginError):
self.auth.perform([self.achall])

def test_missing_auth_password_or_auth_id(self):
dns_test_common.write(
{"cloudns_auth_id": AUTH_ID}, self.config.cloudns_credentials
)
with pytest.raises(errors.PluginError):
self.auth.perform([self.achall])

dns_test_common.write(
{"clouds_auth_password": AUTH_PASSWORD},
self.config.cloudns_credentials,
)
with pytest.raises(errors.PluginError):
self.auth.perform([self.achall])


if __name__ == "__main__":
sys.exit(pytest.main(sys.argv[1:] + [__file__])) # pragma: no cover
Loading

0 comments on commit 40f52b0

Please sign in to comment.