# Path: doac/models.py
# 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)
# 
# Path: tests/tests/test_cases.py
# class ApprovalTestCase(OAuthTestCase):
# 
#     def setUp(self):
#         super(ApprovalTestCase, self).setUp()
# 
#         self.redirect_uri = RedirectUri(client=self.oauth_client, url="http://localhost/redirect_endpoint/")
#         self.redirect_uri.save()
# 
#         self.authorization_code = AuthorizationCode(client=self.oauth_client, redirect_uri=self.redirect_uri)
#         self.authorization_code.save()
# 
#         self.authorization_code.scope = [self.scope]
#         self.authorization_code.save()


from django.core.urlresolvers import reverse
from oauthlib.oauth2 import WebApplicationClient
from doac.models import AuthorizationToken
from ..test_cases import ApprovalTestCase



class TestOauthlib(ApprovalTestCase):
    
    def setUp(self):
        super(TestOauthlib, self).setUp()
        
        self.libclient = WebApplicationClient(self.oauth_client.id)
        
        self.authorization_code.response_type = "code"
        self.authorization_code.save()
    

# Path: doac/models.py
# 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)
# 
# Path: tests/tests/test_cases.py
# class ApprovalTestCase(OAuthTestCase):
# 
#     def setUp(self):
#         super(ApprovalTestCase, self).setUp()
# 
#         self.redirect_uri = RedirectUri(client=self.oauth_client, url="http://localhost/redirect_endpoint/")
#         self.redirect_uri.save()
# 
#         self.authorization_code = AuthorizationCode(client=self.oauth_client, redirect_uri=self.redirect_uri)
#         self.authorization_code.save()
# 
#         self.authorization_code.scope = [self.scope]
#         self.authorization_code.save()


# Path: tests/tests/integrations/test_idan_oauthlib.py
from django.core.urlresolvers import reverse
from oauthlib.oauth2 import WebApplicationClient
from doac.models import AuthorizationToken
from ..test_cases import ApprovalTestCase




class TestOauthlib(ApprovalTestCase):
    
    def setUp(self):
        super(TestOauthlib, self).setUp()
        
        self.libclient = WebApplicationClient(self.oauth_client.id)
        
        self.authorization_code.response_type = "code"
        self.authorization_code.save()
    

    def test_flow(self):