Skip to content

Commit c149f36

Browse files
authored
Merge pull request #61 from adobe-apiplatform/v2
prepare for v2.10 release
2 parents 1c0eee5 + e0c0e61 commit c149f36

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

HISTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,10 @@ Bug fix release:
126126

127127
* [Issue 58](https://github.com/adobe-apiplatform/umapi-client.py/issues/58)
128128
* Error when adding more than 10 groups in a single action step.
129+
130+
### Version 2.10
131+
132+
Enhancement release:
133+
134+
* [Issue 33](https://github.com/adobe-apiplatform/umapi-client.py/issues/33)
135+
* Allow connection configuration via environment variables.

tests/test_connections.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
import time
2222
from email.utils import formatdate
2323

24-
import six
2524
import mock
2625
import pytest
2726
import requests
27+
import six
2828

2929
from conftest import mock_connection_params, MockResponse
30-
from umapi_client import Connection, UnavailableError, ServerError, RequestError, UserAction, GroupTypes, \
31-
IdentityTypes, RoleTypes
30+
31+
from umapi_client import Connection
32+
from umapi_client import ArgumentError, UnavailableError, ServerError, RequestError
33+
from umapi_client import UserAction, GroupTypes, IdentityTypes, RoleTypes
3234
from umapi_client import __version__ as umapi_version
35+
from umapi_client.auth import Auth
3336

3437

3538
def test_remote_status_success():
@@ -78,6 +81,35 @@ def test_ua_string_additional():
7881
assert ua_header.startswith("additional/1.0 umapi-client/" + umapi_version)
7982

8083

84+
def test_mock_proxy_get():
85+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
86+
mock_get.return_value = MockResponse(200, body=["test", "body"])
87+
with mock.patch("umapi_client.connection.os.getenv") as mock_getenv:
88+
mock_getenv.return_value = "proxy"
89+
conn = Connection(**mock_connection_params)
90+
conn.make_call("").json()
91+
mock_get.assert_called_with('http://test/', auth='N/A', timeout=120.0)
92+
93+
94+
def test_mock_playback_get():
95+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
96+
mock_get.return_value = MockResponse(200, body=["test", "body"])
97+
with mock.patch("umapi_client.connection.os.getenv") as mock_getenv:
98+
mock_getenv.return_value = "playback"
99+
conn = Connection(**mock_connection_params)
100+
conn.make_call("").json()
101+
assert mock_get.call_args[0][0] == 'http://test/'
102+
assert isinstance(mock_get.call_args[1]['auth'], Auth)
103+
104+
105+
def test_mock_proxy_get():
106+
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
107+
mock_get.return_value = MockResponse(200, body=["test", "body"])
108+
with mock.patch("umapi_client.connection.os.getenv") as mock_getenv:
109+
mock_getenv.return_value = "error"
110+
pytest.raises(ArgumentError, Connection, tuple(), mock_connection_params)
111+
112+
81113
def test_get_success():
82114
with mock.patch("umapi_client.connection.requests.Session.get") as mock_get:
83115
mock_get.return_value = MockResponse(200, body=["test", "body"])

umapi_client/connection.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import json
2222
import logging
23+
import os
2324
from email.utils import parsedate_tz, mktime_tz
2425
from platform import python_version, version as platform_version
2526
from random import randint
@@ -39,6 +40,7 @@ class Connection:
3940
An org-specific, authorized connection to the UMAPI service. Each method
4041
makes a single call on the endpoint and returns the result (or raises an error).
4142
"""
43+
mock_env_var = "UMAPI_MOCK"
4244

4345
def __init__(self,
4446
org_id,
@@ -98,6 +100,21 @@ def __init__(self,
98100
Additional keywords are allowed to make it easy to pass a big dictionary with other values
99101
:param kwargs: any keywords passed that we ignore.
100102
"""
103+
# for testing we mock the server, either by using an http relay
104+
# which relays and records the requests and responses, or by
105+
# using a robot which plays back a previously recorded run.
106+
mock_spec = os.getenv(self.mock_env_var, None)
107+
if mock_spec:
108+
if mock_spec not in ["proxy", "playback"]:
109+
raise ArgumentError("Unknown value for %s: %s" % (self.mock_env_var, mock_spec))
110+
if logger: logger.warning("%s override specified as '%s'", self.mock_env_var, mock_spec)
111+
# mocked servers don't support https
112+
if user_management_endpoint.lower().startswith("https://"):
113+
user_management_endpoint = "http" + user_management_endpoint[5:]
114+
# playback servers don't use authentication/authorization
115+
if mock_spec == "playback":
116+
auth = Auth("mock", "mock")
117+
101118
self.org_id = str(org_id)
102119
self.endpoint = user_management_endpoint
103120
self.test_mode = test_mode

umapi_client/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
# SOFTWARE.
2020

21-
__version__ = "2.9"
21+
__version__ = "2.10"

0 commit comments

Comments
 (0)