Skip to content

Commit

Permalink
chore: removed useless _assert tests and code
Browse files Browse the repository at this point in the history
  • Loading branch information
peppelinux committed Jul 28, 2021
1 parent 0e4b838 commit 6ca8047
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 188 deletions.
57 changes: 1 addition & 56 deletions src/spid_sp_test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self, *args, **kwargs):

def report_to_dict(self):
res = {self.category: {self.__class__.__name__: self.results}}

return res

def is_ok(self, msg):
Expand Down Expand Up @@ -59,7 +58,7 @@ def handle_result(
self.warnings.append(data)

def handle_error(self, error_message, description="", traceback: str = None):
getattr(self.logger, "error")(error_message)
self.logger.error(error_message)
self.error_counter += 1
# here report as json
value = f"{description}" if not traceback else f"{description}: {traceback }"
Expand Down Expand Up @@ -103,60 +102,6 @@ def _assertFalse(
):
self._assert(not check, error_message, description, traceback, level)

def _assertIsNotNone(
self,
check,
error_message,
description="",
traceback: str = None,
level: str = "info",
):
self._assert(check, error_message, description, traceback, level)

def _assertIn(
self,
first,
second,
error_message,
description="",
traceback: str = None,
level: str = "info",
):
self._assert((first in second), error_message, description, traceback, level)

def _assertGreaterEqual(
self,
first,
second,
error_message,
description="",
traceback: str = None,
level: str = "info",
):
self._assert((first >= second), error_message, description, traceback, level)

def _assertGreater(
self,
first,
second,
error_message,
description="",
traceback: str = None,
level: str = "info",
):
self._assert((first > second), error_message, description, traceback, level)

def _assertEqual(
self,
first,
second,
error_message,
description="",
traceback: str = None,
level: str = "info",
):
self._assert((first == second), error_message, description, traceback, level)

def _assertIsValidHttpsUrl(
self,
check,
Expand Down
112 changes: 48 additions & 64 deletions src/spid_sp_test/authn_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,23 +401,22 @@ def test_AuthnRequest(self):

value = req.get(attr)
if attr == "ID":
self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 8 ",
**error_kwargs,
)

if attr == "Version":
exp = "2.0"
self._assertEqual(
value,
exp,
self._assertTrue(
value == exp,
f"The {attr} attribute MUST be {exp} - TR pag. 8 ",
**error_kwargs,
)

if attr == "IssueInstant":
self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 8 ",
**error_kwargs,
Expand All @@ -429,7 +428,7 @@ def test_AuthnRequest(self):
)

if attr == "Destination":
self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 8 ",
**error_kwargs,
Expand Down Expand Up @@ -514,14 +513,13 @@ def test_AuthnRequest_SPID(self):
description=acss_desc,
)

self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value- TR pag. 8 ",
**error_kwargs,
)
self._assertGreaterEqual(
int(value),
0,
self._assertTrue(
int(value) >= 0,
f"The {attr} attribute MUST be >= 0 - TR pag. 8 and pag. 20",
**error_kwargs,
)
Expand All @@ -541,7 +539,7 @@ def test_AuthnRequest_SPID(self):
)

value = req.get(attr)
self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 8 ",
**error_kwargs,
Expand All @@ -566,9 +564,8 @@ def test_AuthnRequest_SPID(self):

if attr == "ProtocolBinding":
exp = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
self._assertEqual(
value,
exp,
self._assertTrue(
value == exp,
f"The {attr} attribute MUST be {exp} - TR pag. 8 ",
**error_kwargs,
)
Expand All @@ -585,14 +582,13 @@ def test_AuthnRequest_SPID(self):
availableattributeindexes.append(index)

value = req.get(attr)
self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 8",
**error_kwargs,
)
self._assertGreaterEqual(
int(value),
0,
self._assertTrue(
int(value) >= 0,
f"The {attr} attribute MUST be >= 0 - TR pag. 8 and pag. 20",
**error_kwargs,
)
Expand All @@ -613,19 +609,17 @@ def test_Subject(self):
error_kwargs = dict(description=desc) if desc else {}

if len(subj) > 1:
self._assertEqual(
len(subj),
1,
self._assertTrue(
len(subj) == 1,
"Only one Subject element can be present - TR pag. 9",
**error_kwargs,
)

if len(subj) == 1:
subj = subj[0]
name_id = subj.xpath("./NameID")
self._assertEqual(
len(name_id),
1,
self._assertTrue(
len(name_id) == 1,
"One NameID element in Subject element MUST be present - TR pag. 9",
**error_kwargs,
)
Expand All @@ -638,18 +632,16 @@ def test_Subject(self):
)

value = name_id.get(attr)

self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 9",
**error_kwargs,
)

if attr == "Format":
exp = "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"
self._assertEqual(
value,
exp,
self._assertTrue(
value == exp,
f"The {attr} attribute MUST be {exp} - TR pag. 9",
**error_kwargs,
)
Expand All @@ -673,15 +665,14 @@ def test_Issuer(self):
else:
e = e[0]

self._assertIsNotNone(
self._assertTrue(
e.text, "The Issuer element MUST have a value - TR pag. 9", **error_kwargs
)

entitydescriptor = self.md.xpath("//EntityDescriptor")
entityid = entitydescriptor[0].get("entityID")
self._assertEqual(
e.text,
entityid,
self._assertTrue(
e.text == entityid,
"The Issuer's value MUST be equal to entityID - TR pag. 9",
**error_kwargs,
)
Expand All @@ -695,17 +686,16 @@ def test_Issuer(self):

value = e.get(attr)

self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 9",
**error_kwargs,
)

if attr == "Format":
exp = "urn:oasis:names:tc:SAML:2.0:nameid-format:entity"
self._assertEqual(
value,
exp,
self._assertTrue(
value == exp,
f"The {attr} attribute MUST be {exp} - TR pag. 9",
**error_kwargs,
)
Expand Down Expand Up @@ -745,15 +735,16 @@ def test_NameIDPolicy(self):

value = e.get(attr)

self._assertIsNotNone(
value, f"The {attr} attribute MUST have a value - TR pag. 9", **error_kwargs
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 9",
**error_kwargs
)

if attr == "Format":
exp = "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"
self._assertEqual(
value,
exp,
self._assertTrue(
value == exp,
f"The {attr} attribute MUST be {exp} - TR pag. 9",
**error_kwargs,
)
Expand All @@ -767,9 +758,8 @@ def test_Conditions(self):
error_kwargs = dict(description=desc) if desc else {}

if len(e) > 1:
self._assertEqual(
len(1),
1,
self._assertTrue(
len(1) == 1,
"Only one Conditions element is allowed - TR pag. 9",
**error_kwargs,
)
Expand All @@ -785,7 +775,7 @@ def test_Conditions(self):

value = e.get(attr)

self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 9",
**error_kwargs,
Expand All @@ -806,15 +796,13 @@ def test_RequestedAuthnContext(self):
desc = [etree.tostring(ent).decode() for ent in e if e]
error_kwargs = dict(description=desc) if desc else {}

self._assertEqual(
len(e),
1,
self._assertTrue(
len(e) == 1,
"Only one RequestedAuthnContext element MUST be present - TR pag. 9",
**error_kwargs,
)
if e:
e = e[0]

attr = "Comparison"
self._assertTrue(
(attr in e.attrib),
Expand All @@ -823,31 +811,29 @@ def test_RequestedAuthnContext(self):
)

value = e.get(attr)
self._assertIsNotNone(
self._assertTrue(
value,
f"The {attr} attribute MUST have a value - TR pag. 10",
**error_kwargs,
)

allowed = ["exact", "minimum", "better", "maximum"]
self._assertIn(
value,
allowed,
self._assertTrue(
value in allowed,
"Attribute not valid - TR pag. 10",
description=f"The {attr} attribute MUST be one of [{', '.join(allowed)}]",
)

acr = e.xpath("./AuthnContextClassRef")
self._assertEqual(
len(acr),
1,
self._assertTrue(
len(acr) == 1,
"Only one AuthnContexClassRef element MUST be present - TR pag. 9",
description=[etree.tostring(_acr).decode() for _acr in acr],
)

if acr:
acr = acr[0]
self._assertIsNotNone(
self._assertTrue(
acr.text,
"The AuthnContexClassRef element MUST have a value - TR pag. 9",
description=etree.tostring(acr),
Expand Down Expand Up @@ -890,9 +876,8 @@ def test_Signature(self):
)

alg = method[0].get("Algorithm")
self._assertIn(
alg,
constants.ALLOWED_XMLDSIG_ALGS,
self._assertTrue(
alg in constants.ALLOWED_XMLDSIG_ALGS,
"The signature algorithm MUST be valid - TR pag. 10",
description=f"One of {', '.join(constants.ALLOWED_XMLDSIG_ALGS)}",
) # noqa
Expand All @@ -912,9 +897,8 @@ def test_Signature(self):
)

alg = method[0].get("Algorithm")
self._assertIn(
alg,
constants.ALLOWED_DGST_ALGS,
self._assertTrue(
alg in constants.ALLOWED_DGST_ALGS,
(
("The digest algorithm MUST be one of [%s] - TR pag. 10")
% (", ".join(constants.ALLOWED_DGST_ALGS))
Expand Down
Loading

0 comments on commit 6ca8047

Please sign in to comment.