def test_client_hit_url_extra_slash(): """The configured api url is ok even with an extra slash.""" with patch("charmcraft.commands.store.client._AuthHolder") as mock_auth: client = Client("https://local.test:1234/", "http://storage.test") client._hit("GET", "/somepath") mock_auth().request.assert_called_once_with( "GET", "https://local.test:1234/somepath", None )
def test_client_hit_failure(): """Hits the server, got a failure.""" response_value = "raw data" fake_response = FakeResponse(content=response_value, status_code=404) with patch("charmcraft.commands.store.client._AuthHolder") as mock_auth: mock_auth().request.return_value = fake_response client = Client("http://api.test", "http://storage.test") expected = r"Failure working with the Store: \[404\] 'raw data'" with pytest.raises(CommandError, match=expected): client._hit("GET", "/somepath")
def test_client_hit_success_without_json_parsing(): """Hits the server, all ok, return the raw response without parsing the json.""" response_value = "whatever test response" fake_response = FakeResponse(content=response_value, status_code=200) with patch("charmcraft.commands.store.client._AuthHolder") as mock_auth: mock_auth().request.return_value = fake_response client = Client("http://api.test", "http://storage.test") result = client._hit("GET", "/somepath", parse_json=False) mock_auth().request.assert_called_once_with("GET", "http://api.test/somepath", None) assert result == response_value
def test_client_hit_success_withbody(caplog): """Hits the server including a body, all ok.""" caplog.set_level(logging.DEBUG, logger="charmcraft.commands") response_value = {"foo": "bar"} fake_response = FakeResponse(content=json.dumps(response_value), status_code=200) with patch("charmcraft.commands.store.client._AuthHolder") as mock_auth: mock_auth().request.return_value = fake_response client = Client("http://api.test", "http://storage.test") result = client._hit("POST", "/somepath", "somebody") mock_auth().request.assert_called_once_with("POST", "http://api.test/somepath", "somebody") assert result == response_value expected = [ "Hitting the store: POST http://api.test/somepath somebody", "Store ok: 200", ] assert expected == [rec.message for rec in caplog.records]
def test_client_hit_success_withbody(caplog): """Hits the server including a body, all ok.""" caplog.set_level(logging.DEBUG, logger="charmcraft.commands") response_value = {"foo": "bar"} fake_response = FakeResponse(content=json.dumps(response_value), status_code=200) with patch('charmcraft.commands.store.client._AuthHolder') as mock_auth: mock_auth().request.return_value = fake_response client = Client() result = client._hit('POST', '/somepath', 'somebody') mock_auth().request.assert_called_once_with('POST', API_BASE_URL + '/somepath', 'somebody') assert result == response_value expected = [ "Hitting the store: POST {}/somepath somebody".format(API_BASE_URL), "Store ok: 200", ] assert expected == [rec.message for rec in caplog.records]
def test_client_hit_success_simple(caplog): """Hits the server, all ok.""" caplog.set_level(logging.DEBUG, logger="charmcraft.commands") response_value = {"foo": "bar"} fake_response = FakeResponse(content=json.dumps(response_value), status_code=200) with patch('charmcraft.commands.store.client._AuthHolder') as mock_auth: mock_auth().request.return_value = fake_response client = Client('http://api.test', 'http://storage.test') result = client._hit('GET', '/somepath') mock_auth().request.assert_called_once_with('GET', 'http://api.test/somepath', None) assert result == response_value expected = [ "Hitting the store: GET http://api.test/somepath None", "Store ok: 200", ] assert expected == [rec.message for rec in caplog.records]