33from django .test import TestCase
44from django .utils import timezone
55from push_notifications .models import GCMDevice , APNSDevice
6- from tests .mock_responses import GCM_PLAIN_RESPONSE , GCM_MULTIPLE_JSON_RESPONSE
7-
6+ from tests .mock_responses import GCM_PLAIN_RESPONSE , \
7+ GCM_MULTIPLE_JSON_RESPONSE , GCM_PLAIN_RESPONSE_ERROR , \
8+ GCM_JSON_RESPONSE_ERROR , GCM_PLAIN_RESPONSE_ERROR_B
9+ from push_notifications .gcm import GCMError
810
911class ModelTestCase (TestCase ):
1012 def test_can_save_gcm_device (self ):
@@ -105,6 +107,46 @@ def test_gcm_send_message_collapse_to_multiple_devices(self):
105107 "registration_ids" : ["abc" , "abc1" ]
106108 }, separators = ("," , ":" ), sort_keys = True ).encode ("utf-8" ), "application/json" )
107109
110+ def test_gcm_send_message_to_single_device_with_error (self ):
111+ # these errors are device specific, device.active will be set false
112+ device_list = ['abc' , 'abc1' ]
113+ self .create_devices (device_list )
114+ for index , error in enumerate (GCM_PLAIN_RESPONSE_ERROR ):
115+ with mock .patch ("push_notifications.gcm._gcm_send" ,
116+ return_value = error ) as p :
117+ device = GCMDevice .objects . \
118+ get (registration_id = device_list [index ])
119+ device .send_message ("Hello World!" )
120+ assert GCMDevice .objects .get (registration_id = device_list [index ]) \
121+ .active is False
122+
123+ def test_gcm_send_message_to_single_device_with_error_b (self ):
124+ # these errors are not device specific, GCMError should be thrown
125+ device_list = ['abc' ]
126+ self .create_devices (device_list )
127+ with mock .patch ("push_notifications.gcm._gcm_send" ,
128+ return_value = GCM_PLAIN_RESPONSE_ERROR_B ) as p :
129+ device = GCMDevice .objects . \
130+ get (registration_id = device_list [0 ])
131+ with self .assertRaises (GCMError ):
132+ device .send_message ("Hello World!" )
133+ assert GCMDevice .objects .get (registration_id = device_list [0 ]) \
134+ .active is True
135+
136+ def test_gcm_send_message_to_multiple_devices_with_error (self ):
137+ device_list = ['abc' , 'abc1' , 'abc2' ]
138+ self .create_devices (device_list )
139+ with mock .patch ("push_notifications.gcm._gcm_send" ,
140+ return_value = GCM_JSON_RESPONSE_ERROR ) as p :
141+ devices = GCMDevice .objects .all ()
142+ devices .send_message ("Hello World" )
143+ assert GCMDevice .objects .get (registration_id = device_list [0 ]) \
144+ .active is False
145+ assert GCMDevice .objects .get (registration_id = device_list [1 ]) \
146+ .active is True
147+ assert GCMDevice .objects .get (registration_id = device_list [2 ]) \
148+ .active is False
149+
108150 def test_apns_send_message (self ):
109151 device = APNSDevice .objects .create (
110152 registration_id = "abc" ,
@@ -122,3 +164,9 @@ def test_apns_send_message_extra(self):
122164 with mock .patch ("push_notifications.apns._apns_pack_frame" ) as p :
123165 device .send_message ("Hello world" , extra = {"foo" : "bar" }, socket = socket , identifier = 1 , expiration = 2 , priority = 5 )
124166 p .assert_called_once_with ("abc" , b'{"aps":{"alert":"Hello world"},"foo":"bar"}' , 1 , 2 , 5 )
167+
168+ def create_devices (self , devices ):
169+ for device in devices :
170+ GCMDevice .objects .create (
171+ registration_id = device ,
172+ )
0 commit comments