Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def gerrit_api(request):
"""Create a Gerrit container for the given version and return an API."""
with GerritContainer(request.param) as gerrit:
port = gerrit.get_exposed_port(8080)
url = "http://localhost:%s" % port
api = GerritRestAPI(url=url, auth=Anonymous())
_initialize(api)
auth = HTTPBasicAuth("admin", "secret")
api = GerritRestAPI(url=url, auth=auth)
yield api
def test_kwargs_unchanged_when_no_data_or_json(self):
"""Test that `json` or `data` are not added when not passed."""
api = GerritRestAPI(url="http://review.example.com")
kwargs = {"a": "a", "b": "b"}
result = api.translate_kwargs(**kwargs)
assert "json" not in result
assert "data" not in result
assert "a" in result
assert "b" in result
assert "headers" in result
headers = result["headers"]
assert "Content-Type" not in headers
def test_default_to_basic_auth_from_netrc(self):
"""Test auth defaults to HTTP basic from netrc when not specified."""
with patch("pygerrit2.rest.auth._get_netrc_auth") as mock_netrc:
mock_netrc.return_value = ("netrcuser", "netrcpass")
api = GerritRestAPI(url="http://review.example.com")
assert isinstance(api.auth, HTTPBasicAuthFromNetrc)
assert api.url.endswith("/a/")
def test_invalid_auth_type(self):
"""Test that an exception is raised for invalid auth type."""
with self.assertRaises(ValueError) as exc:
GerritRestAPI(url="http://review.example.com", auth="foo")
assert re.search(r"Invalid auth type", str(exc.exception))
def test_json_no_side_effect_on_subsequent_call(self):
"""Test that subsequent call is not polluted with results of previous.
If the translate_kwargs method is called, resulting in the content-type
header being added, the header should not also be added on a subsequent
call that does not need it.
"""
api = GerritRestAPI(url="http://review.example.com")
json = {"a": "a"}
result = api.translate_kwargs(json=json)
assert "json" in result
assert "data" not in result
assert "headers" in result
headers = result["headers"]
assert "Content-Type" in headers
assert result["json"] == {"a": "a"}
assert headers["Content-Type"] == "application/json;charset=UTF-8"
kwargs = {"a": "a", "b": "b"}
result = api.translate_kwargs(**kwargs)
assert "json" not in result
assert "data" not in result
assert "a" in result
assert "b" in result
assert "headers" in result
def gerrit_api(request):
"""Create a Gerrit container for the given version and return an API."""
with GerritContainer(request.param) as gerrit:
port = gerrit.get_exposed_port(8080)
url = "http://localhost:%s" % port
api = GerritRestAPI(url=url, auth=Anonymous())
_initialize(api)
auth = HTTPBasicAuth("admin", "secret")
api = GerritRestAPI(url=url, auth=auth)
yield api
def test_explicit_anonymous_without_netrc(self):
"""Test explicit anonymous access when credentials are not in netrc."""
with patch("pygerrit2.rest.auth._get_netrc_auth") as mock_netrc:
mock_netrc.return_value = None
auth = Anonymous()
api = GerritRestAPI(url="http://review.example.com", auth=auth)
assert api.auth is None
assert not api.url.endswith("/a/")
def test_json_is_unchanged_and_header_added(self):
"""Test that `json` is unchanged and a Content-Type header is added."""
api = GerritRestAPI(url="http://review.example.com")
json = {"a": "a"}
result = api.translate_kwargs(json=json)
assert "json" in result
assert "data" not in result
assert "headers" in result
headers = result["headers"]
assert "Content-Type" in headers
assert result["json"] == {"a": "a"}
assert headers["Content-Type"] == "application/json;charset=UTF-8"
def test_data_and_json(self):
"""Test that `json` and `data` cannot be used at the same time."""
api = GerritRestAPI(url="http://review.example.com")
with self.assertRaises(ValueError) as exc:
api.translate_kwargs(data="d", json="j")
assert re.search(r"Cannot use data and json together", str(exc.exception))
)
self.assertEqual(
str(obj3), '{"comments": {"Makefile": [{"line": 10, "message": "test"}]}}'
)
obj4 = GerritReview(
labels={"Verified": 1, "Code-Review": -1},
comments=[{"filename": "Makefile", "line": 10, "message": "test"}],
)
self.assertEqual(
str(obj4),
'{"comments": {"Makefile": [{"line": 10, "message": "test"}]},'
' "labels": {"Code-Review": -1, "Verified": 1}}',
)
obj5 = GerritReview(
comments=[
{"filename": "Makefile", "line": 15, "message": "test"},
{"filename": "Make", "line": 10, "message": "test1"},
]
)
self.assertEqual(
str(obj5),
'{"comments": {"Make": [{"line": 10, "message": "test1"}],'
' "Makefile": [{"line": 15, "message": "test"}]}}',