This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
/
test_register_device.py
145 lines (116 loc) · 4.77 KB
/
test_register_device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
Tests for registering a device
"""
from django.test import TestCase
from uuid import uuid4
# Third party
import responses
# Local stuff
from .factories import TestUserFactory, request_register_callback
from push_notifications.services.zeropush import ZEROPUSH_REGISTER_URL
from push_notifications.models import PushDevice
from push_notifications.utils import register_push_device
class RegisterDeviceTests(TestCase):
def _create_token(self):
return uuid4()
def _create_user(self):
return TestUserFactory.create()
@responses.activate
def test_register_device_manager(self):
"""
Test if register_device() on the manager works as expected
"""
responses.add_callback(
responses.POST, ZEROPUSH_REGISTER_URL,
callback=request_register_callback,
content_type='application/json',
)
user = TestUserFactory.create()
device = PushDevice.objects.register_push_device(
user, self._create_token())
assert device is not None
assert device.user.pk == user.pk
@responses.activate
def test_register_device_manager_notify_types(self):
"""
Test if manager.register_push_device() accepts notify_types
"""
responses.add_callback(
responses.POST, ZEROPUSH_REGISTER_URL,
callback=request_register_callback,
content_type='application/json',
)
user = self._create_user()
device = PushDevice.objects.register_push_device(
user, self._create_token(), notify_types='likes')
notification = device.notification_settings.first()
assert device.notification_settings.count() == 1
assert notification.name == 'likes'
device = PushDevice.objects.register_push_device(
user, self._create_token(), notify_types=['likes', 'comments'])
assert device.notification_settings.count() == 2
notification_likes = device.notification_settings.filter(name='likes').first()
assert notification_likes.send is True
notification_comments = device.notification_settings.filter(name='comments').first()
assert notification_comments.send is True
@responses.activate
def test_register_device_service(self):
"""
Tests if register_push_device in services works as expected
"""
responses.add_callback(
responses.POST, ZEROPUSH_REGISTER_URL,
callback=request_register_callback,
content_type='application/json',
)
user = self._create_user()
device = register_push_device(user, self._create_token())
assert device is not None
assert device.user.pk == user.pk
@responses.activate
def test_register_device_token_duplicate_removed(self):
"""
Test if other push devices with the same token are getting
deleted.
"""
responses.add_callback(
responses.POST, ZEROPUSH_REGISTER_URL,
callback=request_register_callback,
content_type='application/json',
)
user = self._create_user()
token = self._create_token()
PushDevice.objects.register_push_device(
user, token, notify_types='likes')
# Create other user
other_user = self._create_user()
PushDevice.objects.register_push_device(
other_user, token)
assert PushDevice.objects.filter(token=token).count() == 1
@responses.activate
def test_register_device_service_notify_types(self):
"""
Tests if register_push_device in services workw with extra
notice_types
"""
responses.add_callback(
responses.POST, ZEROPUSH_REGISTER_URL,
callback=request_register_callback,
content_type='application/json',
)
user = self._create_user()
device = register_push_device(user, self._create_token(), notice_types='likes')
# Check if notice_types has 'likes' in it
notification = device.notification_settings.first()
assert device.notification_settings.count() == 1
assert notification.name == 'likes'
assert notification.send is True
# Test with multiple notice_types
device = register_push_device(user, self._create_token(),
notice_types=['likes', 'comments'])
# Check if notice_types has 'likes' and 'comments'
assert device.notification_settings.count() == 2
notification_likes = device.notification_settings.filter(name='likes').first()
assert notification_likes.send is True
notification_comments = device.notification_settings.filter(name='comments').first()
assert notification_comments.send is True