Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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))
elif options.username and options.password:
if options.netrc:
logging.warning("--netrc option ignored")
if options.digest_auth:
auth = HTTPDigestAuth(options.username, options.password)
else:
auth = HTTPBasicAuth(options.username, options.password)
elif options.netrc:
if options.digest_auth:
auth = HTTPDigestAuthFromNetrc(url=options.gerrit_url)
else:
auth = HTTPBasicAuthFromNetrc(url=options.gerrit_url)
else:
auth = None
rest = GerritRestAPI(url=options.gerrit_url, auth=auth)
try:
query = ["status:open"]
if auth:
query += ["owner:self"]
else:
query += ["limit:10"]
changes = rest.get("/changes/?q=%s" % "%20".join(query))
logging.info("%d changes", len(changes))
for change in changes:
logging.info(change["change_id"])
except RequestException as err:
logging.error("Error: %s", str(err))