# Path: api/views/index_views.py
# @api_view(['POST'])
# def user_login(request):
#     """
#     Validates request data and logs user.
#     """
#     validate_api_secret_key(request.data.get('app_key'))
# 
#     username_request_serializer = UserLoginUsernameRequest(data=request.data)
#     email_request_serializer = UserLoginEmailRequest(data=request.data)
# 
#     if email_request_serializer.is_valid():
#         user_obj = User.objects.filter(email=request.data.get('username'))
#         username = user_obj[0] if len(user_obj) else None
# 
#         return login_response(request, username)
# 
#     elif username_request_serializer.is_valid():
#         return login_response(request, request.data.get('username'))
# 
#     return invalid_data_response(username_request_serializer)
# 
# Path: app/models.py
# class TheUser(models.Model):
#     """
#     Class for user objects in database.
#     """
#     REMINDER_TEMPLATE = json.dumps({
#         "common": {
#             "fb_page": True,
#             "fb_group": True,
#             "twitter": True,
#             "vk": True,
#             "disabled_all": False
#         },
#         "api": {
#             "app_rate": True
#         },
#         "web": {
#             "app_download": True,
#         },
#     })
# 
#     id_user = models.OneToOneField(User)
#     user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())
#     auth_token = models.CharField(max_length=50, null=True, blank=True)
#     subscription = models.BooleanField(default=True)
#     reminder = models.CharField(max_length=256, null=False, default=REMINDER_TEMPLATE)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def __str__(self):
#         return str(self.id_user)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_api_reminders(self):
#         """
#         Returns the reminders only necessary for API endpoints.
#         """
#         data = json.loads(self.reminder)
# 
#         mobile_data = dict(data['common'])
#         mobile_data.update(data['api'])
# 
#         return mobile_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_web_reminders(self):
#         """
#         Returns the reminders only necessary for web part.
#         """
#         data = json.loads(self.reminder)
#         
#         web_data = dict(data['common'])
#         web_data.update(data['web'])
# 
#         return web_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def update_reminder(self, field, value):
#         """
#         Updates the reminder status.
#         """
#         data = json.loads(self.reminder)
# 
#         for key in data:
#             if field in data[key]:
#                 data[key][field] = value
# 
#         self.reminder = json.dumps(data)
#         self.save()


from django.conf import settings
from django.contrib.auth.models import User
from django.shortcuts import reverse
from django.test import TestCase
from rest_framework.test import APIClient
from ...views.index_views import user_login
from app.models import TheUser

# -*- coding: utf-8 -*-





# ----------------------------------------------------------------------------------------------------------------------
class IndexViewsTestCase(TestCase):

    # ------------------------------------------------------------------------------------------------------------------
    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user(username='api_login', email='api_login@email.com', password='123456')
        cls.the_user = TheUser.objects.get(id_user=cls.user)

        cls.client = APIClient()
        cls.api_key = settings.API_SECRET_KEY

    # ------------------------------------------------------------------------------------------------------------------
    def test_user_login_missing_params(self):
        response = self.client.post(reverse('user_login_api'), {'app_key': self.api_key, 'username': 'username'})


# Path: api/views/index_views.py
# @api_view(['POST'])
# def user_login(request):
#     """
#     Validates request data and logs user.
#     """
#     validate_api_secret_key(request.data.get('app_key'))
# 
#     username_request_serializer = UserLoginUsernameRequest(data=request.data)
#     email_request_serializer = UserLoginEmailRequest(data=request.data)
# 
#     if email_request_serializer.is_valid():
#         user_obj = User.objects.filter(email=request.data.get('username'))
#         username = user_obj[0] if len(user_obj) else None
# 
#         return login_response(request, username)
# 
#     elif username_request_serializer.is_valid():
#         return login_response(request, request.data.get('username'))
# 
#     return invalid_data_response(username_request_serializer)
# 
# Path: app/models.py
# class TheUser(models.Model):
#     """
#     Class for user objects in database.
#     """
#     REMINDER_TEMPLATE = json.dumps({
#         "common": {
#             "fb_page": True,
#             "fb_group": True,
#             "twitter": True,
#             "vk": True,
#             "disabled_all": False
#         },
#         "api": {
#             "app_rate": True
#         },
#         "web": {
#             "app_download": True,
#         },
#     })
# 
#     id_user = models.OneToOneField(User)
#     user_photo = models.ImageField(blank=True, upload_to='user', storage=OverwriteStorage())
#     auth_token = models.CharField(max_length=50, null=True, blank=True)
#     subscription = models.BooleanField(default=True)
#     reminder = models.CharField(max_length=256, null=False, default=REMINDER_TEMPLATE)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def __str__(self):
#         return str(self.id_user)
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_api_reminders(self):
#         """
#         Returns the reminders only necessary for API endpoints.
#         """
#         data = json.loads(self.reminder)
# 
#         mobile_data = dict(data['common'])
#         mobile_data.update(data['api'])
# 
#         return mobile_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def get_web_reminders(self):
#         """
#         Returns the reminders only necessary for web part.
#         """
#         data = json.loads(self.reminder)
#         
#         web_data = dict(data['common'])
#         web_data.update(data['web'])
# 
#         return web_data
# 
#     # ------------------------------------------------------------------------------------------------------------------
#     def update_reminder(self, field, value):
#         """
#         Updates the reminder status.
#         """
#         data = json.loads(self.reminder)
# 
#         for key in data:
#             if field in data[key]:
#                 data[key][field] = value
# 
#         self.reminder = json.dumps(data)
#         self.save()


# Path: api/tests/test_views/test_index.py
from django.conf import settings
from django.contrib.auth.models import User
from django.shortcuts import reverse
from django.test import TestCase
from rest_framework.test import APIClient
from ...views.index_views import user_login
from app.models import TheUser

# -*- coding: utf-8 -*-





# ----------------------------------------------------------------------------------------------------------------------
class IndexViewsTestCase(TestCase):

    # ------------------------------------------------------------------------------------------------------------------
    @classmethod
    def setUpTestData(cls):
        cls.user = User.objects.create_user(username='api_login', email='api_login@email.com', password='123456')
        cls.the_user = TheUser.objects.get(id_user=cls.user)

        cls.client = APIClient()
        cls.api_key = settings.API_SECRET_KEY

    # ------------------------------------------------------------------------------------------------------------------
    def test_user_login_missing_params(self):
        response = self.client.post(reverse('user_login_api'), {'app_key': self.api_key, 'username': 'username'})


        self.assertEqual(response.resolver_match.func, user_login)
