Revision 8759c981
Added by Serghei Mihai over 9 years ago
| tests/test_api.py | ||
|---|---|---|
|
import pytest
|
||
|
import json
|
||
|
from uuid import uuid4
|
||
|
|
||
|
|
||
|
from django.core.urlresolvers import reverse
|
||
|
from django.utils.http import urlencode
|
||
|
|
||
|
from corbo.models import Category, Announce, Broadcast
|
||
|
from corbo.models import Category, Announce, Broadcast, Subscription
|
||
|
from corbo.models import channel_choices
|
||
|
|
||
|
pytestmark = pytest.mark.django_db
|
||
|
|
||
| ... | ... | |
|
announces = []
|
||
|
for category in Category.objects.all():
|
||
|
a = Announce.objects.create(category=category, title='By email')
|
||
|
Broadcast.objects.create(announce=a, channel='mailto')
|
||
|
Broadcast.objects.create(announce=a)
|
||
|
announces.append(a)
|
||
|
a = Announce.objects.create(category=category, title='On homepage')
|
||
|
Broadcast.objects.create(announce=a, channel='homepage')
|
||
|
Broadcast.objects.create(announce=a)
|
||
|
announces.append(a)
|
||
|
return announces
|
||
|
|
||
| ... | ... | |
|
assert category['transports'] == [{'id': 'mailto', 'text': 'Email'},
|
||
|
{'id': 'homepage', 'text': 'Homepage'}
|
||
|
]
|
||
|
|
||
|
|
||
|
def test_get_subscriptions_by_email(app, categories, announces):
|
||
|
resp = app.get(reverse('subscriptions'), status=403)
|
||
|
foo = 'foo@example.com'
|
||
|
for identifier, name in channel_choices[:1]:
|
||
|
for category in categories:
|
||
|
uri = '%s:%s' % (identifier, foo)
|
||
|
subscription = Subscription.objects.create(identifier=uri,
|
||
|
category=category)
|
||
|
resp = app.get(reverse('subscriptions'), {'email': foo}, status=200)
|
||
|
assert 'data' in resp.json
|
||
|
data = resp.json['data']
|
||
|
for d in data:
|
||
|
assert d['id'] in [str(category.id) for category in categories]
|
||
|
assert d['text'] in [category.name for category in categories]
|
||
|
for t in d['transports']:
|
||
|
assert t['id'] == identifier
|
||
|
|
||
|
|
||
|
def test_update_subscriptions(app, categories, announces):
|
||
|
params = urlencode({'email': 'foo@example.com',
|
||
|
'uuid': str(uuid4())})
|
||
|
subscriptions_url = reverse('subscriptions') + '?' + params
|
||
|
for category in categories:
|
||
|
transports = []
|
||
|
for identifier, name in channel_choices[:1]:
|
||
|
transports.append(identifier)
|
||
|
category_id = str(category.id)
|
||
|
subscriptions = [{'id': category_id,
|
||
|
'text': category.name,
|
||
|
'transports': transports}]
|
||
|
resp = app.post_json(subscriptions_url , subscriptions)
|
||
|
if resp.json['data']:
|
||
|
resp = app.get(subscriptions_url, status=200)
|
||
|
print resp.json['data']
|
||
|
for cat in resp.json['data']:
|
||
|
if cat['id'] == category_id:
|
||
|
sub_transports = [c['id'] for c in cat['transports']]
|
||
|
assert sub_transports == transports
|
||
|
|
||
|
|
||
|
def test_delete_subscriptions(app, categories, announces):
|
||
|
params = urlencode({'email': 'foo@example.com', 'uuid': str(uuid4())})
|
||
|
subscriptions_url = reverse('subscriptions') + '?' + params
|
||
|
resp = app.delete(subscriptions_url)
|
||
|
if resp.json['data']:
|
||
|
resp = app.get(subscriptions_url, status=200)
|
||
|
assert resp.json['data'] == []
|
||
Also available in: Unified diff
api: subscriptions management endpoint (#10794)