From 08f8fb4ff264dd52ba8562788d051ebeac02a32a Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Tue, 6 Feb 2024 11:09:24 -0300 Subject: [PATCH] generalize unit tests for requests-verify codemod --- tests/codemods/test_request_verify.py | 165 ++++++++++++++------------ 1 file changed, 87 insertions(+), 78 deletions(-) diff --git a/tests/codemods/test_request_verify.py b/tests/codemods/test_request_verify.py index 503c4d4e..0f7c7e7a 100644 --- a/tests/codemods/test_request_verify.py +++ b/tests/codemods/test_request_verify.py @@ -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): @@ -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)