'''
(c) Copyright 2013 Telefonica, I+D. Printed in Spain (Europe). All Rights
Reserved.

The copyright to the software program(s) is property of Telefonica I+D.
The program(s) may be used and or copied only with the express written
consent of Telefonica I+D or in accordance with the terms and conditions
stipulated in the agreement/contract under which the program(s) have
been supplied.
'''
from unittest import TestCase
from mock import MagicMock, patch
from commons.json_schema_validator.schema_reader import SchemaField
from commons.json_schema_validator.schema_reader import SchemaReader
from users.serializers import UserCollectionSerializer


class UserSerializerTests(TestCase):

    def setUp(self):
        super(UserSerializerTests, self).setUp()
        mock_schema_instance = MagicMock(name='mock_schema_instance')
        mock_schema_instance.return_value = [
        SchemaField(name='username', field_type='string', required=True),
        SchemaField(name='password', field_type='string', required=True),
        SchemaField(name='is_admin', field_type='boolean', required=True, default=False)
        ]

        mock_get_schema_fields = MagicMock(name='mock_get_schema')
        mock_get_schema_fields.return_value = mock_schema_instance
        # mock schema instance
        schema_reader = SchemaReader()
        self.patcher_validate = patch.object(schema_reader, 'validate_object')  # @UndefinedVariable
        self.patcher_schema = patch.object(schema_reader,  # @UndefinedVariable
                                        'get_schema_fields', mock_schema_instance)
        self.patcher_schema.start()
        self.patcher_validate.start()

    def tearDown(self):
        self.patcher_schema.stop()
        self.patcher_validate.stop()

    def test_deserialize_user_should_work(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'username': 'user', 'password': 'pass'})
        self.assertEquals(True, serializer.is_valid(), "Serialization invalid")

    def test_deserialize_user_invalid_is_admin_should_work(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'username': 'user', 'password': 'pass', 'is_admin': 'si'})
        self.assertEquals(False, serializer.is_valid(), "Serialization invalid")

    def test_deserialize_user_empty_user_should_give_error_invalid(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'username': '', 'password': 'pass'})
        self.assertEquals(False, serializer.is_valid(), "Serialization invalid")
        self.assertEquals(u"invalid",
                          serializer.errors['username'][0],
                          'Invalid error message')

    def test_deserialize_user_null_user_should_give_required_error(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'password': 'pass'})
        self.assertEquals(False, serializer.is_valid(), "Serialization invalid")
        self.assertEquals(u"required",
                          serializer.errors['username'][0],
                          'Invalid error message')

    def test_deserialize_user_large_user_ne_should_give_invalid_error(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'username': 'a' * 600, 'password': 'pass'})
        self.assertEquals(False, serializer.is_valid(), "Serialization invalid")
        self.assertEquals(u"invalid",
                          serializer.errors['username'][0],
                          'Invalid error message')

    def test_deserialize_user_with_invalid_origins_should_give_error(self):
        serializer = UserCollectionSerializer(data={'username': 'user', 'password': 'pass', 'origins': ["????"]})
        self.assertEquals(False, serializer.is_valid())
        self.assertEquals(u"invalid",
                          serializer.errors['origins'][0],
                          'Invalid error message')
        serializer = UserCollectionSerializer(data={'username': 'user', 'password': 'pass', 'origins': [" tugo"]})
        self.assertEquals(False, serializer.is_valid())
        self.assertEquals(u"invalid",
                          serializer.errors['origins'][0],
                          'Invalid error message')

    def test_deserialize_user_with_invalid_classes_should_give_error(self):
        serializer = UserCollectionSerializer(data={'username': 'user', 'password': 'pass', 'classes': ["????"]})
        self.assertEquals(False, serializer.is_valid())
        self.assertEquals(u"invalid",
                          serializer.errors['classes'][0],
                          'Invalid error message')
        serializer = UserCollectionSerializer(data={'username': 'user', 'password': 'pass', 'classes': [" sms"]})
        self.assertEquals(False, serializer.is_valid())
        self.assertEquals(u"invalid",
                          serializer.errors['classes'][0],
                          'Invalid error message')

    def test_deserialize_user_invalid_username_should_give_error(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'username': 'User.user', 'password': 'pass'})
        self.assertEquals(False, serializer.is_valid(), "Serialization invalid")
        self.assertEquals(u"invalid",
                          serializer.errors['username'][0],
                          'Invalid error message')

    def test_deserialize_user_invalid_is_admin_should_give_error(self):
        # We need to do import here in order generic patches work
        serializer = UserCollectionSerializer(data={'username': 'usera', 'password': 'pass', 'is_admin': 0})
        self.assertEquals(False, serializer.is_valid(), "Serialization invalid")
        self.assertEquals(u"invalid",
                          serializer.errors['is_admin'][0],
                          'Invalid error message')

[((790, 828), 'mock.MagicMock', 'MagicMock', ([], {'name': '"""mock_schema_instance"""'}), "(name='mock_schema_instance')\n", (799, 828), False, 'from mock import MagicMock, patch\n'), ((1156, 1189), 'mock.MagicMock', 'MagicMock', ([], {'name': '"""mock_get_schema"""'}), "(name='mock_get_schema')\n", (1165, 1189), False, 'from mock import MagicMock, patch\n'), ((1312, 1326), 'commons.json_schema_validator.schema_reader.SchemaReader', 'SchemaReader', ([], {}), '()\n', (1324, 1326), False, 'from commons.json_schema_validator.schema_reader import SchemaReader\n'), ((1359, 1405), 'mock.patch.object', 'patch.object', (['schema_reader', '"""validate_object"""'], {}), "(schema_reader, 'validate_object')\n", (1371, 1405), False, 'from mock import MagicMock, patch\n'), ((1458, 1528), 'mock.patch.object', 'patch.object', (['schema_reader', '"""get_schema_fields"""', 'mock_schema_instance'], {}), "(schema_reader, 'get_schema_fields', mock_schema_instance)\n", (1470, 1528), False, 'from mock import MagicMock, patch\n'), ((1899, 1970), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'user', 'password': 'pass'}"}), "(data={'username': 'user', 'password': 'pass'})\n", (1923, 1970), False, 'from users.serializers import UserCollectionSerializer\n'), ((2205, 2298), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'user', 'password': 'pass', 'is_admin': 'si'}"}), "(data={'username': 'user', 'password': 'pass',\n    'is_admin': 'si'})\n", (2229, 2298), False, 'from users.serializers import UserCollectionSerializer\n'), ((2538, 2605), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': '', 'password': 'pass'}"}), "(data={'username': '', 'password': 'pass'})\n", (2562, 2605), False, 'from users.serializers import UserCollectionSerializer\n'), ((2998, 3049), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'password': 'pass'}"}), "(data={'password': 'pass'})\n", (3022, 3049), False, 'from users.serializers import UserCollectionSerializer\n'), ((3446, 3520), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'a' * 600, 'password': 'pass'}"}), "(data={'username': 'a' * 600, 'password': 'pass'})\n", (3470, 3520), False, 'from users.serializers import UserCollectionSerializer\n'), ((3849, 3945), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'user', 'password': 'pass', 'origins': ['????']}"}), "(data={'username': 'user', 'password': 'pass',\n    'origins': ['????']})\n", (3873, 3945), False, 'from users.serializers import UserCollectionSerializer\n'), ((4167, 4264), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'user', 'password': 'pass', 'origins': [' tugo']}"}), "(data={'username': 'user', 'password': 'pass',\n    'origins': [' tugo']})\n", (4191, 4264), False, 'from users.serializers import UserCollectionSerializer\n'), ((4563, 4659), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'user', 'password': 'pass', 'classes': ['????']}"}), "(data={'username': 'user', 'password': 'pass',\n    'classes': ['????']})\n", (4587, 4659), False, 'from users.serializers import UserCollectionSerializer\n'), ((4881, 4977), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'user', 'password': 'pass', 'classes': [' sms']}"}), "(data={'username': 'user', 'password': 'pass',\n    'classes': [' sms']})\n", (4905, 4977), False, 'from users.serializers import UserCollectionSerializer\n'), ((5338, 5414), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'User.user', 'password': 'pass'}"}), "(data={'username': 'User.user', 'password': 'pass'})\n", (5362, 5414), False, 'from users.serializers import UserCollectionSerializer\n'), ((5805, 5896), 'users.serializers.UserCollectionSerializer', 'UserCollectionSerializer', ([], {'data': "{'username': 'usera', 'password': 'pass', 'is_admin': 0}"}), "(data={'username': 'usera', 'password': 'pass',\n    'is_admin': 0})\n", (5829, 5896), False, 'from users.serializers import UserCollectionSerializer\n'), ((883, 947), 'commons.json_schema_validator.schema_reader.SchemaField', 'SchemaField', ([], {'name': '"""username"""', 'field_type': '"""string"""', 'required': '(True)'}), "(name='username', field_type='string', required=True)\n", (894, 947), False, 'from commons.json_schema_validator.schema_reader import SchemaField\n'), ((957, 1021), 'commons.json_schema_validator.schema_reader.SchemaField', 'SchemaField', ([], {'name': '"""password"""', 'field_type': '"""string"""', 'required': '(True)'}), "(name='password', field_type='string', required=True)\n", (968, 1021), False, 'from commons.json_schema_validator.schema_reader import SchemaField\n'), ((1031, 1116), 'commons.json_schema_validator.schema_reader.SchemaField', 'SchemaField', ([], {'name': '"""is_admin"""', 'field_type': '"""boolean"""', 'required': '(True)', 'default': '(False)'}), "(name='is_admin', field_type='boolean', required=True, default=False\n    )\n", (1042, 1116), False, 'from commons.json_schema_validator.schema_reader import SchemaField\n')]