Skip to content

Commit

Permalink
Ruff: Add and fix SIM117
Browse files Browse the repository at this point in the history
  • Loading branch information
kiblik committed Jan 25, 2025
1 parent feabd7b commit 881a469
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 106 deletions.
3 changes: 2 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ ignore = [
"SIM113",
"SIM115",
"SIM116",
"SIM117",
"RUF012",
"RUF015",
"D205",
"FURB122",
"RUF047",
"D211", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible.
"D212", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible.
]
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_anchore_enterprise_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ 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(__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())
with open(path.join(Path(__file__).parent, "../scans/anchore_enterprise/invalid_checks_format.json"), encoding="utf-8") as testfile, \
self.assertRaises(Exception):
parser = AnchoreEnterpriseParser()
parser.get_findings(testfile, Test())

def test_anchore_policy_check_extract_vulnerability_id(self):
vulnerability_id = extract_vulnerability_id("CVE-2019-14540+openapi-generator-cli-4.0.0.jar:jackson-databind")
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_auditjs_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def test_auditjs_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(400, findings[4].cwe)

def test_auditjs_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open("unittests/scans/auditjs/empty_with_error.json", encoding="utf-8") as testfile:
parser = AuditJSParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open("unittests/scans/auditjs/empty_with_error.json", encoding="utf-8") as testfile:
parser = AuditJSParser()
parser.get_findings(testfile, Test())

self.assertIn(
"Invalid JSON format. Are you sure you used --json option ?", str(context.exception),
Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_aws_inspector2_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def test_aws_inspector2_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(8, len(findings))

def test_aws_inspector2_parser_empty_with_error(self):
with self.assertRaises(TypeError) as context:
with open("unittests/scans/aws_inspector2/empty_with_error.json", encoding="utf-8") as testfile:
parser = AWSInspector2Parser()
parser.get_findings(testfile, Test())
testfile.close()
self.assertTrue(
"Incorrect Inspector2 report format" in str(context.exception),
)
with self.assertRaises(TypeError) as context, \
open("unittests/scans/aws_inspector2/empty_with_error.json", encoding="utf-8") as testfile:
parser = AWSInspector2Parser()
parser.get_findings(testfile, Test())
testfile.close()
self.assertTrue(
"Incorrect Inspector2 report format" in str(context.exception),
)
9 changes: 4 additions & 5 deletions unittests/tools/test_burp_graphql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ def test_burp_no_findings(self):
self.assertEqual(0, len(findings))

def test_burp_null_title(self):
with open(path.join(Path(__file__).parent, "../scans/burp_graphql/null_title.json"), encoding="utf-8") as test_file:

with self.assertRaises(ValueError):
parser = BurpGraphQLParser()
parser.get_findings(test_file, Test())
with open(path.join(Path(__file__).parent, "../scans/burp_graphql/null_title.json"), encoding="utf-8") as test_file, \
self.assertRaises(ValueError):
parser = BurpGraphQLParser()
parser.get_findings(test_file, Test())

def test_burp_null_request_segments(self):
with open(path.join(Path(__file__).parent, "../scans/burp_graphql/null_request_segments.json"), encoding="utf-8") as test_file:
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_coverity_api_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class TestZapParser(DojoTestCase):
def test_parse_wrong_file(self):
with self.assertRaises(ValueError):
with open("unittests/scans/coverity_api/wrong.json", encoding="utf-8") as testfile:
parser = CoverityApiParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open("unittests/scans/coverity_api/wrong.json", encoding="utf-8") as testfile:
parser = CoverityApiParser()
parser.get_findings(testfile, Test())

def test_parse_no_findings(self):
with open("unittests/scans/coverity_api/empty.json", encoding="utf-8") as testfile:
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_gitlab_api_fuzzing_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def test_gitlab_api_fuzzing_parser_with_one_criticle_vuln_has_one_findings_v15(s
)

def test_gitlab_api_fuzzing_parser_with_invalid_json(self):
with open(f"{get_unit_tests_path()}/scans/gitlab_api_fuzzing/gitlab_api_fuzzing_invalid.json", encoding="utf-8") as testfile:
with open(f"{get_unit_tests_path()}/scans/gitlab_api_fuzzing/gitlab_api_fuzzing_invalid.json", encoding="utf-8") as testfile, \
self.assertRaises((KeyError, ValueError)):
# Something is wrong with JSON file
with self.assertRaises((KeyError, ValueError)):
parser = GitlabAPIFuzzingParser()
parser.get_findings(testfile, Test())
parser = GitlabAPIFuzzingParser()
parser.get_findings(testfile, Test())
8 changes: 4 additions & 4 deletions unittests/tools/test_govulncheck_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestGovulncheckParser(DojoTestCase):

def test_parse_empty(self):
with self.assertRaises(ValueError) as exp:
with open("unittests/scans/govulncheck/empty.json", encoding="utf-8") as testfile:
parser = GovulncheckParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as exp, \
open("unittests/scans/govulncheck/empty.json", encoding="utf-8") as testfile:
parser = GovulncheckParser()
parser.get_findings(testfile, Test())
self.assertIn(
"Invalid JSON format", str(exp.exception),
)
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_intsights_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def test_intsights_parser_with_many_vuln_has_many_findings_csv(self):
self.assertEqual(9, len(findings))

def test_intsights_parser_invalid_text_with_error_csv(self):
with self.assertRaises(ValueError):
with open("unittests/scans/intsights/intsights_invalid_file.txt", encoding="utf-8") as testfile:
parser = IntSightsParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open("unittests/scans/intsights/intsights_invalid_file.txt", encoding="utf-8") as testfile:
parser = IntSightsParser()
parser.get_findings(testfile, Test())

def test_intsights_parser_with_no_alerts_json(self):
with open("unittests/scans/intsights/intsights_zero_vuln.json", encoding="utf-8") as testfile:
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_kubehunter_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def test_kubehunter_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(8, len(findings))

def test_kubehunter_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open("unittests/scans/kubehunter/empty.json", encoding="utf-8") as testfile:
parser = KubeHunterParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open("unittests/scans/kubehunter/empty.json", encoding="utf-8") as testfile:
parser = KubeHunterParser()
parser.get_findings(testfile, Test())

self.assertEqual(
"Expecting value: line 1 column 1 (char 0)", str(context.exception),
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_meterian_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestMeterianParser(DojoTestCase):

def test_meterianParser_invalid_security_report_raise_ValueError_exception(self):
with self.assertRaises(ValueError):
with open("unittests/scans/meterian/report_invalid.json", encoding="utf-8") as testfile:
parser = MeterianParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open("unittests/scans/meterian/report_invalid.json", encoding="utf-8") as testfile:
parser = MeterianParser()
parser.get_findings(testfile, Test())

def test_meterianParser_report_has_no_finding(self):
with open("unittests/scans/meterian/report_no_vulns.json", encoding="utf-8") as testfile:
Expand Down
20 changes: 10 additions & 10 deletions unittests/tools/test_noseyparker_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def test_noseyparker_parser_many_vulns(self):
self.assertEqual(3, len(findings))

def test_noseyparker_parser_error(self):
with self.assertRaises(ValueError) as context:
with open("unittests/scans/noseyparker/empty_with_error.json", encoding="utf-8") as testfile:
parser = NoseyParkerParser()
findings = parser.get_findings(testfile, Test())
testfile.close()
self.assertEqual(0, len(findings))
self.assertIn(
"Invalid Nosey Parker data, make sure to use Nosey Parker v0.16.0", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))
with self.assertRaises(ValueError) as context, \
open("unittests/scans/noseyparker/empty_with_error.json", encoding="utf-8") as testfile:
parser = NoseyParkerParser()
findings = parser.get_findings(testfile, Test())
testfile.close()
self.assertEqual(0, len(findings))
self.assertIn(
"Invalid Nosey Parker data, make sure to use Nosey Parker v0.16.0", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))
16 changes: 8 additions & 8 deletions unittests/tools/test_npm_audit_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ def test_npm_audit_parser_with_one_criticle_vuln_has_null_as_cwe(self):
self.assertEqual("1.9.2", findings[0].component_version)

def test_npm_audit_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(path.join(Path(__file__).parent, "../scans/npm_audit/empty_with_error.json"), encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(path.join(Path(__file__).parent, "../scans/npm_audit/empty_with_error.json"), encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())

self.assertIn("npm audit report contains errors:", str(context.exception))
self.assertIn("ENOAUDIT", str(context.exception))

def test_npm_audit_parser_many_vuln_npm7(self):
with self.assertRaises(ValueError) as context:
with open(path.join(Path(__file__).parent, "../scans/npm_audit/many_vuln_npm7.json"), encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(path.join(Path(__file__).parent, "../scans/npm_audit/many_vuln_npm7.json"), encoding="utf-8") as testfile:
parser = NpmAuditParser()
parser.get_findings(testfile, Test())

self.assertIn("npm7 with auditReportVersion 2 or higher not yet supported", str(context.exception))

Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_risk_recon_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
class TestRiskReconAPIParser(DojoTestCase):

def test_api_with_bad_url(self):
with open("unittests/scans/risk_recon/bad_url.json", encoding="utf-8") as testfile:
with self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())
with open("unittests/scans/risk_recon/bad_url.json", encoding="utf-8") as testfile, \
self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())

def test_api_with_bad_key(self):
with open("unittests/scans/risk_recon/bad_key.json", encoding="utf-8") as testfile:
with self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())
with open("unittests/scans/risk_recon/bad_key.json", encoding="utf-8") as testfile, \
self.assertRaises(Exception):
parser = RiskReconParser()
parser.get_findings(testfile, Test())

def test_parser_without_api(self):
with open("unittests/scans/risk_recon/findings.json", encoding="utf-8") as testfile:
Expand Down
28 changes: 14 additions & 14 deletions unittests/tools/test_sysdig_reports_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ def test_sysdig_parser_with_many_vuln_has_many_findings(self):
self.assertEqual(50, len(findings))

def test_sysdig_parser_missing_cve_field_id_from_csv_file(self):
with self.assertRaises(ValueError) as context:
with open("unittests/scans/sysdig_reports/sysdig_reports_missing_cve_field.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
with self.assertRaises(ValueError) as context, \
open("unittests/scans/sysdig_reports/sysdig_reports_missing_cve_field.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
self.assertEqual(
"Number of fields in row (22) does not match number of headers (21)", str(context.exception),
)

def test_sysdig_parser_missing_cve_field_not_starting_with_cve(self):
with self.assertRaises(ValueError) as context:
with open("unittests/scans/sysdig_reports/sysdig_reports_not_starting_with_cve.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
with self.assertRaises(ValueError) as context, \
open("unittests/scans/sysdig_reports/sysdig_reports_not_starting_with_cve.csv", encoding="utf-8") as testfile:
parser = SysdigReportsParser()
findings = parser.get_findings(testfile, Test())
for finding in findings:
for endpoint in finding.unsaved_endpoints:
endpoint.clean()
self.assertEqual(
"Number of fields in row (22) does not match number of headers (21)", str(context.exception),
)
Expand Down
8 changes: 4 additions & 4 deletions unittests/tools/test_threat_composer_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def test_threat_composer_parser_with_many_threats_has_many_findings(self):
self.assertEqual(21, len(findings))

def test_threat_composer_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open(get_unit_tests_path() + sample_path("threat_composer_no_threats_with_error.json"), encoding="utf-8") as testfile:
parser = ThreatComposerParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError) as context, \
open(get_unit_tests_path() + sample_path("threat_composer_no_threats_with_error.json"), encoding="utf-8") as testfile:
parser = ThreatComposerParser()
parser.get_findings(testfile, Test())

self.assertNotIn("No threats found in the JSON file", str(context.exception))

Expand Down
16 changes: 8 additions & 8 deletions unittests/tools/test_whitehat_sentinel_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
class TestWhiteHatSentinelParser(DojoTestCase):

def test_parse_file_with_no_vuln_has_no_findings(self):
with self.assertRaises(ValueError):
with open("unittests/scans/whitehat_sentinel/empty_file.json", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open("unittests/scans/whitehat_sentinel/empty_file.json", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())

def test_parse_file_with_one_vuln_has_one_findings(self):
with open("unittests/scans/whitehat_sentinel/one_vuln.json", encoding="utf-8") as testfile:
Expand All @@ -24,7 +24,7 @@ def test_parse_file_with_multiple_vuln_has_multiple_finding(self):
self.assertEqual(3, len(findings))

def test_parse_file_with_invalid_data(self):
with self.assertRaises(ValueError):
with open("unittests/scans/whitehat_sentinel/invalid_data.txt", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())
with self.assertRaises(ValueError), \
open("unittests/scans/whitehat_sentinel/invalid_data.txt", encoding="utf-8") as testfile:
parser = WhiteHatSentinelParser()
parser.get_findings(testfile, Test())
16 changes: 8 additions & 8 deletions unittests/tools/test_yarn_audit_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def test_yarn_audit_parser_with_multiple_cwes_per_finding_list(self):
self.assertEqual(findings[1].unsaved_vulnerability_ids[0], "CVE-2021-3807")

def test_yarn_audit_parser_empty_with_error(self):
with self.assertRaises(ValueError) as context:
with open("unittests/scans/yarn_audit/empty_with_error.json", encoding="utf-8") as testfile:
parser = YarnAuditParser()
parser.get_findings(testfile, self.get_test())
self.assertIn(
"yarn audit report contains errors:", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))
with self.assertRaises(ValueError) as context, \
open("unittests/scans/yarn_audit/empty_with_error.json", encoding="utf-8") as testfile:
parser = YarnAuditParser()
parser.get_findings(testfile, self.get_test())
self.assertIn(
"yarn audit report contains errors:", str(context.exception),
)
self.assertIn("ECONNREFUSED", str(context.exception))

def test_yarn_audit_parser_issue_6495(self):
with open("unittests/scans/yarn_audit/issue_6495.json", encoding="utf-8") as testfile:
Expand Down

0 comments on commit 881a469

Please sign in to comment.