2014-09-08 17:24:05 +04:00
|
|
|
from django.test import TestCase
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2014-09-08 17:24:05 +04:00
|
|
|
from rest_framework import serializers
|
2014-01-14 15:25:44 +04:00
|
|
|
|
|
|
|
|
2014-09-08 17:24:05 +04:00
|
|
|
class WriteOnlyFieldTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
email = serializers.EmailField()
|
|
|
|
password = serializers.CharField(write_only=True)
|
2014-01-14 15:25:44 +04:00
|
|
|
|
2014-09-08 17:24:05 +04:00
|
|
|
self.Serializer = ExampleSerializer
|
2014-01-14 15:25:44 +04:00
|
|
|
|
2018-05-08 11:27:35 +03:00
|
|
|
def test_write_only_fields_are_present_on_input(self):
|
2014-09-08 17:24:05 +04:00
|
|
|
data = {
|
|
|
|
'email': 'foo@example.com',
|
|
|
|
'password': '123'
|
|
|
|
}
|
|
|
|
serializer = self.Serializer(data=data)
|
2016-11-23 17:05:34 +03:00
|
|
|
assert serializer.is_valid()
|
|
|
|
assert serializer.validated_data == data
|
2014-01-14 15:25:44 +04:00
|
|
|
|
2018-05-08 11:27:35 +03:00
|
|
|
def test_write_only_fields_are_not_present_on_output(self):
|
2014-09-08 17:24:05 +04:00
|
|
|
instance = {
|
|
|
|
'email': 'foo@example.com',
|
|
|
|
'password': '123'
|
|
|
|
}
|
|
|
|
serializer = self.Serializer(instance)
|
2016-11-23 17:05:34 +03:00
|
|
|
assert serializer.data == {'email': 'foo@example.com'}
|