@@ -537,11 +537,9 @@ def test_apns_payload(self):
537
537
538
538
class TestApsEncoder (object ):
539
539
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 ))))
545
543
546
544
@pytest .mark .parametrize ('data' , NON_OBJECT_ARGS )
547
545
def test_invalid_aps (self , data ):
@@ -555,38 +553,68 @@ def test_invalid_aps(self, data):
555
553
@pytest .mark .parametrize ('data' , NON_STRING_ARGS )
556
554
def test_invalid_alert (self , data ):
557
555
aps = messaging .Aps (alert = data )
558
- excinfo = self ._check_aps (aps )
556
+ with pytest .raises (ValueError ) as excinfo :
557
+ self ._encode_aps (aps )
559
558
expected = 'Aps.alert must be a string or an instance of ApsAlert class.'
560
559
assert str (excinfo .value ) == expected
561
560
562
561
@pytest .mark .parametrize ('data' , [list (), tuple (), dict (), 'foo' ])
563
562
def test_invalid_badge (self , data ):
564
563
aps = messaging .Aps (badge = data )
565
- excinfo = self ._check_aps (aps )
564
+ with pytest .raises (ValueError ) as excinfo :
565
+ self ._encode_aps (aps )
566
566
expected = 'Aps.badge must be a number.'
567
567
assert str (excinfo .value ) == expected
568
568
569
569
@pytest .mark .parametrize ('data' , NON_STRING_ARGS )
570
570
def test_invalid_sound (self , data ):
571
571
aps = messaging .Aps (sound = data )
572
- excinfo = self ._check_aps (aps )
572
+ with pytest .raises (ValueError ) as excinfo :
573
+ self ._encode_aps (aps )
573
574
expected = 'Aps.sound must be a string.'
574
575
assert str (excinfo .value ) == expected
575
576
576
577
@pytest .mark .parametrize ('data' , NON_STRING_ARGS )
577
578
def test_invalid_category (self , data ):
578
579
aps = messaging .Aps (category = data )
579
- excinfo = self ._check_aps (aps )
580
+ with pytest .raises (ValueError ) as excinfo :
581
+ self ._encode_aps (aps )
580
582
expected = 'Aps.category must be a string.'
581
583
assert str (excinfo .value ) == expected
582
584
583
585
@pytest .mark .parametrize ('data' , NON_STRING_ARGS )
584
586
def test_invalid_thread_id (self , data ):
585
587
aps = messaging .Aps (thread_id = data )
586
- excinfo = self ._check_aps (aps )
588
+ with pytest .raises (ValueError ) as excinfo :
589
+ self ._encode_aps (aps )
587
590
expected = 'Aps.thread_id must be a string.'
588
591
assert str (excinfo .value ) == expected
589
592
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
+
590
618
def test_aps (self ):
591
619
msg = messaging .Message (
592
620
topic = 'topic' ,
@@ -597,6 +625,7 @@ def test_aps(self):
597
625
badge = 42 ,
598
626
sound = 's' ,
599
627
content_available = True ,
628
+ mutable_content = True ,
600
629
category = 'c' ,
601
630
thread_id = 't'
602
631
),
@@ -612,6 +641,7 @@ def test_aps(self):
612
641
'badge' : 42 ,
613
642
'sound' : 's' ,
614
643
'content-available' : 1 ,
644
+ 'mutable-content' : 1 ,
615
645
'category' : 'c' ,
616
646
'thread-id' : 't' ,
617
647
},
@@ -620,6 +650,32 @@ def test_aps(self):
620
650
}
621
651
check_encoding (msg , expected )
622
652
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
+
623
679
624
680
class TestApsAlertEncoder (object ):
625
681
0 commit comments