def test_rest_methods(mock_session, hostname): mock_get = mock.MagicMock() mock_post = mock.MagicMock() mock_put = mock.MagicMock() mock_delete = mock.MagicMock() mock_session.return_value.get = mock_get mock_session.return_value.post = mock_post mock_session.return_value.put = mock_put mock_session.return_value.delete = mock_delete my_session = pyxis_session.PyxisSession(hostname) my_session.get("items", params={"param1": "value1"}) mock_get.assert_called_once_with(hostname + "v1/items", params={"param1": "value1"}) my_session = pyxis_session.PyxisSession(hostname) my_session.post("add-item", data={"param2": "value2"}) mock_post.assert_called_once_with(urljoin(hostname, "v1/add-item"), data={"param2": "value2"}) my_session = pyxis_session.PyxisSession(hostname) my_session.put("edit-item", data={"param3": "value3"}) mock_put.assert_called_once_with(urljoin(hostname, "v1/edit-item"), data={"param3": "value3"}) my_session = pyxis_session.PyxisSession(hostname) my_session.delete("rm-item", params={"param4": "value4"}) mock_delete.assert_called_once_with(urljoin(hostname, "v1/rm-item"), params={"param4": "value4"})
def test_delete_container_signatures_server_error(hostname): ids = ["g1g1g1g1", "h2h2h2h2"] with requests_mock.Mocker() as m: m.delete(urljoin(hostname, "/v1/signatures/id/g1g1g1g1"), status_code=500) m.delete(urljoin(hostname, "/v1/signatures/id/h2h2h2h2")) my_client = pyxis_client.PyxisClient(hostname, 5, None, 3, True) with pytest.raises(requests.exceptions.HTTPError, match="500 Server Error.*"): my_client.delete_container_signatures(ids) assert len(m.request_history) == 1 assert m.request_history[0].url == urljoin( hostname, "/v1/signatures/id/g1g1g1g1" )
def test_delete_container_signatures_tolerate_404(hostname): ids = ["g1g1g1g1", "h2h2h2h2"] with requests_mock.Mocker() as m: m.delete(urljoin(hostname, "/v1/signatures/id/g1g1g1g1"), status_code=404) m.delete(urljoin(hostname, "/v1/signatures/id/h2h2h2h2")) my_client = pyxis_client.PyxisClient(hostname, 5, None, 3, True) my_client.delete_container_signatures(ids) assert len(m.request_history) == 2 assert m.request_history[0].url == urljoin( hostname, "/v1/signatures/id/g1g1g1g1" ) assert m.request_history[1].url == urljoin( hostname, "/v1/signatures/id/h2h2h2h2" )
def test_upload_signature_file(capsys): hostname = "https://pyxis.remote.host/" data_file_path = "@tests/data/signatures.json" responses = json.loads(load_response("post_signatures_ok")) args = [ "dummy", "--pyxis-server", hostname, "--pyxis-ssl-crtfile", "/root/name.crt", "--pyxis-ssl-keyfile", "/root/name.key", "--signatures", data_file_path, ] with requests_mock.Mocker() as m: m.post( urljoin(hostname, "/v1/signatures"), [{ "status_code": 200, "json": resp } for resp in responses], ) retval = pyxis_ops.upload_signatures_main(args) assert len(retval) == len(responses) assert all(ret_item in responses for ret_item in retval) out, _ = capsys.readouterr() assert all(resp["signature_data"] in out for resp in responses)
def test_upload_signature_json(capsys, hostname): items_to_upload = load_data("signatures") responses = json.loads(load_response("post_signatures_ok")) args = [ "dummy", "--pyxis-server", hostname, "--pyxis-ssl-crtfile", "/root/name.crt", "--pyxis-ssl-keyfile", "/root/name.key", "--signatures", items_to_upload, ] with requests_mock.Mocker() as m: m.post( urljoin(hostname, "/v1/signatures"), [{ "status_code": 200, "json": resp } for resp in responses], ) retval = pyxis_ops.upload_signatures_main(args) assert len(retval) == len(responses) assert all(ret_item in responses for ret_item in retval) out, _ = capsys.readouterr() assert all(resp["signature_data"] in out for resp in responses)