# Path: tests/v1/python/accounts/test_models.py
# class UserFactory(factory.DjangoModelFactory):
#     first_name = 'John'
#     last_name = 'Doe'
#     is_active = True
#     facebook_uid = str(uuid.uuid4())
# 
#     class Meta:
#         model = User
#         django_get_or_create = ('email',)
# 
# Path: tests/v1/python/device/test_serializers.py
# class OrderDeviceSerializerTest(CustomTestCase, APITestCase):
#     INVALID_DATA_DICT = [
#         {
#             'data': {
#                 'user': 1,
#                 'address': 'Street John 221',
#                 'phone_number': '+35190123450232'
#             },
#             'error': ('phone_number', ['Phone number is not valid.']),
#             'label': 'Invalid phone number format.',
#             'method': 'POST',
#             'status': status.HTTP_400_BAD_REQUEST
#         }
#     ]
#     VALID_DATA_DICT = [
#         {
#             'user': '28cae40c-7caf-4e24-87d6-d9ec68853b7d',
#             'address': 'Street John 221',
#             'phone_number': '+23412345678'
#         },
#     ]
# 
#     def setUp(self):
#         self.required_fields = ['user', 'address', 'phone_number']
#         self.not_required_fields = ['id', 'payment']
#         self.user = UserFactory(id='28cae40c-7caf-4e24-87d6-d9ec68853b7d', email='a@a.com')
# 
#     def test_fields(self):
#         serializer = OrderDeviceSerializer()
#         self.assert_fields_required(True, serializer, self.required_fields)
#         self.assert_fields_required(False, serializer, self.not_required_fields)
#         self.assertEqual(len(serializer.fields), len(self.required_fields) + len(self.not_required_fields))
# 
#     def test_invalid_data(self):
#         serializer = OrderDeviceSerializer
#         self.assert_invalid_data(serializer, self.INVALID_DATA_DICT)
# 
#     def test_valid_data(self):
#         serializer = OrderDeviceSerializer
#         self.assert_valid_data(serializer, self.VALID_DATA_DICT)


from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from device.models import DeviceOrders
from lib.v1.testutils import CustomTestCase, get_basic_auth_header
from tests.v1.python.accounts.test_models import UserFactory
from tests.v1.python.device.test_serializers import OrderDeviceSerializerTest

    def test_order_device_success(self):
        url = reverse('accounts_v1:login')
        self.client.credentials(HTTP_AUTHORIZATION=get_basic_auth_header('a@a.com', 'test'))
        data = {'app_version': '1.0'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % response.data['token']))

        url = reverse('device_v1:order_device')
        data = {'address': 'Street John 221',
                'phone_number': '+23412345678'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        device = DeviceOrders.objects.values('user', 'address', 'phone_number').latest('date_request')
        self.assertEqual(device['user'], self.user.pk)
        self.assertEqual(device['address'], data['address'])
        self.assertEqual(device['phone_number'], data['phone_number'])

        url = reverse('accounts_v1:login')
        self.client.credentials(HTTP_AUTHORIZATION=get_basic_auth_header('a@a.com', 'test'))
        data = {'app_version': '1.0'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % response.data['token']))
        self.assertTrue(len(response.data['user']['device']) > 0)


class ValidateOrderDeviceViewTests(CustomTestCase, APITestCase):
    def setUp(self):

# Path: tests/v1/python/accounts/test_models.py
# class UserFactory(factory.DjangoModelFactory):
#     first_name = 'John'
#     last_name = 'Doe'
#     is_active = True
#     facebook_uid = str(uuid.uuid4())
# 
#     class Meta:
#         model = User
#         django_get_or_create = ('email',)
# 
# Path: tests/v1/python/device/test_serializers.py
# class OrderDeviceSerializerTest(CustomTestCase, APITestCase):
#     INVALID_DATA_DICT = [
#         {
#             'data': {
#                 'user': 1,
#                 'address': 'Street John 221',
#                 'phone_number': '+35190123450232'
#             },
#             'error': ('phone_number', ['Phone number is not valid.']),
#             'label': 'Invalid phone number format.',
#             'method': 'POST',
#             'status': status.HTTP_400_BAD_REQUEST
#         }
#     ]
#     VALID_DATA_DICT = [
#         {
#             'user': '28cae40c-7caf-4e24-87d6-d9ec68853b7d',
#             'address': 'Street John 221',
#             'phone_number': '+23412345678'
#         },
#     ]
# 
#     def setUp(self):
#         self.required_fields = ['user', 'address', 'phone_number']
#         self.not_required_fields = ['id', 'payment']
#         self.user = UserFactory(id='28cae40c-7caf-4e24-87d6-d9ec68853b7d', email='a@a.com')
# 
#     def test_fields(self):
#         serializer = OrderDeviceSerializer()
#         self.assert_fields_required(True, serializer, self.required_fields)
#         self.assert_fields_required(False, serializer, self.not_required_fields)
#         self.assertEqual(len(serializer.fields), len(self.required_fields) + len(self.not_required_fields))
# 
#     def test_invalid_data(self):
#         serializer = OrderDeviceSerializer
#         self.assert_invalid_data(serializer, self.INVALID_DATA_DICT)
# 
#     def test_valid_data(self):
#         serializer = OrderDeviceSerializer
#         self.assert_valid_data(serializer, self.VALID_DATA_DICT)


# Path: tests/v1/python/device/test_views.py
from django.core.urlresolvers import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from device.models import DeviceOrders
from lib.v1.testutils import CustomTestCase, get_basic_auth_header
from tests.v1.python.accounts.test_models import UserFactory
from tests.v1.python.device.test_serializers import OrderDeviceSerializerTest


    def test_order_device_success(self):
        url = reverse('accounts_v1:login')
        self.client.credentials(HTTP_AUTHORIZATION=get_basic_auth_header('a@a.com', 'test'))
        data = {'app_version': '1.0'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % response.data['token']))

        url = reverse('device_v1:order_device')
        data = {'address': 'Street John 221',
                'phone_number': '+23412345678'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        device = DeviceOrders.objects.values('user', 'address', 'phone_number').latest('date_request')
        self.assertEqual(device['user'], self.user.pk)
        self.assertEqual(device['address'], data['address'])
        self.assertEqual(device['phone_number'], data['phone_number'])

        url = reverse('accounts_v1:login')
        self.client.credentials(HTTP_AUTHORIZATION=get_basic_auth_header('a@a.com', 'test'))
        data = {'app_version': '1.0'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % response.data['token']))
        self.assertTrue(len(response.data['user']['device']) > 0)


class ValidateOrderDeviceViewTests(CustomTestCase, APITestCase):
    def setUp(self):

        self.user = UserFactory(email='a@a.com')