def setUp(self): self.oclient = Client(name="Test Client", access_host="http://localhost/") self.oclient.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="Scope for testing") self.scope.save() self.user = User(username="******", password="******", email="*****@*****.**") self.user.save() self.at = AuthorizationToken(client=self.oclient, user=self.user) self.at.save() self.at.scope = [self.scope] self.at.save() self.rt = self.at.generate_refresh_token() self.token = self.rt.generate_access_token() self.handler = BearerHandler() self.factory = RequestFactory()
def setUp(self): from django.contrib.auth.models import User self.user = User.objects.create_user("test", "*****@*****.**", "test") self.oauth_client = Client(name="Test Client", access_host="http://localhost/") self.oauth_client.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="This is a test scope.") self.scope.save()
class TestAuthorizationTokenModel(TestCase): def setUp(self): self.oclient = Client(name="Test Client", access_host="http://localhost/") self.oclient.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="Scope for testing") self.scope.save() self.user = User(username="******", password="******", email="*****@*****.**") self.user.save() self.token = AuthorizationToken(client=self.oclient, user=self.user) self.token.save() self.token.scope = [self.scope] self.token.save() def test_unicode(self): self.assertEqual(unicode(self.token), self.token.token) def test_generate_refresh_token_creates(self): rt = self.token.generate_refresh_token() self.assertEqual(RefreshToken.objects.count(), 1) self.assertIsInstance(rt, RefreshToken) def test_generate_refresh_token_no_create_twice(self): self.token.generate_refresh_token() rt = self.token.generate_refresh_token() self.assertEqual(RefreshToken.objects.count(), 1) self.assertIsNone(rt) def test_generate_refresh_token_never_creates_twice(self): self.token.generate_refresh_token() self.token.is_active = True rt = self.token.generate_refresh_token() self.assertEqual(RefreshToken.objects.count(), 1) self.assertIsNone(rt)
class OAuthTestCase(TestCase): def setUp(self): from django.contrib.auth.models import User self.user = User.objects.create_user("test", "*****@*****.**", "test") self.oauth_client = Client(name="Test Client", access_host="http://localhost/") self.oauth_client.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="This is a test scope.") self.scope.save() def assertExceptionRendered(self, request, exception): self.assertEquals(request.content, exception.reason) self.assertEquals(request.status_code, 401) def assertExceptionJson(self, request, exception): try: import simplejson as json except ImportError: import json data = { "error": exception.error, "error_description": exception.reason, } self.assertEquals(request.content, json.dumps(data)) self.assertEquals(request.status_code, getattr(exception, 'code', 400)) def assertExceptionRedirect(self, request, exception): params = { "error": exception.error, "error_description": exception.reason, "state": "o2cs", } url = self.redirect_uri.url + "?" + urllib.urlencode(params) self.assertRedirects(request, url) self.assertEquals(request.status_code, 302)
class OAuthTestCase(TestCase): def setUp(self): from django.contrib.auth.models import User self.user = User.objects.create_user("test", "*****@*****.**", "test") self.oauth_client = Client(name="Test Client", access_host="http://localhost/") self.oauth_client.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="This is a test scope.") self.scope.save() def assertExceptionRendered(self, request, exception): self.assertEquals(request.content, exception.reason) self.assertEquals(request.status_code, 401) def assertExceptionJson(self, request, exception): try: import simplejson as json except ImportError: import json data = { "error": exception.error, "error_description": exception.reason, } self.assertEquals(request.content, json.dumps(data)) self.assertEquals(request.status_code, 200) def assertExceptionRedirect(self, request, exception): params = { "error": exception.error, "error_description": exception.reason, "state": "o2cs", } url = self.redirect_uri.url + "?" + urllib.urlencode(params) self.assertRedirects(request, url) self.assertEquals(request.status_code, 302)
def setUp(self): self.oclient = Client(name="Test Client", access_host="http://localhost/") self.oclient.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="Scope for testing") self.scope.save() self.user = User(username="******", password="******", email="*****@*****.**") self.user.save() self.token = AuthorizationToken(client=self.oclient, user=self.user) self.token.save() self.token.scope = [self.scope] self.token.save()
class TestBearerHandler(TestCase): def setUp(self): self.oclient = Client(name="Test Client", access_host="http://localhost/") self.oclient.save() self.scope = Scope(short_name="test", full_name="Test Scope", description="Scope for testing") self.scope.save() self.user = User(username="******", password="******", email="*****@*****.**") self.user.save() self.at = AuthorizationToken(client=self.oclient, user=self.user) self.at.save() self.at.scope = [self.scope] self.at.save() self.rt = self.at.generate_refresh_token() self.token = self.rt.generate_access_token() self.handler = BearerHandler() self.factory = RequestFactory() def test_access_token(self): request = self.factory.get("/") token = self.handler.access_token(self.token.token, request) self.assertEqual(token, self.token) token = self.handler.access_token("invalid", request) self.assertEqual(token, None) def test_authenticate(self): request = self.factory.get("/") user = self.handler.authenticate(self.token.token, request) self.assertEqual(user, self.user) user = self.handler.authenticate("invalid", request) self.assertEqual(user, None) def test_validate(self): from doac.exceptions.base import InvalidToken from doac.exceptions.invalid_request import CredentialsNotProvided request = self.factory.get("/") result = self.handler.validate(self.token.token, request) self.assertEqual(result, None) response = self.handler.validate("invalid", request) self.assertNotEqual(response, None) self.assertEqual(response.status_code, 401) self.assertEqual(response["WWW-Authenticate"], request_error_header(InvalidToken)) response = self.handler.validate("", request) self.assertNotEqual(response, None) self.assertEqual(response.status_code, 400) self.assertEqual(response["WWW-Authenticate"], request_error_header(CredentialsNotProvided))