|
| 1 | +import json |
1 | 2 | import mock |
2 | 3 | from django.test import TestCase |
3 | 4 | from django.utils import timezone |
4 | 5 | from push_notifications.models import GCMDevice, APNSDevice |
5 | | -from tests.mock_responses import GCM_PLAIN_RESPONSE |
| 6 | +from tests.mock_responses import GCM_PLAIN_RESPONSE, GCM_MULTIPLE_JSON_RESPONSE |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class ModelTestCase(TestCase): |
9 | | - def test_can_save_gcm_device(self): |
10 | | - device = GCMDevice.objects.create( |
11 | | - registration_id="a valid registration id" |
12 | | - ) |
13 | | - assert device.id is not None |
14 | | - assert device.date_created is not None |
15 | | - assert device.date_created.date() == timezone.now().date() |
16 | | - |
17 | | - def test_can_create_save_device(self): |
18 | | - device = APNSDevice.objects.create( |
19 | | - registration_id="a valid registration id" |
20 | | - ) |
21 | | - assert device.id is not None |
22 | | - assert device.date_created is not None |
23 | | - assert device.date_created.date() == timezone.now().date() |
24 | | - |
25 | | - def test_gcm_send_message(self): |
26 | | - device = GCMDevice.objects.create( |
27 | | - registration_id="abc", |
28 | | - ) |
29 | | - with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_PLAIN_RESPONSE) as p: |
30 | | - device.send_message("Hello world") |
31 | | - p.assert_called_once_with( |
32 | | - b"data.message=Hello+world®istration_id=abc", |
33 | | - "application/x-www-form-urlencoded;charset=UTF-8") |
34 | | - |
35 | | - def test_gcm_send_message_extra(self): |
36 | | - device = GCMDevice.objects.create( |
37 | | - registration_id="abc", |
38 | | - ) |
39 | | - with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_PLAIN_RESPONSE) as p: |
40 | | - device.send_message("Hello world", extra={"foo": "bar"}) |
41 | | - p.assert_called_once_with( |
42 | | - b"data.foo=bar&data.message=Hello+world®istration_id=abc", |
43 | | - "application/x-www-form-urlencoded;charset=UTF-8") |
44 | | - |
45 | | - def test_apns_send_message(self): |
46 | | - device = APNSDevice.objects.create( |
47 | | - registration_id="abc", |
48 | | - ) |
49 | | - socket = mock.MagicMock() |
50 | | - with mock.patch("push_notifications.apns._apns_pack_frame") as p: |
51 | | - device.send_message("Hello world", socket=socket, expiration=1) |
52 | | - p.assert_called_once_with("abc", b'{"aps":{"alert":"Hello world"}}', 0, 1, 10) |
53 | | - |
54 | | - def test_apns_send_message_extra(self): |
55 | | - device = APNSDevice.objects.create( |
56 | | - registration_id="abc", |
57 | | - ) |
58 | | - socket = mock.MagicMock() |
59 | | - with mock.patch("push_notifications.apns._apns_pack_frame") as p: |
60 | | - device.send_message("Hello world", extra={"foo": "bar"}, socket=socket, identifier=1, expiration=2, priority=5) |
61 | | - p.assert_called_once_with("abc", b'{"aps":{"alert":"Hello world"},"foo":"bar"}', 1, 2, 5) |
| 10 | + def test_can_save_gcm_device(self): |
| 11 | + device = GCMDevice.objects.create( |
| 12 | + registration_id="a valid registration id" |
| 13 | + ) |
| 14 | + assert device.id is not None |
| 15 | + assert device.date_created is not None |
| 16 | + assert device.date_created.date() == timezone.now().date() |
| 17 | + |
| 18 | + def test_can_create_save_device(self): |
| 19 | + device = APNSDevice.objects.create( |
| 20 | + registration_id="a valid registration id" |
| 21 | + ) |
| 22 | + assert device.id is not None |
| 23 | + assert device.date_created is not None |
| 24 | + assert device.date_created.date() == timezone.now().date() |
| 25 | + |
| 26 | + def test_gcm_send_message(self): |
| 27 | + device = GCMDevice.objects.create( |
| 28 | + registration_id="abc", |
| 29 | + ) |
| 30 | + with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_PLAIN_RESPONSE) as p: |
| 31 | + device.send_message("Hello world") |
| 32 | + p.assert_called_once_with( |
| 33 | + b"data.message=Hello+world®istration_id=abc", |
| 34 | + "application/x-www-form-urlencoded;charset=UTF-8") |
| 35 | + |
| 36 | + def test_gcm_send_message_extra(self): |
| 37 | + device = GCMDevice.objects.create( |
| 38 | + registration_id="abc", |
| 39 | + ) |
| 40 | + with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_PLAIN_RESPONSE) as p: |
| 41 | + device.send_message("Hello world", extra={"foo": "bar"}) |
| 42 | + p.assert_called_once_with( |
| 43 | + b"data.foo=bar&data.message=Hello+world®istration_id=abc", |
| 44 | + "application/x-www-form-urlencoded;charset=UTF-8") |
| 45 | + |
| 46 | + def test_gcm_send_message_collapse_key(self): |
| 47 | + device = GCMDevice.objects.create( |
| 48 | + registration_id="abc", |
| 49 | + ) |
| 50 | + with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_PLAIN_RESPONSE) as p: |
| 51 | + device.send_message("Hello world", collapse_key="test_key") |
| 52 | + p.assert_called_once_with( |
| 53 | + b"collapse_key=test_key&data.message=Hello+world®istration_id=abc", |
| 54 | + "application/x-www-form-urlencoded;charset=UTF-8") |
| 55 | + |
| 56 | + def test_gcm_send_message_to_multiple_devices(self): |
| 57 | + GCMDevice.objects.create( |
| 58 | + registration_id="abc", |
| 59 | + ) |
| 60 | + |
| 61 | + GCMDevice.objects.create( |
| 62 | + registration_id="abc1", |
| 63 | + ) |
| 64 | + |
| 65 | + with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_MULTIPLE_JSON_RESPONSE) as p: |
| 66 | + GCMDevice.objects.all().send_message("Hello world") |
| 67 | + p.assert_called_once_with( |
| 68 | + json.dumps({ |
| 69 | + "data": { "message": "Hello world" }, |
| 70 | + "registration_ids": ["abc", "abc1"] |
| 71 | + }, separators=(",", ":"), sort_keys=True).encode("utf-8"), "application/json") |
| 72 | + |
| 73 | + def test_gcm_send_message_extra_to_multiple_devices(self): |
| 74 | + GCMDevice.objects.create( |
| 75 | + registration_id="abc", |
| 76 | + ) |
| 77 | + |
| 78 | + GCMDevice.objects.create( |
| 79 | + registration_id="abc1", |
| 80 | + ) |
| 81 | + |
| 82 | + with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_MULTIPLE_JSON_RESPONSE) as p: |
| 83 | + GCMDevice.objects.all().send_message("Hello world", extra={"foo": "bar"}) |
| 84 | + p.assert_called_once_with( |
| 85 | + json.dumps({ |
| 86 | + "data": { "foo": "bar", "message": "Hello world" }, |
| 87 | + "registration_ids": ["abc", "abc1"] |
| 88 | + }, separators=(",", ":"), sort_keys=True).encode("utf-8"), "application/json") |
| 89 | + |
| 90 | + def test_gcm_send_message_collapse_to_multiple_devices(self): |
| 91 | + GCMDevice.objects.create( |
| 92 | + registration_id="abc", |
| 93 | + ) |
| 94 | + |
| 95 | + GCMDevice.objects.create( |
| 96 | + registration_id="abc1", |
| 97 | + ) |
| 98 | + |
| 99 | + with mock.patch("push_notifications.gcm._gcm_send", return_value=GCM_MULTIPLE_JSON_RESPONSE) as p: |
| 100 | + GCMDevice.objects.all().send_message("Hello world", collapse_key="test_key") |
| 101 | + p.assert_called_once_with( |
| 102 | + json.dumps({ |
| 103 | + "collapse_key": "test_key", |
| 104 | + "data": { "message": "Hello world" }, |
| 105 | + "registration_ids": ["abc", "abc1"] |
| 106 | + }, separators=(",", ":"), sort_keys=True).encode("utf-8"), "application/json") |
| 107 | + |
| 108 | + def test_apns_send_message(self): |
| 109 | + device = APNSDevice.objects.create( |
| 110 | + registration_id="abc", |
| 111 | + ) |
| 112 | + socket = mock.MagicMock() |
| 113 | + with mock.patch("push_notifications.apns._apns_pack_frame") as p: |
| 114 | + device.send_message("Hello world", socket=socket, expiration=1) |
| 115 | + p.assert_called_once_with("abc", b'{"aps":{"alert":"Hello world"}}', 0, 1, 10) |
| 116 | + |
| 117 | + def test_apns_send_message_extra(self): |
| 118 | + device = APNSDevice.objects.create( |
| 119 | + registration_id="abc", |
| 120 | + ) |
| 121 | + socket = mock.MagicMock() |
| 122 | + with mock.patch("push_notifications.apns._apns_pack_frame") as p: |
| 123 | + device.send_message("Hello world", extra={"foo": "bar"}, socket=socket, identifier=1, expiration=2, priority=5) |
| 124 | + p.assert_called_once_with("abc", b'{"aps":{"alert":"Hello world"},"foo":"bar"}', 1, 2, 5) |
0 commit comments