From 23b74d950d0e4c9ad4dd0f63c945dc9011ebb7bd Mon Sep 17 00:00:00 2001 From: black Date: Fri, 21 Jun 2024 14:52:08 +0200 Subject: [PATCH] Add test cases for sanitize_record() --- tests/test_misc.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/test_misc.py diff --git a/tests/test_misc.py b/tests/test_misc.py new file mode 100644 index 0000000..c208167 --- /dev/null +++ b/tests/test_misc.py @@ -0,0 +1,67 @@ +import pytest + +import desec + + +@pytest.mark.parametrize( + "rtype, subname, records", + [ + ("A", "", ["192.0.2.1"]), + ("A", "", ["192.0.2.1", "192.0.2.2"]), + ("AAAA", "", ["2001:db8:10::1"]), + ("TXT", "test", ['"test"']), + ("CNAME", "www", ["test-suite.test."]), + ("MX", "", ["test-suite.test."]), + ("NS", "", ["ns1.test-suite.test.", "ns2.test-suite.test."]), + ], +) +def test_sanitize_records_valid(rtype, subname, records): + """Test sanitize_records() with valid input. + + Assert that output is the unchanged input. + """ + rrset = desec.sanitize_records(rtype, subname, records) + + assert rrset == records + + +@pytest.mark.parametrize( + "rtype, subname, records, expected", + [ + ("TXT", "test", ["test"], ['"test"']), + ("CNAME", "www", ["test-suite.test"], ["test-suite.test."]), + ("MX", "", ["test-suite.test"], ["test-suite.test."]), + ( + "NS", + "", + ["ns1.test-suite.test", "ns2.test-suite.test"], + ["ns1.test-suite.test.", "ns2.test-suite.test."], + ), + ], +) +def test_sanitize_records_fixable(rtype, subname, records, expected): + """Test sanitize_records() with fixable errors. + + Assert that the output is fixed. + """ + rrset = desec.sanitize_records(rtype, subname, records) + + assert rrset == expected + + +@pytest.mark.parametrize( + "rtype, subname, records", + [ + ("CNAME", "www", ["test-suite.test.", "prod-suite.test."]), + ("CNAME", "", ["test-suite.test."]), + ("NS", "*", ["ns1.test-suite.test", "ns2.test-suite.test"]), + ("NS", "*.test", ["ns1.test-suite.test", "ns2.test-suite.test"]), + ], +) +def test_sanitize_records_unfixable(rtype, subname, records): + """Test sanitize_records() with unfixable errors. + + Assert that an appropriate exception is raised. + """ + with pytest.raises(desec.ParameterCheckError): + desec.sanitize_records(rtype, subname, records)