Repository navigation
Expand file tree
/
Copy pathtest_request_verify.py
More file actions
103 lines (80 loc) · 2.87 KB
/
Copy pathtest_request_verify.py
File metadata and controls
103 lines (80 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pytest
from core_codemods.requests_verify import RequestsVerify
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest
REQUEST_FUNCS = ["get", "post", "request"]
class TestRequestsVerify(BaseSemgrepCodemodTest):
codemod = RequestsVerify
def test_name(self):
assert self.codemod.name == "requests-verify"
@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_default_verify(self, tmpdir, func):
input_code = f"""import requests
requests.{func}("www.google.com")
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, input_code)
@pytest.mark.parametrize("func", REQUEST_FUNCS)
@pytest.mark.parametrize("verify_val", ["True", "'/some/path'"])
def test_verify(self, tmpdir, verify_val, func):
input_code = f"""import requests
requests.{func}("www.google.com", verify={verify_val})
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, input_code)
@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_import(self, tmpdir, func):
input_code = f"""import requests
requests.{func}("www.google.com", verify=False)
var = "hello"
"""
expected = f"""import requests
requests.{func}("www.google.com", verify=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_from_import(self, tmpdir, func):
input_code = f"""from requests import {func}
{func}("www.google.com", verify=False)
var = "hello"
"""
expected = f"""from requests import {func}
{func}("www.google.com", verify=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_multifunctions(self, tmpdir, func):
input_code = f"""import requests
requests.{func}("www.google.com", verify=False)
requests.HTTPError()
var = "hello"
"""
expected = f"""import requests
requests.{func}("www.google.com", verify=True)
requests.HTTPError()
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_import_alias(self, tmpdir, func):
input_code = f"""import requests as req_mod
req_mod.{func}("www.google.com", verify=False)
var = "hello"
"""
expected = f"""import requests as req_mod
req_mod.{func}("www.google.com", verify=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)
@pytest.mark.parametrize("func", REQUEST_FUNCS)
def test_multiple_kwargs(self, tmpdir, func):
input_code = f"""import requests
requests.{func}("www.google.com", headers={{"Content-Type":"text"}}, verify=False)
var = "hello"
"""
expected = f"""import requests
requests.{func}("www.google.com", headers={{"Content-Type":"text"}}, verify=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)