From 8789594ad9e91d8a1ee2b6c413012447f3dc9420 Mon Sep 17 00:00:00 2001 From: Ferdinando Formica Date: Tue, 30 Jan 2024 11:59:31 +0000 Subject: [PATCH] A couple of fixes --- ns1/redirect.py | 36 +++++++++++++------ ns1/rest/redirect.py | 4 ++- tests/unit/test_redirect.py | 70 ++++++++++++++++++++++++++++++------- 3 files changed, 87 insertions(+), 23 deletions(-) diff --git a/ns1/redirect.py b/ns1/redirect.py index 8ec2b9e..1a5f906 100644 --- a/ns1/redirect.py +++ b/ns1/redirect.py @@ -22,7 +22,11 @@ def __init__(self, config): self.data = None def __repr__(self): - return "" % (self.__getitem__("domain"), self.__getitem__("path"), self.__getitem__("target")) + return "" % ( + self.__getitem__("domain"), + self.__getitem__("path"), + self.__getitem__("target"), + ) def __getitem__(self, item): if not self.data: @@ -42,9 +46,9 @@ def load(self, id=None, callback=None, errback=None, reload=False): """ if not reload and self.data: raise RedirectException("redirect already loaded") - if id == None and self.data: + if id is None and self.data: id = self.__getitem__("id") - if id == None: + if id is None: raise RedirectException("no redirect id: did you mean to create?") def success(result, *args): @@ -61,7 +65,9 @@ def loadFromDict(self, cfg): Load redirect data from a dictionary. :param dict cfg: dictionary containing *at least* either an id or domain/path/target """ - if "id" in cfg or ("domain" in cfg and "path" in cfg and "target" in cfg): + if "id" in cfg or ( + "domain" in cfg and "path" in cfg and "target" in cfg + ): self.data = cfg return self else: @@ -95,7 +101,9 @@ def success(result, *args): self.data, callback=success, errback=errback, **kwargs ) - def create(self, domain, path, target, callback=None, errback=None, **kwargs): + def create( + self, domain, path, target, callback=None, errback=None, **kwargs + ): """ Create a new redirect. Pass a list of keywords and their values to configure. For the list of keywords available for zone configuration, @@ -115,14 +123,18 @@ def success(result, *args): else: return self - return self._rest.create(domain, path, target, callback=success, errback=errback, **kwargs) + return self._rest.create( + domain, path, target, callback=success, errback=errback, **kwargs + ) def retrieveCertificate(self, callback=None, errback=None): """ Retrieve the certificate associated to this redirect. :return: the RedirectCertificate object """ - return RedirectCertificate(self.config).load(self.__getitem__("certificate_id")) + return RedirectCertificate(self.config).load( + self.__getitem__("certificate_id") + ) def listRedirects(config, callback=None, errback=None): @@ -130,6 +142,7 @@ def listRedirects(config, callback=None, errback=None): Lists all redirects currently configured. :return: a list of Redirect objects """ + def success(result, *args): ret = [] cfgs = result.get("results", None) @@ -179,10 +192,12 @@ def load(self, id=None, callback=None, errback=None, reload=False): """ if not reload and self.data: raise RedirectException("redirect certificate already loaded") - if id == None and self.data: + if id is None and self.data: id = self.__getitem__("id") - if id == None: - raise RedirectException("no redirect certificate id: did you mean to create?") + if id is None: + raise RedirectException( + "no redirect certificate id: did you mean to create?" + ) def success(result, *args): self.data = result @@ -241,6 +256,7 @@ def listRedirectCertificates(config, callback=None, errback=None): Lists all redirects certificates currently configured. :return: a list of RedirectCertificate objects """ + def success(result, *args): ret = [] cfgs = result.get("results", None) diff --git a/ns1/rest/redirect.py b/ns1/rest/redirect.py index 3aa70bb..587c44b 100644 --- a/ns1/rest/redirect.py +++ b/ns1/rest/redirect.py @@ -36,7 +36,9 @@ def import_file(self, cfg, cfgFile, callback=None, errback=None, **kwargs): errback=errback, ) - def create(self, domain, path, target, callback=None, errback=None, **kwargs): + def create( + self, domain, path, target, callback=None, errback=None, **kwargs + ): body = self._buildBody(domain, path, target, **kwargs) return self._make_request( "PUT", diff --git a/tests/unit/test_redirect.py b/tests/unit/test_redirect.py index 6b734c5..7c1f8af 100644 --- a/tests/unit/test_redirect.py +++ b/tests/unit/test_redirect.py @@ -1,5 +1,4 @@ from ns1 import NS1 -from ns1.redirect import Redirect, RedirectCertificate, listRedirects import ns1.rest.redirect import pytest @@ -41,7 +40,15 @@ def test_rest_redirect_list(redirect_config): ) -@pytest.mark.parametrize("cfgId, url", [("96529d62-fb0c-4150-b5ad-6e5b8b2736f6", "redirect/96529d62-fb0c-4150-b5ad-6e5b8b2736f6")]) +@pytest.mark.parametrize( + "cfgId, url", + [ + ( + "96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + "redirect/96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + ) + ], +) def test_rest_redirect_retrieve(redirect_config, cfgId, url): z = NS1(config=redirect_config).redirects() z._make_request = mock.MagicMock() @@ -61,7 +68,7 @@ def test_rest_redirect_create(redirect_config): z._make_request.assert_called_once_with( "PUT", "redirect", - body = { + body={ "domain": "www.test.com", "path": "/", "target": "http://localhost", @@ -71,7 +78,15 @@ def test_rest_redirect_create(redirect_config): ) -@pytest.mark.parametrize("cfgId, url", [("96529d62-fb0c-4150-b5ad-6e5b8b2736f6", "redirect/96529d62-fb0c-4150-b5ad-6e5b8b2736f6")]) +@pytest.mark.parametrize( + "cfgId, url", + [ + ( + "96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + "redirect/96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + ) + ], +) def test_rest_redirect_update(redirect_config, cfgId, url): z = NS1(config=redirect_config).redirects() z._make_request = mock.MagicMock() @@ -85,7 +100,7 @@ def test_rest_redirect_update(redirect_config, cfgId, url): z._make_request.assert_called_once_with( "POST", url, - body = { + body={ "id": cfgId, "domain": "www.test.com", "path": "/", @@ -96,7 +111,15 @@ def test_rest_redirect_update(redirect_config, cfgId, url): ) -@pytest.mark.parametrize("cfgId, url", [("96529d62-fb0c-4150-b5ad-6e5b8b2736f6", "redirect/96529d62-fb0c-4150-b5ad-6e5b8b2736f6")]) +@pytest.mark.parametrize( + "cfgId, url", + [ + ( + "96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + "redirect/96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + ) + ], +) def test_rest_redirect_delete(redirect_config, cfgId, url): z = NS1(config=redirect_config).redirects() z._make_request = mock.MagicMock() @@ -137,7 +160,15 @@ def test_rest_certificate_list(redirect_config): ) -@pytest.mark.parametrize("cfgId, url", [("96529d62-fb0c-4150-b5ad-6e5b8b2736f6", "redirect/certificates/96529d62-fb0c-4150-b5ad-6e5b8b2736f6")]) +@pytest.mark.parametrize( + "cfgId, url", + [ + ( + "96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + "redirect/certificates/96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + ) + ], +) def test_rest_certificate_retrieve(redirect_config, cfgId, url): z = NS1(config=redirect_config).redirect_certificates() z._make_request = mock.MagicMock() @@ -157,7 +188,7 @@ def test_rest_certificate_create(redirect_config): z._make_request.assert_called_once_with( "PUT", "redirect/certificates", - body = { + body={ "domain": "www.test.com", }, callback=None, @@ -165,7 +196,15 @@ def test_rest_certificate_create(redirect_config): ) -@pytest.mark.parametrize("certId, url", [("96529d62-fb0c-4150-b5ad-6e5b8b2736f6", "redirect/certificates/96529d62-fb0c-4150-b5ad-6e5b8b2736f6")]) +@pytest.mark.parametrize( + "certId, url", + [ + ( + "96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + "redirect/certificates/96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + ) + ], +) def test_rest_certificate_update(redirect_config, certId, url): z = NS1(config=redirect_config).redirect_certificates() z._make_request = mock.MagicMock() @@ -178,7 +217,15 @@ def test_rest_certificate_update(redirect_config, certId, url): ) -@pytest.mark.parametrize("certId, url", [("96529d62-fb0c-4150-b5ad-6e5b8b2736f6", "redirect/certificates/96529d62-fb0c-4150-b5ad-6e5b8b2736f6")]) +@pytest.mark.parametrize( + "certId, url", + [ + ( + "96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + "redirect/certificates/96529d62-fb0c-4150-b5ad-6e5b8b2736f6", + ) + ], +) def test_rest_certificate_delete(redirect_config, certId, url): z = NS1(config=redirect_config).redirect_certificates() z._make_request = mock.MagicMock() @@ -192,7 +239,7 @@ def test_rest_certificate_delete(redirect_config, certId, url): def test_rest_certificate_buildbody(redirect_config): - z = ns1.rest.redirect.Redirects(redirect_config) + z = ns1.rest.redirect.RedirectCertificates(redirect_config) kwargs = { "domain": "www.test.com", } @@ -200,4 +247,3 @@ def test_rest_certificate_buildbody(redirect_config): "domain": "www.test.com", } assert z._buildBody(**kwargs) == body -