pwa: add test_webpush (#25462)
This commit is contained in:
parent
7dbdf23e3b
commit
9e950ee28b
|
@ -62,3 +62,8 @@ FAMILY_SERVICE = {'root': '/'}
|
|||
|
||||
BOOKING_CALENDAR_CELL_ENABLED = True
|
||||
NEWSLETTERS_CELL_ENABLED = True
|
||||
|
||||
PUSH_NOTIFICATIONS_SETTINGS = {
|
||||
'WP_PRIVATE_KEY': '_uD4QQmmnbUvIkhVy556T1NbNn0FumiemxTBPLi8gx8',
|
||||
'APP_SERVER_KEY': 'BKmOfIj8pmiDAM3OlnjMbKttVEKjfyZRc80kRvdrI_Eqqw6iorkecYlq4DP_IKprt6FjNqMYMvNUiz3kbydZjIk'
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# combo-plugin-gnm - Combo WebPush App
|
||||
# Copyright (C) 2018 Entr'ouvert
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License as published
|
||||
# by the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import base64
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
from mock import patch
|
||||
|
||||
import django
|
||||
from django.core.management import call_command
|
||||
from django.utils.timezone import make_aware
|
||||
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
if django.VERSION >= (1, 11):
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
from push_notifications.models import WebPushDevice
|
||||
from combo.apps.pwa.models import WebPushRecord
|
||||
from combo.apps.notifications.models import Notification
|
||||
|
||||
@pytest.fixture
|
||||
def subscription_keys_dict(recv_key=None, endpoint="https://example.com/"):
|
||||
'''From pywebpush tests'''
|
||||
recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
|
||||
p256dh = base64.urlsafe_b64encode(
|
||||
recv_key.public_key().public_numbers().encode_point()
|
||||
).strip(b'=')
|
||||
return {
|
||||
"keys": {
|
||||
'auth': base64.urlsafe_b64encode(os.urandom(16)).strip(b'='),
|
||||
'p256dh': p256dh,
|
||||
}
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def webpush_device(jane_doe, subscription_keys_dict):
|
||||
return WebPushDevice.objects.create(
|
||||
name='test-webpush-device',
|
||||
browser='CHROME',
|
||||
active=True,
|
||||
auth=subscription_keys_dict['keys']['auth'],
|
||||
p256dh=subscription_keys_dict['keys']['p256dh'],
|
||||
user=jane_doe,
|
||||
registration_id='test',
|
||||
)
|
||||
|
||||
@pytest.fixture
|
||||
def notification(jane_doe):
|
||||
return Notification.notify(jane_doe, u'tèst headér', body=u'test uniçode bodéï')
|
||||
|
||||
@pytest.mark.skipif(django.VERSION < (1, 11),
|
||||
reason="send_webpush requires Django >= 1.11")
|
||||
@patch('pywebpush.WebPusher.send')
|
||||
def test_command_send_webpush(mock_send, webpush_device, notification):
|
||||
'''
|
||||
Call the send_webpush management command and test dabatase data
|
||||
'''
|
||||
mock_send.return_value.status_code = 201
|
||||
call_command('send_webpush', '--verbosity=1')
|
||||
assert WebPushRecord.objects.filter(status=WebPushRecord.ERR_STATUS).count() == 0
|
||||
assert WebPushRecord.objects.filter(status=WebPushRecord.DEFAULT_STATUS).count() == 0
|
||||
ok_push = WebPushRecord.objects.filter(status=WebPushRecord.OK_STATUS)
|
||||
assert ok_push.count() == 1
|
||||
ok_push = ok_push.first()
|
||||
assert ok_push.subscription == webpush_device
|
||||
assert ok_push.notification == notification
|
||||
assert ok_push.creation_date < make_aware(datetime.now())
|
Loading…
Reference in New Issue