diff --git a/.drone.jsonnet b/.drone.jsonnet index 53a80ee..d34a910 100644 --- a/.drone.jsonnet +++ b/.drone.jsonnet @@ -7,7 +7,7 @@ local PythonVersion(pyversion='2.7') = { commands: [ 'pip install -r test-requirements.txt -qq', 'pip install -qq .', - 'pytest --cov=corenetworks --no-cov-on-fail', + 'pytest corenetworks --cov=corenetworks --no-cov-on-fail', ], depends_on: [ 'clone', @@ -98,7 +98,7 @@ local PipelineSecurity = { commands: [ 'pip install -r test-requirements.txt -qq', 'pip install -qq .', - 'bandit -r ./corenetworks -x ./corenetworks/tests', + 'bandit -r ./corenetworks -x ./corenetworks/test', ], }, ], diff --git a/.drone.yml b/.drone.yml index 23b7235..531512e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -36,7 +36,7 @@ steps: commands: - pip install -r test-requirements.txt -qq - pip install -qq . - - pytest --cov=corenetworks --no-cov-on-fail + - pytest corenetworks --cov=corenetworks --no-cov-on-fail environment: PY_COLORS: 1 depends_on: @@ -47,7 +47,7 @@ steps: commands: - pip install -r test-requirements.txt -qq - pip install -qq . - - pytest --cov=corenetworks --no-cov-on-fail + - pytest corenetworks --cov=corenetworks --no-cov-on-fail environment: PY_COLORS: 1 depends_on: @@ -58,7 +58,7 @@ steps: commands: - pip install -r test-requirements.txt -qq - pip install -qq . - - pytest --cov=corenetworks --no-cov-on-fail + - pytest corenetworks --cov=corenetworks --no-cov-on-fail environment: PY_COLORS: 1 depends_on: @@ -69,7 +69,7 @@ steps: commands: - pip install -r test-requirements.txt -qq - pip install -qq . - - pytest --cov=corenetworks --no-cov-on-fail + - pytest corenetworks --cov=corenetworks --no-cov-on-fail environment: PY_COLORS: 1 depends_on: @@ -80,7 +80,7 @@ steps: commands: - pip install -r test-requirements.txt -qq - pip install -qq . - - pytest --cov=corenetworks --no-cov-on-fail + - pytest corenetworks --cov=corenetworks --no-cov-on-fail environment: PY_COLORS: 1 depends_on: @@ -125,7 +125,7 @@ steps: commands: - pip install -r test-requirements.txt -qq - pip install -qq . - - bandit -r ./corenetworks -x ./corenetworks/tests + - bandit -r ./corenetworks -x ./corenetworks/test environment: PY_COLORS: 1 @@ -306,6 +306,6 @@ depends_on: --- kind: signature -hmac: d81d3b26c9f5d2a75eb6805c47d74217383771d3787bcf8de50eeb0987e1f6ff +hmac: dd83d3ebb552aa3998d4868389b730635039fba3c34040c53c380bc6ff2587e4 ... diff --git a/corenetworks/client.py b/corenetworks/client.py index bbf59c7..5c25b26 100644 --- a/corenetworks/client.py +++ b/corenetworks/client.py @@ -23,7 +23,7 @@ from .exceptions import ValidationError class CoreNetworks(): """Create authenticated API client.""" - def __init__(self, user=None, password=None, api_token=None): + def __init__(self, user=None, password=None, api_token=None, auto_commit=False): self.__endpoint = "https://beta.api.core-networks.de" self.__user_agent = "Core Networks Python API {version}".format( version=corenetworks.__version__ @@ -117,7 +117,7 @@ class CoreNetworks(): Example: params={"type": ["NS", "SOA"]} will result in filter=?type[]=NS&type[]=SOA Returns: - list: List of entry dicts. + list: List of matching records. """ schema = copy.deepcopy(self._filter_schema) @@ -136,11 +136,12 @@ class CoreNetworks(): Create a record for the given domain. Args: - param1: The first parameter. - param2: The second parameter. + zone (str): Name of the target DNS zone. + params (dict): Dictionary of record parameters. + See https://beta.api.core-networks.de/doc/#functon_dnszones_records_add Returns: - True if successful, False otherwise. + list: List of added records. """ schema = copy.deepcopy(self._schema) @@ -160,6 +161,12 @@ class CoreNetworks(): Delete all DNS records of a zone that match the data. Args: + zone (str): Name of the target DNS zone. + params (dict): Dictionary of record parameters. + See https://beta.api.core-networks.de/doc/#functon_dnszones_records_add + + Returns: + list: List of removed records. """ schema = copy.deepcopy(self._schema) @@ -178,10 +185,8 @@ class CoreNetworks(): if params.get("force_all"): params = {} - print(params) - result = self.__rest_helper( - "/dnszones/{zone}/records/delete/".format(zone=zone), data=params, method="POST" + "/dnszones/{zone}/records/delete".format(zone=zone), data=params, method="POST" ) return self.__normalize(result) diff --git a/corenetworks/tests/__init__.py b/corenetworks/test/__init__.py similarity index 100% rename from corenetworks/tests/__init__.py rename to corenetworks/test/__init__.py diff --git a/corenetworks/tests/fixtures/__init__.py b/corenetworks/test/fixtures/__init__.py similarity index 100% rename from corenetworks/tests/fixtures/__init__.py rename to corenetworks/test/fixtures/__init__.py diff --git a/corenetworks/tests/fixtures/callback.py b/corenetworks/test/fixtures/callback.py similarity index 100% rename from corenetworks/tests/fixtures/callback.py rename to corenetworks/test/fixtures/callback.py diff --git a/corenetworks/tests/unit/__init__.py b/corenetworks/test/unit/__init__.py similarity index 100% rename from corenetworks/tests/unit/__init__.py rename to corenetworks/test/unit/__init__.py diff --git a/corenetworks/tests/unit/test_client.py b/corenetworks/test/unit/test_client.py similarity index 64% rename from corenetworks/tests/unit/test_client.py rename to corenetworks/test/unit/test_client.py index ec0cccd..aabb9be 100644 --- a/corenetworks/tests/unit/test_client.py +++ b/corenetworks/test/unit/test_client.py @@ -6,6 +6,7 @@ from six.moves.urllib.parse import unquote # noqa from corenetworks import CoreNetworks from corenetworks.exceptions import CorenetworksError +from corenetworks.exceptions import ValidationError from corenetworks.tests.fixtures.callback import records_get_callback from corenetworks.tests.fixtures.callback import records_post_callback @@ -81,3 +82,39 @@ def test_add_record(requests_mock, client): } ) assert resp == [{"type": "A", "ttl": 1800, "name": "test", "data": "127.0.0.1"}] + + +def test_delete_record(requests_mock, client): + requests_mock.post( + "https://beta.api.core-networks.de/dnszones/example.com/records/delete", + text=records_post_callback, + ) + + filtered = client.delete_record(zone="example.com", params={ + "type": "A", + }) + assert filtered == [] + + forced = client.delete_record(zone="example.com", params={ + "force_all": True, + }) + assert forced == [] + + +def test_delete_record_invalid(requests_mock, client): + requests_mock.post( + "https://beta.api.core-networks.de/dnszones/example.com/records/delete", + text=records_post_callback, + ) + + with pytest.raises(ValidationError) as wrong: + assert client.delete_record(zone="example.com", params={"wrong": "attr"}) + assert str(wrong.value).startswith("Dataset invalid:") + + with pytest.raises(ValidationError) as ntype: + assert client.delete_record(zone="example.com", params={"type": 1}) + assert str(ntype.value).startswith("Dataset invalid:") + + with pytest.raises(ValidationError) as empty: + assert client.delete_record(zone="example.com", params={}) + assert str(empty.value).startswith("Dataset invalid:") diff --git a/setup.cfg b/setup.cfg index ea77071..c3ed6af 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,4 +27,4 @@ filterwarnings = ignore:.*pep8.*:FutureWarning [coverage:run] -omit = **/tests/* +omit = **/test/* diff --git a/setup.py b/setup.py index 97fdc02..8c04efb 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ setup( license=get_property("__license__", PACKAGE_NAME), long_description=get_readme(), long_description_content_type="text/markdown", - packages=find_packages(exclude=["*.tests", "tests", "tests.*"]), + packages=find_packages(exclude=["*.test", "test", "test.*"]), include_package_data=True, zip_safe=False, python_requires=">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,<4", @@ -61,5 +61,5 @@ setup( ], install_requires=["six", "jsonschema"], dependency_links=[], - test_suite="tests", + test_suite="test", ) diff --git a/test-requirements.txt b/test-requirements.txt index 06de617..c75d784 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -9,10 +9,10 @@ flake8-logging-format flake8-polyfill flake8-quotes pep8-naming +wheel pytest pytest-mock pytest-cov bandit requests-mock -autopep8 yapf