<s> from __future__ import unicode_literals <EOL> import unittest <EOL> from collections import namedtuple <EOL> from rest_framework import serializers <EOL> from rest_framework . response import Response <EOL> from rest_framework . test import APIRequestFactory , APISimpleTestCase , force_authenticate <EOL> from rest_framework . views import APIView <EOL> from nodeconductor . core . fields import JsonField <EOL> from nodeconductor . core . fields import TimestampField <EOL> from nodeconductor . core . serializers import Base64Field , RestrictedSerializerMixin <EOL> from nodeconductor . core import utils <EOL> class Base64Serializer ( serializers . Serializer ) : <EOL> content = Base64Field ( ) <EOL> class Base64FieldTest ( unittest . TestCase ) : <EOL> def test_text_gets_base64_encoded_on_serialization ( self ) : <EOL> serializer = Base64Serializer ( instance = { '<STR_LIT:content>' : '<STR_LIT:hello>' } ) <EOL> actual = serializer . data [ '<STR_LIT:content>' ] <EOL> self . assertEqual ( '<STR_LIT>' , actual ) <EOL> def test_text_remains_base64_encoded_on_deserialization ( self ) : <EOL> serializer = Base64Serializer ( data = { '<STR_LIT:content>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> actual = serializer . validated_data [ '<STR_LIT:content>' ] <EOL> self . assertEqual ( '<STR_LIT>' , actual ) <EOL> def test_deserialization_fails_validation_on_incorrect_base64 ( self ) : <EOL> serializer = Base64Serializer ( data = { '<STR_LIT:content>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( serializer . is_valid ( ) ) <EOL> self . assertIn ( '<STR_LIT:content>' , serializer . errors , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , <EOL> serializer . errors [ '<STR_LIT:content>' ] ) <EOL> class JsonSerializer ( serializers . Serializer ) : <EOL> content = JsonField ( ) <EOL> class JsonFieldTest ( unittest . TestCase ) : <EOL> def test_dict_gets_parsed_as_dict_on_serialization ( self ) : <EOL> serializer = JsonSerializer ( instance = { '<STR_LIT:content>' : { u'<STR_LIT:key>' : u'<STR_LIT:value>' } } ) <EOL> actual = serializer . data [ '<STR_LIT:content>' ] <EOL> self . assertEqual ( { u'<STR_LIT:key>' : u'<STR_LIT:value>' } , actual ) <EOL> def test_text_gets_json_parsed_on_deserialization ( self ) : <EOL> serializer = JsonSerializer ( data = { '<STR_LIT:content>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> actual = serializer . validated_data [ '<STR_LIT:content>' ] <EOL> self . assertEqual ( { u'<STR_LIT:key>' : u'<STR_LIT:value>' } , actual ) <EOL> def test_deserialization_fails_validation_on_incorrect_json ( self ) : <EOL> serializer = JsonSerializer ( data = { '<STR_LIT:content>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( serializer . is_valid ( ) ) <EOL> self . assertIn ( '<STR_LIT:content>' , serializer . errors , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , <EOL> serializer . errors [ '<STR_LIT:content>' ] ) <EOL> class TimestampSerializer ( serializers . Serializer ) : <EOL> content = TimestampField ( ) <EOL> class TimestampFieldTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . datetime = utils . timeshift ( days = - <NUM_LIT:1> ) <EOL> self . timestamp = utils . datetime_to_timestamp ( self . datetime ) <EOL> def test_datetime_serialized_as_timestamp ( self ) : <EOL> serializer = TimestampSerializer ( instance = { '<STR_LIT:content>' : self . datetime } ) <EOL> actual = serializer . data [ '<STR_LIT:content>' ] <EOL> self . assertEqual ( self . timestamp , actual ) <EOL> def test_timestamp_parsed_as_datetime ( self ) : <EOL> serializer = TimestampSerializer ( data = { '<STR_LIT:content>' : str ( self . timestamp ) } ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> actual = serializer . validated_data [ '<STR_LIT:content>' ] <EOL> self . assertEqual ( self . datetime , actual ) <EOL> def test_incorrect_timestamp ( self ) : <EOL> serializer = TimestampSerializer ( data = { '<STR_LIT:content>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( serializer . is_valid ( ) ) <EOL> self . assertIn ( '<STR_LIT:content>' , serializer . errors , <EOL> '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , <EOL> serializer . errors [ '<STR_LIT:content>' ] ) <EOL> class RestrictedSerializer ( RestrictedSerializerMixin , serializers . Serializer ) : <EOL> name = serializers . ReadOnlyField ( ) <EOL> url = serializers . ReadOnlyField ( ) <EOL> id = serializers . ReadOnlyField ( ) <EOL> class RestrictedSerializerView ( APIView ) : <EOL> def get ( self , request ) : <EOL> User = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT:url>' , '<STR_LIT:id>' ) ) <EOL> user = User ( name = '<STR_LIT>' , url = '<STR_LIT>' , id = <NUM_LIT:1> ) <EOL> serializer = RestrictedSerializer ( user , context = { '<STR_LIT>' : request } ) <EOL> return Response ( serializer . data ) <EOL> class RestrictedSerializerTest ( APISimpleTestCase ) : <EOL> def test_serializer_returns_fields_required_in_request ( self ) : <EOL> fields = [ '<STR_LIT:name>' , '<STR_LIT:url>' ] <EOL> response = self . make_request ( fields ) <EOL> self . assertEqual ( fields , response . data . keys ( ) ) <EOL> def make_request ( self , fields ) : <EOL> from nodeconductor . structure . tests . factories import UserFactory <EOL> request = APIRequestFactory ( ) . get ( '<STR_LIT:/>' , { '<STR_LIT>' : fields } ) <EOL> force_authenticate ( request , UserFactory ( ) ) <EOL> response = RestrictedSerializerView . as_view ( ) ( request ) <EOL> return response </s>
