Skip to content

Commit f00ae35

Browse files
committed
Fixed more unit tests
1 parent 9cd8ba3 commit f00ae35

File tree

6 files changed

+330
-12
lines changed

6 files changed

+330
-12
lines changed

tests/codemods/defectdojo/semgrep/test_avoid_insecure_deserialization.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def test_pickle_load(self, adds_dependency, tmpdir):
6262
6363
result = fickling.load("data")
6464
"""
65-
6665
findings = {
6766
"results": [
6867
{
@@ -100,6 +99,31 @@ def test_pickle_and_yaml(self, adds_dependency, tmpdir):
10099
result = fickling.load("data")
101100
result = yaml.load("data", Loader=yaml.SafeLoader)
102101
"""
102+
expected_diff_per_change = [
103+
"""\
104+
---
105+
+++
106+
@@ -1,6 +1,6 @@
107+
108+
-import pickle
109+
import yaml
110+
+import fickling
111+
112+
-result = pickle.load("data")
113+
+result = fickling.load("data")
114+
result = yaml.load("data")
115+
""",
116+
"""\
117+
---
118+
+++
119+
@@ -3,4 +3,4 @@
120+
import yaml
121+
122+
result = pickle.load("data")
123+
-result = yaml.load("data")
124+
+result = yaml.load("data", Loader=yaml.SafeLoader)
125+
""",
126+
]
103127

104128
findings = {
105129
"results": [
@@ -122,18 +146,19 @@ def test_pickle_and_yaml(self, adds_dependency, tmpdir):
122146
tmpdir,
123147
input_code,
124148
expected,
149+
expected_diff_per_change,
125150
results=json.dumps(findings),
126151
num_changes=2,
127152
)
128153
adds_dependency.assert_called_once_with(Fickling)
129154

130155
assert changes is not None
131156
assert changes[0].changes[0].fixedFindings is not None
132-
assert changes[0].changes[0].fixedFindings[0].id == "4"
157+
assert changes[0].changes[0].fixedFindings[0].id == "3"
133158
assert changes[0].changes[0].fixedFindings[0].rule.id == RULE_ID
134-
assert changes[0].changes[1].fixedFindings is not None
135-
assert changes[0].changes[1].fixedFindings[0].id == "3"
136-
assert changes[0].changes[1].fixedFindings[0].rule.id == RULE_ID
159+
assert changes[1].changes[0].fixedFindings is not None
160+
assert changes[1].changes[0].fixedFindings[0].id == "4"
161+
assert changes[1].changes[0].fixedFindings[0].rule.id == RULE_ID
137162

138163
@mock.patch("codemodder.codemods.api.FileContext.add_dependency")
139164
def test_pickle_loads(self, adds_dependency, tmpdir):

tests/codemods/sonar/test_sonar_enable_jinja2_autoescape.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@ def test_simple(self, tmpdir):
2424
env = Environment(autoescape=True)
2525
env = Environment(autoescape=True)
2626
"""
27+
expected_diff_per_change = [
28+
"""\
29+
---
30+
+++
31+
@@ -1,4 +1,4 @@
32+
33+
from jinja2 import Environment
34+
-env = Environment()
35+
+env = Environment(autoescape=True)
36+
env = Environment(autoescape=False)
37+
""",
38+
"""\
39+
---
40+
+++
41+
@@ -1,4 +1,4 @@
42+
43+
from jinja2 import Environment
44+
env = Environment()
45+
-env = Environment(autoescape=False)
46+
+env = Environment(autoescape=True)
47+
""",
48+
]
49+
2750
hotspots = {
2851
"hotspots": [
2952
{
@@ -54,6 +77,7 @@ def test_simple(self, tmpdir):
5477
tmpdir,
5578
input_code,
5679
expected_output,
80+
expected_diff_per_change,
5781
results=json.dumps(hotspots),
5882
num_changes=2,
5983
)

tests/codemods/sonar/test_sonar_secure_cookie.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,55 @@ def test_simple(self, tmpdir):
3434
var = "hello"
3535
response2.set_cookie("name", "value", secure=True, httponly=True, samesite='Lax')
3636
"""
37+
expected_diff_per_change = [
38+
"""\
39+
---
40+
+++
41+
@@ -3,7 +3,7 @@
42+
43+
response = flask.make_response()
44+
var = "hello"
45+
-response.set_cookie("name", "value")
46+
+response.set_cookie("name", "value", secure=True, httponly=True, samesite='Lax')
47+
48+
response2 = flask.Response()
49+
var = "hello"
50+
""",
51+
"""\
52+
---
53+
+++
54+
@@ -7,4 +7,4 @@
55+
56+
response2 = flask.Response()
57+
var = "hello"
58+
-response2.set_cookie("name", "value")
59+
+response2.set_cookie("name", "value", secure=True, httponly=True, samesite='Lax')
60+
""",
61+
"""\
62+
---
63+
+++
64+
@@ -3,7 +3,7 @@
65+
66+
response = flask.make_response()
67+
var = "hello"
68+
-response.set_cookie("name", "value")
69+
+response.set_cookie("name", "value", secure=True, httponly=True, samesite='Lax')
70+
71+
response2 = flask.Response()
72+
var = "hello"
73+
""",
74+
"""\
75+
---
76+
+++
77+
@@ -7,4 +7,4 @@
78+
79+
response2 = flask.Response()
80+
var = "hello"
81+
-response2.set_cookie("name", "value")
82+
+response2.set_cookie("name", "value", secure=True, httponly=True, samesite='Lax')
83+
""",
84+
]
85+
3786
issues = {
3887
"hotspots": [
3988
{
@@ -83,5 +132,10 @@ def test_simple(self, tmpdir):
83132
],
84133
}
85134
self.run_and_assert(
86-
tmpdir, input_code, expected, results=json.dumps(issues), num_changes=2
135+
tmpdir,
136+
input_code,
137+
expected,
138+
expected_diff_per_change,
139+
results=json.dumps(issues),
140+
num_changes=4,
87141
)

tests/codemods/sonar/test_sonar_secure_random.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,48 @@ def test_simple(self, tmpdir):
2626
secrets.SystemRandom().randint(0, 9)
2727
secrets.SystemRandom().random()
2828
"""
29+
expected_diff_per_change = [
30+
"""\
31+
---
32+
+++
33+
@@ -1,6 +1,7 @@
34+
35+
import random
36+
+import secrets
37+
38+
-random.getrandbits(1)
39+
+secrets.SystemRandom().getrandbits(1)
40+
random.randint(0, 9)
41+
random.random()
42+
""",
43+
"""\
44+
---
45+
+++
46+
@@ -1,6 +1,7 @@
47+
48+
import random
49+
+import secrets
50+
51+
random.getrandbits(1)
52+
-random.randint(0, 9)
53+
+secrets.SystemRandom().randint(0, 9)
54+
random.random()
55+
""",
56+
"""\
57+
---
58+
+++
59+
@@ -1,6 +1,7 @@
60+
61+
import random
62+
+import secrets
63+
64+
random.getrandbits(1)
65+
random.randint(0, 9)
66+
-random.random()
67+
+secrets.SystemRandom().random()
68+
""",
69+
]
70+
2971
hotspots = {
3072
"hotspots": [
3173
{
@@ -67,6 +109,7 @@ def test_simple(self, tmpdir):
67109
tmpdir,
68110
input_code,
69111
expected_output,
112+
expected_diff_per_change,
70113
results=json.dumps(hotspots),
71114
num_changes=3,
72115
)

tests/codemods/test_fix_hasattr_call.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,75 @@ class Test:
3737
if callable(obj):
3838
print(1)
3939
"""
40-
self.run_and_assert(tmpdir, input_code, expected, num_changes=5)
40+
expected_diff_per_change = [
41+
"""\
42+
---
43+
+++
44+
@@ -2,7 +2,7 @@
45+
class Test:
46+
pass
47+
48+
-hasattr(Test(), "__call__")
49+
+callable(Test())
50+
hasattr("hi", '__call__')
51+
52+
assert hasattr(1, '__call__')
53+
""",
54+
"""\
55+
---
56+
+++
57+
@@ -3,7 +3,7 @@
58+
pass
59+
60+
hasattr(Test(), "__call__")
61+
-hasattr("hi", '__call__')
62+
+callable("hi")
63+
64+
assert hasattr(1, '__call__')
65+
obj = Test()
66+
""",
67+
"""\
68+
---
69+
+++
70+
@@ -5,7 +5,7 @@
71+
hasattr(Test(), "__call__")
72+
hasattr("hi", '__call__')
73+
74+
-assert hasattr(1, '__call__')
75+
+assert callable(1)
76+
obj = Test()
77+
var = hasattr(obj, "__call__")
78+
79+
""",
80+
"""\
81+
---
82+
+++
83+
@@ -7,7 +7,7 @@
84+
85+
assert hasattr(1, '__call__')
86+
obj = Test()
87+
-var = hasattr(obj, "__call__")
88+
+var = callable(obj)
89+
90+
if hasattr(obj, "__call__"):
91+
print(1)
92+
""",
93+
"""\
94+
---
95+
+++
96+
@@ -9,5 +9,5 @@
97+
obj = Test()
98+
var = hasattr(obj, "__call__")
99+
100+
-if hasattr(obj, "__call__"):
101+
+if callable(obj):
102+
print(1)
103+
""",
104+
]
105+
106+
self.run_and_assert(
107+
tmpdir, input_code, expected, expected_diff_per_change, num_changes=5
108+
)
41109

42110
def test_other_hasattr(self, tmpdir):
43111
code = """

0 commit comments

Comments
 (0)