Skip to content

Commit 7d2e224

Browse files
authored
SG-13267: Config fixes for Mockgun (#215)
scheme, server and api_path are now set on the config object in Mockgun.
1 parent d0d134d commit 7d2e224

3 files changed

Lines changed: 55 additions & 8 deletions

File tree

shotgun_api3/lib/mockgun/mockgun.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def __init__(self,
193193
# they way they would expect to in the real API.
194194
self.config = _Config(self)
195195

196+
self.config.set_server_params(base_url)
197+
196198
# load in the shotgun schema to associate with this Shotgun
197199
(schema_path, schema_entity_path) = self.get_schema_paths()
198200

shotgun_api3/shotgun.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,30 @@ def __init__(self, sg):
429429
self.no_ssl_validation = False
430430
self.localized = False
431431

432+
def set_server_params(self, base_url):
433+
"""
434+
Set the different server related fields based on the passed in URL.
435+
436+
This will impact the following attributes:
437+
438+
- scheme: http or https
439+
- api_path: usually /api3/json
440+
- server: usually something.shotgunstudio.com
441+
442+
:param str base_url: The server URL.
443+
444+
:raises ValueError: Raised if protocol is not http or https.
445+
"""
446+
self.scheme, self.server, api_base, _, _ = \
447+
urllib.parse.urlsplit(base_url)
448+
if self.scheme not in ("http", "https"):
449+
raise ValueError(
450+
"base_url must use http or https got '%s'" % base_url
451+
)
452+
self.api_path = urllib.parse.urljoin(urllib.parse.urljoin(
453+
api_base or "/", self.api_ver + "/"), "json"
454+
)
455+
432456
@property
433457
def records_per_page(self):
434458
"""
@@ -646,17 +670,14 @@ def __init__(self,
646670
self.__ca_certs = os.environ.get("SHOTGUN_API_CACERTS")
647671

648672
self.base_url = (base_url or "").lower()
649-
self.config.scheme, self.config.server, api_base, _, _ = \
650-
urllib.parse.urlsplit(self.base_url)
651-
if self.config.scheme not in ("http", "https"):
652-
raise ValueError("base_url must use http or https got '%s'" %
653-
self.base_url)
654-
self.config.api_path = urllib.parse.urljoin(urllib.parse.urljoin(
655-
api_base or "/", self.config.api_ver + "/"), "json")
673+
self.config.set_server_params(self.base_url)
656674

657675
# if the service contains user information strip it out
658676
# copied from the xmlrpclib which turned the user:password into
659677
# and auth header
678+
# Do NOT urlsplit(self.base_url) here, as it contains the lower case version
679+
# of the base_url argument. Doing so would base64-encode the lowercase
680+
# version of the credentials.
660681
auth, self.config.server = urllib.parse.splituser(urllib.parse.urlsplit(base_url).netloc)
661682
if auth:
662683
auth = base64.encodestring(six.ensure_binary(urllib.parse.unquote(auth))).decode("utf-8")
@@ -2054,7 +2075,7 @@ def schema_field_update(self, entity_type, field_name, properties, project_entit
20542075
:param properties: Dictionary with key/value pairs where the key is the property to be
20552076
updated and the value is the new value.
20562077
:param dict project_entity: Optional Project entity specifying which project to modify the
2057-
``visible`` property for. If the ``visible`` is present in ``properties`` and
2078+
``visible`` property for. If ``visible`` is present in ``properties`` and
20582079
``project_entity`` is not set, an exception will be raised. Example:
20592080
``{'type': 'Project', 'id': 3}``
20602081
:returns: ``True`` if the field was updated.

tests/test_mockgun.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,5 +433,29 @@ def test_invalid_operator(self):
433433
)
434434

435435

436+
class TestConfig(unittest.TestCase):
437+
"""
438+
Tests the shotgun._Config class
439+
"""
440+
441+
def test_set_server_params_with_regular_url(self):
442+
"""
443+
Make sure it works with a normal URL.
444+
"""
445+
mockgun = Mockgun("https://server.shotgunstudio.com/")
446+
self.assertEqual(mockgun.config.scheme, "https")
447+
self.assertEqual(mockgun.config.server, "server.shotgunstudio.com")
448+
self.assertEqual(mockgun.config.api_path, "/api3/json")
449+
450+
def test_set_server_params_with_url_with_path(self):
451+
"""
452+
Make sure it works with a URL with a path
453+
"""
454+
mockgun = Mockgun("https://local/something/")
455+
self.assertEqual(mockgun.config.scheme, "https")
456+
self.assertEqual(mockgun.config.server, "local")
457+
self.assertEqual(mockgun.config.api_path, "/something/api3/json")
458+
459+
436460
if __name__ == '__main__':
437461
unittest.main()

0 commit comments

Comments
 (0)