Revision 8759c981
Added by Serghei Mihai over 8 years ago
tests/test_api.py | ||
---|---|---|
1 | 1 |
import pytest |
2 | 2 |
import json |
3 |
from uuid import uuid4 |
|
3 | 4 |
|
4 | 5 |
|
5 | 6 |
from django.core.urlresolvers import reverse |
7 |
from django.utils.http import urlencode |
|
6 | 8 |
|
7 |
from corbo.models import Category, Announce, Broadcast |
|
9 |
from corbo.models import Category, Announce, Broadcast, Subscription |
|
10 |
from corbo.models import channel_choices |
|
8 | 11 |
|
9 | 12 |
pytestmark = pytest.mark.django_db |
10 | 13 |
|
... | ... | |
24 | 27 |
announces = [] |
25 | 28 |
for category in Category.objects.all(): |
26 | 29 |
a = Announce.objects.create(category=category, title='By email') |
27 |
Broadcast.objects.create(announce=a, channel='mailto')
|
|
30 |
Broadcast.objects.create(announce=a) |
|
28 | 31 |
announces.append(a) |
29 | 32 |
a = Announce.objects.create(category=category, title='On homepage') |
30 |
Broadcast.objects.create(announce=a, channel='homepage')
|
|
33 |
Broadcast.objects.create(announce=a) |
|
31 | 34 |
announces.append(a) |
32 | 35 |
return announces |
33 | 36 |
|
... | ... | |
44 | 47 |
assert category['transports'] == [{'id': 'mailto', 'text': 'Email'}, |
45 | 48 |
{'id': 'homepage', 'text': 'Homepage'} |
46 | 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'] == [] |
Also available in: Unified diff
api: subscriptions management endpoint (#10794)