4 |
4 |
import pytest
|
5 |
5 |
from decimal import Decimal
|
6 |
6 |
|
7 |
|
from django.contrib.auth.models import User
|
8 |
7 |
from django.test.client import RequestFactory
|
9 |
8 |
from django.utils.timezone import timedelta, now
|
10 |
9 |
from django.core.urlresolvers import reverse
|
... | ... | |
20 |
19 |
client = Client()
|
21 |
20 |
|
22 |
21 |
|
23 |
|
@pytest.fixture
|
24 |
|
def user():
|
25 |
|
try:
|
26 |
|
admin = User.objects.get(username='admin')
|
27 |
|
except User.DoesNotExist:
|
28 |
|
admin = User.objects.create_user('admin', email=None, password='admin')
|
29 |
|
admin.email = 'admin@example.net'
|
30 |
|
admin.save()
|
31 |
|
return admin
|
32 |
|
|
33 |
|
|
34 |
|
@pytest.fixture
|
35 |
|
def user2():
|
36 |
|
try:
|
37 |
|
admin2 = User.objects.get(username='admin2')
|
38 |
|
except User.DoesNotExist:
|
39 |
|
admin2 = User.objects.create_user('admin2', email=None, password='admin2')
|
40 |
|
return admin2
|
41 |
|
|
42 |
|
|
43 |
|
def login(username='admin', password='admin'):
|
44 |
|
resp = client.post('/login/', {'username': username, 'password': password})
|
|
22 |
def login(user):
|
|
23 |
resp = client.post('/login/', {'username': user.username, 'password': user.username})
|
45 |
24 |
assert resp.status_code == 302
|
46 |
25 |
|
47 |
26 |
|
... | ... | |
60 |
39 |
return regie
|
61 |
40 |
|
62 |
41 |
|
63 |
|
def test_notification_api(user, user2):
|
64 |
|
notification = Notification.notify(user, 'notifoo')
|
|
42 |
def test_notification_api(john_doe, jane_doe):
|
|
43 |
notification = Notification.notify(john_doe, 'notifoo')
|
65 |
44 |
assert Notification.objects.count() == 1
|
66 |
45 |
assert notification.summary == 'notifoo'
|
67 |
46 |
assert notification.body == ''
|
... | ... | |
70 |
49 |
assert notification.external_id is None
|
71 |
50 |
assert notification.end_timestamp - notification.start_timestamp == timedelta(3)
|
72 |
51 |
assert notification.acked is False
|
73 |
|
Notification.objects.visible(user).ack()
|
|
52 |
Notification.objects.visible(john_doe).ack()
|
74 |
53 |
assert Notification.objects.get().acked is True
|
75 |
54 |
|
76 |
|
Notification.notify(user, 'notirefoo', id=str(notification.pk), acked=False)
|
|
55 |
Notification.notify(john_doe, 'notirefoo', id=str(notification.pk), acked=False)
|
77 |
56 |
assert Notification.objects.count() == 1
|
78 |
57 |
assert Notification.objects.get().summary == 'notirefoo'
|
79 |
58 |
# we updated the notification, it's un-acked
|
80 |
59 |
assert Notification.objects.get().acked is False
|
81 |
60 |
|
82 |
|
Notification.notify(user, 'notirefoo', id=str(notification.pk), duration=3600)
|
|
61 |
Notification.notify(john_doe, 'notirefoo', id=str(notification.pk), duration=3600)
|
83 |
62 |
noti = Notification.objects.get()
|
84 |
63 |
assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600)
|
85 |
64 |
|
86 |
|
notification = Notification.notify(user, 'notibar', id='ns:notibar')
|
|
65 |
notification = Notification.notify(john_doe, 'notibar', id='ns:notibar')
|
87 |
66 |
assert Notification.objects.count() == 2
|
88 |
|
notification = Notification.notify(user, 'notirebar', id='ns:notibar')
|
|
67 |
notification = Notification.notify(john_doe, 'notirebar', id='ns:notibar')
|
89 |
68 |
assert Notification.objects.count() == 2
|
90 |
69 |
|
91 |
|
notification = Notification.notify(user2, 'notiother')
|
|
70 |
notification = Notification.notify(jane_doe, 'notiother')
|
92 |
71 |
notification.forget()
|
93 |
|
assert Notification.objects.filter(user=user2).count() == 1
|
94 |
|
notification = Notification.objects.filter(user=user2).get()
|
|
72 |
assert Notification.objects.filter(user=jane_doe).count() == 1
|
|
73 |
notification = Notification.objects.filter(user=jane_doe).get()
|
95 |
74 |
assert notification.end_timestamp < now()
|
96 |
75 |
assert notification.acked is True
|
97 |
76 |
|
98 |
77 |
|
99 |
|
def test_notification_cell(user, user2):
|
|
78 |
def test_notification_cell(john_doe, jane_doe):
|
100 |
79 |
page = Page(title='notif', slug='test_notification_cell', template_name='standard')
|
101 |
80 |
page.save()
|
102 |
81 |
cell = NotificationsCell(page=page, placeholder='content', order=0)
|
... | ... | |
106 |
85 |
|
107 |
86 |
context['request'].user = None
|
108 |
87 |
assert cell.is_visible(context['request'].user) is False
|
109 |
|
context['request'].user = user
|
|
88 |
context['request'].user = john_doe
|
110 |
89 |
assert cell.is_visible(context['request'].user) is True
|
111 |
90 |
assert cell.get_badge(context) is None
|
112 |
91 |
|
113 |
|
notification1 = Notification.notify(user, 'notibar')
|
114 |
|
notification2 = Notification.notify(user, 'notifoo')
|
|
92 |
notification1 = Notification.notify(john_doe, 'notibar')
|
|
93 |
notification2 = Notification.notify(john_doe, 'notifoo')
|
115 |
94 |
content = cell.render(context)
|
116 |
95 |
assert 'notibar' in content
|
117 |
96 |
assert 'notifoo' in content
|
... | ... | |
123 |
102 |
assert 'notifoo' not in content
|
124 |
103 |
assert cell.get_badge(context) == {'badge': '1'}
|
125 |
104 |
|
126 |
|
Notification.notify(user, 'notirebar', id=str(notification1.pk))
|
|
105 |
Notification.notify(john_doe, 'notirebar', id=str(notification1.pk))
|
127 |
106 |
content = cell.render(context)
|
128 |
107 |
assert 'notirebar' in content
|
129 |
108 |
assert 'notibar' not in content
|
130 |
109 |
|
131 |
|
Notification.notify(user, 'notiurl', id=str(notification1.pk), url='https://www.example.net/')
|
|
110 |
Notification.notify(john_doe, 'notiurl', id=str(notification1.pk), url='https://www.example.net/')
|
132 |
111 |
content = cell.render(context)
|
133 |
112 |
assert 'notiurl' in content
|
134 |
113 |
assert 'https://www.example.net/' in content
|
135 |
114 |
|
136 |
|
notification3 = Notification.notify(user, 'ackme')
|
|
115 |
notification3 = Notification.notify(john_doe, 'ackme')
|
137 |
116 |
notification3.ack()
|
138 |
117 |
content = cell.render(context)
|
139 |
118 |
assert 'acked' in content
|
... | ... | |
143 |
122 |
content = cell.render(context)
|
144 |
123 |
assert cell.get_badge(context) is None
|
145 |
124 |
|
146 |
|
Notification.notify(user2, 'notiother')
|
|
125 |
Notification.notify(jane_doe, 'notiother')
|
147 |
126 |
content = cell.render(context)
|
148 |
127 |
assert 'notiurl' in content
|
149 |
128 |
assert 'notiother' not in content
|
150 |
129 |
assert cell.get_badge(context) is None
|
151 |
|
context['request'].user = user2
|
|
130 |
context['request'].user = jane_doe
|
152 |
131 |
content = cell.render(context)
|
153 |
132 |
assert 'notiurl' not in content
|
154 |
133 |
assert 'notiother' in content
|
155 |
134 |
assert cell.get_badge(context) == {'badge': '1'}
|
156 |
135 |
|
157 |
136 |
|
158 |
|
def test_notification_ws(user):
|
|
137 |
def test_notification_ws(john_doe):
|
159 |
138 |
|
160 |
139 |
def notify(data, check_id, count):
|
161 |
140 |
resp = client.post(reverse('api-notification-add'), json.dumps(data),
|
... | ... | |
163 |
142 |
assert resp.status_code == 200
|
164 |
143 |
result = json.loads(resp.content)
|
165 |
144 |
assert result == {'data': {'id': check_id}, 'err': 0}
|
166 |
|
assert Notification.objects.filter(user=user).count() == count
|
167 |
|
return Notification.objects.find(user, check_id).get()
|
|
145 |
assert Notification.objects.filter(user=john_doe).count() == count
|
|
146 |
return Notification.objects.find(john_doe, check_id).get()
|
168 |
147 |
|
169 |
|
login()
|
|
148 |
login(john_doe)
|
170 |
149 |
notify({'summary': 'foo'}, '1', 1)
|
171 |
150 |
notify({'summary': 'bar'}, '2', 2)
|
172 |
151 |
notify({'summary': 'bar', 'id': 'ns:noti3'}, 'ns:noti3', 3)
|
... | ... | |
203 |
182 |
resp = client.get(reverse('api-notification-forget', kwargs={'notification_id': '5'}))
|
204 |
183 |
assert resp.status_code == 200
|
205 |
184 |
assert Notification.objects.filter(acked=True).count() == 2
|
206 |
|
notif = Notification.objects.find(user, '5').get()
|
|
185 |
notif = Notification.objects.find(john_doe, '5').get()
|
207 |
186 |
assert notif.public_id == '5'
|
208 |
187 |
assert notif.acked is True
|
209 |
188 |
assert notif.end_timestamp < now()
|
210 |
189 |
|
211 |
190 |
|
212 |
|
def test_notification_ws_badrequest(user):
|
|
191 |
def test_notification_ws_badrequest(john_doe):
|
213 |
192 |
|
214 |
193 |
def check_error(data, message):
|
215 |
194 |
resp = client.post(reverse('api-notification-add'),
|
... | ... | |
220 |
199 |
assert result['err'] == 1
|
221 |
200 |
assert message in result['err_desc'].values()[0][0]
|
222 |
201 |
|
223 |
|
login()
|
|
202 |
login(john_doe)
|
224 |
203 |
check_error(None, 'required')
|
225 |
204 |
check_error('blahblah', 'Invalid data')
|
226 |
205 |
check_error({'summary': ''}, 'may not be blank')
|
... | ... | |
251 |
230 |
kwargs={'notification_id': 'noti1'}) == '/api/notification/forget/noti1/'
|
252 |
231 |
|
253 |
232 |
|
254 |
|
def test_notification_id_and_origin(user):
|
255 |
|
login()
|
|
233 |
def test_notification_id_and_origin(john_doe):
|
|
234 |
login(john_doe)
|
256 |
235 |
|
257 |
236 |
def notify(data):
|
258 |
237 |
resp = client.post(reverse('api-notification-add'), json.dumps(data),
|
... | ... | |
263 |
242 |
result = notify({'summary': 'foo', 'id': '1'})
|
264 |
243 |
assert result['err'] == 1
|
265 |
244 |
|
266 |
|
notification = Notification.notify(user, 'foo')
|
|
245 |
notification = Notification.notify(john_doe, 'foo')
|
267 |
246 |
|
268 |
247 |
result = notify({'summary': 'foo', 'id': str(notification.id)})
|
269 |
248 |
assert result['err'] == 0
|
... | ... | |
276 |
255 |
|
277 |
256 |
|
278 |
257 |
@mock.patch('combo.apps.lingo.models.requests.get')
|
279 |
|
def test_notify_remote_items(mock_get, app, user, user2, regie):
|
|
258 |
def test_notify_remote_items(mock_get, app, john_doe, jane_doe, regie):
|
280 |
259 |
|
281 |
260 |
datetime_format = '%Y-%m-%dT%H:%M:%S'
|
282 |
261 |
invoice_now = now()
|
... | ... | |
286 |
265 |
FAKE_PENDING_INVOICES = {
|
287 |
266 |
"data":
|
288 |
267 |
{
|
289 |
|
"admin": {
|
|
268 |
john_doe.username: {
|
290 |
269 |
"invoices": [
|
291 |
270 |
{
|
292 |
271 |
'id': '01',
|
... | ... | |
308 |
287 |
}
|
309 |
288 |
]
|
310 |
289 |
},
|
311 |
|
'admin2': {
|
|
290 |
jane_doe.username: {
|
312 |
291 |
'invoices': [
|
313 |
292 |
{
|
314 |
293 |
'id': '02',
|
... | ... | |
357 |
336 |
cell = ActiveItems(page=page, placeholder='content', order=0)
|
358 |
337 |
cell.save()
|
359 |
338 |
|
360 |
|
for user in FAKE_PENDING_INVOICES['data']:
|
361 |
|
for invoice in FAKE_PENDING_INVOICES['data'][user]['invoices']:
|
|
339 |
for john_doe in FAKE_PENDING_INVOICES['data']:
|
|
340 |
for invoice in FAKE_PENDING_INVOICES['data'][john_doe]['invoices']:
|
362 |
341 |
invoice['pay_limit_date'] = new_pay_limit_date
|
363 |
342 |
|
364 |
343 |
# create remind notifications
|
365 |
|
-
|