Skip to content

Commit

Permalink
Ruff: Fix PTH118, merge PTH11
Browse files Browse the repository at this point in the history
  • Loading branch information
kiblik committed Jan 25, 2025
1 parent feabd7b commit 31e7d50
Show file tree
Hide file tree
Showing 214 changed files with 1,345 additions and 1,414 deletions.
2 changes: 1 addition & 1 deletion dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2428,7 +2428,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 contextlib import suppress
Expand Down Expand Up @@ -150,7 +149,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
6 changes: 2 additions & 4 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 @@ -912,8 +912,6 @@ def saml2_attrib_map_format(dict):
SAML2_LOGIN_BUTTON_TEXT = env("DD_SAML2_LOGIN_BUTTON_TEXT")
SAML2_LOGOUT_URL = env("DD_SAML2_LOGOUT_URL")
if SAML2_ENABLED:
from os import path

import saml2
import saml2.saml
# SSO_URL = env('DD_SSO_URL')
Expand Down Expand Up @@ -949,7 +947,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", "PTH10", "PTH110", "PTH111", "PTH112", "PTH113", "PTH114", "PTH115", "PTH116", "PTH117", "PTH119", "PTH120", "PTH121", "PTH122", "PTH124",
"PTH2", "PTH10", "PTH11", "PTH120", "PTH121", "PTH122", "PTH124",
"TD001", "TD004", "TD005",
"PD",
"PGH",
Expand Down
8 changes: 4 additions & 4 deletions tests/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ 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 = 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)
driver.find_element(By.ID, "id_form-0-file").send_keys(str(image_path))
# Save uploaded image
with WaitForPageLoad(driver, timeout=50):
driver.find_element(By.CSS_SELECTOR, "button.btn.btn-success").click()
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 = 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 = 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
8 changes: 4 additions & 4 deletions tests/finding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ 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")
driver.find_element(By.ID, "id_form-0-file").send_keys(image_path)
image_path = dir_path / "finding_image.png"
driver.find_element(By.ID, "id_form-0-file").send_keys(str(image_path))
driver.find_element(By.ID, "id_form-0-title").send_keys("Image Title")
# Save uploaded image
with WaitForPageLoad(driver, timeout=50):
Expand Down Expand Up @@ -466,8 +466,8 @@ 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")
driver.find_element(By.NAME, "file").send_keys(file_path)
file_path = dir_path / "zap_sample.xml"
driver.find_element(By.NAME, "file").send_keys(str(file_path))
# Click Submit button
with WaitForPageLoad(driver, timeout=50):
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()
Expand Down
4 changes: 2 additions & 2 deletions tests/ibm_appscan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ 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")
driver.find_element(By.NAME, "file").send_keys(scanner_file)
scanner_file = dir_path / "ibm_appscan_xml_file.xml"
driver.find_element(By.NAME, "file").send_keys(str(scanner_file))
# click on upload button
driver.find_elements(By.CSS_SELECTOR, "button.btn.btn-primary")[1].click()
# Query the site to determine if the finding has been added
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(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(filename, encoding="utf-8") as testfile:
payload = {
"create_endpoints": create_endpoints,
"create_tags": create_tags,
Expand Down
12 changes: 6 additions & 6 deletions unittests/test_endpoint_meta_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
# test methods to be used both by API Test and UI Test
class EndpointMetaImportMixin:
def __init__(self, *args, **kwargs):
self.meta_import_full = "endpoint_meta_import/full_endpoint_meta_import.csv"
self.meta_import_no_hostname = "endpoint_meta_import/no_hostname_endpoint_meta_import.csv"
self.meta_import_updated_added = "endpoint_meta_import/updated_added_endpoint_meta_import.csv"
self.meta_import_updated_removed = "endpoint_meta_import/updated_removed_endpoint_meta_import.csv"
self.meta_import_updated_changed = "endpoint_meta_import/updated_changed_endpoint_meta_import.csv"
self.meta_import_full = get_unit_tests_path() / "endpoint_meta_import" / "full_endpoint_meta_import.csv"
self.meta_import_no_hostname = get_unit_tests_path() / "endpoint_meta_import" / "no_hostname_endpoint_meta_import.csv"
self.meta_import_updated_added = get_unit_tests_path() / "endpoint_meta_import" / "updated_added_endpoint_meta_import.csv"
self.meta_import_updated_removed = get_unit_tests_path() / "endpoint_meta_import" / "updated_removed_endpoint_meta_import.csv"
self.meta_import_updated_changed = get_unit_tests_path() / "endpoint_meta_import" / "updated_changed_endpoint_meta_import.csv"
self.updated_tag_host = "feedback.internal.google.com"

def test_endpoint_meta_import_endpoint_create_tag_create_meta_create(self):
Expand Down 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(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
Loading

0 comments on commit 31e7d50

Please sign in to comment.