|
1 |
# -*- coding: utf-8 -*-
|
|
2 |
# combo-plugin-gnm - Combo WebPush App
|
|
3 |
# Copyright (C) 2018 Entr'ouvert
|
|
4 |
#
|
|
5 |
# This program is free software: you can redistribute it and/or modify it
|
|
6 |
# under the terms of the GNU Affero General Public License as published
|
|
7 |
# by the Free Software Foundation, either version 3 of the License, or
|
|
8 |
# (at your option) any later version.
|
|
9 |
#
|
|
10 |
# This program is distributed in the hope that it will be useful,
|
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
# GNU Affero General Public License for more details.
|
|
14 |
#
|
|
15 |
# You should have received a copy of the GNU Affero General Public License
|
|
16 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 |
|
|
18 |
import os
|
|
19 |
import base64
|
|
20 |
from datetime import datetime
|
|
21 |
|
|
22 |
import pytest
|
|
23 |
from mock import patch
|
|
24 |
|
|
25 |
import django
|
|
26 |
from django.core.management import call_command
|
|
27 |
from django.utils.timezone import make_aware
|
|
28 |
|
|
29 |
|
|
30 |
pytestmark = pytest.mark.django_db
|
|
31 |
|
|
32 |
|
|
33 |
if django.VERSION >= (1, 11):
|
|
34 |
from cryptography.hazmat.backends import default_backend
|
|
35 |
from cryptography.hazmat.primitives.asymmetric import ec
|
|
36 |
from push_notifications.models import WebPushDevice
|
|
37 |
from combo.apps.pwa.models import WebPushRecord
|
|
38 |
from combo.apps.notifications.models import Notification
|
|
39 |
|
|
40 |
@pytest.fixture
|
|
41 |
def subscription_keys_dict(recv_key=None, endpoint="https://example.com/"):
|
|
42 |
'''From pywebpush tests'''
|
|
43 |
recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
|
|
44 |
p256dh = base64.urlsafe_b64encode(
|
|
45 |
recv_key.public_key().public_numbers().encode_point()
|
|
46 |
).strip(b'=')
|
|
47 |
return {
|
|
48 |
"keys": {
|
|
49 |
'auth': base64.urlsafe_b64encode(os.urandom(16)).strip(b'='),
|
|
50 |
'p256dh': p256dh,
|
|
51 |
}
|
|
52 |
}
|
|
53 |
|
|
54 |
@pytest.fixture
|
|
55 |
def webpush_device(jane_doe, subscription_keys_dict):
|
|
56 |
return WebPushDevice.objects.create(
|
|
57 |
name='test-webpush-device',
|
|
58 |
browser='CHROME',
|
|
59 |
active=True,
|
|
60 |
auth=subscription_keys_dict['keys']['auth'],
|
|
61 |
p256dh=subscription_keys_dict['keys']['p256dh'],
|
|
62 |
user=jane_doe,
|
|
63 |
registration_id='test',
|
|
64 |
)
|
|
65 |
|
|
66 |
@pytest.fixture
|
|
67 |
def notification(jane_doe):
|
|
68 |
return Notification.notify(jane_doe, u'tèst headér', body=u'test uniçode bodéï')
|
|
69 |
|
|
70 |
@pytest.mark.skipif(django.VERSION < (1, 11),
|
|
71 |
reason="send_webpush requires Django >= 1.11")
|
|
72 |
@patch('pywebpush.WebPusher.send')
|
|
73 |
def test_command_send_webpush(mock_send, webpush_device, notification):
|
|
74 |
'''
|
|
75 |
Call the send_webpush management command and test dabatase data
|
|
76 |
'''
|
|
77 |
mock_send.return_value.status_code = 201
|
|
78 |
call_command('send_webpush', '--verbosity=1')
|
|
79 |
assert WebPushRecord.objects.filter(status=WebPushRecord.ERR_STATUS).count() == 0
|
|
80 |
assert WebPushRecord.objects.filter(status=WebPushRecord.DEFAULT_STATUS).count() == 0
|
|
81 |
ok_push = WebPushRecord.objects.filter(status=WebPushRecord.OK_STATUS)
|
|
82 |
assert ok_push.count() == 1
|
|
83 |
ok_push = ok_push.first()
|
|
84 |
assert ok_push.subscription == webpush_device
|
|
85 |
assert ok_push.notification == notification
|
|
86 |
assert ok_push.creation_date < make_aware(datetime.now())
|
0 |
|
-
|