# 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 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
# class AuthorizeTestCase(OAuthTestCase):
# 
#     def setUp(self):
#         super(AuthorizeTestCase, self).setUp()
# 
#         self.redirect_uri = RedirectUri(client=self.oauth_client, url="http://localhost/redirect_endpoint/")
#         self.redirect_uri.save()


from django.core.urlresolvers import reverse
from doac.models import AuthorizationCode, Client, RedirectUri, Scope
from ..test_cases import AuthorizeTestCase
        from doac.exceptions.invalid_request import ClientNotProvided
        from doac.exceptions.invalid_client import ClientDoesNotExist
        from doac.exceptions.invalid_request import RedirectUriNotProvided, RedirectUriDoesNotValidate
        from doac.exceptions.invalid_scope import ScopeNotProvided, ScopeNotValid
        from doac.exceptions.invalid_request import ResponseTypeNotProvided
        from doac.exceptions.unsupported_response_type import ResponseTypeNotValid

        request = self.client.get(reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, ))
        self.assertExceptionRedirect(request, ResponseTypeNotProvided())

        request = self.client.get(reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=invalid" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, ))
        self.assertExceptionRedirect(request, ResponseTypeNotValid())


class TestAuthorizeResponse(AuthorizeTestCase):

    def test_login_redirect(self):
        auth_url = reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=token" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, )

        request = self.client.get(auth_url)

        self.assertEqual(request.status_code, 302)

    def test_approval_form(self):
        self.client.login(username="test", password="test")

        for response_type in ["token", "code", ]:
            request = self.client.get(reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=code" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, ))

            self.assertTemplateUsed(request, "doac/authorize.html")

            self.assertEqual(request.context["authorization_code"], AuthorizationCode.objects.latest("id"))
            self.assertEqual(request.context["client"], self.oauth_client)
            self.assertEqual(request.context["scopes"], [self.scope])
            self.assertEqual(request.context["state"], "o2cs")


# 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 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
# class AuthorizeTestCase(OAuthTestCase):
# 
#     def setUp(self):
#         super(AuthorizeTestCase, self).setUp()
# 
#         self.redirect_uri = RedirectUri(client=self.oauth_client, url="http://localhost/redirect_endpoint/")
#         self.redirect_uri.save()


# Path: tests/tests/views/test_authorize.py
from django.core.urlresolvers import reverse
from doac.models import AuthorizationCode, Client, RedirectUri, Scope
from ..test_cases import AuthorizeTestCase
        from doac.exceptions.invalid_request import ClientNotProvided
        from doac.exceptions.invalid_client import ClientDoesNotExist
        from doac.exceptions.invalid_request import RedirectUriNotProvided, RedirectUriDoesNotValidate
        from doac.exceptions.invalid_scope import ScopeNotProvided, ScopeNotValid
        from doac.exceptions.invalid_request import ResponseTypeNotProvided
        from doac.exceptions.unsupported_response_type import ResponseTypeNotValid


        request = self.client.get(reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, ))
        self.assertExceptionRedirect(request, ResponseTypeNotProvided())

        request = self.client.get(reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=invalid" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, ))
        self.assertExceptionRedirect(request, ResponseTypeNotValid())


class TestAuthorizeResponse(AuthorizeTestCase):

    def test_login_redirect(self):
        auth_url = reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=token" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, )

        request = self.client.get(auth_url)

        self.assertEqual(request.status_code, 302)

    def test_approval_form(self):
        self.client.login(username="test", password="test")

        for response_type in ["token", "code", ]:
            request = self.client.get(reverse("oauth2_authorize") + "?client_id=%s&redirect_uri=%s&scope=%s&response_type=code" % (self.oauth_client.id, self.redirect_uri.url, self.scope.short_name, ))

            self.assertTemplateUsed(request, "doac/authorize.html")

            self.assertEqual(request.context["authorization_code"], AuthorizationCode.objects.latest("id"))
            self.assertEqual(request.context["client"], self.oauth_client)
            self.assertEqual(request.context["scopes"], [self.scope])
            self.assertEqual(request.context["state"], "o2cs")


        self.assertEqual(AuthorizationCode.objects.count(), 2)