@@ -537,11 +537,9 @@ def test_apns_payload(self):
537537
538538class TestApsEncoder (object ):
539539
540- def _check_aps (self , aps ):
541- with pytest .raises (ValueError ) as excinfo :
542- check_encoding (messaging .Message (
543- topic = 'topic' , apns = messaging .APNSConfig (payload = messaging .APNSPayload (aps = aps ))))
544- return excinfo
540+ def _encode_aps (self , aps ):
541+ return check_encoding (messaging .Message (
542+ topic = 'topic' , apns = messaging .APNSConfig (payload = messaging .APNSPayload (aps = aps ))))
545543
546544 @pytest .mark .parametrize ('data' , NON_OBJECT_ARGS )
547545 def test_invalid_aps (self , data ):
@@ -555,38 +553,68 @@ def test_invalid_aps(self, data):
555553 @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
556554 def test_invalid_alert (self , data ):
557555 aps = messaging .Aps (alert = data )
558- excinfo = self ._check_aps (aps )
556+ with pytest .raises (ValueError ) as excinfo :
557+ self ._encode_aps (aps )
559558 expected = 'Aps.alert must be a string or an instance of ApsAlert class.'
560559 assert str (excinfo .value ) == expected
561560
562561 @pytest .mark .parametrize ('data' , [list (), tuple (), dict (), 'foo' ])
563562 def test_invalid_badge (self , data ):
564563 aps = messaging .Aps (badge = data )
565- excinfo = self ._check_aps (aps )
564+ with pytest .raises (ValueError ) as excinfo :
565+ self ._encode_aps (aps )
566566 expected = 'Aps.badge must be a number.'
567567 assert str (excinfo .value ) == expected
568568
569569 @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
570570 def test_invalid_sound (self , data ):
571571 aps = messaging .Aps (sound = data )
572- excinfo = self ._check_aps (aps )
572+ with pytest .raises (ValueError ) as excinfo :
573+ self ._encode_aps (aps )
573574 expected = 'Aps.sound must be a string.'
574575 assert str (excinfo .value ) == expected
575576
576577 @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
577578 def test_invalid_category (self , data ):
578579 aps = messaging .Aps (category = data )
579- excinfo = self ._check_aps (aps )
580+ with pytest .raises (ValueError ) as excinfo :
581+ self ._encode_aps (aps )
580582 expected = 'Aps.category must be a string.'
581583 assert str (excinfo .value ) == expected
582584
583585 @pytest .mark .parametrize ('data' , NON_STRING_ARGS )
584586 def test_invalid_thread_id (self , data ):
585587 aps = messaging .Aps (thread_id = data )
586- excinfo = self ._check_aps (aps )
588+ with pytest .raises (ValueError ) as excinfo :
589+ self ._encode_aps (aps )
587590 expected = 'Aps.thread_id must be a string.'
588591 assert str (excinfo .value ) == expected
589592
593+ @pytest .mark .parametrize ('data' , ['' , list (), tuple (), True , False , 1 , 0 , ])
594+ def test_invalid_custom_data_dict (self , data ):
595+ if isinstance (data , dict ):
596+ return
597+ aps = messaging .Aps (custom_data = data )
598+ with pytest .raises (ValueError ) as excinfo :
599+ self ._encode_aps (aps )
600+ expected = 'Aps.custom_data must be a dict.'
601+ assert str (excinfo .value ) == expected
602+
603+ @pytest .mark .parametrize ('data' , [True , False , 1 , 0 ])
604+ def test_invalid_custom_field_name (self , data ):
605+ aps = messaging .Aps (custom_data = {data : 'foo' })
606+ with pytest .raises (ValueError ) as excinfo :
607+ self ._encode_aps (aps )
608+ expected = 'Aps.custom_data key must be a string.'
609+ assert str (excinfo .value ) == expected
610+
611+ def test_multiple_field_specifications (self ):
612+ aps = messaging .Aps (thread_id = 'foo' , custom_data = {'thread-id' : 'foo' })
613+ with pytest .raises (ValueError ) as excinfo :
614+ self ._encode_aps (aps )
615+ expected = 'Multiple specifications for thread-id in Aps.'
616+ assert str (excinfo .value ) == expected
617+
590618 def test_aps (self ):
591619 msg = messaging .Message (
592620 topic = 'topic' ,
@@ -597,6 +625,7 @@ def test_aps(self):
597625 badge = 42 ,
598626 sound = 's' ,
599627 content_available = True ,
628+ mutable_content = True ,
600629 category = 'c' ,
601630 thread_id = 't'
602631 ),
@@ -612,6 +641,7 @@ def test_aps(self):
612641 'badge' : 42 ,
613642 'sound' : 's' ,
614643 'content-available' : 1 ,
644+ 'mutable-content' : 1 ,
615645 'category' : 'c' ,
616646 'thread-id' : 't' ,
617647 },
@@ -620,6 +650,32 @@ def test_aps(self):
620650 }
621651 check_encoding (msg , expected )
622652
653+ def test_aps_custom_data (self ):
654+ msg = messaging .Message (
655+ topic = 'topic' ,
656+ apns = messaging .APNSConfig (
657+ payload = messaging .APNSPayload (
658+ aps = messaging .Aps (
659+ alert = 'alert text' ,
660+ custom_data = {'k1' : 'v1' , 'k2' : 1 },
661+ ),
662+ )
663+ )
664+ )
665+ expected = {
666+ 'topic' : 'topic' ,
667+ 'apns' : {
668+ 'payload' : {
669+ 'aps' : {
670+ 'alert' : 'alert text' ,
671+ 'k1' : 'v1' ,
672+ 'k2' : 1 ,
673+ },
674+ }
675+ },
676+ }
677+ check_encoding (msg , expected )
678+
623679
624680class TestApsAlertEncoder (object ):
625681
0 commit comments