summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIb Lundgren <ib.lundgren@gmail.com>2012-11-17 12:46:09 +0100
committerIb Lundgren <ib.lundgren@gmail.com>2012-11-17 12:46:09 +0100
commitbef0fae821e2aa09caee777f0f47b7383d3c46a2 (patch)
tree677792e97ff91d5a02c6f893f8c26ade3e8401ae
parentac4e62ef9b4a045c4dcea72f80be32090726db66 (diff)
downloadoauthlib-bef0fae821e2aa09caee777f0f47b7383d3c46a2.tar.gz
An initial batch of OAuth 2 provider tests
-rw-r--r--tests/oauth2/draft25/test_grant_types.py184
-rw-r--r--tests/oauth2/draft25/test_server.py11
2 files changed, 195 insertions, 0 deletions
diff --git a/tests/oauth2/draft25/test_grant_types.py b/tests/oauth2/draft25/test_grant_types.py
new file mode 100644
index 0000000..091ec9a
--- /dev/null
+++ b/tests/oauth2/draft25/test_grant_types.py
@@ -0,0 +1,184 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import, unicode_literals
+from ...unittest import TestCase
+
+import json
+import mock
+from oauthlib import common
+from oauthlib.common import Request
+from oauthlib.oauth2.draft25.errors import UnsupportedGrantTypeError
+from oauthlib.oauth2.draft25.errors import InvalidRequestError
+from oauthlib.oauth2.draft25.errors import UnauthorizedClientError
+from oauthlib.oauth2.draft25.errors import InvalidGrantError
+from oauthlib.oauth2.draft25.grant_types import AuthorizationCodeGrant
+from oauthlib.oauth2.draft25.grant_types import ImplicitGrant
+from oauthlib.oauth2.draft25.grant_types import ResourceOwnerPasswordCredentialsGrant
+from oauthlib.oauth2.draft25.grant_types import ClientCredentialsGrant
+from oauthlib.oauth2.draft25.tokens import BearerToken
+
+
+class RequestValidatorTest(TestCase):
+
+ def test_client_id(self):
+ pass
+
+ def test_client(self):
+ pass
+
+ def test_response_type(self):
+ pass
+
+ def test_scopes(self):
+ pass
+
+ def test_redirect_uri(self):
+ pass
+
+
+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'
+
+ mock_validator = mock.MagicMock()
+ self.auth = AuthorizationCodeGrant(request_validator=mock_validator)
+
+ 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()
+ bearer.save_token = mock.MagicMock()
+ token = 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.validate_client = mock.MagicMock(return_value=False)
+ request.code = 'waffles'
+ request.client = 'batman'
+ self.assertRaises(UnauthorizedClientError,
+ auth.validate_token_request, request)
+
+ mock_validator.validate_client = mock.MagicMock(return_value=True)
+ mock_validator.validate_code = mock.MagicMock(return_value=False)
+ self.assertRaises(InvalidGrantError,
+ auth.validate_token_request, request)
+
+
+class ImplicitGrantTest(TestCase):
+
+ def setUp(self):
+ self.request = Request('http://a.b/path')
+ self.request.scopes = ('hello', 'world')
+ self.request.client = 'batman'
+ 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()
+ bearer.save_token = mock.MagicMock()
+ orig_generate_token = common.generate_token
+ self.addCleanup(setattr, common, 'generage_token', orig_generate_token)
+ common.generate_token = lambda *args, **kwargs: '1234'
+ uri, headers, body = self.auth.create_token_response(
+ self.request, bearer)
+ correct_uri = 'https://b.c/p#access_token=1234&token_type=Bearer&expires_in=3600&state=xyz&scope=hello+world'
+ self.assertURLEqual(uri, correct_uri, parse_fragment=True)
+
+ def test_error_response(self):
+ pass
+
+
+class ResourceOwnerPasswordCredentialsGrantTest(TestCase):
+
+ def setUp(self):
+ self.request = Request('http://a.b/path')
+ self.request.grant_type = 'password'
+ self.request.username = 'john'
+ self.request.password = 'doe'
+ self.request.client = 'mock authenticated'
+ 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()
+ bearer.save_token = mock.MagicMock()
+ uri, headers, body = 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
+
+
+class ClientCredentialsGrantTest(TestCase):
+
+ def setUp(self):
+ self.request = Request('http://a.b/path')
+ self.request.grant_type = 'client_credentials'
+ self.request.client = 'mock authenticated'
+ 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()
+ bearer.save_token = mock.MagicMock()
+ uri, headers, body = 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_validate_token_response(self):
+ # wrong grant type, scope
+ pass
diff --git a/tests/oauth2/draft25/test_server.py b/tests/oauth2/draft25/test_server.py
new file mode 100644
index 0000000..3e0429b
--- /dev/null
+++ b/tests/oauth2/draft25/test_server.py
@@ -0,0 +1,11 @@
+# -*- coding: utf-8 -*-
+from __future__ import absolute_import
+from ...unittest import TestCase
+
+
+class AuthorizationEndpoint(TestCase):
+ pass
+
+
+class TokenEndpoint(TestCase):
+ pass