1
|
import urlparse
|
2
|
import pytest
|
3
|
import json
|
4
|
from uuid import uuid4
|
5
|
import os
|
6
|
import re
|
7
|
import urllib
|
8
|
|
9
|
from django.core.urlresolvers import reverse
|
10
|
from django.utils.http import urlencode
|
11
|
from django.core import mail, signing
|
12
|
from django.utils import timezone
|
13
|
from django.core.files.storage import DefaultStorage
|
14
|
from django.core.urlresolvers import reverse
|
15
|
from django.conf import settings
|
16
|
|
17
|
from corbo.models import Category, Announce, Subscription, Broadcast
|
18
|
from corbo.models import channel_choices
|
19
|
|
20
|
pytestmark = pytest.mark.django_db
|
21
|
|
22
|
CATEGORIES = ('Alerts', 'News')
|
23
|
|
24
|
|
25
|
@pytest.fixture
|
26
|
def categories():
|
27
|
categories = []
|
28
|
for category in CATEGORIES:
|
29
|
c, created = Category.objects.get_or_create(name=category)
|
30
|
categories.append(c)
|
31
|
return categories
|
32
|
|
33
|
@pytest.fixture
|
34
|
def announces():
|
35
|
announces = []
|
36
|
for category in Category.objects.all():
|
37
|
a = Announce.objects.create(category=category, title='Announce 1',
|
38
|
publication_time=timezone.now(),
|
39
|
text='<h2>Announce 1</h2>')
|
40
|
Broadcast.objects.create(announce=a)
|
41
|
announces.append(a)
|
42
|
a = Announce.objects.create(category=category, title='Announce 2',
|
43
|
publication_time=timezone.now(),
|
44
|
text='<h2>Announce 2</h2>')
|
45
|
Broadcast.objects.create(announce=a)
|
46
|
announces.append(a)
|
47
|
return announces
|
48
|
|
49
|
def test_emailing_with_no_subscriptions(app, categories, announces):
|
50
|
for announce in announces:
|
51
|
broadcast = Broadcast.objects.get(announce=announce)
|
52
|
broadcast.send()
|
53
|
assert not broadcast.result
|
54
|
assert not mail.outbox
|
55
|
|
56
|
def test_send_email(app, categories, announces):
|
57
|
for category in categories:
|
58
|
uuid = uuid4()
|
59
|
s = Subscription.objects.create(category=category,
|
60
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
61
|
for announce in announces:
|
62
|
broadcast= Broadcast.objects.get(announce=announce)
|
63
|
broadcast.send()
|
64
|
assert broadcast.result
|
65
|
assert mail.outbox
|
66
|
|
67
|
def test_check_inline_css(app, categories, announces):
|
68
|
for announce in announces:
|
69
|
announce.text = '<style type="text/css">h2 {color: #F00}</style>' + announce.text
|
70
|
announce.save()
|
71
|
uuid = uuid4()
|
72
|
s = Subscription.objects.create(category=announce.category,
|
73
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
74
|
broadcast = Broadcast.objects.get(announce=announce)
|
75
|
broadcast.send()
|
76
|
assert broadcast.result
|
77
|
assert mail.outbox
|
78
|
assert 'h2 style="color:#F00"' in mail.outbox[0].html
|
79
|
mail.outbox = []
|
80
|
|
81
|
def test_check_inline_images(app, categories, announces):
|
82
|
storage = DefaultStorage()
|
83
|
media_path = os.path.join(os.path.dirname(__file__), 'media')
|
84
|
image_name = 'logo.png'
|
85
|
storage.save(image_name, file(os.path.join(media_path, image_name)))
|
86
|
for announce in announces:
|
87
|
announce.text = announce.text + '<img src="/media/%s" />' % image_name
|
88
|
announce.save()
|
89
|
uuid = uuid4()
|
90
|
s = Subscription.objects.create(category=announce.category,
|
91
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
92
|
broadcast = Broadcast.objects.get(announce=announce)
|
93
|
broadcast.send()
|
94
|
assert broadcast.result
|
95
|
assert mail.outbox
|
96
|
assert storage.url(image_name) in mail.outbox[0].attachments.keys()
|
97
|
mail.outbox = []
|
98
|
storage.delete(image_name)
|
99
|
|
100
|
def test_unsubscription_link(app, categories, announces):
|
101
|
for category in categories:
|
102
|
uuid = uuid4()
|
103
|
scheme = 'mailto:'
|
104
|
uri = scheme + '%s@example.net' % uuid
|
105
|
s = Subscription.objects.create(category=category,
|
106
|
identifier=uri,
|
107
|
uuid=str(uuid))
|
108
|
for announce in announces:
|
109
|
if announce.category != category:
|
110
|
continue
|
111
|
broadcast= Broadcast.objects.get(announce=announce)
|
112
|
broadcast.send()
|
113
|
assert broadcast.result
|
114
|
assert mail.outbox
|
115
|
|
116
|
signature = urllib.unquote(re.findall('/unsubscribe/(.*)"', mail.outbox[0].html)[0])
|
117
|
unsubscription_link = reverse('unsubscribe', kwargs={'unsubscription_token': signature})
|
118
|
assert signing.loads(signature) == {
|
119
|
'category': announce.category.pk, 'identifier': uri}
|
120
|
assert mail.outbox[0].subject == announce.title
|
121
|
assert unsubscription_link in mail.outbox[0].html
|
122
|
assert unsubscription_link in mail.outbox[0].text
|
123
|
mail.outbox = []
|
124
|
# make sure the uri schema is not in the page
|
125
|
resp = app.get(unsubscription_link)
|
126
|
assert scheme not in resp.content
|