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