# Path: doac/models.py
# class AuthorizationCode(models.Model):
#     client = models.ForeignKey("Client", related_name="authorization_codes")
#     scope = models.ManyToManyField("Scope", related_name="authorization_codes")
#     redirect_uri = models.ForeignKey("RedirectUri", related_name="authorization_codes")
# 
#     token = models.CharField(
#         max_length=options.auth_code["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     response_type = models.CharField(choices=(("token", "token"), ("code", "code"), ), max_length=10)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     expires_at = models.DateTimeField()
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.AuthorizationCodeManager()
# 
#     def __unicode__(self):
#         return self.token
# 
#     def generate_token(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.auth_code["length"])
# 
#     def save(self, *args, **kwargs):
#         from .compat import now
# 
#         if not self.token:
#             self.token = self.generate_token()
# 
#         if not self.expires_at:
#             self.expires_at = now() + options.auth_code["expires"]
# 
#         super(AuthorizationCode, self).save(*args, **kwargs)
# 
# class AuthorizationToken(models.Model):
#     user = models.ForeignKey(user_model, related_name="authorization_tokens")
#     client = models.ForeignKey("Client", related_name="authorization_tokens")
#     token = models.CharField(
#         max_length=options.auth_token["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     scope = models.ManyToManyField("Scope", related_name="authorization_tokens")
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     expires_at = models.DateTimeField()
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.AuthorizationTokenManager()
# 
#     def __unicode__(self):
#         return self.token
# 
#     def generate_refresh_token(self):
#         if self.is_active:
#             try:
#                 temp = self.refresh_token
# 
#                 return None
#             except RefreshToken.DoesNotExist:
#                 self.refresh_token = RefreshToken()
# 
#                 self.refresh_token.client = self.client
#                 self.refresh_token.user = self.user
#                 self.refresh_token.save()
# 
#                 self.refresh_token.scope = self.scope.all()
#                 self.refresh_token.save()
# 
#                 self.is_active = False
#                 self.save()
# 
#                 return self.refresh_token
# 
#         return None
# 
#     def generate_token(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.auth_token["length"])
# 
#     def revoke_tokens(self):
#         """
#         Revoke the authorization token and all tokens that were generated using it.
#         """
# 
#         self.is_active = False
#         self.save()
# 
#         self.refresh_token.revoke_tokens()
# 
#     def save(self, *args, **kwargs):
#         from .compat import now
# 
#         if not self.token:
#             self.token = self.generate_token()
# 
#         if not self.expires_at:
#             self.expires_at = now() + options.auth_token["expires"]
# 
#         super(AuthorizationToken, self).save(*args, **kwargs)
# 
# class Client(models.Model):
#     name = models.CharField(max_length=255)
#     secret = models.CharField(
#         max_length=options.client["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     access_host = models.URLField(max_length=255)
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.ClientManager()
# 
#     def __unicode__(self):
#         return self.name
# 
#     def generate_secret(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.client["length"])
# 
#     def save(self, *args, **kwargs):
#         if not self.secret:
#             self.secret = self.generate_secret()
# 
#         super(Client, self).save(*args, **kwargs)
# 
# class RedirectUri(models.Model):
#     client = models.ForeignKey("Client", related_name="redirect_uris")
#     url = models.URLField(max_length=255)
# 
#     objects = managers.RedirectUriManager()
# 
#     class Meta:
#         verbose_name = "redirect URI"
# 
#     def __unicode__(self):
#         return self.url
# 
# class Scope(models.Model):
#     short_name = models.CharField(max_length=40, unique=True)
#     full_name = models.CharField(max_length=255)
#     description = models.TextField()
# 
#     objects = managers.ScopeManager()
# 
#     def __unicode__(self):
#         return "%s (%s)" % (self.full_name, self.short_name, )


from django.test import TestCase
from doac.models import AuthorizationCode, AuthorizationToken, Client, RedirectUri, Scope
        from django.contrib.auth.models import User
        from django.http import HttpRequest
        from doac.middleware import AuthenticationMiddleware
        from django.test.client import RequestFactory
import urllib
            import simplejson as json
            import json


class OAuthTestCase(TestCase):

    def setUp(self):

        self.user = User.objects.create_user("test", "test@test.com", "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):

# Path: doac/models.py
# class AuthorizationCode(models.Model):
#     client = models.ForeignKey("Client", related_name="authorization_codes")
#     scope = models.ManyToManyField("Scope", related_name="authorization_codes")
#     redirect_uri = models.ForeignKey("RedirectUri", related_name="authorization_codes")
# 
#     token = models.CharField(
#         max_length=options.auth_code["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     response_type = models.CharField(choices=(("token", "token"), ("code", "code"), ), max_length=10)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     expires_at = models.DateTimeField()
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.AuthorizationCodeManager()
# 
#     def __unicode__(self):
#         return self.token
# 
#     def generate_token(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.auth_code["length"])
# 
#     def save(self, *args, **kwargs):
#         from .compat import now
# 
#         if not self.token:
#             self.token = self.generate_token()
# 
#         if not self.expires_at:
#             self.expires_at = now() + options.auth_code["expires"]
# 
#         super(AuthorizationCode, self).save(*args, **kwargs)
# 
# class AuthorizationToken(models.Model):
#     user = models.ForeignKey(user_model, related_name="authorization_tokens")
#     client = models.ForeignKey("Client", related_name="authorization_tokens")
#     token = models.CharField(
#         max_length=options.auth_token["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     scope = models.ManyToManyField("Scope", related_name="authorization_tokens")
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     expires_at = models.DateTimeField()
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.AuthorizationTokenManager()
# 
#     def __unicode__(self):
#         return self.token
# 
#     def generate_refresh_token(self):
#         if self.is_active:
#             try:
#                 temp = self.refresh_token
# 
#                 return None
#             except RefreshToken.DoesNotExist:
#                 self.refresh_token = RefreshToken()
# 
#                 self.refresh_token.client = self.client
#                 self.refresh_token.user = self.user
#                 self.refresh_token.save()
# 
#                 self.refresh_token.scope = self.scope.all()
#                 self.refresh_token.save()
# 
#                 self.is_active = False
#                 self.save()
# 
#                 return self.refresh_token
# 
#         return None
# 
#     def generate_token(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.auth_token["length"])
# 
#     def revoke_tokens(self):
#         """
#         Revoke the authorization token and all tokens that were generated using it.
#         """
# 
#         self.is_active = False
#         self.save()
# 
#         self.refresh_token.revoke_tokens()
# 
#     def save(self, *args, **kwargs):
#         from .compat import now
# 
#         if not self.token:
#             self.token = self.generate_token()
# 
#         if not self.expires_at:
#             self.expires_at = now() + options.auth_token["expires"]
# 
#         super(AuthorizationToken, self).save(*args, **kwargs)
# 
# class Client(models.Model):
#     name = models.CharField(max_length=255)
#     secret = models.CharField(
#         max_length=options.client["length"],
#         blank=True,
#         help_text=AUTO_GENERATION_HELP_TEXT,
#     )
#     access_host = models.URLField(max_length=255)
#     is_active = models.BooleanField(default=True)
# 
#     objects = managers.ClientManager()
# 
#     def __unicode__(self):
#         return self.name
# 
#     def generate_secret(self):
#         from .compat import get_random_string
# 
#         return get_random_string(options.client["length"])
# 
#     def save(self, *args, **kwargs):
#         if not self.secret:
#             self.secret = self.generate_secret()
# 
#         super(Client, self).save(*args, **kwargs)
# 
# class RedirectUri(models.Model):
#     client = models.ForeignKey("Client", related_name="redirect_uris")
#     url = models.URLField(max_length=255)
# 
#     objects = managers.RedirectUriManager()
# 
#     class Meta:
#         verbose_name = "redirect URI"
# 
#     def __unicode__(self):
#         return self.url
# 
# class Scope(models.Model):
#     short_name = models.CharField(max_length=40, unique=True)
#     full_name = models.CharField(max_length=255)
#     description = models.TextField()
# 
#     objects = managers.ScopeManager()
# 
#     def __unicode__(self):
#         return "%s (%s)" % (self.full_name, self.short_name, )


# Path: tests/tests/test_cases.py
from django.test import TestCase
from doac.models import AuthorizationCode, AuthorizationToken, Client, RedirectUri, Scope
        from django.contrib.auth.models import User
        from django.http import HttpRequest
        from doac.middleware import AuthenticationMiddleware
        from django.test.client import RequestFactory
import urllib
            import simplejson as json
            import json



class OAuthTestCase(TestCase):

    def setUp(self):

        self.user = User.objects.create_user("test", "test@test.com", "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: