Skip to content

Commit

Permalink
generalize unit tests for requests-verify codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Feb 6, 2024
1 parent ba8b12f commit 08f8fb4
Showing 1 changed file with 87 additions and 78 deletions.
165 changes: 87 additions & 78 deletions tests/codemods/test_request_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from core_codemods.requests_verify import RequestsVerify
from tests.codemods.base_codemod_test import BaseSemgrepCodemodTest

REQUEST_FUNCS = ["get", "post", "request"]
# todo: add stream, etc specific to httpx
each_func = pytest.mark.parametrize("func", ["get", "post", "request"])
each_library = pytest.mark.parametrize("library", ["requests", "httpx"])


class TestRequestsVerify(BaseSemgrepCodemodTest):
Expand All @@ -11,93 +13,100 @@ class TestRequestsVerify(BaseSemgrepCodemodTest):
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"
"""
@each_func
@each_library
def test_default_verify(self, tmpdir, library, func):
input_code = f"""
import {library}
{library}.{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"
"""
@each_func
@each_library
@pytest.mark.parametrize("verify_val", ["True", "'/some/palibrary, th'"])
def test_verify(self, tmpdir, verify_val, library, func):
input_code = f"""
import {library}
{library}.{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"
"""
@each_func
@each_library
def test_import(self, tmpdir, library, func):
input_code = f"""
import {library}
{library}.{func}("www.google.com", verify=False)
var = "hello"
"""
expected = f"""
import {library}
{library}.{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"
"""
@each_func
@each_library
def test_from_import(self, tmpdir, library, func):
input_code = f"""
from {library} import {func}
{func}("www.google.com", verify=False)
var = "hello"
"""
expected = f"""
from {library} 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"
"""
@each_func
@each_library
def test_multifunctions(self, tmpdir, library, func):
input_code = f"""
import {library}
{library}.{func}("www.google.com", verify=False)
{library}.HTTPError()
var = "hello"
"""
expected = f"""
import {library}
{library}.{func}("www.google.com", verify=True)
{library}.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"
"""
@each_func
@each_library
def test_import_alias(self, tmpdir, library, func):
input_code = f"""
import {library} as req_mod
req_mod.{func}("www.google.com", verify=False)
var = "hello"
"""
expected = f"""
import {library} 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"
"""
@each_func
@each_library
def test_multiple_kwargs(self, tmpdir, library, func):
input_code = f"""
import {library}
{library}.{func}("www.google.com", headers={{"Content-Type":"text"}}, verify=False)
var = "hello"
"""
expected = f"""
import {library}
{library}.{func}("www.google.com", headers={{"Content-Type":"text"}}, verify=True)
var = "hello"
"""
self.run_and_assert(tmpdir, input_code, expected)

0 comments on commit 08f8fb4

Please sign in to comment.