95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
# -*- 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
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import ec
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
import pytest
|
|
from mock import patch
|
|
|
|
from django.contrib.auth.models import User
|
|
from django.core.management import call_command
|
|
from django.utils.timezone import make_aware
|
|
|
|
from webpush.models import SubscriptionInfo, PushInformation
|
|
|
|
from combo.apps.notifications.models import Notification
|
|
import combo_plugin_gnm
|
|
from combo_plugin_gnm.models import WebPushRecord
|
|
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
|
|
@pytest.fixture
|
|
def user1():
|
|
user1 = User.objects.create_user('user1', email='user1@example.net', password='user1')
|
|
user1.save()
|
|
return user1
|
|
|
|
|
|
def _get_pubkey_str(priv_key):
|
|
'''From pywebpush tests'''
|
|
return base64.urlsafe_b64encode(
|
|
priv_key.public_key().public_numbers().encode_point()
|
|
).strip(b'=')
|
|
|
|
|
|
def _gen_subscription_info(recv_key=None,
|
|
endpoint="https://example.com/"):
|
|
'''From pywebpush tests'''
|
|
if not recv_key:
|
|
recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
|
|
return {
|
|
"endpoint": endpoint,
|
|
"keys": {
|
|
'auth': base64.urlsafe_b64encode(os.urandom(16)).strip(b'='),
|
|
'p256dh': _get_pubkey_str(recv_key),
|
|
}
|
|
}
|
|
|
|
|
|
@patch('pywebpush.WebPusher.send')
|
|
def test_command_send_webpush(mock_send, user1):
|
|
mock_send.return_value.status_code = 201
|
|
recv_key = ec.generate_private_key(ec.SECP256R1, default_backend())
|
|
subscription_keys_dict = _gen_subscription_info(recv_key)
|
|
subscription_record = SubscriptionInfo.objects.create(
|
|
browser='test browser',
|
|
endpoint=subscription_keys_dict['endpoint'],
|
|
auth=subscription_keys_dict['keys']['auth'],
|
|
p256dh=subscription_keys_dict['keys']['p256dh'],
|
|
)
|
|
push_info = PushInformation.objects.create(
|
|
user=user1,
|
|
subscription=subscription_record
|
|
)
|
|
notification = Notification.notify(user1, u'tèst headér', body=u'test uniçode bodéï')
|
|
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 == push_info
|
|
assert ok_push.notification == notification
|
|
assert ok_push.creation_date < make_aware(datetime.now())
|