From b770d49572b7b8755aa691b5509b321d04603415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Boismenu?= Date: Mon, 23 Sep 2019 13:22:54 -0400 Subject: [PATCH 1/6] fixed typos --- shotgun_api3/shotgun.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 423e728e..186dce02 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -2054,7 +2054,7 @@ def schema_field_update(self, entity_type, field_name, properties, project_entit :param properties: Dictionary with key/value pairs where the key is the property to be updated and the value is the new value. :param dict project_entity: Optional Project entity specifying which project to modify the - ``visible`` property for. If the ``visible`` is present in ``properties`` and + ``visible`` property for. If ``visible`` is present in ``properties`` and ``project_entity`` is not set, an exception will be raised. Example: ``{'type': 'Project', 'id': 3}`` :returns: ``True`` if the field was updated. From 06c0a5240e08ecfea3287808b5acc3eedec81088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Boismenu?= Date: Wed, 23 Oct 2019 12:46:13 -0400 Subject: [PATCH 2/6] updates a bunch of server related fields on the config object. --- shotgun_api3/lib/mockgun/mockgun.py | 2 ++ shotgun_api3/shotgun.py | 51 ++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/shotgun_api3/lib/mockgun/mockgun.py b/shotgun_api3/lib/mockgun/mockgun.py index e94d92b4..acabd8f8 100644 --- a/shotgun_api3/lib/mockgun/mockgun.py +++ b/shotgun_api3/lib/mockgun/mockgun.py @@ -193,6 +193,8 @@ def __init__(self, # they way they would expect to in the real API. self.config = _Config(self) + self.config.set_server_params(base_url) + # load in the shotgun schema to associate with this Shotgun (schema_path, schema_entity_path) = self.get_schema_paths() diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 186dce02..448ae844 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -429,6 +429,41 @@ def __init__(self, sg): self.no_ssl_validation = False self.localized = False + def set_server_params(self, base_url): + """ + Set the different server related fields based on the passed in URL. + + This will impact the following attributes: + + - scheme: http or https + - api_path: usually /api3/json + - server: usually something.shotgunstudio.com + - authorization: Basic user@pass, when user credentials + are passed in the url. + + :param str base_url: The server URL. + + :raises ValueError: Raised if protocol is not http or https. + """ + self.scheme, self.server, api_base, _, _ = \ + urllib.parse.urlsplit(base_url) + if self.scheme not in ("http", "https"): + raise ValueError( + "base_url must use http or https got '%s'" % base_url + ) + self.api_path = urllib.parse.urljoin(urllib.parse.urljoin( + api_base or "/", self.api_ver + "/"), "json" + ) + + # if the service contains user information strip it out + # copied from the xmlrpclib which turned the user:password into + # and auth header + auth, self.server = urllib.parse.splituser(urllib.parse.urlsplit(base_url).netloc) + if auth: + auth = base64.encodestring(six.ensure_binary(urllib.parse.unquote(auth))).decode("utf-8") + self.authorization = "Basic " + auth.strip() + + @property def records_per_page(self): """ @@ -646,21 +681,7 @@ def __init__(self, self.__ca_certs = os.environ.get("SHOTGUN_API_CACERTS") self.base_url = (base_url or "").lower() - self.config.scheme, self.config.server, api_base, _, _ = \ - urllib.parse.urlsplit(self.base_url) - if self.config.scheme not in ("http", "https"): - raise ValueError("base_url must use http or https got '%s'" % - self.base_url) - self.config.api_path = urllib.parse.urljoin(urllib.parse.urljoin( - api_base or "/", self.config.api_ver + "/"), "json") - - # if the service contains user information strip it out - # copied from the xmlrpclib which turned the user:password into - # and auth header - auth, self.config.server = urllib.parse.splituser(urllib.parse.urlsplit(base_url).netloc) - if auth: - auth = base64.encodestring(six.ensure_binary(urllib.parse.unquote(auth))).decode("utf-8") - self.config.authorization = "Basic " + auth.strip() + self.config.set_server_params(self.base_url) # foo:bar@123.456.789.012:3456 if http_proxy: From 17e5011c8139346d3e6201be8ea4658d13aaf42b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Boismenu?= Date: Wed, 23 Oct 2019 13:37:53 -0400 Subject: [PATCH 3/6] fixes tests --- tests/test_mockgun.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_mockgun.py b/tests/test_mockgun.py index 6efa5f27..c68f07a4 100644 --- a/tests/test_mockgun.py +++ b/tests/test_mockgun.py @@ -433,5 +433,41 @@ def test_invalid_operator(self): ) +class TestConfig(unittest.TestCase): + """ + Tests the shotgun._Config class + """ + + def test_set_server_params_with_regular_url(self): + """ + Make sure it works with a normal URL. + """ + mockgun = Mockgun("https://server.shotgunstudio.com/") + self.assertEqual(mockgun.config.scheme, "https") + self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") + self.assertEqual(mockgun.config.api_path, "/api3/json") + self.assertIsNone(mockgun.config.authorization) + + def test_set_server_params_with_url_with_path(self): + """ + Make sure it works with a URL with a path + """ + mockgun = Mockgun("https://local/something/") + self.assertEqual(mockgun.config.scheme, "https") + self.assertEqual(mockgun.config.server, "local") + self.assertEqual(mockgun.config.api_path, "/something/api3/json") + self.assertIsNone(mockgun.config.authorization) + + def test_set_server_with_basic_auth(self): + """ + Make sure it works with a URL than has some auth parameter. + """ + mockgun = Mockgun("https://user:pass@server.shotgunstudio.com/") + self.assertEqual(mockgun.config.scheme, "https") + self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") + self.assertEqual(mockgun.config.api_path, "/api3/json") + self.assertIsNotNone(mockgun.config.authorization) + + if __name__ == '__main__': unittest.main() From 78c849b05ffe89ec650bb2ed0837fcc6f2f91b4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Boismenu?= Date: Wed, 23 Oct 2019 13:41:19 -0400 Subject: [PATCH 4/6] fixing flake8 --- tests/test_mockgun.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_mockgun.py b/tests/test_mockgun.py index c68f07a4..f01a281a 100644 --- a/tests/test_mockgun.py +++ b/tests/test_mockgun.py @@ -443,8 +443,8 @@ def test_set_server_params_with_regular_url(self): Make sure it works with a normal URL. """ mockgun = Mockgun("https://server.shotgunstudio.com/") - self.assertEqual(mockgun.config.scheme, "https") - self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") + self.assertEqual(mockgun.config.scheme, "https") + self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") self.assertEqual(mockgun.config.api_path, "/api3/json") self.assertIsNone(mockgun.config.authorization) @@ -453,8 +453,8 @@ def test_set_server_params_with_url_with_path(self): Make sure it works with a URL with a path """ mockgun = Mockgun("https://local/something/") - self.assertEqual(mockgun.config.scheme, "https") - self.assertEqual(mockgun.config.server, "local") + self.assertEqual(mockgun.config.scheme, "https") + self.assertEqual(mockgun.config.server, "local") self.assertEqual(mockgun.config.api_path, "/something/api3/json") self.assertIsNone(mockgun.config.authorization) @@ -463,8 +463,8 @@ def test_set_server_with_basic_auth(self): Make sure it works with a URL than has some auth parameter. """ mockgun = Mockgun("https://user:pass@server.shotgunstudio.com/") - self.assertEqual(mockgun.config.scheme, "https") - self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") + self.assertEqual(mockgun.config.scheme, "https") + self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") self.assertEqual(mockgun.config.api_path, "/api3/json") self.assertIsNotNone(mockgun.config.authorization) From 285bf1f1067552e73e01d30b5bc36486bdc37d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Boismenu?= Date: Wed, 23 Oct 2019 14:16:33 -0400 Subject: [PATCH 5/6] python 2.6 compliance --- tests/test_mockgun.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_mockgun.py b/tests/test_mockgun.py index f01a281a..90c85eef 100644 --- a/tests/test_mockgun.py +++ b/tests/test_mockgun.py @@ -446,7 +446,7 @@ def test_set_server_params_with_regular_url(self): self.assertEqual(mockgun.config.scheme, "https") self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") self.assertEqual(mockgun.config.api_path, "/api3/json") - self.assertIsNone(mockgun.config.authorization) + self.assertEqual(mockgun.config.authorization, None) def test_set_server_params_with_url_with_path(self): """ @@ -456,7 +456,7 @@ def test_set_server_params_with_url_with_path(self): self.assertEqual(mockgun.config.scheme, "https") self.assertEqual(mockgun.config.server, "local") self.assertEqual(mockgun.config.api_path, "/something/api3/json") - self.assertIsNone(mockgun.config.authorization) + self.assertEqual(mockgun.config.authorization, None) def test_set_server_with_basic_auth(self): """ @@ -466,7 +466,7 @@ def test_set_server_with_basic_auth(self): self.assertEqual(mockgun.config.scheme, "https") self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") self.assertEqual(mockgun.config.api_path, "/api3/json") - self.assertIsNotNone(mockgun.config.authorization) + self.assertNotEqual(mockgun.config.authorization, None) if __name__ == '__main__': From 96fad623b24c6c99c27cf2c22471a1b4ba39a5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Boismenu?= Date: Wed, 23 Oct 2019 15:07:17 -0400 Subject: [PATCH 6/6] removes authorization refactoring --- shotgun_api3/shotgun.py | 22 +++++++++++----------- tests/test_mockgun.py | 12 ------------ 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/shotgun_api3/shotgun.py b/shotgun_api3/shotgun.py index 448ae844..142e445f 100755 --- a/shotgun_api3/shotgun.py +++ b/shotgun_api3/shotgun.py @@ -438,8 +438,6 @@ def set_server_params(self, base_url): - scheme: http or https - api_path: usually /api3/json - server: usually something.shotgunstudio.com - - authorization: Basic user@pass, when user credentials - are passed in the url. :param str base_url: The server URL. @@ -455,15 +453,6 @@ def set_server_params(self, base_url): api_base or "/", self.api_ver + "/"), "json" ) - # if the service contains user information strip it out - # copied from the xmlrpclib which turned the user:password into - # and auth header - auth, self.server = urllib.parse.splituser(urllib.parse.urlsplit(base_url).netloc) - if auth: - auth = base64.encodestring(six.ensure_binary(urllib.parse.unquote(auth))).decode("utf-8") - self.authorization = "Basic " + auth.strip() - - @property def records_per_page(self): """ @@ -683,6 +672,17 @@ def __init__(self, self.base_url = (base_url or "").lower() self.config.set_server_params(self.base_url) + # if the service contains user information strip it out + # copied from the xmlrpclib which turned the user:password into + # and auth header + # Do NOT urlsplit(self.base_url) here, as it contains the lower case version + # of the base_url argument. Doing so would base64-encode the lowercase + # version of the credentials. + auth, self.config.server = urllib.parse.splituser(urllib.parse.urlsplit(base_url).netloc) + if auth: + auth = base64.encodestring(six.ensure_binary(urllib.parse.unquote(auth))).decode("utf-8") + self.config.authorization = "Basic " + auth.strip() + # foo:bar@123.456.789.012:3456 if http_proxy: # check if we're using authentication. Start from the end since there might be diff --git a/tests/test_mockgun.py b/tests/test_mockgun.py index 90c85eef..ce851135 100644 --- a/tests/test_mockgun.py +++ b/tests/test_mockgun.py @@ -446,7 +446,6 @@ def test_set_server_params_with_regular_url(self): self.assertEqual(mockgun.config.scheme, "https") self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") self.assertEqual(mockgun.config.api_path, "/api3/json") - self.assertEqual(mockgun.config.authorization, None) def test_set_server_params_with_url_with_path(self): """ @@ -456,17 +455,6 @@ def test_set_server_params_with_url_with_path(self): self.assertEqual(mockgun.config.scheme, "https") self.assertEqual(mockgun.config.server, "local") self.assertEqual(mockgun.config.api_path, "/something/api3/json") - self.assertEqual(mockgun.config.authorization, None) - - def test_set_server_with_basic_auth(self): - """ - Make sure it works with a URL than has some auth parameter. - """ - mockgun = Mockgun("https://user:pass@server.shotgunstudio.com/") - self.assertEqual(mockgun.config.scheme, "https") - self.assertEqual(mockgun.config.server, "server.shotgunstudio.com") - self.assertEqual(mockgun.config.api_path, "/api3/json") - self.assertNotEqual(mockgun.config.authorization, None) if __name__ == '__main__':