Skip to content

Commit

Permalink
feat(unittests): Try to avoid assertTrue/False (#10817)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiblik authored Sep 6, 2024
1 parent 7c52e50 commit 6e937ef
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 67 deletions.
6 changes: 3 additions & 3 deletions tests/base_test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def set_block_execution(self, block_execution=True):
# save settings
driver.find_element(By.CSS_SELECTOR, "input.btn.btn-primary").click()
# check if it's enabled after reload
self.assertTrue(
driver.find_element(By.ID, "id_block_execution").is_selected()
== block_execution,
self.assertEqual(
driver.find_element(By.ID, "id_block_execution").is_selected(),
block_execution,
)
return driver

Expand Down
4 changes: 2 additions & 2 deletions tests/close_old_findings_dedupe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ def test_delete_findings(self):
text = driver.find_element(By.ID, "no_findings").text

self.assertIsNotNone(text)
self.assertTrue("No findings found." in text)
self.assertIn("No findings found.", text)
# check that user was redirect back to url where it came from based on return_url
self.assertTrue(driver.current_url.endswith("page=1"))
self.assertTrue(driver.current_url.endswith("page=1"), driver.current_url)

# --------------------------------------------------------------------------------------------------------
# Same scanner deduplication - Deduplication on engagement
Expand Down
4 changes: 2 additions & 2 deletions tests/close_old_findings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def test_delete_findings(self):
text = driver.find_element(By.ID, "no_findings").text

self.assertIsNotNone(text)
self.assertTrue("No findings found." in text)
self.assertIn("No findings found.", text)
# check that user was redirect back to url where it came from based on return_url
self.assertTrue(driver.current_url.endswith("page=1"))
self.assertTrue(driver.current_url.endswith("page=1"), driver.current_url)

# --------------------------------------------------------------------------------------------------------
# Same scanner import - Close Old Findings on engagement
Expand Down
4 changes: 2 additions & 2 deletions tests/dedupe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def test_delete_findings(self):
text = driver.find_element(By.ID, "no_findings").text

self.assertIsNotNone(text)
self.assertTrue("No findings found." in text)
self.assertIn("No findings found.", text)
# check that user was redirect back to url where it came from based on return_url
self.assertTrue(driver.current_url.endswith("page=1"))
self.assertTrue(driver.current_url.endswith("page=1"), driver.current_url)

# --------------------------------------------------------------------------------------------------------
# Same scanner deduplication - Deduplication on engagement
Expand Down
8 changes: 4 additions & 4 deletions tests/finding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_close_finding(self):
# This will throw exception if the test fails due to invalid xpath
post_status = driver.find_element(By.XPATH, '//*[@id="remd_endpoints"]/tbody/tr/td[3]').text
# Assert ot the query to dtermine status of failure
self.assertTrue(pre_status != post_status)
self.assertNotEqual(pre_status, post_status)

def test_open_finding(self):
driver = self.driver
Expand All @@ -303,7 +303,7 @@ def test_open_finding(self):
# This will throw exception if the test fails due to invalid xpath
post_status = driver.find_element(By.XPATH, '//*[@id="vuln_endpoints"]/tbody/tr/td[3]').text
# Assert ot the query to dtermine status of failure
self.assertTrue(pre_status != post_status)
self.assertNotEqual(pre_status, post_status)

@on_exception_html_source_logger
def test_simple_accept_finding(self):
Expand All @@ -328,7 +328,7 @@ def test_simple_accept_finding(self):
# This will throw exception if the test fails due to invalid xpath
# TODO: risk acceptance doesn't mitigate endpoints currently
# post_status = driver.find_element(By.XPATH, '//*[@id="remd_endpoints"]/tbody/tr/td[3]').text
# self.assertTrue(pre_status != post_status)
# self.assertNotEqual(pre_status, post_status)

def test_unaccept_finding(self):
driver = self.driver
Expand All @@ -352,7 +352,7 @@ def test_unaccept_finding(self):
# This will throw exception if the test fails due to invalid xpath
# TODO: risk acceptance doesn't mitigate endpoints currently
# post_status = driver.find_element(By.XPATH, '//*[@id="remd_endpoints"]/tbody/tr/td[3]').text
# self.assertTrue(pre_status != post_status)
# self.assertNotEqual(pre_status, post_status)

def test_make_finding_a_template(self):
driver = self.driver
Expand Down
5 changes: 1 addition & 4 deletions tests/notes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def create_public_note(self, driver, level):
if not driver.find_element(By.ID, "add_note").is_displayed():
self.uncollapse_all(driver)
text = driver.find_element(By.TAG_NAME, "body").text
pass_test = "Test public note" in text
if not pass_test:
logger.info(f"Public note created at the {level} level")
self.assertTrue(pass_test)
self.assertIn("Test public note", text, f"Public note created at the {level} level")

def create_private_note(self, driver, level):
time.sleep(1)
Expand Down
2 changes: 1 addition & 1 deletion tests/product_type_member_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def test_product_type_delete_product_type_member(self):
# Assert the message to determine success status
self.assertTrue(self.is_success_message_present(text="Product type member deleted successfully."))
# Query the site to determine if the member has been deleted
self.assertTrue(len(driver.find_elements(By.NAME, "member_user")) == 1)
self.assertEqual(len(driver.find_elements(By.NAME, "member_user")), 1)
else:
logger.info("--------------------------------")
logger.info("test_product_delete_product_member: Not executed because legacy authorization is active")
Expand Down
2 changes: 1 addition & 1 deletion tests/report_builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def generate_HTML_report(self):
Select(driver.find_element(By.ID, "id_report_type")).select_by_visible_text("HTML")
driver.find_element(By.ID, "id_report_name").send_keys("Test Report")
driver.find_element(By.CLASS_NAME, "run_report").click()
self.assertTrue(driver.current_url == self.base_url + "reports/custom")
self.assertEqual(driver.current_url, self.base_url + "reports/custom")

def test_product_type_report(self):
driver = self.driver
Expand Down
12 changes: 6 additions & 6 deletions unittests/test_deduplication_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ def test_hash_code_onetime(self):
self.assertEqual(finding_new.hash_code, None)

finding_new.save()
self.assertTrue(finding_new.hash_code) # True -> not None
self.assertIsNotNone(finding_new.hash_code)
hash_code_at_creation = finding_new.hash_code

finding_new.title = "new_title"
Expand Down Expand Up @@ -1111,17 +1111,17 @@ def test_hash_code_without_dedupe(self):
finding_new.save(dedupe_option=False)

# save skips hash_code generation if dedupe_option==False
self.assertFalse(finding_new.hash_code)
self.assertIsNone(finding_new.hash_code)

finding_new.save(dedupe_option=True)

self.assertTrue(finding_new.hash_code)
self.assertIsNotNone(finding_new.hash_code)

finding_new, _finding_124 = self.copy_and_reset_finding(id=124)
finding_new.save()

# by default hash_code should be generated
self.assertTrue(finding_new.hash_code)
self.assertIsNotNone(finding_new.hash_code)

# # utility methods

Expand Down Expand Up @@ -1248,11 +1248,11 @@ def assert_finding(self, finding, not_pk=None, duplicate=False, duplicate_findin

self.assertEqual(finding.duplicate, duplicate)
if not duplicate:
self.assertFalse(finding.duplicate_finding) # False -> None
self.assertIsNone(finding.duplicate_finding)

if duplicate_finding_id:
logger.debug("asserting that finding %i is a duplicate of %i", finding.id if finding.id is not None else "None", duplicate_finding_id if duplicate_finding_id is not None else "None")
self.assertTrue(finding.duplicate_finding) # True -> not None
self.assertIsNotNone(finding.duplicate_finding)
self.assertEqual(finding.duplicate_finding.id, duplicate_finding_id)

if not_hash_code:
Expand Down
2 changes: 1 addition & 1 deletion unittests/test_jira_config_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_add_jira_instance_unknown_host(self):
self.assertEqual(200, response.status_code)
content = response.content.decode("utf-8")
# debian throws 'Name or service not known' error and alpine 'Name does not resolve'
self.assertTrue(("Name or service not known" in content) or ("Name does not resolve" in content))
self.assertTrue(("Name or service not known" in content) or ("Name does not resolve" in content), content)

# test raw connection error
with self.assertRaises(requests.exceptions.RequestException):
Expand Down
9 changes: 4 additions & 5 deletions unittests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
from pathlib import Path

from .dojo_test_case import DojoTestCase, get_unit_tests_path
Expand Down Expand Up @@ -33,17 +32,17 @@ def test_file_existence(self):
)

content = Path(doc_file).read_text(encoding="utf-8")
self.assertTrue(re.search("title:", content),
self.assertRegex(content, "title:",
f"Documentation file '{doc_file}' does not contain a title",
)
self.assertTrue(re.search("toc_hide: true", content),
self.assertRegex(content, "toc_hide: true",
f"Documentation file '{doc_file}' does not contain toc_hide: true",
)
if category == "file":
self.assertTrue(re.search("### Sample Scan Data", content),
self.assertRegex(content, "### Sample Scan Data",
f"Documentation file '{doc_file}' does not contain ### Sample Scan Data",
)
self.assertTrue(re.search("https://github.com/DefectDojo/django-DefectDojo/tree/master/unittests/scans", content),
self.assertRegex(content, "https://github.com/DefectDojo/django-DefectDojo/tree/master/unittests/scans",
f"Documentation file '{doc_file}' does not contain https://github.com/DefectDojo/django-DefectDojo/tree/master/unittests/scans",
)

Expand Down
Loading

0 comments on commit 6e937ef

Please sign in to comment.