diff options
-rw-r--r-- | tests/oauth2/draft25/test_client.py | 56 | ||||
-rw-r--r-- | tests/oauth2/draft25/test_parameters.py | 14 | ||||
-rw-r--r-- | tests/test_common.py | 30 | ||||
-rw-r--r-- | tests/unittest/__init__.py | 34 |
4 files changed, 84 insertions, 50 deletions
diff --git a/tests/oauth2/draft25/test_client.py b/tests/oauth2/draft25/test_client.py index 63e2f92..5741a78 100644 --- a/tests/oauth2/draft25/test_client.py +++ b/tests/oauth2/draft25/test_client.py @@ -37,8 +37,8 @@ class ClientTest(TestCase): client = Client(self.client_id, access_token=self.access_token) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers) - self.assertEqual(uri, self.uri) - self.assertEqual(body, self.body) + self.assertURLEqual(uri, self.uri) + self.assertFormBodyEqual(body, self.body) self.assertEqual(headers, self.bearer_header) # Setting default placements of tokens @@ -46,46 +46,46 @@ class ClientTest(TestCase): default_token_placement=AUTH_HEADER) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers) - self.assertEqual(uri, self.uri) - self.assertEqual(body, self.body) + self.assertURLEqual(uri, self.uri) + self.assertFormBodyEqual(body, self.body) self.assertEqual(headers, self.bearer_header) client = Client(self.client_id, access_token=self.access_token, default_token_placement=URI_QUERY) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers) - self.assertEqual(uri, self.bearer_query) - self.assertEqual(body, self.body) + self.assertURLEqual(uri, self.bearer_query) + self.assertFormBodyEqual(body, self.body) self.assertEqual(headers, self.headers) client = Client(self.client_id, access_token=self.access_token, default_token_placement=BODY) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers) - self.assertEqual(uri, self.uri) - self.assertEqual(body, self.bearer_body) + self.assertURLEqual(uri, self.uri) + self.assertFormBodyEqual(body, self.bearer_body) self.assertEqual(headers, self.headers) # Asking for specific placement in the add_token method client = Client(self.client_id, access_token=self.access_token) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers, token_placement=AUTH_HEADER) - self.assertEqual(uri, self.uri) - self.assertEqual(body, self.body) + self.assertURLEqual(uri, self.uri) + self.assertFormBodyEqual(body, self.body) self.assertEqual(headers, self.bearer_header) client = Client(self.client_id, access_token=self.access_token) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers, token_placement=URI_QUERY) - self.assertEqual(uri, self.bearer_query) - self.assertEqual(body, self.body) + self.assertURLEqual(uri, self.bearer_query) + self.assertFormBodyEqual(body, self.body) self.assertEqual(headers, self.headers) client = Client(self.client_id, access_token=self.access_token) uri, headers, body = client.add_token(self.uri, body=self.body, headers=self.headers, token_placement=BODY) - self.assertEqual(uri, self.uri) - self.assertEqual(body, self.bearer_body) + self.assertURLEqual(uri, self.uri) + self.assertFormBodyEqual(body, self.bearer_body) self.assertEqual(headers, self.headers) # Invalid token placement @@ -146,19 +146,19 @@ class WebApplicationClientTest(TestCase): # Basic, no extra arguments uri = client.prepare_request_uri(self.uri) - self.assertEqual(uri, self.uri_id) + self.assertURLEqual(uri, self.uri_id) # With redirection uri uri = client.prepare_request_uri(self.uri, redirect_uri=self.redirect_uri) - self.assertEqual(uri, self.uri_redirect) + self.assertURLEqual(uri, self.uri_redirect) # With scope uri = client.prepare_request_uri(self.uri, scope=self.scope) - self.assertEqual(uri, self.uri_scope) + self.assertURLEqual(uri, self.uri_scope) # With state uri = client.prepare_request_uri(self.uri, state=self.state) - self.assertEqual(uri, self.uri_state) + self.assertURLEqual(uri, self.uri_state) # With extra parameters through kwargs, checking using len since order # of dict items is undefined @@ -170,15 +170,15 @@ class WebApplicationClientTest(TestCase): # Basic, no extra arguments body = client.prepare_request_body(body=self.body) - self.assertEqual(body, self.body_code) + self.assertFormBodyEqual(body, self.body_code) rclient = WebApplicationClient(self.client_id) body = rclient.prepare_request_body(code=self.code, body=self.body) - self.assertEqual(body, self.body_code) + self.assertFormBodyEqual(body, self.body_code) # With redirection uri body = client.prepare_request_body(body=self.body, redirect_uri=self.redirect_uri) - self.assertEqual(body, self.body_redirect) + self.assertFormBodyEqual(body, self.body_redirect) # With extra parameters, checked using length since order of # dict items is undefined @@ -248,19 +248,19 @@ class UserAgentClientTest(TestCase): # Basic, no extra arguments uri = client.prepare_request_uri(self.uri) - self.assertEqual(uri, self.uri_id) + self.assertURLEqual(uri, self.uri_id) # With redirection uri uri = client.prepare_request_uri(self.uri, redirect_uri=self.redirect_uri) - self.assertEqual(uri, self.uri_redirect) + self.assertURLEqual(uri, self.uri_redirect) # With scope uri = client.prepare_request_uri(self.uri, scope=self.scope) - self.assertEqual(uri, self.uri_scope) + self.assertURLEqual(uri, self.uri_scope) # With state uri = client.prepare_request_uri(self.uri, state=self.state) - self.assertEqual(uri, self.uri_state) + self.assertURLEqual(uri, self.uri_state) # With extra parameters through kwargs, checking using len since order # of dict items is undefined @@ -318,7 +318,7 @@ class PasswordCredentialsClientTest(TestCase): # Basic, no extra arguments body = client.prepare_request_body(body=self.body) - self.assertEqual(body, self.body_up) + self.assertFormBodyEqual(body, self.body_up) # With extra parameters, checked using length since order of # dict items is undefined @@ -372,11 +372,11 @@ class ClientCredentialsClientTest(TestCase): # Basic, no extra arguments body = client.prepare_request_body(body=self.body) - self.assertEqual(body, self.body_up) + self.assertFormBodyEqual(body, self.body_up) rclient = ClientCredentialsClient(self.client_id) body = rclient.prepare_request_body(body=self.body) - self.assertEqual(body, self.body_up) + self.assertFormBodyEqual(body, self.body_up) # With extra parameters, checked using length since order of # dict items is undefined diff --git a/tests/oauth2/draft25/test_parameters.py b/tests/oauth2/draft25/test_parameters.py index 5289126..45bab30 100644 --- a/tests/oauth2/draft25/test_parameters.py +++ b/tests/oauth2/draft25/test_parameters.py @@ -114,16 +114,16 @@ class ParameterTests(TestCase): def test_prepare_grant_uri(self): """Verify correct authorization URI construction.""" - self.assertEqual(prepare_grant_uri(**self.auth_grant), self.auth_grant_uri) - self.assertEqual(prepare_grant_uri(**self.auth_grant_list_scope), self.auth_grant_uri_list_scope) - self.assertEqual(prepare_grant_uri(**self.auth_implicit), self.auth_implicit_uri) - self.assertEqual(prepare_grant_uri(**self.auth_implicit_list_scope), self.auth_implicit_uri_list_scope) + self.assertURLEqual(prepare_grant_uri(**self.auth_grant), self.auth_grant_uri) + self.assertURLEqual(prepare_grant_uri(**self.auth_grant_list_scope), self.auth_grant_uri_list_scope) + self.assertURLEqual(prepare_grant_uri(**self.auth_implicit), self.auth_implicit_uri) + self.assertURLEqual(prepare_grant_uri(**self.auth_implicit_list_scope), self.auth_implicit_uri_list_scope) def test_prepare_token_request(self): """Verify correct access token request body construction.""" - self.assertEqual(prepare_token_request(**self.grant_body), self.auth_grant_body) - self.assertEqual(prepare_token_request(**self.pwd_body), self.password_body) - self.assertEqual(prepare_token_request(**self.cred_grant), self.cred_body) + self.assertFormBodyEqual(prepare_token_request(**self.grant_body), self.auth_grant_body) + self.assertFormBodyEqual(prepare_token_request(**self.pwd_body), self.password_body) + self.assertFormBodyEqual(prepare_token_request(**self.cred_grant), self.cred_body) def test_grant_response(self): """Verify correct parameter parsing and validation for auth code responses.""" diff --git a/tests/test_common.py b/tests/test_common.py index 85945ee..46556bf 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -11,14 +11,14 @@ class CommonTests(TestCase): uri = 'http://www.someuri.com' def test_urldecode(self): - self.assertEqual(urldecode(''), []) - self.assertEqual(urldecode('='), [('', '')]) - self.assertEqual(urldecode('%20'), [(' ', '')]) - self.assertEqual(urldecode('+'), [(' ', '')]) - self.assertEqual(urldecode('c2'), [('c2', '')]) - self.assertEqual(urldecode('c2='), [('c2', '')]) - self.assertEqual(urldecode('foo=bar'), [('foo', 'bar')]) - self.assertEqual(urldecode('foo_%20~=.bar-'), [('foo_ ~', '.bar-')]) + self.assertItemsEqual(urldecode(''), []) + self.assertItemsEqual(urldecode('='), [('', '')]) + self.assertItemsEqual(urldecode('%20'), [(' ', '')]) + self.assertItemsEqual(urldecode('+'), [(' ', '')]) + self.assertItemsEqual(urldecode('c2'), [('c2', '')]) + self.assertItemsEqual(urldecode('c2='), [('c2', '')]) + self.assertItemsEqual(urldecode('foo=bar'), [('foo', 'bar')]) + self.assertItemsEqual(urldecode('foo_%20~=.bar-'), [('foo_ ~', '.bar-')]) self.assertRaises(ValueError, urldecode, 'foo bar') self.assertRaises(ValueError, urldecode, '?') self.assertRaises(ValueError, urldecode, '%R') @@ -27,19 +27,19 @@ class CommonTests(TestCase): self.assertRaises(ValueError, urldecode, '%RR') def test_extract_params_dict(self): - self.assertEqual(extract_params(self.params_dict), self.params_twotuple) + self.assertItemsEqual(extract_params(self.params_dict), self.params_twotuple) def test_extract_params_twotuple(self): - self.assertEqual(extract_params(self.params_twotuple), self.params_twotuple) + self.assertItemsEqual(extract_params(self.params_twotuple), self.params_twotuple) def test_extract_params_formencoded(self): - self.assertEqual(extract_params(self.params_formencoded), self.params_twotuple) + self.assertItemsEqual(extract_params(self.params_formencoded), self.params_twotuple) def test_extract_params_blank_string(self): - self.assertEqual(extract_params(''), []) + self.assertItemsEqual(extract_params(''), []) def test_extract_params_empty_list(self): - self.assertEqual(extract_params([]), []) + self.assertItemsEqual(extract_params([]), []) def test_extract_non_formencoded_string(self): self.assertEqual(extract_params('not a formencoded string'), None) @@ -75,11 +75,11 @@ class CommonTests(TestCase): def test_list_body(self): r = Request(self.uri, body=self.params_twotuple) - self.assertEqual(r.decoded_body, self.params_twotuple) + self.assertItemsEqual(r.decoded_body, self.params_twotuple) def test_dict_body(self): r = Request(self.uri, body=self.params_dict) - self.assertEqual(r.decoded_body, self.params_twotuple) + self.assertItemsEqual(r.decoded_body, self.params_twotuple) def test_generate_timestamp(self): """ TODO: Better test here """ diff --git a/tests/unittest/__init__.py b/tests/unittest/__init__.py index 9d9fafa..d715c2d 100644 --- a/tests/unittest/__init__.py +++ b/tests/unittest/__init__.py @@ -1,4 +1,8 @@ import sys +try: + import urlparse +except ImportError: + import urllib.parse as urlparse try: # check the system path first @@ -10,5 +14,35 @@ except ImportError: else: raise +# Python 3.1 does not provide assertIsInstance if sys.version_info[1] == 1: TestCase.assertIsInstance = lambda self, obj, cls: self.assertTrue(isinstance(obj, cls)) + +# Python 3 does not provide assertItemsEqual +# TODO (ib-lundgren): Find out why and what their recommended alternative is +if sys.version_info[0] == 3: + TestCase.assertItemsEqual = lambda self, a, b: self.assertEqual(sorted(a), sorted(b)) + + +# URL comparison where query param order is insignifcant +def url_equals(self, a, b): + parsed_a = urlparse.urlparse(a) + parsed_b = urlparse.urlparse(b) + query_a = urlparse.parse_qsl(a) + query_b = urlparse.parse_qsl(b) + self.assertEqual(parsed_a.scheme, parsed_b.scheme) + self.assertEqual(parsed_a.netloc, parsed_b.netloc) + self.assertEqual(parsed_a.path, parsed_b.path) + self.assertEqual(parsed_a.params, parsed_b.params) + self.assertEqual(parsed_a.fragment, parsed_b.fragment) + self.assertEqual(parsed_a.username, parsed_b.username) + self.assertEqual(parsed_a.password, parsed_b.password) + self.assertEqual(parsed_a.hostname, parsed_b.hostname) + self.assertEqual(parsed_a.port, parsed_b.port) + self.assertItemsEqual(query_a, query_b) + +TestCase.assertURLEqual = url_equals + +# Form body comparison where order is insignificant +TestCase.assertFormBodyEqual = lambda self, a, b: self.assertItemsEqual( + urlparse.parse_qsl(a), urlparse.parse_qsl(b)) |