def test_invalid_update_librarian(self):
        """read_only fields are just ignored"""
        lib = erp_factories.StandardLibrarianFactory(
            user__username='******'
        )

        lib_update = {'user': {'username': '******'}}
        lib_ser = erp_serializers.LibrarianSerializer(
            instance=lib,
            data=lib_update,
            partial=True
        )
        lib_ser.is_valid(raise_exception=True)
        lib = lib_ser.save()

        self.assertEqual(lib.user.username, 'JackCash')
    def test_deserialize_librarian_with_complex_name(self):
        librarian = {
            "is_manager": False,
            "user": {
                "email": "*****@*****.**",
                "first_name": "Jean-Léonard",
                "last_name": "de la Fiérière",
                "password": "******",
            }
        }

        serializer = erp_serializers.LibrarianSerializer(data=librarian)
        serializer.is_valid(raise_exception=True)
        lib = serializer.save()

        self.assertEqual(lib.user.username, 'jdelafieriere')
        self.assertEqual(lib.user.groups.first(), self.librarians_group)
    def test_deserialize_invalid_librarian_missing_required_field(self):
        librarian = {
            "user": {
                "email": "*****@*****.**",
                "first_name": "Lin",
                "last_name": "Liu",
                "password": "******",
            }
        }

        serializer = erp_serializers.LibrarianSerializer(data=librarian)
        with self.assertRaises(ValidationError):
            serializer.is_valid(raise_exception=True)
        self.assertEqual(
            serializer.errors,
            {'is_manager': [ErrorDetail(string='This field is required.', code='required')]}
        )
    def test_valid_update_librarian(self):
        lib = erp_factories.StandardLibrarianFactory(
            user__first_name='Jack',
            user__last_name='Credit'
        )

        lib_update = {"user": {"last_name": "Cash"}}

        lib_ser = erp_serializers.LibrarianSerializer(
            instance=lib,
            data=lib_update,
            partial=True
        )
        lib_ser.is_valid(raise_exception=True)
        lib = lib_ser.save()

        self.assertEqual(lib.user.last_name, 'Cash')
        self.assertEqual(lib.user.first_name, 'Jack')
    def test_deserialize_valid_manager(self):
        librarian = {
            "is_manager": True,
            "user": {
                "email": "*****@*****.**",
                "first_name": "Lin",
                "last_name": "Liu",
                "password": "******",
            }
        }

        serializer = erp_serializers.LibrarianSerializer(data=librarian)
        serializer.is_valid(raise_exception=True)
        lib = serializer.save()

        self.assertEqual(lib.user.groups.first(), self.managers_group)
        self.assertEqual(lib.user.username, 'lliu')
        self.assertTrue(lib.user.is_active)
    def test_deserialize_invalid_librarian_missing_required_field_in_related_model(self):
        librarian = {
            "is_manager": False,
            "user": {
                "email": "*****@*****.**",
                "first_name": "Lin",
                "password": "******",
            }
        }

        serializer = erp_serializers.LibrarianSerializer(data=librarian)
        with self.assertRaises(ValidationError):
            serializer.is_valid(raise_exception=True)
        self.assertEqual(
            serializer.errors,
            # DRF gives the name of the field concerned by the error,
            # only when the field is part of the Model and is auto-detected by DRF
            {'non_field_errors': [ErrorDetail(string='The user last_name is missing', code='invalid')]}
        )
    def test_serialize_librarian(self):
        sub = erp_factories.StandardLibrarianFactory()

        sub_ser = erp_serializers.LibrarianSerializer(sub)
        self.assertTrue('is_manager' in set(sub_ser.data))
        self.assertTrue('first_name' in sub_ser.data['user'].keys())