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
|
9
|
from django.utils import timezone
|
10
|
from django.core.files.storage import DefaultStorage
|
11
|
|
12
|
from corbo.models import Category, Announce, Subscription, Broadcast
|
13
|
from corbo.models import channel_choices
|
14
|
|
15
|
pytestmark = pytest.mark.django_db
|
16
|
|
17
|
CATEGORIES = ('Alerts', 'News')
|
18
|
|
19
|
|
20
|
@pytest.fixture
|
21
|
def categories():
|
22
|
categories = []
|
23
|
for category in CATEGORIES:
|
24
|
c, created = Category.objects.get_or_create(name=category)
|
25
|
categories.append(c)
|
26
|
return categories
|
27
|
|
28
|
@pytest.fixture
|
29
|
def announces():
|
30
|
announces = []
|
31
|
for category in Category.objects.all():
|
32
|
a = Announce.objects.create(category=category, title='Announce 1',
|
33
|
publication_time=timezone.now(),
|
34
|
text='<h2>Announce 1</h2>')
|
35
|
Broadcast.objects.create(announce=a)
|
36
|
announces.append(a)
|
37
|
a = Announce.objects.create(category=category, title='Announce 2',
|
38
|
publication_time=timezone.now(),
|
39
|
text='<h2>Announce 2</h2>')
|
40
|
Broadcast.objects.create(announce=a)
|
41
|
announces.append(a)
|
42
|
return announces
|
43
|
|
44
|
def test_emailing_with_no_subscriptions(app, categories, announces):
|
45
|
for announce in announces:
|
46
|
broadcast = Broadcast.objects.get(announce=announce)
|
47
|
broadcast.send()
|
48
|
assert not broadcast.result
|
49
|
assert not mail.outbox
|
50
|
|
51
|
def test_send_email(app, categories, announces):
|
52
|
for category in categories:
|
53
|
uuid = uuid4()
|
54
|
s = Subscription.objects.create(category=category,
|
55
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
56
|
for announce in announces:
|
57
|
broadcast= Broadcast.objects.get(announce=announce)
|
58
|
broadcast.send()
|
59
|
assert broadcast.result
|
60
|
assert mail.outbox
|
61
|
|
62
|
def test_check_inline_css(app, categories, announces):
|
63
|
for announce in announces:
|
64
|
announce.text = '<style type="text/css">h2 {color: #F00}</style>' + announce.text
|
65
|
announce.save()
|
66
|
uuid = uuid4()
|
67
|
s = Subscription.objects.create(category=announce.category,
|
68
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
69
|
broadcast = Broadcast.objects.get(announce=announce)
|
70
|
broadcast.send()
|
71
|
assert broadcast.result
|
72
|
assert mail.outbox
|
73
|
assert 'h2 style="color:#F00"' in mail.outbox[0].html
|
74
|
mail.outbox = []
|
75
|
|
76
|
def test_check_inline_images(app, categories, announces):
|
77
|
storage = DefaultStorage()
|
78
|
media_path = os.path.join(os.path.dirname(__file__), 'media')
|
79
|
image_name = 'logo.png'
|
80
|
storage.save(image_name, file(os.path.join(media_path, image_name)))
|
81
|
for announce in announces:
|
82
|
announce.text = announce.text + '<img src="/media/%s" />' % image_name
|
83
|
announce.save()
|
84
|
uuid = uuid4()
|
85
|
s = Subscription.objects.create(category=announce.category,
|
86
|
identifier='%s@example.net' % uuid, uuid=uuid)
|
87
|
broadcast = Broadcast.objects.get(announce=announce)
|
88
|
broadcast.send()
|
89
|
assert broadcast.result
|
90
|
assert mail.outbox
|
91
|
assert storage.url(image_name) in mail.outbox[0].attachments.keys()
|
92
|
mail.outbox = []
|
93
|
storage.delete(image_name)
|