summaryrefslogtreecommitdiff
path: root/tests/oauth2/rfc6749/grant_types
diff options
context:
space:
mode:
authorIb Lundgren <ib.lundgren@gmail.com>2013-09-12 10:05:33 +0100
committerIb Lundgren <ib.lundgren@gmail.com>2013-09-12 10:05:33 +0100
commit62058f2d031d91bb6173fe06a1f6f11e22a9f03e (patch)
tree91f947a737da32e53ef5bd16500285440499fbc8 /tests/oauth2/rfc6749/grant_types
parent1122945efbf3d1be6fed0e2279dfb81f785ad706 (diff)
downloadoauthlib-62058f2d031d91bb6173fe06a1f6f11e22a9f03e.tar.gz
Restructure OAuth2 tests.
Diffstat (limited to 'tests/oauth2/rfc6749/grant_types')
-rw-r--r--tests/oauth2/rfc6749/grant_types/__init__.py0
-rw-r--r--tests/oauth2/rfc6749/grant_types/test_authorization_code.py78
-rw-r--r--tests/oauth2/rfc6749/grant_types/test_client_credentials.py39
-rw-r--r--tests/oauth2/rfc6749/grant_types/test_implicit.py41
-rw-r--r--tests/oauth2/rfc6749/grant_types/test_refresh_token.py74
-rw-r--r--tests/oauth2/rfc6749/grant_types/test_resource_owner_password.py41
6 files changed, 273 insertions, 0 deletions
diff --git a/tests/oauth2/rfc6749/grant_types/__init__.py b/tests/oauth2/rfc6749/grant_types/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/oauth2/rfc6749/grant_types/__init__.py
diff --git a/tests/oauth2/rfc6749/grant_types/test_authorization_code.py b/tests/oauth2/rfc6749/grant_types/test_authorization_code.py
new file mode 100644
index 0000000..a9c3e51
--- /dev/null
+++ b/tests/oauth2/rfc6749/grant_types/test_authorization_code.py
@@ -0,0 +1,78 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+from ....unittest import TestCase
+
+import json
+import mock
+from oauthlib.common import Request
+from oauthlib.oauth2.rfc6749.errors import UnsupportedGrantTypeError
+from oauthlib.oauth2.rfc6749.errors import InvalidRequestError
+from oauthlib.oauth2.rfc6749.errors import InvalidClientError
+from oauthlib.oauth2.rfc6749.errors import InvalidGrantError
+from oauthlib.oauth2.rfc6749.grant_types import AuthorizationCodeGrant
+from oauthlib.oauth2.rfc6749.tokens import BearerToken
+
+
+class AuthorizationCodeGrantTest(TestCase):
+
+ def setUp(self):
+ self.request = Request('http://a.b/path')
+ self.request.scopes = ('hello', 'world')
+ self.request.expires_in = 1800
+ self.request.client = 'batman'
+ self.request.client_id = 'abcdef'
+ self.request.code = '1234'
+ self.request.response_type = 'code'
+ self.request.grant_type = 'authorization_code'
+
+ self.request_state = Request('http://a.b/path')
+ self.request_state.state = 'abc'
+
+ self.mock_validator = mock.MagicMock()
+ self.mock_validator.authenticate_client.side_effect = self.set_client
+ self.auth = AuthorizationCodeGrant(request_validator=self.mock_validator)
+
+ def set_client(self, request):
+ request.client = mock.MagicMock()
+ request.client.client_id = 'mocked'
+ return True
+
+ def test_create_authorization_grant(self):
+ grant = self.auth.create_authorization_code(self.request)
+ self.assertIn('code', grant)
+
+ grant = self.auth.create_authorization_code(self.request_state)
+ self.assertIn('code', grant)
+ self.assertIn('state', grant)
+
+ def test_create_token_response(self):
+ bearer = BearerToken(self.mock_validator)
+ h, token, s = self.auth.create_token_response(self.request, bearer)
+ token = json.loads(token)
+ self.assertIn('access_token', token)
+ self.assertIn('refresh_token', token)
+ self.assertIn('expires_in', token)
+ self.assertIn('scope', token)
+
+ def test_validate_token_request(self):
+ mock_validator = mock.MagicMock()
+ auth = AuthorizationCodeGrant(request_validator=mock_validator)
+ request = Request('http://a.b/path')
+ self.assertRaises(UnsupportedGrantTypeError,
+ auth.validate_token_request, request)
+
+ request.grant_type = 'authorization_code'
+ self.assertRaises(InvalidRequestError,
+ auth.validate_token_request, request)
+
+ mock_validator.authenticate_client.return_value = False
+ mock_validator.authenticate_client_id.return_value = False
+ request.code = 'waffles'
+ self.assertRaises(InvalidClientError,
+ auth.validate_token_request, request)
+
+ request.client = 'batman'
+ mock_validator.authenticate_client = self.set_client
+ mock_validator.validate_code.return_value = False
+ self.assertRaises(InvalidGrantError,
+ auth.validate_token_request, request)
diff --git a/tests/oauth2/rfc6749/grant_types/test_client_credentials.py b/tests/oauth2/rfc6749/grant_types/test_client_credentials.py
new file mode 100644
index 0000000..80b92d3
--- /dev/null
+++ b/tests/oauth2/rfc6749/grant_types/test_client_credentials.py
@@ -0,0 +1,39 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+from ....unittest import TestCase
+
+import json
+import mock
+from oauthlib.common import Request
+from oauthlib.oauth2.rfc6749.grant_types import ClientCredentialsGrant
+from oauthlib.oauth2.rfc6749.tokens import BearerToken
+
+
+class ClientCredentialsGrantTest(TestCase):
+
+ def setUp(self):
+ mock_client = mock.MagicMock()
+ mock_client.user.return_value = 'mocked user'
+ self.request = Request('http://a.b/path')
+ self.request.grant_type = 'client_credentials'
+ self.request.client = mock_client
+ self.request.scopes = ('mocked', 'scopes')
+ self.mock_validator = mock.MagicMock()
+ self.auth = ClientCredentialsGrant(
+ request_validator=self.mock_validator)
+
+ def test_create_token_response(self):
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertIn('access_token', token)
+ self.assertIn('token_type', token)
+ self.assertIn('expires_in', token)
+
+ def test_error_response(self):
+ pass
+
+ def test_validate_token_response(self):
+ # wrong grant type, scope
+ pass
diff --git a/tests/oauth2/rfc6749/grant_types/test_implicit.py b/tests/oauth2/rfc6749/grant_types/test_implicit.py
new file mode 100644
index 0000000..df30c9a
--- /dev/null
+++ b/tests/oauth2/rfc6749/grant_types/test_implicit.py
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+from ....unittest import TestCase
+
+import mock
+from oauthlib import common
+from oauthlib.common import Request
+from oauthlib.oauth2.rfc6749.grant_types import ImplicitGrant
+from oauthlib.oauth2.rfc6749.tokens import BearerToken
+
+
+class ImplicitGrantTest(TestCase):
+
+ def setUp(self):
+ mock_client = mock.MagicMock()
+ mock_client.user.return_value = 'mocked user'
+ self.request = Request('http://a.b/path')
+ self.request.scopes = ('hello', 'world')
+ self.request.client = mock_client
+ self.request.client_id = 'abcdef'
+ self.request.response_type = 'token'
+ self.request.state = 'xyz'
+ self.request.redirect_uri = 'https://b.c/p'
+
+ self.mock_validator = mock.MagicMock()
+ self.auth = ImplicitGrant(request_validator=self.mock_validator)
+
+ def test_create_token_response(self):
+ bearer = BearerToken(self.mock_validator, expires_in=1800)
+ orig_generate_token = common.generate_token
+ self.addCleanup(setattr, common, 'generate_token', orig_generate_token)
+ common.generate_token = lambda *args, **kwargs: '1234'
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ correct_uri = 'https://b.c/p#access_token=1234&token_type=Bearer&expires_in=1800&state=xyz&scope=hello+world'
+ self.assertEqual(status_code, 302)
+ self.assertIn('Location', headers)
+ self.assertURLEqual(headers['Location'], correct_uri, parse_fragment=True)
+
+ def test_error_response(self):
+ pass
diff --git a/tests/oauth2/rfc6749/grant_types/test_refresh_token.py b/tests/oauth2/rfc6749/grant_types/test_refresh_token.py
new file mode 100644
index 0000000..25c261c
--- /dev/null
+++ b/tests/oauth2/rfc6749/grant_types/test_refresh_token.py
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+from ....unittest import TestCase
+
+import json
+import mock
+from oauthlib.common import Request
+from oauthlib.oauth2.rfc6749.grant_types import RefreshTokenGrant
+from oauthlib.oauth2.rfc6749.tokens import BearerToken
+
+
+class RefreshTokenGrantTest(TestCase):
+
+ def setUp(self):
+ mock_client = mock.MagicMock()
+ mock_client.user.return_value = 'mocked user'
+ self.request = Request('http://a.b/path')
+ self.request.grant_type = 'refresh_token'
+ self.request.refresh_token = 'lsdkfhj230'
+ self.request.client = mock_client
+ self.request.scope = 'foo'
+ self.mock_validator = mock.MagicMock()
+ self.auth = RefreshTokenGrant(
+ request_validator=self.mock_validator)
+
+ def test_create_token_response(self):
+ self.mock_validator.get_original_scopes.return_value = ['foo', 'bar']
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertIn('access_token', token)
+ self.assertIn('token_type', token)
+ self.assertIn('expires_in', token)
+ self.assertEqual(token['scope'], 'foo')
+
+ def test_create_token_inherit_scope(self):
+ self.request.scope = None
+ self.mock_validator.get_original_scopes.return_value = ['foo', 'bar']
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertIn('access_token', token)
+ self.assertIn('token_type', token)
+ self.assertIn('expires_in', token)
+ self.assertEqual(token['scope'], 'foo bar')
+
+ def test_invalid_scope(self):
+ self.mock_validator.get_original_scopes.return_value = ['baz']
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertEqual(token['error'], 'invalid_scope')
+ self.assertEqual(status_code, 401)
+
+ def test_invalid_token(self):
+ self.mock_validator.validate_refresh_token.return_value = False
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertEqual(token['error'], 'invalid_grant')
+ self.assertEqual(status_code, 400)
+
+ def test_invalid_client(self):
+ self.mock_validator.authenticate_client.return_value = False
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertEqual(token['error'], 'invalid_client')
+ self.assertEqual(status_code, 401)
diff --git a/tests/oauth2/rfc6749/grant_types/test_resource_owner_password.py b/tests/oauth2/rfc6749/grant_types/test_resource_owner_password.py
new file mode 100644
index 0000000..aaea440
--- /dev/null
+++ b/tests/oauth2/rfc6749/grant_types/test_resource_owner_password.py
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+from ....unittest import TestCase
+
+import json
+import mock
+from oauthlib.common import Request
+from oauthlib.oauth2.rfc6749.grant_types import ResourceOwnerPasswordCredentialsGrant
+from oauthlib.oauth2.rfc6749.tokens import BearerToken
+
+
+class ResourceOwnerPasswordCredentialsGrantTest(TestCase):
+
+ def setUp(self):
+ mock_client = mock.MagicMock()
+ mock_client.user.return_value = 'mocked user'
+ self.request = Request('http://a.b/path')
+ self.request.grant_type = 'password'
+ self.request.username = 'john'
+ self.request.password = 'doe'
+ self.request.client = mock_client
+ self.request.scopes = ('mocked', 'scopes')
+ self.mock_validator = mock.MagicMock()
+ self.auth = ResourceOwnerPasswordCredentialsGrant(
+ request_validator=self.mock_validator)
+
+ def test_create_token_response(self):
+ bearer = BearerToken(self.mock_validator)
+ headers, body, status_code = self.auth.create_token_response(
+ self.request, bearer)
+ token = json.loads(body)
+ self.assertIn('access_token', token)
+ self.assertIn('token_type', token)
+ self.assertIn('expires_in', token)
+ self.assertIn('refresh_token', token)
+
+ def test_error_response(self):
+ pass
+
+ def test_scopes(self):
+ pass