1
|
import pytest
|
2
|
import json
|
3
|
from uuid import uuid4
|
4
|
|
5
|
|
6
|
from django.core.urlresolvers import reverse
|
7
|
from django.utils.http import urlencode
|
8
|
|
9
|
from corbo.models import Category, Announce, Broadcast, Subscription
|
10
|
from corbo.models import channel_choices
|
11
|
|
12
|
pytestmark = pytest.mark.django_db
|
13
|
|
14
|
CATEGORIES = ('Alerts', 'News')
|
15
|
|
16
|
|
17
|
@pytest.fixture
|
18
|
def categories():
|
19
|
categories = []
|
20
|
for category in CATEGORIES:
|
21
|
c, created = Category.objects.get_or_create(name=category)
|
22
|
categories.append(c)
|
23
|
return categories
|
24
|
|
25
|
@pytest.fixture
|
26
|
def announces():
|
27
|
announces = []
|
28
|
for category in Category.objects.all():
|
29
|
a = Announce.objects.create(category=category, title='By email')
|
30
|
Broadcast.objects.create(announce=a)
|
31
|
announces.append(a)
|
32
|
a = Announce.objects.create(category=category, title='On homepage')
|
33
|
Broadcast.objects.create(announce=a)
|
34
|
announces.append(a)
|
35
|
return announces
|
36
|
|
37
|
|
38
|
def test_get_newsletters(app, categories, announces):
|
39
|
resp = app.get(reverse('newsletters'), status=200)
|
40
|
data = resp.json
|
41
|
assert data['data']
|
42
|
for category in data['data']:
|
43
|
assert 'id' in category
|
44
|
assert 'text' in category
|
45
|
assert category['text'] in CATEGORIES
|
46
|
assert 'transports' in category
|
47
|
assert category['transports'] == [{'id': 'mailto', 'text': 'Email'},
|
48
|
{'id': 'homepage', 'text': 'Homepage'}
|
49
|
]
|
50
|
|
51
|
|
52
|
def test_get_subscriptions_by_email(app, categories, announces):
|
53
|
resp = app.get(reverse('subscriptions'), status=403)
|
54
|
foo = 'foo@example.com'
|
55
|
for identifier, name in channel_choices[:1]:
|
56
|
for category in categories:
|
57
|
uri = '%s:%s' % (identifier, foo)
|
58
|
subscription = Subscription.objects.create(identifier=uri,
|
59
|
category=category)
|
60
|
resp = app.get(reverse('subscriptions'), {'email': foo}, status=200)
|
61
|
assert 'data' in resp.json
|
62
|
data = resp.json['data']
|
63
|
for d in data:
|
64
|
assert d['id'] in [str(category.id) for category in categories]
|
65
|
assert d['text'] in [category.name for category in categories]
|
66
|
for t in d['transports']:
|
67
|
assert t['id'] == identifier
|
68
|
|
69
|
|
70
|
def test_update_subscriptions(app, categories, announces):
|
71
|
params = urlencode({'email': 'foo@example.com',
|
72
|
'uuid': str(uuid4())})
|
73
|
subscriptions_url = reverse('subscriptions') + '?' + params
|
74
|
for category in categories:
|
75
|
transports = []
|
76
|
for identifier, name in channel_choices[:1]:
|
77
|
transports.append(identifier)
|
78
|
category_id = str(category.id)
|
79
|
subscriptions = [{'id': category_id,
|
80
|
'text': category.name,
|
81
|
'transports': transports}]
|
82
|
resp = app.post_json(subscriptions_url , subscriptions)
|
83
|
if resp.json['data']:
|
84
|
resp = app.get(subscriptions_url, status=200)
|
85
|
print resp.json['data']
|
86
|
for cat in resp.json['data']:
|
87
|
if cat['id'] == category_id:
|
88
|
sub_transports = [c['id'] for c in cat['transports']]
|
89
|
assert sub_transports == transports
|
90
|
|
91
|
|
92
|
def test_delete_subscriptions(app, categories, announces):
|
93
|
params = urlencode({'email': 'foo@example.com', 'uuid': str(uuid4())})
|
94
|
subscriptions_url = reverse('subscriptions') + '?' + params
|
95
|
resp = app.delete(subscriptions_url)
|
96
|
if resp.json['data']:
|
97
|
resp = app.get(subscriptions_url, status=200)
|
98
|
assert resp.json['data'] == []
|