Skip to content

Commit 0d7d041

Browse files
committed
Fixed some integration tests
1 parent 2df0383 commit 0d7d041

File tree

5 files changed

+54
-57
lines changed

5 files changed

+54
-57
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
from codemodder.codemods.test import SonarIntegrationTest
1+
from codemodder.codemods.test.integration_utils import SonarRemediationIntegrationTest
22
from core_codemods.enable_jinja2_autoescape import EnableJinja2AutoescapeTransformer
33
from core_codemods.sonar.sonar_enable_jinja2_autoescape import (
44
SonarEnableJinja2Autoescape,
55
)
66

77

8-
class TestSonarEnableJinja2Autoescape(SonarIntegrationTest):
8+
class TestSonarEnableJinja2Autoescape(SonarRemediationIntegrationTest):
99
codemod = SonarEnableJinja2Autoescape
1010
code_path = "tests/samples/jinja2_autoescape.py"
11-
replacement_lines = [
12-
(3, "env = Environment(autoescape=True)\n"),
13-
(4, "env = Environment(autoescape=True)\n"),
11+
expected_diff_per_change = [
12+
"--- \n+++ \n@@ -1,4 +1,4 @@\n from jinja2 import Environment\n \n-env = Environment()\n+env = Environment(autoescape=True)\n env = Environment(autoescape=False)\n",
13+
"--- \n+++ \n@@ -1,4 +1,4 @@\n from jinja2 import Environment\n \n env = Environment()\n-env = Environment(autoescape=False)\n+env = Environment(autoescape=True)\n",
1414
]
15-
expected_diff = "--- \n+++ \n@@ -1,4 +1,4 @@\n from jinja2 import Environment\n \n-env = Environment()\n-env = Environment(autoescape=False)\n+env = Environment(autoescape=True)\n+env = Environment(autoescape=True)\n"
16-
expected_line_change = "3"
15+
16+
expected_lines_changed = [3, 4]
1717
num_changes = 2
1818
change_description = EnableJinja2AutoescapeTransformer.change_description
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from requests.exceptions import ConnectionError
22

3-
from codemodder.codemods.test import BaseIntegrationTest
3+
from codemodder.codemods.test.integration_utils import BaseRemediationIntegrationTest
44
from core_codemods.add_requests_timeouts import (
55
AddRequestsTimeouts,
66
TransformAddRequestsTimeouts,
77
)
88

99

10-
class TestAddRequestsTimeouts(BaseIntegrationTest):
10+
class TestAddRequestsTimeouts(BaseRemediationIntegrationTest):
1111
codemod = AddRequestsTimeouts
1212
original_code = """
1313
import requests
@@ -18,27 +18,13 @@ class TestAddRequestsTimeouts(BaseIntegrationTest):
1818
requests.post("https://example.com", verify=False)
1919
"""
2020

21-
replacement_lines = [
22-
(3, 'requests.get("https://example.com", timeout=60)\n'),
23-
(6, 'requests.post("https://example.com", verify=False, timeout=60)\n'),
21+
expected_diff_per_change = [
22+
'--- \n+++ \n@@ -1,6 +1,6 @@\n import requests\n \n-requests.get("https://example.com")\n+requests.get("https://example.com", timeout=60)\n requests.get("https://example.com", timeout=1)\n requests.get("https://example.com", timeout=(1, 10), verify=False)\n requests.post("https://example.com", verify=False)',
23+
'--- \n+++ \n@@ -3,4 +3,4 @@\n requests.get("https://example.com")\n requests.get("https://example.com", timeout=1)\n requests.get("https://example.com", timeout=(1, 10), verify=False)\n-requests.post("https://example.com", verify=False)\n+requests.post("https://example.com", verify=False, timeout=60)',
2424
]
2525

26-
expected_diff = """\
27-
---
28-
+++
29-
@@ -1,6 +1,6 @@
30-
import requests
31-
32-
-requests.get("https://example.com")
33-
+requests.get("https://example.com", timeout=60)
34-
requests.get("https://example.com", timeout=1)
35-
requests.get("https://example.com", timeout=(1, 10), verify=False)
36-
-requests.post("https://example.com", verify=False)
37-
+requests.post("https://example.com", verify=False, timeout=60)
38-
"""
39-
4026
num_changes = 2
41-
expected_line_change = "3"
27+
expected_lines_changed = [3, 6]
4228
change_description = TransformAddRequestsTimeouts.change_description
4329
# expected because requests are made
4430
allowed_exceptions = (ConnectionError,)
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from codemodder.codemods.test import BaseIntegrationTest
1+
from codemodder.codemods.test.integration_utils import BaseRemediationIntegrationTest
22
from core_codemods.jwt_decode_verify import JwtDecodeVerify, JwtDecodeVerifyTransformer
33

44

5-
class TestJwtDecodeVerify(BaseIntegrationTest):
5+
class TestJwtDecodeVerify(BaseRemediationIntegrationTest):
66
codemod = JwtDecodeVerify
77
original_code = """
88
import jwt
@@ -20,17 +20,11 @@ class TestJwtDecodeVerify(BaseIntegrationTest):
2020
2121
var = "something"
2222
"""
23-
replacement_lines = [
24-
(
25-
11,
26-
"""decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], verify=True)\n""",
27-
),
28-
(
29-
12,
30-
"""decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], options={"verify_signature": True})\n""",
31-
),
23+
expected_diff_per_change = [
24+
'--- \n+++ \n@@ -8,7 +8,7 @@\n \n encoded_jwt = jwt.encode(payload, SECRET_KEY, algorithm="HS256")\n \n-decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], verify=False)\n+decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], verify=True)\n decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], options={"verify_signature": False})\n \n var = "something"',
25+
'--- \n+++ \n@@ -9,6 +9,6 @@\n encoded_jwt = jwt.encode(payload, SECRET_KEY, algorithm="HS256")\n \n decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], verify=False)\n-decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], options={"verify_signature": False})\n+decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], options={"verify_signature": True})\n \n var = "something"',
3226
]
33-
expected_diff = '--- \n+++ \n@@ -8,7 +8,7 @@\n \n encoded_jwt = jwt.encode(payload, SECRET_KEY, algorithm="HS256")\n \n-decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], verify=False)\n-decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], options={"verify_signature": False})\n+decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], verify=True)\n+decoded_payload = jwt.decode(encoded_jwt, SECRET_KEY, algorithms=["HS256"], options={"verify_signature": True})\n \n var = "something"\n'
34-
expected_line_change = "11"
27+
28+
expected_lines_changed = [11, 12]
3529
num_changes = 2
3630
change_description = JwtDecodeVerifyTransformer.change_description

integration_tests/test_request_verify.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from requests import exceptions
22

3-
from codemodder.codemods.test import BaseIntegrationTest
3+
from codemodder.codemods.test.integration_utils import BaseRemediationIntegrationTest
44
from core_codemods.requests_verify import RequestsVerify
55

66

7-
class TestRequestsVerify(BaseIntegrationTest):
7+
class TestRequestsVerify(BaseRemediationIntegrationTest):
88
codemod = RequestsVerify
99
original_code = """
1010
import requests
@@ -14,15 +14,12 @@ class TestRequestsVerify(BaseIntegrationTest):
1414
var = "hello"
1515
"""
1616

17-
replacement_lines = [
18-
(3, """requests.get("https://www.google.com", verify=True)\n"""),
19-
(
20-
4,
21-
"""requests.post("https://some-api/", json={"id": 1234, "price": 18}, verify=True)\n""",
22-
),
17+
expected_diff_per_change = [
18+
'--- \n+++ \n@@ -1,5 +1,5 @@\n import requests\n \n-requests.get("https://www.google.com", verify=False)\n+requests.get("https://www.google.com", verify=True)\n requests.post("https://some-api/", json={"id": 1234, "price": 18}, verify=False)\n var = "hello"',
19+
'--- \n+++ \n@@ -1,5 +1,5 @@\n import requests\n \n requests.get("https://www.google.com", verify=False)\n-requests.post("https://some-api/", json={"id": 1234, "price": 18}, verify=False)\n+requests.post("https://some-api/", json={"id": 1234, "price": 18}, verify=True)\n var = "hello"',
2320
]
24-
expected_diff = '--- \n+++ \n@@ -1,5 +1,5 @@\n import requests\n \n-requests.get("https://www.google.com", verify=False)\n-requests.post("https://some-api/", json={"id": 1234, "price": 18}, verify=False)\n+requests.get("https://www.google.com", verify=True)\n+requests.post("https://some-api/", json={"id": 1234, "price": 18}, verify=True)\n var = "hello"\n'
25-
expected_line_change = "3"
21+
22+
expected_lines_changed = [3, 4]
2623
num_changes = 2
2724
change_description = RequestsVerify.change_description
2825
# expected because when executing the output code it will make a request which fails, which is OK.

src/codemodder/codemods/test/integration_utils.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ def setup_class(cls):
6969
manage_py_path = parent_dir / "manage.py"
7070
manage_py_path.touch()
7171

72+
if cls.original_code is not NotImplementedError:
73+
# Some tests are easier to understand with the expected new code provided
74+
# instead of calculated
75+
cls.original_code = dedent(cls.original_code).strip("\n")
76+
else:
77+
with open(cls.code_path, "r", encoding="utf-8") as f: # type: ignore
78+
cls.original_code = f.read()
79+
7280
def _assert_run_fields(self, run, output_path):
7381
assert run["vendor"] == "pixee"
7482
assert run["tool"] == "codemodder-python"
@@ -152,14 +160,15 @@ def _assert_codetf_output(self, codetf_schema):
152160
# CodeTf2 spec requires relative paths
153161
self._assert_results_fields(results, self.code_filename)
154162

163+
def write_original_code(self):
164+
with open(self.code_path, "w", encoding="utf-8") as f:
165+
f.write(self.original_code)
166+
155167
def test_codetf_output(self, codetf_schema):
156168
"""
157169
Tests correct codetf output.
158170
"""
159171

160-
with open(self.code_path, "r", encoding="utf-8") as f: # type: ignore
161-
original_code = f.read()
162-
163172
command = [
164173
"codemodder",
165174
self.code_dir,
@@ -175,6 +184,8 @@ def test_codetf_output(self, codetf_schema):
175184
if self.sonar_hotspots_json:
176185
command.append(f"--sonar-hotspots-json={self.sonar_hotspots_json}")
177186

187+
self.write_original_code()
188+
178189
completed_process = subprocess.run(
179190
command,
180191
check=False,
@@ -189,7 +200,7 @@ def test_codetf_output(self, codetf_schema):
189200
# check that the original file is not rewritten
190201
with open(self.code_path, "r", encoding="utf-8") as f:
191202
original_file_code = f.read()
192-
assert original_file_code == original_code
203+
assert original_file_code == self.original_code
193204

194205
def apply_hunk_to_lines(self, lines, hunk):
195206
# The hunk target line numbers are 1-indexed.
@@ -266,7 +277,7 @@ def apply_diff(self, diff_str, original_str):
266277
return "".join(patched_lines)
267278

268279
def _get_patched_code_for_each_change(self) -> list[str]:
269-
with open(self.output_path, "r", encoding="utf-8") as f:
280+
with open(self.output_path, "r", encoding="utf-8") as f: # type: ignore
270281
codetf = json.load(f)
271282
changes = codetf["results"][0]["changeset"]
272283
patched_codes = []
@@ -349,7 +360,7 @@ def _assert_run_fields(self, run, output_path):
349360
assert run["elapsed"] != ""
350361
assert run[
351362
"commandLine"
352-
] == f'codemodder {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
363+
] == f'codemodder_hardening {self.code_dir} --output {output_path} --codemod-include={self.codemod_instance.id} --path-include={self.code_filename} --path-exclude=""' + (
353364
f" --sonar-issues-json={self.sonar_issues_json}"
354365
if self.sonar_issues_json
355366
else ""
@@ -438,7 +449,7 @@ def test_file_rewritten(self, codetf_schema):
438449
output files
439450
"""
440451
command = [
441-
"codemodder",
452+
"codemodder_hardening",
442453
self.code_dir,
443454
"--output",
444455
self.output_path,
@@ -510,6 +521,15 @@ def setup_class(cls):
510521
cls.code_dir = SAMPLES_DIR
511522
cls.code_filename = os.path.relpath(cls.code_path, SAMPLES_DIR)
512523

524+
if cls.original_code is not NotImplementedError:
525+
# Some tests are easier to understand with the expected new code provided
526+
# instead of calculated
527+
cls.original_code = dedent(cls.original_code).strip("\n")
528+
else:
529+
with open(cls.code_path, "r", encoding="utf-8") as f: # type: ignore
530+
cls.original_code = f.read()
531+
print(cls.original_code)
532+
513533
# TODO: support sonar integration tests that add a dependency to
514534
# `requirements_file_name`. These tests would not be able to run
515535
# in parallel at this time since they would all override the same

0 commit comments

Comments
 (0)