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