From 5d1c65170b29ecd6c25d630b2334eb2d9428faf7 Mon Sep 17 00:00:00 2001 From: Jessica Negara Date: Thu, 4 Apr 2019 13:40:25 -0700 Subject: [PATCH 1/5] Import cleanup. --- tabpy-tools/tabpy_tools/rest.py | 4 ++-- tabpy-tools/tools_tests/test_client.py | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tabpy-tools/tabpy_tools/rest.py b/tabpy-tools/tabpy_tools/rest.py index 434066cc..0ee775d8 100755 --- a/tabpy-tools/tabpy_tools/rest.py +++ b/tabpy-tools/tabpy_tools/rest.py @@ -3,11 +3,11 @@ import requests from requests.auth import HTTPBasicAuth from re import compile +import json +import simplejson from collections import MutableMapping as _MutableMapping -import json as json - logger = logging.getLogger(__name__) diff --git a/tabpy-tools/tools_tests/test_client.py b/tabpy-tools/tools_tests/test_client.py index 86d30d72..c86dda9a 100644 --- a/tabpy-tools/tools_tests/test_client.py +++ b/tabpy-tools/tools_tests/test_client.py @@ -1,8 +1,6 @@ import unittest from unittest.mock import Mock -import requests - from tabpy_tools.client import Client @@ -77,3 +75,8 @@ def test_get_endpoint_upload_destination(self): {"path": "foo"} self.assertEqual(self.client._get_endpoint_upload_destination(), "foo") + + # TODO + def test_set_credentials(selfs): + pass + From ec7e075e1cffc7ea14a1701e3f815d0e32dcc0d4 Mon Sep 17 00:00:00 2001 From: Jessica Negara Date: Thu, 4 Apr 2019 13:41:24 -0700 Subject: [PATCH 2/5] Added unit tests for RequestsNetworkWrapper.set_credentials(...). --- tabpy-tools/tools_tests/test_rest.py | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tabpy-tools/tools_tests/test_rest.py b/tabpy-tools/tools_tests/test_rest.py index 5f9ca33a..68a2e576 100644 --- a/tabpy-tools/tools_tests/test_rest.py +++ b/tabpy-tools/tools_tests/test_rest.py @@ -3,6 +3,7 @@ import sys import unittest from unittest.mock import Mock +from requests.auth import HTTPBasicAuth from tabpy_tools.rest import (RequestsNetworkWrapper, ServiceClient) @@ -148,6 +149,64 @@ def test_DELETE_InvalidURL(self): e = sys.exc_info()[0] self.assertEqual(e, TypeError) + def test_set_credentials(self): + expected_auth = None + self.assertEqual(self.rnw.auth, expected_auth) + + username, password = 'username', 'password' + expected_auth = HTTPBasicAuth(username, password) + self.rnw.set_credentials(username, password) + self.assertEqual(self.rnw.auth, expected_auth) + + def _test_METHOD_with_credentials( + self, + http_method_function, + http_session_method_function, + headers=None, + params=False, + data=False, + response=None): + username, password = 'username', 'password' + self.rnw.set_credentials(username, password) + + url = 'url' + _data = {'foo': 'bar'} + + self.assertEqual(http_method_function(url, _data), response) + + pargs = {url} + kwargs = {'timeout': None, 'auth': self.rnw.auth} + if data: + kwargs['data'] = json.dumps(_data) + if headers: + kwargs['headers'] = headers + if params: + kwargs['params'] = _data + + http_session_method_function.assert_called_once_with(*pargs, **kwargs) + self.assertEqual(self.rnw.auth, HTTPBasicAuth(username, password)) + + def test_GET_with_credentials(self): + self._test_METHOD_with_credentials( + self.rnw.GET, + self.rnw.session.get, + params=True, + response='json') + + def test_POST_with_credentials(self): + self._test_METHOD_with_credentials( + self.rnw.POST, self.rnw.session.post, headers={ + 'content-type': 'application/json'}, data=True, response='json') + + def test_PUT_with_credentials(self): + self._test_METHOD_with_credentials( + self.rnw.PUT, self.rnw.session.put, data=True, headers={ + 'content-type': 'application/json'}, response='json') + + def test_DELETE_with_credentials(self): + self._test_METHOD_with_credentials( + self.rnw.DELETE, self.rnw.session.delete, data=True) + class TestServiceClient(unittest.TestCase): @@ -179,3 +238,7 @@ def test_DELETE(self): self.assertEqual(self.sc.DELETE('test'), None) self.sc.network_wrapper.DELETE.assert_called_once_with('endpoint/test', None, None) + + # TODO + def test_set_credentials(self): + pass From 4698aeec1ba1d0190b5865df1eaddf4c95ac1eb6 Mon Sep 17 00:00:00 2001 From: Jessica Negara Date: Thu, 4 Apr 2019 14:57:43 -0700 Subject: [PATCH 3/5] Added unit tests for set_credentials in Client and ServiceClient. --- tabpy-tools/tools_tests/test_client.py | 3 +-- tabpy-tools/tools_tests/test_rest.py | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tabpy-tools/tools_tests/test_client.py b/tabpy-tools/tools_tests/test_client.py index c86dda9a..d00c9c36 100644 --- a/tabpy-tools/tools_tests/test_client.py +++ b/tabpy-tools/tools_tests/test_client.py @@ -77,6 +77,5 @@ def test_get_endpoint_upload_destination(self): self.assertEqual(self.client._get_endpoint_upload_destination(), "foo") # TODO - def test_set_credentials(selfs): + def test_set_credentials(self): pass - diff --git a/tabpy-tools/tools_tests/test_rest.py b/tabpy-tools/tools_tests/test_rest.py index 68a2e576..bf37e6cb 100644 --- a/tabpy-tools/tools_tests/test_rest.py +++ b/tabpy-tools/tools_tests/test_rest.py @@ -239,6 +239,8 @@ def test_DELETE(self): self.sc.network_wrapper.DELETE.assert_called_once_with('endpoint/test', None, None) - # TODO def test_set_credentials(self): - pass + username, password = 'username', 'password' + self.sc.set_credentials(username, password) + self.sc.network_wrapper.set_credentials.assert_called_once_with( + username, password) From 7ad955b62b31667bbfd01ac7526a1633d7ab4436 Mon Sep 17 00:00:00 2001 From: Jessica Negara Date: Thu, 4 Apr 2019 15:05:58 -0700 Subject: [PATCH 4/5] Applied pep8 to file; added twisted test output to .gitignore. --- .gitignore | 1 + tabpy-tools/tools_tests/test_rest.py | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9bac616b..cfa814e9 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,7 @@ coverage.xml *.cover .hypothesis/ .pytest_cache/ +*_trial_temp* # Translations *.mo diff --git a/tabpy-tools/tools_tests/test_rest.py b/tabpy-tools/tools_tests/test_rest.py index bf37e6cb..6f798e87 100644 --- a/tabpy-tools/tools_tests/test_rest.py +++ b/tabpy-tools/tools_tests/test_rest.py @@ -195,8 +195,13 @@ def test_GET_with_credentials(self): def test_POST_with_credentials(self): self._test_METHOD_with_credentials( - self.rnw.POST, self.rnw.session.post, headers={ - 'content-type': 'application/json'}, data=True, response='json') + self.rnw.POST, + self.rnw.session.post, + headers={ + 'content-type': 'application/json' + }, + data=True, + response='json') def test_PUT_with_credentials(self): self._test_METHOD_with_credentials( From 7379f2c8f2185691494fe5e6aa7c22c4681bf047 Mon Sep 17 00:00:00 2001 From: Jessica Negara Date: Thu, 4 Apr 2019 15:12:05 -0700 Subject: [PATCH 5/5] Added unit test for Client.set_credentials(...). --- tabpy-tools/tools_tests/test_client.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tabpy-tools/tools_tests/test_client.py b/tabpy-tools/tools_tests/test_client.py index d00c9c36..1a7c18dc 100644 --- a/tabpy-tools/tools_tests/test_client.py +++ b/tabpy-tools/tools_tests/test_client.py @@ -54,7 +54,7 @@ def test_query(self): self.assertEqual(self.client.query("foo", 1, 2, 3), "ok") - self.client._service.query.sssert_called_once_with("foo", 1, 2, 3) + self.client._service.query.assert_called_once_with("foo", 1, 2, 3) self.client._service.query.reset_mock() @@ -76,6 +76,9 @@ def test_get_endpoint_upload_destination(self): self.assertEqual(self.client._get_endpoint_upload_destination(), "foo") - # TODO def test_set_credentials(self): - pass + username, password = "username", "password" + self.client.set_credentials(username, password) + + self.client._service.set_credentials.assert_called_once_with( + username, password)