Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff: Fix PTH118, merge PTH11 #11503

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,7 @@ def get_jira_issue_template_dir_choices():

for dirname in dirnames:
clean_base_dir = base_dir.removeprefix(settings.TEMPLATE_DIR_PREFIX)
template_dir_list.append((os.path.join(clean_base_dir, dirname), dirname))
template_dir_list.append((str(Path(clean_base_dir) / dirname), dirname))

logger.debug("templates: %s", template_dir_list)
return template_dir_list
Expand Down
5 changes: 2 additions & 3 deletions dojo/jira_link/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import io
import json
import logging
import os
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -333,8 +332,8 @@ def get_jira_issue_template(obj):
template_dir = "issue-trackers/jira_full/"

if isinstance(obj, Finding_Group):
return os.path.join(template_dir, "jira-finding-group-description.tpl")
return os.path.join(template_dir, "jira-description.tpl")
return Path(template_dir) / "jira-finding-group-description.tpl"
return Path(template_dir) / "jira-description.tpl"


def get_jira_creation(obj):
Expand Down
3 changes: 1 addition & 2 deletions dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import copy
import hashlib
import logging
import os
import re
import warnings
from datetime import datetime
Expand Down Expand Up @@ -149,7 +148,7 @@ def __call__(self, model_instance, filename):
filename += ext
if self.directory is None:
return filename
return os.path.join(now().strftime(self.directory), filename)
return Path(now().strftime(self.directory)) / filename


class Regulation(models.Model):
Expand Down
4 changes: 2 additions & 2 deletions dojo/settings/settings.dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,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(Path(DOJO_ROOT).parent, "components", "node_modules"),
Path(DOJO_ROOT).parent / "components" / "node_modules",
)

# List of finder classes that know how to find static files in
Expand Down Expand Up @@ -949,7 +949,7 @@ def saml2_attrib_map_format(dict):
"entityid": str(SAML2_ENTITY_ID),

# directory with attribute mapping
"attribute_map_dir": path.join(BASEDIR, "attribute-maps"),
"attribute_map_dir": Path(BASEDIR) / "attribute-maps",
# do now discard attributes not specified in attribute-maps
"allow_unknown_attributes": SAML_ALLOW_UNKNOWN_ATTRIBUTES,
# this block states what services we provide
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def requires_tool_type(scan_type):
package_dir = str(Path(__file__).resolve().parent)
for module_name in os.listdir(package_dir): # noqa: PTH208
# check if it's dir
if Path(os.path.join(package_dir, module_name)).is_dir():
if (Path(package_dir) / module_name).is_dir():
try:
# check if it's a Python module
if find_spec(f"dojo.tools.{module_name}.parser"):
Expand Down
5 changes: 2 additions & 3 deletions dojo/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os
from pathlib import Path

from auditlog.models import LogEntry
Expand Down Expand Up @@ -151,7 +150,7 @@ def manage_files(request, oid, obj_type):

for o in files_formset.deleted_objects:
logger.debug("removing file: %s", o.file.name)
Path(os.path.join(settings.MEDIA_ROOT, o.file.name)).unlink()
(Path(settings.MEDIA_ROOT) / o.file.name).unlink()

for o in files_formset.new_objects:
logger.debug("adding file: %s", o.file.name)
Expand All @@ -162,7 +161,7 @@ def manage_files(request, oid, obj_type):
finding__isnull=True)
for o in orphan_files:
logger.debug("purging orphan file: %s", o.file.name)
Path(os.path.join(settings.MEDIA_ROOT, o.file.name)).unlink()
(Path(settings.MEDIA_ROOT) / o.file.name).unlink()
o.delete()

messages.add_message(
Expand Down
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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", "PTH120", "PTH121", "PTH122", "PTH124",
"PTH2", "PTH101", "PTH102", "PTH103", "PTH104", "PTH105", "PTH106", "PTH107", "PTH108", "PTH109", "PTH11", "PTH120", "PTH121", "PTH122", "PTH124",
"TD001", "TD004", "TD005",
"PD",
"PGH",
Expand Down
6 changes: 3 additions & 3 deletions tests/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_add_file_finding_level(self):
driver.find_element(By.LINK_TEXT, "Manage Files").click()
# select first file input field: form-0-image
# Set full image path for image file 'strange.png
image_path = os.path.join(dir_path, "finding_image.png")
image_path = Path(dir_path) / "finding_image.png"
driver.find_element(By.ID, "id_form-0-title").send_keys("Finding Title")
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
# Save uploaded image
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_add_file_test_level(self):
driver.find_element(By.NAME, "Manage Files").click()
# select first file input field: form-0-image
# Set full image path for image file 'strange.png
image_path = os.path.join(dir_path, "finding_image.png")
image_path = Path(dir_path) / "finding_image.png"
driver.find_element(By.ID, "id_form-0-title").send_keys("Test Title")
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
# Save uploaded image
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_add_file_engagement_level(self):
driver.find_element(By.NAME, "Manage Files").click()
# select first file input field: form-0-image
# Set full image path for image file 'strange.png
image_path = os.path.join(dir_path, "finding_image.png")
image_path = Path(dir_path) / "finding_image.png"
driver.find_element(By.ID, "id_form-0-title").send_keys("Engagement Title")
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
# Save uploaded image
Expand Down
4 changes: 2 additions & 2 deletions tests/finding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_add_image(self):
driver.find_element(By.LINK_TEXT, "Manage Files").click()
# select first file input field: form-0-image
# Set full image path for image file 'strange.png
image_path = os.path.join(dir_path, "finding_image.png")
image_path = Path(dir_path) / "finding_image.png"
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
driver.find_element(By.ID, "id_form-0-title").send_keys("Image Title")
# Save uploaded image
Expand Down Expand Up @@ -466,7 +466,7 @@ def test_import_scan_result(self):
# Select `Default` as the Environment
Select(driver.find_element(By.ID, "id_environment")).select_by_visible_text("Development")
# upload scan file
file_path = os.path.join(dir_path, "zap_sample.xml")
file_path = Path(dir_path) / "zap_sample.xml"
driver.find_element(By.NAME, "file").send_keys(file_path)
# Click Submit button
with WaitForPageLoad(driver, timeout=50):
Expand Down
2 changes: 1 addition & 1 deletion tests/ibm_appscan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_import_ibm_app_scan_result(self):
# Select `Default` as the Environment
Select(driver.find_element(By.ID, "id_environment")).select_by_visible_text("Development")
# Upload Scan result file
scanner_file = os.path.join(dir_path, "ibm_appscan_xml_file.xml")
scanner_file = Path(dir_path) / "ibm_appscan_xml_file.xml"
driver.find_element(By.NAME, "file").send_keys(scanner_file)
# click on upload button
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()
Expand Down
13 changes: 8 additions & 5 deletions unittests/dojo_test_case.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import copy
import json
import logging
import os
from functools import wraps
from itertools import chain
from pathlib import Path
Expand Down Expand Up @@ -40,7 +39,11 @@


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


def get_unit_tests_scans_path(parser):
return Path(__file__).parent / "scans" / parser


def toggle_system_setting_boolean(flag_name, value):
Expand Down Expand Up @@ -504,7 +507,7 @@ def import_scan_with_params(self, filename, scan_type="ZAP Scan", engagement=1,
product_name=None, product_type_name=None, auto_create_context=None, expected_http_status_code=201, test_title=None,
scan_date=None, service=None, forceActive=True, forceVerified=True):

with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
payload = {
"minimum_severity": minimum_severity,
"active": active,
Expand Down Expand Up @@ -556,7 +559,7 @@ def import_scan_with_params(self, filename, scan_type="ZAP Scan", engagement=1,
def reimport_scan_with_params(self, test_id, filename, scan_type="ZAP Scan", engagement=1, minimum_severity="Low", active=True, verified=False, push_to_jira=None,
tags=None, close_old_findings=True, group_by=None, engagement_name=None, scan_date=None,
product_name=None, product_type_name=None, auto_create_context=None, expected_http_status_code=201, test_title=None):
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
payload = {
"minimum_severity": minimum_severity,
"active": active,
Expand Down Expand Up @@ -605,7 +608,7 @@ def reimport_scan_with_params(self, test_id, filename, scan_type="ZAP Scan", eng
def endpoint_meta_import_scan_with_params(self, filename, product=1, product_name=None,
create_endpoints=True, create_tags=True, create_dojo_meta=True,
expected_http_status_code=201):
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
payload = {
"create_endpoints": create_endpoints,
"create_tags": create_tags,
Expand Down
2 changes: 1 addition & 1 deletion unittests/test_endpoint_meta_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def endpoint_meta_import_ui(self, product, payload):

def endpoint_meta_import_scan_with_params_ui(self, filename, product=1, create_endpoints=True,
create_tags=True, create_dojo_meta=True, expected_http_status_code=201):
with open(get_unit_tests_path() + "/" + filename, encoding="utf-8") as testfile:
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
payload = {
"create_endpoints": create_endpoints,
"create_tags": create_tags,
Expand Down
11 changes: 5 additions & 6 deletions unittests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os
from importlib import import_module
from importlib.util import find_spec
from inspect import isclass
Expand All @@ -16,25 +15,25 @@ class TestFactory(DojoTestCase):
def test_get_parser(self):
with self.subTest(scan_type="Acunetix Scan"):
scan_type = "Acunetix Scan"
testfile = open(get_unit_tests_path() + "/scans/acunetix/one_finding.xml", encoding="utf-8")
testfile = open(get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml", encoding="utf-8")
parser = get_parser(scan_type)
parser.get_findings(testfile, Test())
testfile.close()
with self.subTest(scan_type="Anchore Engine Scan"):
scan_type = "Anchore Engine Scan"
testfile = open(get_unit_tests_path() + "/scans/anchore_engine/one_vuln.json", encoding="utf-8")
testfile = open(get_unit_tests_path() / "scans" / "anchore_engine" / "one_vuln.json", encoding="utf-8")
parser = get_parser(scan_type)
parser.get_findings(testfile, Test())
testfile.close()
with self.subTest(scan_type="Tenable Scan"):
scan_type = "Tenable Scan"
testfile = open(get_unit_tests_path() + "/scans/tenable/nessus/nessus_v_unknown.xml", encoding="utf-8")
testfile = open(get_unit_tests_path() / "scans" / "tenable/nessus" / "nessus_v_unknown.xml", encoding="utf-8")
parser = get_parser(scan_type)
parser.get_findings(testfile, Test())
testfile.close()
with self.subTest(scan_type="ZAP Scan"):
scan_type = "ZAP Scan"
testfile = open(get_unit_tests_path() + "/scans/zap/some_2.9.0.xml", encoding="utf-8")
testfile = open(get_unit_tests_path() / "scans" / "zap" / "some_2.9.0.xml", encoding="utf-8")
parser = get_parser(scan_type)
parser.get_findings(testfile, Test())
testfile.close()
Expand Down Expand Up @@ -73,7 +72,7 @@ def test_parser_name_matches_module(self):
for module_name in module_names:
if module_name in excluded_parsers:
continue
if Path(os.path.join(package_dir, module_name)).is_dir():
if (Path(package_dir) / module_name).is_dir():
found = False
if find_spec(f"dojo.tools.{module_name}.parser"):
module = import_module(f"dojo.tools.{module_name}.parser")
Expand Down
4 changes: 2 additions & 2 deletions unittests/test_import_reimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ def import_scan_with_params_ui(self, filename, scan_type="ZAP Scan", engagement=
elif not verified:
verifiedPayload = "force_to_false"

with open(get_unit_tests_path() + filename, encoding="utf-8") as testfile:
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
payload = {
"minimum_severity": minimum_severity,
"active": activePayload,
Expand Down Expand Up @@ -1861,7 +1861,7 @@ def reimport_scan_with_params_ui(self, test_id, filename, scan_type="ZAP Scan",
if not verified:
verifiedPayload = "force_to_false"

with open(get_unit_tests_path() + filename, encoding="utf-8") as testfile:
with open(get_unit_tests_path() / filename, encoding="utf-8") as testfile:
payload = {
"minimum_severity": minimum_severity,
"active": activePayload,
Expand Down
4 changes: 2 additions & 2 deletions unittests/test_importers_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

class TestDojoDefaultImporter(DojoTestCase):
def test_parse_findings(self):
with open(get_unit_tests_path() + "/scans/acunetix/one_finding.xml", encoding="utf-8") as scan:
with open(get_unit_tests_path() / "scans" / "acunetix" / "one_finding.xml", encoding="utf-8") as scan:
scan_type = "Acunetix Scan"
user, _created = User.objects.get_or_create(username="admin")
product_type, _created = Product_Type.objects.get_or_create(name="test")
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_parse_findings(self):
self.assertIn(finding.numerical_severity, ["S0", "S1", "S2", "S3", "S4"])

def test_import_scan(self):
with open(get_unit_tests_path() + "/scans/sarif/spotbugs.sarif", encoding="utf-8") as scan:
with open(get_unit_tests_path() / "scans" / "sarif" / "spotbugs.sarif", encoding="utf-8") as scan:
scan_type = SarifParser().get_scan_types()[0] # SARIF format implement the new method
user, _ = User.objects.get_or_create(username="admin")
product_type, _ = Product_Type.objects.get_or_create(name="test2")
Expand Down
2 changes: 1 addition & 1 deletion unittests/test_jira_config_engagement_epic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _get_vcr(self, **kwargs):
my_vcr.record_mode = "once"
my_vcr.path_transformer = VCR.ensure_suffix(".yaml")
my_vcr.filter_headers = ["Authorization", "X-Atlassian-Token"]
my_vcr.cassette_library_dir = get_unit_tests_path() + "/vcr/jira/"
my_vcr.cassette_library_dir = get_unit_tests_path() / "vcr" / "jira" # TODO: check this
# filters headers doesn't seem to work for cookies, so use callbacks to filter cookies from being recorded
my_vcr.before_record_request = self.before_record_request
my_vcr.before_record_response = self.before_record_response
Expand Down
2 changes: 1 addition & 1 deletion unittests/test_jira_import_and_pushing_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _get_vcr(self, **kwargs):
my_vcr.record_mode = "once"
my_vcr.path_transformer = VCR.ensure_suffix(".yaml")
my_vcr.filter_headers = ["Authorization", "X-Atlassian-Token"]
my_vcr.cassette_library_dir = get_unit_tests_path() + "/vcr/jira/"
my_vcr.cassette_library_dir = get_unit_tests_path() / "vcr" / "jira" # TODO: check this
# filters headers doesn't seem to work for cookies, so use callbacks to filter cookies from being recorded
my_vcr.before_record_request = self.before_record_request
my_vcr.before_record_response = self.before_record_response
Expand Down
Loading
Loading