|
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, Subscription
|
|
from corbo.models import channel_choices
|
|
|
|
pytestmark = pytest.mark.django_db
|
|
|
|
CATEGORIES = ('Alerts', 'News')
|
|
|
|
|
|
@pytest.fixture
|
|
def categories():
|
|
categories = []
|
|
for category in CATEGORIES:
|
|
c, created = Category.objects.get_or_create(name=category)
|
|
categories.append(c)
|
|
return categories
|
|
|
|
@pytest.fixture
|
|
def announces():
|
|
announces = []
|
|
for category in Category.objects.all():
|
|
a = Announce.objects.create(category=category, title='By email')
|
|
Broadcast.objects.create(announce=a)
|
|
announces.append(a)
|
|
a = Announce.objects.create(category=category, title='On homepage')
|
|
Broadcast.objects.create(announce=a)
|
|
announces.append(a)
|
|
return announces
|
|
|
|
|
|
def test_get_newsletters(app, categories, announces):
|
|
resp = app.get(reverse('newsletters'), status=200)
|
|
data = resp.json
|
|
assert data['data']
|
|
for category in data['data']:
|
|
assert 'id' in category
|
|
assert 'text' in category
|
|
assert category['text'] in CATEGORIES
|
|
assert 'transports' in category
|
|
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'] == []
|