Projet

Général

Profil

Télécharger (3,48 ko) Statistiques
| Branche: | Tag: | Révision:

root / tests / test_api.py @ e147eff6

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='Another one')
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

    
49

    
50
def test_get_subscriptions_by_email(app, categories, announces):
51
    resp = app.get(reverse('subscriptions'), status=403)
52
    foo = 'foo@example.com'
53
    for identifier, name in channel_choices[:1]:
54
        for category in categories:
55
            uri = '%s:%s' % (identifier, foo)
56
            subscription = Subscription.objects.create(identifier=uri,
57
                                category=category)
58
            resp = app.get(reverse('subscriptions'), {'email': foo}, status=200)
59
            assert 'data' in resp.json
60
            data = resp.json['data']
61
            for d in data:
62
                assert d['id'] in [str(category.id) for category in categories]
63
                assert d['text'] in [category.name for category in categories]
64
                for t in d['transports']:
65
                    assert t['id'] == identifier
66

    
67

    
68
def test_update_subscriptions(app, categories, announces):
69
    params = urlencode({'email': 'foo@example.com',
70
                        'uuid': str(uuid4())})
71
    subscriptions_url = reverse('subscriptions') + '?' + params
72
    for category in categories:
73
        transports = []
74
        for identifier, name in channel_choices[:1]:
75
            transports.append(identifier)
76
            category_id = str(category.id)
77
            subscriptions = [{'id': category_id,
78
                              'text': category.name,
79
                              'transports': transports}]
80
            resp = app.post_json(subscriptions_url , subscriptions)
81
            if resp.json['data']:
82
                resp = app.get(subscriptions_url, status=200)
83
                print resp.json['data']
84
                for cat in resp.json['data']:
85
                    if cat['id'] == category_id:
86
                        sub_transports = [c['id'] for c in cat['transports']]
87
                        assert sub_transports == transports
88

    
89

    
90
def test_delete_subscriptions(app, categories, announces):
91
    params = urlencode({'email': 'foo@example.com', 'uuid': str(uuid4())})
92
    subscriptions_url = reverse('subscriptions') + '?' + params
93
    resp = app.delete(subscriptions_url)
94
    if resp.json['data']:
95
        resp = app.get(subscriptions_url, status=200)
96
        assert resp.json['data'] == []
(3-3/4)