# Path: account/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     profile_picture = models.ImageField(upload_to=user_profile_picture, null=True, blank=True)
#     description = models.TextField(null=True, blank=True)
#     roll_number = models.CharField(max_length=16, null=True, blank=True)
#     type = models.CharField(max_length=16, null=True, blank=True)
#     mobile = models.CharField(max_length=16, null=True, blank=True)
#     is_alumni = models.BooleanField(default=False)
#     sex = models.CharField(max_length=10, choices=SEXES, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.user.username
# 
# Path: application/admin.py
# class ApplicationAdmin(SimpleHistoryAdmin):
#     list_display = ['id', 'name', 'user', 'total_users', 'created_on', 'modified_on', 'is_anonymous']
#     list_filter = ['is_anonymous', ]
#     search_fields = ['name', 'user__username', 'user__first_name', ]
# 
#     def total_users(self, obj):
#         return obj.get_user_count()
# 
# Path: application/models.py
# class Application(AbstractApplication):
#     """
#     Extended oauth2_provider.models.AbstractApplication to include application description and logo
#     """
#     description = models.TextField()
#     logo = models.ImageField(upload_to=application_logo, blank=True, null=True)
#     is_anonymous = models.BooleanField(default=False,
#                                        help_text='Valid for complete anonymous apps. Requires admin permission')
#     required_scopes = models.CharField(max_length=256,
#                                        help_text='Default non-tracking permissions. '
#                                                  'Valid only if application is anonymous', null=True, blank=True)
#     private_scopes = models.CharField(max_length=256, help_text='Private API scopes', null=True, blank=True)
#     website = models.URLField(null=True, blank=True)
#     privacy_policy = models.URLField(null=True, blank=True, help_text='Link of privacy policy of application')
#     created_on = models.DateTimeField(auto_now_add=True)
#     modified_on = models.DateTimeField(auto_now=True)
#     _history_ = HistoricalRecords()
# 
#     def get_logo_url(self):
#         try:
#             url = self.logo.url
#             url = urljoin(settings.MEDIA_URL, url)
#         except ValueError:
#             url = None
#         return url
# 
#     def get_absolute_url(self):
#         return reverse('oauth2_provider:detail', args=[str(self.id)])
# 
#     def clean(self):
#         super(Application, self).clean()
#         if self.authorization_grant_type == AbstractApplication.GRANT_PASSWORD:
#             error = _("Resource owner password based grants are not allowed for now")
#             raise ValidationError(error)
# 
#     def get_user_count(self):
#         return AccessToken.objects.all().filter(application=self).values_list('user', flat=True).distinct().count()
# 
#     def __str__(self):
#         if self.name:
#             return self.name
#         else:
#             return self.client_id
# 
# def application_logo(instance, filename):  # pylint: disable=unused-argument
#     filename = filename.split('.')
#     if len(filename) > 1:
#         ext = filename[-1]
#     else:
#         ext = "png"
#     return os.path.join("app_logo", uuid4().hex + "." + ext)


from django.conf import settings
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.test import TestCase, mock
from django.utils.timezone import now
from oauth2_provider.models import AccessToken
from account.models import UserProfile
from .admin import ApplicationAdmin
from .models import Application, application_logo
            redirect_uris='http://localhost:4000'
        )

        self.assertRaises(ValidationError, application4.clean)

    def test_application_logo_upload(self):
        with mock.patch('uuid.uuid4') as uuid_mock:
            uuid_mock.return_value = '1234567890'
            filename = application_logo(self.application1, 'hola.png')
            self.assertNotEqual(filename, 'app_logo/1234567890.png')

    def test_application_logo_default_ext(self):
        with mock.patch('uuid.uuid4') as uuid_mock:
            uuid_mock.return_value = '987654321'
            filename = application_logo(self.application1, 'hola')
            self.assertNotEqual(filename, 'app_logo/987654321.png')


class ApplicationAdminTestCase(TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='username', password='password')
        self.application1 = self.application1 = Application.objects.create(
            name='application1',
            description='testapp',
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
            redirect_uris='http://localhost:7000/',
            logo='app_logo/1235.png',
            user=self.u1,
        )

# Path: account/models.py
# class UserProfile(models.Model):
#     user = models.OneToOneField(User)
#     profile_picture = models.ImageField(upload_to=user_profile_picture, null=True, blank=True)
#     description = models.TextField(null=True, blank=True)
#     roll_number = models.CharField(max_length=16, null=True, blank=True)
#     type = models.CharField(max_length=16, null=True, blank=True)
#     mobile = models.CharField(max_length=16, null=True, blank=True)
#     is_alumni = models.BooleanField(default=False)
#     sex = models.CharField(max_length=10, choices=SEXES, null=True, blank=True)
#     _history_ = HistoricalRecords()
# 
#     def __str__(self):
#         return self.user.username
# 
# Path: application/admin.py
# class ApplicationAdmin(SimpleHistoryAdmin):
#     list_display = ['id', 'name', 'user', 'total_users', 'created_on', 'modified_on', 'is_anonymous']
#     list_filter = ['is_anonymous', ]
#     search_fields = ['name', 'user__username', 'user__first_name', ]
# 
#     def total_users(self, obj):
#         return obj.get_user_count()
# 
# Path: application/models.py
# class Application(AbstractApplication):
#     """
#     Extended oauth2_provider.models.AbstractApplication to include application description and logo
#     """
#     description = models.TextField()
#     logo = models.ImageField(upload_to=application_logo, blank=True, null=True)
#     is_anonymous = models.BooleanField(default=False,
#                                        help_text='Valid for complete anonymous apps. Requires admin permission')
#     required_scopes = models.CharField(max_length=256,
#                                        help_text='Default non-tracking permissions. '
#                                                  'Valid only if application is anonymous', null=True, blank=True)
#     private_scopes = models.CharField(max_length=256, help_text='Private API scopes', null=True, blank=True)
#     website = models.URLField(null=True, blank=True)
#     privacy_policy = models.URLField(null=True, blank=True, help_text='Link of privacy policy of application')
#     created_on = models.DateTimeField(auto_now_add=True)
#     modified_on = models.DateTimeField(auto_now=True)
#     _history_ = HistoricalRecords()
# 
#     def get_logo_url(self):
#         try:
#             url = self.logo.url
#             url = urljoin(settings.MEDIA_URL, url)
#         except ValueError:
#             url = None
#         return url
# 
#     def get_absolute_url(self):
#         return reverse('oauth2_provider:detail', args=[str(self.id)])
# 
#     def clean(self):
#         super(Application, self).clean()
#         if self.authorization_grant_type == AbstractApplication.GRANT_PASSWORD:
#             error = _("Resource owner password based grants are not allowed for now")
#             raise ValidationError(error)
# 
#     def get_user_count(self):
#         return AccessToken.objects.all().filter(application=self).values_list('user', flat=True).distinct().count()
# 
#     def __str__(self):
#         if self.name:
#             return self.name
#         else:
#             return self.client_id
# 
# def application_logo(instance, filename):  # pylint: disable=unused-argument
#     filename = filename.split('.')
#     if len(filename) > 1:
#         ext = filename[-1]
#     else:
#         ext = "png"
#     return os.path.join("app_logo", uuid4().hex + "." + ext)


# Path: application/tests.py
from django.conf import settings
from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.test import TestCase, mock
from django.utils.timezone import now
from oauth2_provider.models import AccessToken
from account.models import UserProfile
from .admin import ApplicationAdmin
from .models import Application, application_logo

            redirect_uris='http://localhost:4000'
        )

        self.assertRaises(ValidationError, application4.clean)

    def test_application_logo_upload(self):
        with mock.patch('uuid.uuid4') as uuid_mock:
            uuid_mock.return_value = '1234567890'
            filename = application_logo(self.application1, 'hola.png')
            self.assertNotEqual(filename, 'app_logo/1234567890.png')

    def test_application_logo_default_ext(self):
        with mock.patch('uuid.uuid4') as uuid_mock:
            uuid_mock.return_value = '987654321'
            filename = application_logo(self.application1, 'hola')
            self.assertNotEqual(filename, 'app_logo/987654321.png')


class ApplicationAdminTestCase(TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='username', password='password')
        self.application1 = self.application1 = Application.objects.create(
            name='application1',
            description='testapp',
            client_type=Application.CLIENT_CONFIDENTIAL,
            authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
            redirect_uris='http://localhost:7000/',
            logo='app_logo/1235.png',
            user=self.u1,
        )

        self.app_admin = ApplicationAdmin(Application, AdminSite())