-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,52 @@ | ||
import pytest | ||
|
||
from unittest.mock import MagicMock, Mock, patch | ||
from unittest.mock import MagicMock, patch | ||
|
||
from .client import Client | ||
from .client import Client, _get_api_key, _get_endpoint | ||
|
||
|
||
class TestClient: | ||
@patch("posit.connect.client.Users") | ||
@patch("posit.connect.client.Session") | ||
@patch("posit.connect.client.ConfigBuilder") | ||
@patch("posit.connect.client.Auth") | ||
def test_init(self, Auth: MagicMock, ConfigBuilder: MagicMock, Session: MagicMock): | ||
def test_init(self, Auth: MagicMock, Session: MagicMock, Users: MagicMock): | ||
api_key = "foobar" | ||
endpoint = "http://foo.bar" | ||
config = Mock() | ||
config.api_key = api_key | ||
builder = ConfigBuilder.return_value | ||
builder.set_api_key = Mock() | ||
builder.set_endpoint = Mock() | ||
builder.build = Mock(return_value=config) | ||
client = Client(api_key=api_key, endpoint=endpoint) | ||
ConfigBuilder.assert_called_once() | ||
builder.set_api_key.assert_called_once_with(api_key) | ||
builder.set_endpoint.assert_called_once_with(endpoint) | ||
builder.build.assert_called_once() | ||
assert client._api_key == api_key | ||
assert client._endpoint == endpoint | ||
Session.assert_called_once() | ||
Auth.assert_called_once_with(api_key) | ||
assert client._config == config | ||
Users.assert_called_once_with(endpoint, Session.return_value) | ||
|
||
@patch("posit.connect.client.ConfigBuilder") | ||
def test_init_without_api_key(self, ConfigBuilder: MagicMock): | ||
api_key = None | ||
endpoint = "http://foo.bar" | ||
config = Mock() | ||
config.api_key = api_key | ||
config.endpoint = endpoint | ||
builder = ConfigBuilder.return_value | ||
builder.set_api_key = Mock() | ||
builder.set_endpoint = Mock() | ||
builder.build = Mock(return_value=config) | ||
|
||
class TestGetApiKey: | ||
@patch.dict("os.environ", {"CONNECT_API_KEY": "foobar"}) | ||
def test_get_api_key(self): | ||
api_key = _get_api_key() | ||
assert api_key == "foobar" | ||
|
||
@patch.dict("os.environ", {"CONNECT_API_KEY": ""}) | ||
def test_get_api_key_empty(self): | ||
with pytest.raises(ValueError): | ||
Client(api_key=api_key, endpoint=endpoint) | ||
_get_api_key() | ||
|
||
@patch("posit.connect.client.ConfigBuilder") | ||
def test_init_without_endpoint(self, ConfigBuilder: MagicMock): | ||
api_key = "foobar" | ||
endpoint = None | ||
config = Mock() | ||
config.api_key = api_key | ||
config.endpoint = endpoint | ||
builder = ConfigBuilder.return_value | ||
builder.set_api_key = Mock() | ||
builder.set_endpoint = Mock() | ||
builder.build = Mock(return_value=config) | ||
def test_get_api_key_miss(self): | ||
with pytest.raises(ValueError): | ||
_get_api_key() | ||
|
||
|
||
class TestGetEndpoint: | ||
@patch.dict("os.environ", {"CONNECT_SERVER": "http://foo.bar"}) | ||
def test_get_endpoint(self): | ||
endpoint = _get_endpoint() | ||
assert endpoint == "http://foo.bar" | ||
|
||
@patch.dict("os.environ", {"CONNECT_SERVER": ""}) | ||
def test_get_endpoint_empty(self): | ||
with pytest.raises(ValueError): | ||
_get_endpoint() | ||
|
||
def test_get_endpoint_miss(self): | ||
with pytest.raises(ValueError): | ||
Client(api_key=api_key, endpoint=endpoint) | ||
_get_endpoint() |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters