combo/tests/test_pwa.py

62 lines
2.3 KiB
Python

import os
import mock
import pytest
try:
import pywebpush
except ImportError:
pywebpush = None
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import override_settings
from combo.apps.notifications.models import Notification
from combo.apps.pwa.models import PushSubscription
from .test_manager import login
pytestmark = pytest.mark.django_db
def test_manifest_json(app):
app.get('/manifest.json', status=404)
templates_settings = [settings.TEMPLATES[0].copy()]
templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]
with override_settings(TEMPLATES=templates_settings):
assert app.get('/manifest.json', status=200).json['name'] == 'test'
def test_service_worker(app):
app.get('/service-worker.js', status=200)
app.get('/service-worker-registration.js', status=200)
def test_webpush_subscription(app, john_doe, jane_doe):
app.post_json(reverse('pwa-subscribe-push'), params={'sample': 'content'}, status=403)
app.get(reverse('pwa-subscribe-push'), status=403)
app = login(app, john_doe.username, john_doe.username)
app.post_json(reverse('pwa-subscribe-push'), params={'sample': 'content'}, status=200)
assert PushSubscription.objects.count() == 1
app.post_json(reverse('pwa-subscribe-push'), params={'sample': 'content2'}, status=200)
assert PushSubscription.objects.count() == 2
app = login(app, jane_doe.username, jane_doe.username)
app.post_json(reverse('pwa-subscribe-push'), params={'sample': 'content'}, status=200)
assert PushSubscription.objects.count() == 3
app = login(app, john_doe.username, john_doe.username)
app.post_json(reverse('pwa-subscribe-push'), params=None, status=200)
assert PushSubscription.objects.count() == 1
@pytest.mark.skipif('pywebpush is None')
def test_webpush_notification(app, john_doe):
PushSubscription.objects.all().delete()
app = login(app, john_doe.username, john_doe.username)
app.post_json(reverse('pwa-subscribe-push'), params={'sample': 'content'}, status=200)
with mock.patch('pywebpush.webpush') as webpush:
notification = Notification.notify(john_doe, 'test', body='hello world')
assert webpush.call_count == 1
assert webpush.call_args[1]['subscription_info'] == {'sample': 'content'}