<s> import datetime , json , mock , unittest <EOL> from django . core . exceptions import ValidationError <EOL> from drf_to_s3 . models import Policy , PolicyCondition <EOL> from drf_to_s3 import naive_serializers <EOL> class TestNaivePolicyConditionFieldToNative ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . field = naive_serializers . NaivePolicyConditionField ( ) <EOL> def test_starts_with ( self ) : <EOL> cond = PolicyCondition ( <EOL> operator = '<STR_LIT>' , <EOL> element_name = '<STR_LIT:key>' , <EOL> value = '<STR_LIT>' <EOL> ) <EOL> result = self . field . to_native ( cond ) <EOL> expected = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEquals ( result , expected ) <EOL> def test_eq ( self ) : <EOL> cond = PolicyCondition ( <EOL> operator = '<STR_LIT>' , <EOL> element_name = '<STR_LIT>' , <EOL> value = '<STR_LIT>' <EOL> ) <EOL> result = self . field . to_native ( cond ) <EOL> expected = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . assertEquals ( result , expected ) <EOL> def test_no_oper ( self ) : <EOL> cond = PolicyCondition ( <EOL> element_name = '<STR_LIT>' , <EOL> value = '<STR_LIT>' <EOL> ) <EOL> result = self . field . to_native ( cond ) <EOL> expected = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . assertEquals ( result , expected ) <EOL> def test_range ( self ) : <EOL> cond = PolicyCondition ( <EOL> element_name = '<STR_LIT>' , <EOL> value_range = [ <NUM_LIT> , <NUM_LIT> ] <EOL> ) <EOL> result = self . field . to_native ( cond ) <EOL> expected = [ '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ] <EOL> self . assertEquals ( result , expected ) <EOL> class TestNaivePolicyConditionFieldFromNative ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . field = naive_serializers . NaivePolicyConditionField ( ) <EOL> def test_starts_with ( self ) : <EOL> cond = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> result = self . field . from_native ( cond ) <EOL> self . assertIsInstance ( result , PolicyCondition ) <EOL> self . assertEquals ( result . operator , '<STR_LIT>' ) <EOL> self . assertEquals ( result . element_name , '<STR_LIT:key>' ) , <EOL> self . assertEquals ( result . value , '<STR_LIT>' ) <EOL> self . assertIsNone ( result . value_range ) <EOL> def test_eq ( self ) : <EOL> cond = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> result = self . field . from_native ( cond ) <EOL> self . assertIsInstance ( result , PolicyCondition ) <EOL> self . assertEquals ( result . operator , '<STR_LIT>' ) <EOL> self . assertEquals ( result . element_name , '<STR_LIT>' ) , <EOL> self . assertEquals ( result . value , '<STR_LIT>' ) <EOL> self . assertIsNone ( result . value_range ) <EOL> def test_range ( self ) : <EOL> cond = [ "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> ] <EOL> result = self . field . from_native ( cond ) <EOL> self . assertIsInstance ( result , PolicyCondition ) <EOL> self . assertIsNone ( result . operator ) <EOL> self . assertEquals ( result . element_name , '<STR_LIT>' ) , <EOL> self . assertIsNone ( result . value ) <EOL> self . assertEquals ( result . value_range , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> def test_empty_array ( self ) : <EOL> cond = [ ] <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> def test_missing_args ( self ) : <EOL> cond = [ "<STR_LIT>" ] <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> def test_missing_key ( self ) : <EOL> cond = [ "<STR_LIT>" ] <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> def test_missing_dollar ( self ) : <EOL> cond = [ "<STR_LIT>" , "<STR_LIT:key>" ] <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , '<STR_LIT:key>' ) <EOL> def test_extra_args ( self ) : <EOL> cond = [ "<STR_LIT>" , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> def test_dict ( self ) : <EOL> cond = { "<STR_LIT>" : "<STR_LIT>" } <EOL> result = self . field . from_native ( cond ) <EOL> self . assertIsInstance ( result , PolicyCondition ) <EOL> self . assertIsNone ( result . operator ) <EOL> self . assertEquals ( result . element_name , '<STR_LIT>' ) <EOL> self . assertEquals ( result . value , '<STR_LIT>' ) <EOL> self . assertIsNone ( result . value_range ) <EOL> def test_dict_extra_keys ( self ) : <EOL> cond = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:foo>" : "<STR_LIT:bar>" } <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> def test_dict_invalid_values ( self ) : <EOL> cond = { "<STR_LIT>" : [ ] } <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertEquals ( exception . message , '<STR_LIT>' ) <EOL> def test_string ( self ) : <EOL> cond = '<STR_LIT>' <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> def test_number ( self ) : <EOL> cond = <NUM_LIT> <EOL> with self . assertRaises ( ValidationError ) as ctx : <EOL> self . field . from_native ( cond ) <EOL> exception = ctx . exception <EOL> self . assertTrue ( exception . message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( exception . params [ '<STR_LIT>' ] , cond ) <EOL> class NaivePolicySerializerTest ( unittest . TestCase ) : <EOL> class TestSerializer ( naive_serializers . NaivePolicySerializer ) : <EOL> allowed_buckets = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def setUp ( self ) : <EOL> self . serializer_class = self . TestSerializer <EOL> def test_that_serialize_with_bucket_and_key_succeeds ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> def test_that_serialize_with_duplicate_condition_fails ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> self . assertFalse ( serializer . is_valid ( ) ) <EOL> expected = [ '<STR_LIT>' ] <EOL> self . assertEquals ( serializer . errors [ '<STR_LIT>' ] , expected ) <EOL> def test_that_after_configuration_serialize_without_bucket_succeeds ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> serializer . required_conditions = [ ] <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> def test_that_after_configuration_serialize_with_extra_condition_succeeds ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> serializer . required_conditions = [ ] <EOL> serializer . optional_conditions = [ '<STR_LIT:foo>' ] <EOL> self . assertTrue ( serializer . is_valid ( ) ) <EOL> def test_that_serializer_invokes_validate_expiration ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> serializer . validate_expiration = mock . MagicMock ( ) <EOL> serializer . is_valid ( ) <EOL> self . assertTrue ( serializer . validate_expiration . called ) <EOL> def test_that_serializer_invokes_validate_condition_methods ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> serializer . optional_conditions = [ '<STR_LIT:foo>' ] <EOL> serializer . validate_condition_bucket = mock . MagicMock ( ) <EOL> serializer . validate_condition_foo = mock . MagicMock ( ) <EOL> serializer . is_valid ( ) <EOL> self . assertTrue ( serializer . validate_condition_bucket . called ) <EOL> self . assertTrue ( serializer . validate_condition_foo . called ) <EOL> def test_that_after_configuration_serialize_with_valid_content_type_succeeds ( self ) : <EOL> json_data = '''<STR_LIT>''' <EOL> data = json . loads ( json_data ) <EOL> serializer = self . serializer_class ( data = data ) <EOL> serializer . required_conditions = [ ] <EOL> self . assertTrue ( serializer . is_valid ( ) ) </s>
