Skip to content

Commit

Permalink
A couple of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fformica committed Jan 30, 2024
1 parent 2e045da commit 8789594
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 23 deletions.
36 changes: 26 additions & 10 deletions ns1/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ def __init__(self, config):
self.data = None

def __repr__(self):
return "<Redirect [%s:%s]=%s>" % (self.__getitem__("domain"), self.__getitem__("path"), self.__getitem__("target"))
return "<Redirect [%s:%s]=%s>" % (
self.__getitem__("domain"),
self.__getitem__("path"),
self.__getitem__("target"),
)

def __getitem__(self, item):
if not self.data:
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -115,21 +123,26 @@ 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):
"""
Lists all redirects currently configured.
:return: a list of Redirect objects
"""

def success(result, *args):
ret = []
cfgs = result.get("results", None)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion ns1/rest/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
70 changes: 58 additions & 12 deletions tests/unit/test_redirect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from ns1 import NS1
from ns1.redirect import Redirect, RedirectCertificate, listRedirects
import ns1.rest.redirect
import pytest

Expand Down Expand Up @@ -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()
Expand All @@ -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",
Expand All @@ -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()
Expand All @@ -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": "/",
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -157,15 +188,23 @@ 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,
errback=None,
)


@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()
Expand All @@ -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()
Expand All @@ -192,12 +239,11 @@ 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",
}
body = {
"domain": "www.test.com",
}
assert z._buildBody(**kwargs) == body

0 comments on commit 8789594

Please sign in to comment.