From dfca758da86ce090fde08e37ad0f10444ddc5a12 Mon Sep 17 00:00:00 2001 From: kiblik <5609770+kiblik@users.noreply.github.com> Date: Thu, 4 Jul 2024 22:19:35 +0200 Subject: [PATCH] Ruff: Solve F821 --- dojo/engagement/views.py | 1 + dojo/jira_link/views.py | 1 + .../management/commands/jira_async_updates.py | 1 + .../commands/rename_mend_findings.py | 7 +++++ .../commands/test_celery_decorator.py | 4 ++- dojo/settings/settings.py | 2 +- dojo/tools/qualys/csv_parser.py | 1 + dojo/tools/qualys_webapp/parser.py | 1 + ruff.toml | 2 +- tests/Import_scanner_test.py | 30 ++----------------- tests/zap.py | 3 +- unittests/test_parsers.py | 1 + unittests/tools/test_api_sonarqube_parser.py | 2 +- 13 files changed, 24 insertions(+), 32 deletions(-) diff --git a/dojo/engagement/views.py b/dojo/engagement/views.py index 138ae89238e..6de26e6a610 100644 --- a/dojo/engagement/views.py +++ b/dojo/engagement/views.py @@ -21,6 +21,7 @@ from django.shortcuts import get_object_or_404, render from django.urls import Resolver404, reverse from django.utils import timezone +from django.utils.translation import gettext as _ from django.views import View from django.views.decorators.cache import cache_page from django.views.decorators.vary import vary_on_cookie diff --git a/dojo/jira_link/views.py b/dojo/jira_link/views.py index 4a540c6d5c7..e618c84f01c 100644 --- a/dojo/jira_link/views.py +++ b/dojo/jira_link/views.py @@ -13,6 +13,7 @@ from django.urls import reverse from django.utils import timezone from django.utils.dateparse import parse_datetime +from django.utils.translation import gettext as _ from django.views import View from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_POST diff --git a/dojo/management/commands/jira_async_updates.py b/dojo/management/commands/jira_async_updates.py index 452bc1bc0b7..78ea8cef6c1 100644 --- a/dojo/management/commands/jira_async_updates.py +++ b/dojo/management/commands/jira_async_updates.py @@ -4,6 +4,7 @@ from django.utils import timezone from jira.exceptions import JIRAError +import dojo.jira_link.helper as jira_helper from dojo.models import Dojo_User, Finding, Notes, User """ diff --git a/dojo/management/commands/rename_mend_findings.py b/dojo/management/commands/rename_mend_findings.py index 4b8b6de5c95..463f1d05a79 100644 --- a/dojo/management/commands/rename_mend_findings.py +++ b/dojo/management/commands/rename_mend_findings.py @@ -1,7 +1,14 @@ +import logging +import re + from django.core.management.base import BaseCommand from pytz import timezone from dojo.celery import app +from dojo.models import Finding, Test_Type +from dojo.utils import get_system_setting + +logger = logging.getLogger(__name__) locale = timezone(get_system_setting("time_zone")) diff --git a/dojo/management/commands/test_celery_decorator.py b/dojo/management/commands/test_celery_decorator.py index f1073f63ab0..da47f147702 100644 --- a/dojo/management/commands/test_celery_decorator.py +++ b/dojo/management/commands/test_celery_decorator.py @@ -4,8 +4,10 @@ from django.core.management.base import BaseCommand -# from dojo.utils import get_system_setting, do_dedupe_finding, dojo_async_task from dojo.celery import app + +# from dojo.utils import get_system_setting, do_dedupe_finding, dojo_async_task +from dojo.decorators import dojo_async_task, dojo_model_from_id, dojo_model_to_id from dojo.models import Finding, Notes from dojo.utils import test_valentijn diff --git a/dojo/settings/settings.py b/dojo/settings/settings.py index abb81685066..6355bedf285 100644 --- a/dojo/settings/settings.py +++ b/dojo/settings/settings.py @@ -12,7 +12,7 @@ optional("local_settings.py"), ) -if not (DEBUG or ("collectstatic" in sys.argv)): +if not (DEBUG or ("collectstatic" in sys.argv)): # noqa: F821 - not declared DEBUG is acceptable because we are sure it will be loaded from 'include' with (Path(__file__).parent / "settings.dist.py").open("rb") as file: real_hash = hashlib.sha256(file.read()).hexdigest() with (Path(__file__).parent / ".settings.dist.py.sha256sum").open("rb") as file: diff --git a/dojo/tools/qualys/csv_parser.py b/dojo/tools/qualys/csv_parser.py index 98a8ec00af9..16ad062fc69 100644 --- a/dojo/tools/qualys/csv_parser.py +++ b/dojo/tools/qualys/csv_parser.py @@ -4,6 +4,7 @@ import re from datetime import datetime +from dateutil import parser from django.conf import settings from dojo.models import Endpoint, Finding diff --git a/dojo/tools/qualys_webapp/parser.py b/dojo/tools/qualys_webapp/parser.py index 871e690e833..4390943dece 100644 --- a/dojo/tools/qualys_webapp/parser.py +++ b/dojo/tools/qualys_webapp/parser.py @@ -1,6 +1,7 @@ import base64 import re from datetime import datetime +from urllib.parse import urlparse from defusedxml import ElementTree diff --git a/ruff.toml b/ruff.toml index cb5f59c0e73..1cbf078763e 100644 --- a/ruff.toml +++ b/ruff.toml @@ -66,7 +66,7 @@ select = [ "NPY", "AIR", ] -ignore = ["E501", "E722", "F821"] +ignore = ["E501", "E722"] # Allow autofix for all enabled rules (when `--fix`) is provided. fixable = ["ALL"] diff --git a/tests/Import_scanner_test.py b/tests/Import_scanner_test.py index 9e5d7b086c1..6b1d35404e9 100644 --- a/tests/Import_scanner_test.py +++ b/tests/Import_scanner_test.py @@ -1,3 +1,4 @@ +# ruff: noqa: F821 import logging import os import re @@ -11,6 +12,8 @@ from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select +dir_path = os.path.dirname(os.path.realpath(__file__)) + logger = logging.getLogger(__name__) @@ -53,33 +56,6 @@ def test_check_test_file(self): logger.info(test) assert len(missing_tests) == 0 - def test_check_for_doc(self): - driver = self.driver - driver.get("https://documentation.defectdojo.com/integrations/import/") - integration_index = integration_text.index("Integrations") + len("Integrations") + 1 - usage_index = integration_text.index("Usage Examples") - len("Models") - 2 - integration_text = integration_text[integration_index:usage_index].lower() - integration_text = integration_text.replace("_", " ").replace("-", " ").replace(".", "").split("\n") - acronyms = [] - for words in integration_text: - acronyms += ["".join(word[0] for word in words.split())] - - missing_docs = [] - for tool in self.tools: - reg = re.compile(".*" + tool.replace("_", " ") + ".*") - if len(list(filter(reg.search, integration_text))) < 1: - if len(list(filter(reg.search, acronyms))) < 1: - missing_docs += [tool] - - if len(missing_docs) > 0: - logger.info("The following scanners are missing documentation") - logger.info("Names must match those listed in /dojo/tools") - logger.info("Documentation can be added here:") - logger.info("https://github.com/DefectDojo/django-DefectDojo/tree/dev/docs\n") - for tool in missing_docs: - logger.info(tool) - assert len(missing_docs) == 0 - def test_check_for_forms(self): forms_path = dir_path[:-5] + "dojo/forms.py" file = open(forms_path, "r+") diff --git a/tests/zap.py b/tests/zap.py index bfda2ada56a..8086899332f 100755 --- a/tests/zap.py +++ b/tests/zap.py @@ -3,6 +3,7 @@ import logging import re import socket +import sys import time from urllib.parse import urlparse @@ -56,7 +57,7 @@ class Main: loginUrl = "http://os.environ['DD_BASE_URL']/login" # loginUrlregex = "\Q" + loginUrl + "\E.*" # The above line is flake8 violation as \Q and \E are not supported by python - loginURLregex = re.escape(loginURL) + loginURLregex = re.escape(loginUrl) result = zap.context.exclude_from_context(contextname, ".*logout.*", apikey) result = zap.context.exclude_from_context(contextname, ".*/static/.*", apikey) diff --git a/unittests/test_parsers.py b/unittests/test_parsers.py index f729df389a7..fa15e6e69be 100644 --- a/unittests/test_parsers.py +++ b/unittests/test_parsers.py @@ -84,6 +84,7 @@ def test_file_existence(self): f = os.path.join(basedir, "dojo", "tools", parser_dir.name, file.name) read_true = False with open(f) as f: + i = 0 for line in f.readlines(): if read_true is True: if ('"utf-8"' in str(line) or "'utf-8'" in str(line) or '"utf-8-sig"' in str(line) or "'utf-8-sig'" in str(line)) and i <= 4: diff --git a/unittests/tools/test_api_sonarqube_parser.py b/unittests/tools/test_api_sonarqube_parser.py index 4a44b39b7e5..aa278b80252 100644 --- a/unittests/tools/test_api_sonarqube_parser.py +++ b/unittests/tools/test_api_sonarqube_parser.py @@ -11,7 +11,7 @@ Tool_Type, ) from dojo.tools.api_sonarqube.parser import ApiSonarQubeParser -from unittests.dojo_test_case import DojoTestCase +from unittests.dojo_test_case import DojoTestCase, get_unit_tests_path def dummy_product(self, *args, **kwargs):