Skip to content

Commit

Permalink
Ruff: Add and fix PTH120 (#11201)
Browse files Browse the repository at this point in the history
* Ruff: Add and fix PTH120

* fix dedupe_test

* fix dedupe_test

* fix

* sha sum

* ruff

* retrigger unittest

* sha sum
  • Loading branch information
manuel-sommer authored Nov 12, 2024
1 parent 3609b3b commit 9b71a37
Show file tree
Hide file tree
Showing 35 changed files with 167 additions and 137 deletions.
3 changes: 2 additions & 1 deletion dojo/management/commands/import_surveys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from pathlib import Path

from django.core.management.base import BaseCommand
from django.db import connection
Expand Down Expand Up @@ -27,7 +28,7 @@ def handle(self, *args, **options):
row = cursor.fetchone()
ctype_id = row[0]
# Find the current id in the surveys file
path = os.path.dirname(os.path.abspath(__file__))
path = Path(os.path.abspath(__file__)).parent
path = path[:-19] + "fixtures/initial_surveys.json"
contents = open(path, encoding="utf-8").readlines()
for line in contents:
Expand Down
2 changes: 1 addition & 1 deletion dojo/settings/.settings.dist.py.sha256sum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25cf15456493c8592141326ab8a955207db2d2b33619b8f5d7b7e8767b8ba3aa
989918ec4f7b8fdb5f44d5c3568c948072265f269de96346002baaeebc67301b
4 changes: 2 additions & 2 deletions dojo/settings/settings.dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(DOJO_ROOT), "components", "node_modules"),
os.path.join(Path(DOJO_ROOT).parent, "components", "node_modules"),
)

# List of finder classes that know how to find static files in
Expand Down Expand Up @@ -941,7 +941,7 @@ def saml2_attrib_map_format(dict):
SAML_ATTRIBUTE_MAPPING = saml2_attrib_map_format(env("DD_SAML2_ATTRIBUTES_MAP"))
SAML_FORCE_AUTH = env("DD_SAML2_FORCE_AUTH")
SAML_ALLOW_UNKNOWN_ATTRIBUTES = env("DD_SAML2_ALLOW_UNKNOWN_ATTRIBUTE")
BASEDIR = path.dirname(path.abspath(__file__))
BASEDIR = Path(path.abspath(__file__)).parent
if len(env("DD_SAML2_ENTITY_ID")) == 0:
SAML2_ENTITY_ID = f"{SITE_URL}/saml2/metadata/"
else:
Expand Down
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ select = [
"TCH",
"INT",
"ARG003", "ARG004", "ARG005",
"PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH121", "PTH124",
"PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH124",
"TD001", "TD004", "TD005",
"PD",
"PGH",
Expand Down
2 changes: 1 addition & 1 deletion tests/Import_scanner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent

logger = logging.getLogger(__name__)

Expand Down
5 changes: 3 additions & 2 deletions tests/close_old_findings_dedupe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import time
import unittest
from pathlib import Path

from base_test_class import BaseTestCase, on_exception_html_source_logger, set_suite_settings
from product_test import ProductTest
Expand All @@ -13,7 +14,7 @@

logger = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class CloseOldDedupeTest(BaseTestCase):
Expand All @@ -24,7 +25,7 @@ class CloseOldDedupeTest(BaseTestCase):
# --------------------------------------------------------------------------------------------------------
def setUp(self):
super().setUp()
self.relative_path = os.path.dirname(os.path.realpath(__file__))
self.relative_path = Path(os.path.realpath(__file__)).parent

def check_nb_duplicates(self, expected_number_of_duplicates):
logger.debug("checking duplicates...")
Expand Down
5 changes: 3 additions & 2 deletions tests/close_old_findings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import unittest
from pathlib import Path

from base_test_class import BaseTestCase, on_exception_html_source_logger, set_suite_settings
from product_test import ProductTest
Expand All @@ -12,7 +13,7 @@

logger = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class CloseOldTest(BaseTestCase):
Expand All @@ -22,7 +23,7 @@ class CloseOldTest(BaseTestCase):
# --------------------------------------------------------------------------------------------------------
def setUp(self):
super().setUp()
self.relative_path = os.path.dirname(os.path.realpath(__file__))
self.relative_path = Path(os.path.realpath(__file__)).parent

@on_exception_html_source_logger
def test_delete_findings(self):
Expand Down
21 changes: 11 additions & 10 deletions tests/dedupe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import time
import unittest
from pathlib import Path

from base_test_class import BaseTestCase, on_exception_html_source_logger, set_suite_settings
from product_test import ProductTest
Expand All @@ -13,7 +14,7 @@

logger = logging.getLogger(__name__)

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class DedupeTest(BaseTestCase):
Expand All @@ -22,7 +23,7 @@ class DedupeTest(BaseTestCase):
# --------------------------------------------------------------------------------------------------------
def setUp(self):
super().setUp()
self.relative_path = os.path.dirname(os.path.realpath(__file__))
self.relative_path = str(Path(os.path.realpath(__file__)).parent)

def check_nb_duplicates(self, expected_number_of_duplicates):
logger.debug("checking duplicates...")
Expand Down Expand Up @@ -140,7 +141,7 @@ def test_import_path_tests(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Path Test 1").click()
driver.find_element(By.ID, "dropdownMenu1").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_path_1.json")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_path_1.json"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

# 'Bandit Scan processed a total of 1 findings created 1 findings did not touch 1 findings.'
Expand All @@ -154,7 +155,7 @@ def test_import_path_tests(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Path Test 2").click()
driver.find_element(By.ID, "dropdownMenu1").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_path_2.json")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_path_2.json"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

# 'Bandit Scan processed a total of 2 findings created 2 findings did not touch 1 findings.'
Expand Down Expand Up @@ -212,7 +213,7 @@ def test_import_endpoint_tests(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Endpoint Test 1").click()
driver.find_element(By.ID, "dropdownMenu1").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_endpoint_1.xml")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_endpoint_1.xml"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

self.assertTrue(self.is_success_message_present(text="a total of 3 findings"))
Expand All @@ -223,7 +224,7 @@ def test_import_endpoint_tests(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Endpoint Test 2").click()
driver.find_element(By.ID, "dropdownMenu1").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_endpoint_2.xml")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_endpoint_2.xml"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

self.assertTrue(self.is_success_message_present(text="a total of 3 findings"))
Expand Down Expand Up @@ -276,7 +277,7 @@ def test_import_same_eng_tests(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Same Eng Test 1").click()
driver.find_element(By.ID, "dropdownMenu1").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_endpoint_1.xml")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_endpoint_1.xml"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

self.assertTrue(self.is_success_message_present(text="a total of 3 findings"))
Expand All @@ -287,7 +288,7 @@ def test_import_same_eng_tests(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Same Eng Test 2").click()
driver.find_element(By.ID, "dropdownMenu1").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_cross_1.csv")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_cross_1.csv"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

self.assertTrue(self.is_success_message_present(text="a total of 3 findings"))
Expand Down Expand Up @@ -419,7 +420,7 @@ def test_import_cross_test(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Immuniweb Test").click()
driver.find_element(By.CSS_SELECTOR, "i.fa-solid.fa-ellipsis-vertical").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan Results").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_endpoint_1.xml")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_endpoint_1.xml"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

self.assertTrue(self.is_success_message_present(text="a total of 3 findings"))
Expand All @@ -430,7 +431,7 @@ def test_import_cross_test(self):
driver.find_element(By.PARTIAL_LINK_TEXT, "Generic Test").click()
driver.find_element(By.CSS_SELECTOR, "i.fa-solid.fa-ellipsis-vertical").click()
driver.find_element(By.LINK_TEXT, "Re-Upload Scan Results").click()
driver.find_element(By.ID, "id_file").send_keys(self.relative_path + "/dedupe_scans/dedupe_cross_1.csv")
driver.find_element(By.ID, "id_file").send_keys(os.path.realpath(self.relative_path + "/dedupe_scans/dedupe_cross_1.csv"))
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()

self.assertTrue(self.is_success_message_present(text="a total of 3 findings"))
Expand Down
3 changes: 2 additions & 1 deletion tests/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import sys
import time
import unittest
from pathlib import Path

from base_test_class import BaseTestCase
from product_test import ProductTest, WaitForPageLoad
from selenium.webdriver.common.by import By

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class FileUploadTest(BaseTestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/finding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from user_test import UserTest

logger = logging.getLogger(__name__)
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class FindingTest(BaseTestCase):
Expand Down
3 changes: 2 additions & 1 deletion tests/ibm_appscan_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import sys
import unittest
from pathlib import Path

from base_test_class import BaseTestCase
from product_test import ProductTest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class IBMAppScanTest(BaseTestCase):
Expand Down
3 changes: 2 additions & 1 deletion tests/report_builder_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import unittest
from pathlib import Path

from base_test_class import BaseTestCase
from product_test import ProductTest
Expand All @@ -9,7 +10,7 @@
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait

dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = Path(os.path.realpath(__file__)).parent


class ReportBuilderTest(BaseTestCase):
Expand Down
3 changes: 2 additions & 1 deletion unittests/dojo_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from functools import wraps
from itertools import chain
from pathlib import Path
from pprint import pformat

from django.test import TestCase
Expand Down Expand Up @@ -39,7 +40,7 @@


def get_unit_tests_path():
return os.path.dirname(os.path.realpath(__file__))
return str(Path(os.path.realpath(__file__)).parent)


def toggle_system_setting_boolean(flag_name, value):
Expand Down
9 changes: 5 additions & 4 deletions unittests/tools/test_anchore_enterprise_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path
from pathlib import Path

from dojo.models import Test
from dojo.tools.anchore_enterprise.parser import AnchoreEnterpriseParser, extract_vulnerability_id, search_filepath
Expand All @@ -7,19 +8,19 @@

class TestAnchoreEnterpriseParser(DojoTestCase):
def test_anchore_policy_check_parser_has_no_findings(self):
with open(path.join(path.dirname(__file__), "../scans/anchore_enterprise/no_checks.json"), encoding="utf-8") as testfile:
with open(path.join(Path(__file__).parent, "../scans/anchore_enterprise/no_checks.json"), encoding="utf-8") as testfile:
parser = AnchoreEnterpriseParser()
findings = parser.get_findings(testfile, Test())
self.assertEqual(0, len(findings))

def test_anchore_policy_check_parser_has_one_finding(self):
with open(path.join(path.dirname(__file__), "../scans/anchore_enterprise/one_check.json"), encoding="utf-8") as testfile:
with open(path.join(Path(__file__).parent, "../scans/anchore_enterprise/one_check.json"), encoding="utf-8") as testfile:
parser = AnchoreEnterpriseParser()
findings = parser.get_findings(testfile, Test())
self.assertEqual(1, len(findings))

def test_anchore_policy_check_parser_has_multiple_findings(self):
with open(path.join(path.dirname(__file__), "../scans/anchore_enterprise/many_checks.json"), encoding="utf-8") as testfile:
with open(path.join(Path(__file__).parent, "../scans/anchore_enterprise/many_checks.json"), encoding="utf-8") as testfile:
parser = AnchoreEnterpriseParser()
findings = parser.get_findings(testfile, Test())
self.assertEqual(57, len(findings))
Expand All @@ -28,7 +29,7 @@ def test_anchore_policy_check_parser_has_multiple_findings(self):
self.assertEqual("CVE-2015-2992", finding.unsaved_vulnerability_ids[0])

def test_anchore_policy_check_parser_invalid_format(self):
with open(path.join(path.dirname(__file__), "../scans/anchore_enterprise/invalid_checks_format.json"), encoding="utf-8") as testfile:
with open(path.join(Path(__file__).parent, "../scans/anchore_enterprise/invalid_checks_format.json"), encoding="utf-8") as testfile:
with self.assertRaises(Exception):
parser = AnchoreEnterpriseParser()
parser.get_findings(testfile, Test())
Expand Down
3 changes: 2 additions & 1 deletion unittests/tools/test_appspider_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path
from pathlib import Path

from dojo.models import Engagement, Finding, Product, Test
from dojo.tools.appspider.parser import AppSpiderParser
Expand All @@ -10,7 +11,7 @@ def test_appspider_parser_has_one_finding(self):
test = Test()
test.engagement = Engagement()
test.engagement.product = Product()
testfile = open(path.join(path.dirname(__file__), "../scans/appspider/one_vuln.xml"), encoding="utf-8")
testfile = open(path.join(Path(__file__).parent, "../scans/appspider/one_vuln.xml"), encoding="utf-8")
parser = AppSpiderParser()
findings = parser.get_findings(testfile, test)
for finding in findings:
Expand Down
5 changes: 3 additions & 2 deletions unittests/tools/test_bundler_audit_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path
from pathlib import Path

from dojo.models import Test
from dojo.tools.bundler_audit.parser import BundlerAuditParser
Expand All @@ -7,7 +8,7 @@

class TestBundlerAuditParser(DojoTestCase):
def test_get_findings(self):
with open(path.join(path.dirname(__file__), "../scans/bundler_audit/bundler-audit_v0.6.1.txt"), encoding="utf-8") as testfile:
with open(path.join(Path(__file__).parent, "../scans/bundler_audit/bundler-audit_v0.6.1.txt"), encoding="utf-8") as testfile:
parser = BundlerAuditParser()
findings = parser.get_findings(testfile, Test())
self.assertEqual(2, len(findings))
Expand All @@ -29,7 +30,7 @@ def test_get_findings(self):
self.assertEqual("2.2.3", finding.component_version)

def test_get_findings_version9(self):
with open(path.join(path.dirname(__file__), "../scans/bundler_audit/version_9.0.txt"), encoding="utf-8") as testfile:
with open(path.join(Path(__file__).parent, "../scans/bundler_audit/version_9.0.txt"), encoding="utf-8") as testfile:
parser = BundlerAuditParser()
findings = parser.get_findings(testfile, Test())
self.assertEqual(4, len(findings))
Expand Down
3 changes: 2 additions & 1 deletion unittests/tools/test_burp_dastardly_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path
from pathlib import Path

from dojo.models import Test
from dojo.tools.burp_dastardly.parser import BurpDastardlyParser
Expand All @@ -8,7 +9,7 @@
class TestBurpParser(DojoTestCase):

def test_burp_dastardly_multiple_findings(self):
with open(path.join(path.dirname(__file__), "../scans/burp_dastardly/many_findings.xml"), encoding="utf-8") as test_file:
with open(path.join(Path(__file__).parent, "../scans/burp_dastardly/many_findings.xml"), encoding="utf-8") as test_file:
parser = BurpDastardlyParser()
findings = parser.get_findings(test_file, Test())
for finding in findings:
Expand Down
5 changes: 3 additions & 2 deletions unittests/tools/test_burp_enterprise_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import path
from pathlib import Path

from dojo.models import Test
from dojo.tools.burp_enterprise.parser import BurpEnterpriseParser
Expand All @@ -8,7 +9,7 @@
class TestBurpEnterpriseParser(DojoTestCase):

def test_burp_enterprise_with_multiple_vulns(self):
with open(path.join(path.dirname(__file__), "../scans/burp_enterprise/many_vulns.html"), encoding="utf-8") as test_file:
with open(path.join(Path(__file__).parent, "../scans/burp_enterprise/many_vulns.html"), encoding="utf-8") as test_file:
parser = BurpEnterpriseParser()
findings = parser.get_findings(test_file, Test())
for finding in findings:
Expand Down Expand Up @@ -37,7 +38,7 @@ def test_burp_enterprise_with_multiple_vulns(self):
self.assertIn("**Issue detail**:\nFingerprint Details:\n\nWAF Type : redacted\nWAF tech. details : Cloud-based CDN, WAF & DDoS prevention", finding.description)

def test_burp_enterprise_with_multiple_vulns_newer_format(self):
with open(path.join(path.dirname(__file__), "../scans/burp_enterprise/many_vulns_updated_format.html"), encoding="utf-8") as test_file:
with open(path.join(Path(__file__).parent, "../scans/burp_enterprise/many_vulns_updated_format.html"), encoding="utf-8") as test_file:
parser = BurpEnterpriseParser()
findings = parser.get_findings(test_file, Test())
for finding in findings:
Expand Down
Loading

0 comments on commit 9b71a37

Please sign in to comment.