# Path: server/apps/authentication/serializers.py
# class AccountSerializer(serializers.ModelSerializer):
#     password = serializers.CharField(write_only=True, required=False)
#     confirm_password = serializers.CharField(write_only=True, required=False)
# 
#     class Meta:
#         model = Account
#         fields = ('id', 'email', 'username', 'created_at', 'updated_at',
#                   'name', 'tagline', 'password',
#                   'confirm_password',)
#         read_only_fields = ('created_at', 'updated_at',)
# 
#     def create(self, validated_data):
#         return Account.objects.create(**validated_data)
# 
#     def update(self, instance, validated_data):
#         instance.username = validated_data.get('username', instance.username)
#         instance.tagline = validated_data.get('tagline', instance.tagline)
# 
#         instance.save()
# 
#         password = validated_data.get('password', None)
#         confirm_password = validated_data.get('confirm_password', None)
# 
#         if password and confirm_password and password == confirm_password:
#             instance.set_password(password)
#             instance.is_active = True
#             instance.save()
# 
#         update_session_auth_hash(self.context.get('request'), instance)
# 
#         return instance
# 
#     def to_representation(self, obj):
#         data = super(AccountSerializer, self).to_representation(obj)
# 
#         return data


import json
import pytz
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.utils import timezone
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.reverse import reverse
from rest_framework.test import APIClient, APIRequestFactory
from .models import *
from .serializers import AccountSerializer
from .tasks import *
from rest_framework.test import APITestCase

    def test_api_token(self):

        url = reverse('api-token')
        u4 = user_model.objects.create_user(username='User4', email='user4@foo.com', password='pass')
        u4.is_active = True
        u4.save()

        try:
            token = Token.objects.get(user=u4)
            # There should be no token for u3
            self.assertEqual(1, 0)
        except:
            pass

        resp = self.client.get(url, data={'format': 'json'})
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
        ok = self.client.login(email='user4@foo.com', password='pass')
        self.assertTrue(ok)
        resp = self.client.get(url, data={'format': 'json'})
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        deserialized = json.loads(resp.content.decode())
        self.assertEqual(len(deserialized), 1)
        token = Token.objects.get(user=u4)
        self.assertEqual(deserialized['token'], token.key)
        resp = self.client.get(url, data={'format': 'json'})
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

    def test_serializer(self):
        account = Account.objects.latest('created_at')

# Path: server/apps/authentication/serializers.py
# class AccountSerializer(serializers.ModelSerializer):
#     password = serializers.CharField(write_only=True, required=False)
#     confirm_password = serializers.CharField(write_only=True, required=False)
# 
#     class Meta:
#         model = Account
#         fields = ('id', 'email', 'username', 'created_at', 'updated_at',
#                   'name', 'tagline', 'password',
#                   'confirm_password',)
#         read_only_fields = ('created_at', 'updated_at',)
# 
#     def create(self, validated_data):
#         return Account.objects.create(**validated_data)
# 
#     def update(self, instance, validated_data):
#         instance.username = validated_data.get('username', instance.username)
#         instance.tagline = validated_data.get('tagline', instance.tagline)
# 
#         instance.save()
# 
#         password = validated_data.get('password', None)
#         confirm_password = validated_data.get('confirm_password', None)
# 
#         if password and confirm_password and password == confirm_password:
#             instance.set_password(password)
#             instance.is_active = True
#             instance.save()
# 
#         update_session_auth_hash(self.context.get('request'), instance)
# 
#         return instance
# 
#     def to_representation(self, obj):
#         data = super(AccountSerializer, self).to_representation(obj)
# 
#         return data


# Path: server/apps/authentication/tests.py
import json
import pytz
from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import Client, TestCase
from django.utils import timezone
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.reverse import reverse
from rest_framework.test import APIClient, APIRequestFactory
from .models import *
from .serializers import AccountSerializer
from .tasks import *
from rest_framework.test import APITestCase


    def test_api_token(self):

        url = reverse('api-token')
        u4 = user_model.objects.create_user(username='User4', email='user4@foo.com', password='pass')
        u4.is_active = True
        u4.save()

        try:
            token = Token.objects.get(user=u4)
            # There should be no token for u3
            self.assertEqual(1, 0)
        except:
            pass

        resp = self.client.get(url, data={'format': 'json'})
        self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN)
        ok = self.client.login(email='user4@foo.com', password='pass')
        self.assertTrue(ok)
        resp = self.client.get(url, data={'format': 'json'})
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        deserialized = json.loads(resp.content.decode())
        self.assertEqual(len(deserialized), 1)
        token = Token.objects.get(user=u4)
        self.assertEqual(deserialized['token'], token.key)
        resp = self.client.get(url, data={'format': 'json'})
        self.assertEqual(resp.status_code, status.HTTP_200_OK)

    def test_serializer(self):
        account = Account.objects.latest('created_at')

        serialized_account = AccountSerializer(account)