Projet

Général

Profil

0005-pwa-add-test_webpush-25462.patch

Anonyme, 24 août 2018 17:21

Télécharger (3,93 ko)

Voir les différences:

Subject: [PATCH 5/6] pwa: add test_webpush (#25462)

 tests/test_webpush.py | 94 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 tests/test_webpush.py
tests/test_webpush.py
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
from cryptography.hazmat.primitives.asymmetric import ec
23
from cryptography.hazmat.backends import default_backend
24

  
25
import pytest
26
from mock import patch
27

  
28
import django
29
from django.contrib.auth.models import User
30
from django.core.management import call_command
31
from django.utils.timezone import make_aware
32

  
33

  
34
if django.VERSION >= (1, 11):
35
    from combo.apps.notifications.models import Notification
36
    from combo.apps.pwa.models import WebPushRecord
37
    from push_notifications.models import WebPushDevice
38

  
39

  
40
pytestmark = pytest.mark.django_db
41

  
42

  
43
@pytest.fixture
44
def user1():
45
    user1 = User.objects.create_user('user1', email='user1@example.net', password='user1')
46
    user1.save()
47
    return user1
48

  
49

  
50
def _get_pubkey_str(priv_key):
51
    '''From pywebpush tests'''
52
    return base64.urlsafe_b64encode(
53
        priv_key.public_key().public_numbers().encode_point()
54
    ).strip(b'=')
55

  
56

  
57
def _gen_subscription_info(recv_key=None,
58
                           endpoint="https://example.com/"):
59
    '''From pywebpush tests'''
60
    if not recv_key:
61
        recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
62
    return {
63
        "keys": {
64
            'auth': base64.urlsafe_b64encode(os.urandom(16)).strip(b'='),
65
            'p256dh': _get_pubkey_str(recv_key),
66
        }
67
    }
68

  
69

  
70
if django.VERSION >= (1, 11):
71
    @patch('pywebpush.WebPusher.send')
72
    def test_command_send_webpush(mock_send, user1):
73
        mock_send.return_value.status_code = 201
74
        recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
75
        subscription_keys_dict = _gen_subscription_info(recv_key)
76
        webpush_device = WebPushDevice.objects.create(
77
            name='test-webpush-device',
78
            browser='CHROME',
79
            active=True,
80
            auth=subscription_keys_dict['keys']['auth'],
81
            p256dh=subscription_keys_dict['keys']['p256dh'],
82
            user=user1,
83
            registration_id='test',
84
        )
85
        notification = Notification.notify(user1, u'tèst headér', body=u'test uniçode bodéï')
86
        call_command('send_webpush', '--verbosity=1')
87
        assert WebPushRecord.objects.filter(status=WebPushRecord.ERR_STATUS).count() == 0
88
        assert WebPushRecord.objects.filter(status=WebPushRecord.DEFAULT_STATUS).count() == 0
89
        ok_push = WebPushRecord.objects.filter(status=WebPushRecord.OK_STATUS)
90
        assert ok_push.count() == 1
91
        ok_push = ok_push.first()
92
        assert ok_push.subscription == webpush_device
93
        assert ok_push.notification == notification
94
        assert ok_push.creation_date < make_aware(datetime.now())
0
-