|
import urlparse
|
|
import pytest
|
|
import json
|
|
from uuid import uuid4
|
|
import os
|
|
import re
|
|
import urllib
|
|
|
|
from django.core.urlresolvers import reverse
|
|
from django.utils.http import urlencode
|
|
from django.core import mail, signing
|
|
from django.utils import timezone
|
|
from django.core.files.storage import DefaultStorage
|
|
from django.core.urlresolvers import reverse
|
|
from django.conf import settings
|
|
|
|
from corbo.models import Category, Announce, Subscription, Broadcast
|
|
from corbo.models import channel_choices
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
CATEGORIES = ('Alerts', 'News')
|
|
|
|
|
|
@pytest.fixture
|
|
def categories():
|
|
categories = []
|
|
for category in CATEGORIES:
|
|
c, created = Category.objects.get_or_create(name=category)
|
|
categories.append(c)
|
|
return categories
|
|
|
|
@pytest.fixture
|
|
def announces():
|
|
announces = []
|
|
for category in Category.objects.all():
|
|
a = Announce.objects.create(category=category, title='Announce 1',
|
|
publication_time=timezone.now(),
|
|
text='<h2>Announce 1</h2>')
|
|
Broadcast.objects.create(announce=a)
|
|
announces.append(a)
|
|
a = Announce.objects.create(category=category, title='Announce 2',
|
|
publication_time=timezone.now(),
|
|
text='<h2>Announce 2</h2>')
|
|
Broadcast.objects.create(announce=a)
|
|
announces.append(a)
|
|
return announces
|
|
|
|
def test_emailing_with_no_subscriptions(app, categories, announces):
|
|
for announce in announces:
|
|
broadcast = Broadcast.objects.get(announce=announce)
|
|
broadcast.send()
|
|
assert not broadcast.result
|
|
assert not mail.outbox
|
|
|
|
def test_send_email(app, categories, announces):
|
|
for category in categories:
|
|
uuid = uuid4()
|
|
s = Subscription.objects.create(category=category,
|
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
|
for announce in announces:
|
|
broadcast= Broadcast.objects.get(announce=announce)
|
|
broadcast.send()
|
|
assert broadcast.result
|
|
assert mail.outbox
|
|
|
|
def test_check_inline_css(app, categories, announces):
|
|
for announce in announces:
|
|
announce.text = '<style type="text/css">h2 {color: #F00}</style>' + announce.text
|
|
announce.save()
|
|
uuid = uuid4()
|
|
s = Subscription.objects.create(category=announce.category,
|
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
|
broadcast = Broadcast.objects.get(announce=announce)
|
|
broadcast.send()
|
|
assert broadcast.result
|
|
assert mail.outbox
|
|
assert 'h2 style="color:#F00"' in mail.outbox[0].html
|
|
mail.outbox = []
|
|
|
|
def test_check_inline_images(app, categories, announces):
|
|
storage = DefaultStorage()
|
|
media_path = os.path.join(os.path.dirname(__file__), 'media')
|
|
image_name = 'logo.png'
|
|
storage.save(image_name, file(os.path.join(media_path, image_name)))
|
|
for announce in announces:
|
|
announce.text = announce.text + '<img src="/media/%s" />' % image_name
|
|
announce.save()
|
|
uuid = uuid4()
|
|
s = Subscription.objects.create(category=announce.category,
|
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
|
broadcast = Broadcast.objects.get(announce=announce)
|
|
broadcast.send()
|
|
assert broadcast.result
|
|
assert mail.outbox
|
|
assert storage.url(image_name) in mail.outbox[0].attachments.keys()
|
|
mail.outbox = []
|
|
storage.delete(image_name)
|
|
|
|
def test_unsubscription_link(app, categories, announces):
|
|
for category in categories:
|
|
uuid = uuid4()
|
|
scheme = 'mailto:'
|
|
uri = scheme + '%s@example.net' % uuid
|
|
s = Subscription.objects.create(category=category,
|
|
identifier=uri,
|
|
uuid=str(uuid))
|
|
for announce in announces:
|
|
if announce.category != category:
|
|
continue
|
|
broadcast= Broadcast.objects.get(announce=announce)
|
|
broadcast.send()
|
|
assert broadcast.result
|
|
assert mail.outbox
|
|
|
|
signature = urllib.unquote(re.findall('/unsubscribe/(.*)"', mail.outbox[0].html)[0])
|
|
unsubscription_link = reverse('unsubscribe', kwargs={'unsubscription_token': signature})
|
|
assert signing.loads(signature) == {
|
|
'category': announce.category.pk, 'identifier': uri}
|
|
assert mail.outbox[0].subject == announce.title
|
|
assert unsubscription_link in mail.outbox[0].html
|
|
assert unsubscription_link in mail.outbox[0].text
|
|
mail.outbox = []
|
|
# make sure the uri schema is not in the page
|
|
resp = app.get(unsubscription_path)
|
|
assert scheme not in resp.content
|