Projet

Général

Profil

Télécharger (5,22 ko) Statistiques
| Branche: | Tag: | Révision:

root / tests / test_api.py @ 40b65753

1
import pytest
2
from uuid import uuid4
3

    
4

    
5
from django.core.urlresolvers import reverse
6
from django.utils.http import urlencode
7
from django.contrib.auth import get_user_model
8
from django.utils.text import slugify
9

    
10
from corbo.models import Category, Announce, Broadcast, Subscription
11
from corbo.models import channel_choices
12

    
13
pytestmark = pytest.mark.django_db
14

    
15
CATEGORIES = (u'Alerts', u'News')
16

    
17

    
18
@pytest.fixture
19
def categories():
20
    categories = []
21
    for category in CATEGORIES:
22
        c, created = Category.objects.get_or_create(name=category, slug=slugify(category))
23
        categories.append(c)
24
    return categories
25

    
26

    
27
@pytest.fixture
28
def announces():
29
    announces = []
30
    for category in Category.objects.all():
31
        a = Announce.objects.create(category=category, title='By email')
32
        Broadcast.objects.create(announce=a)
33
        announces.append(a)
34
        a = Announce.objects.create(category=category, title='Another one')
35
        Broadcast.objects.create(announce=a)
36
        announces.append(a)
37
    return announces
38

    
39

    
40
@pytest.fixture
41
def user():
42
    User = get_user_model()
43
    user = User.objects.create(username='john.doe', first_name=u'John',
44
                               last_name=u'Doe', email='john.doe@example.net')
45
    user.set_password('password')
46
    user.save()
47
    return user
48

    
49

    
50
def test_get_newsletters(app, categories, announces, user):
51
    resp = app.get(reverse('newsletters'), status=403)
52
    app.authorization = ('Basic', ('john.doe', 'password'))
53
    resp = app.get(reverse('newsletters'))
54
    data = resp.json
55
    assert data['data']
56
    for category in data['data']:
57
        assert 'id' in category
58
        assert 'text' in category
59
        assert category['id'] in [slugify(c) for c in CATEGORIES]
60
        assert category['text'] in CATEGORIES
61
        assert 'transports' in category
62
        assert category['transports'] == [{'id': 'mailto', 'text': 'Email'}]
63

    
64

    
65
def test_get_subscriptions_by_email(app, categories, announces, user):
66
    resp = app.get(reverse('subscriptions'), status=403)
67
    foo = 'foo@example.com'
68
    resp = app.get(reverse('subscriptions'), {'email': foo}, status=403)
69
    app.authorization = ('Basic', ('john.doe', 'password'))
70
    for identifier, name in channel_choices[:1]:
71
        for category in categories:
72
            uri = '%s:%s' % (identifier, foo)
73
            Subscription.objects.create(identifier=uri, category=category)
74
            resp = app.get(reverse('subscriptions'), {'email': foo})
75
            assert 'data' in resp.json
76
            data = resp.json['data']
77
            for d in data:
78
                assert d['id'] in [category.slug for category in categories]
79
                assert d['text'] in [category.name for category in categories]
80
                for t in d['transports']:
81
                    assert t['id'] == identifier
82

    
83

    
84
def test_update_subscriptions(app, categories, announces, user):
85
    params = urlencode({'email': 'foo@example.com',
86
                        'uuid': str(uuid4())})
87
    app.authorization = ('Basic', ('john.doe', 'password'))
88
    subscriptions_url = reverse('subscriptions') + '?' + params
89
    for category in categories:
90
        transports = []
91
        for identifier, name in channel_choices[:1]:
92
            transports.append(identifier)
93
            category_id = str(category.id)
94
            subscriptions = [{'id': category_id,
95
                              'text': category.name,
96
                              'transports': transports}]
97
            resp = app.post_json(subscriptions_url, subscriptions)
98
            if resp.json['data']:
99
                resp = app.get(subscriptions_url)
100

    
101
                for cat in resp.json['data']:
102
                    if cat['id'] == category_id:
103
                        sub_transports = [c['id'] for c in cat['transports']]
104
                        assert sub_transports == transports
105

    
106

    
107
def test_delete_subscriptions(app, categories, announces, user):
108
    params = urlencode({'email': 'foo@example.com', 'uuid': str(uuid4())})
109
    subscriptions_url = reverse('subscriptions') + '?' + params
110
    resp = app.delete(subscriptions_url, status=403)
111
    app.authorization = ('Basic', ('john.doe', 'password'))
112
    resp = app.delete(subscriptions_url)
113
    if resp.json['data']:
114
        resp = app.get(subscriptions_url)
115
        assert resp.json['data'] == []
116

    
117

    
118
def test_simple_subscription(app, categories, user):
119
    payload = {'category_id': 'alerts'}
120
    url = '/api/subscribe/?email=john@example.net'
121

    
122
    # anonymous
123
    resp = app.post_json(url, payload, status=403)
124
    assert resp.json['detail'] == 'Authentication credentials were not provided.'
125

    
126
    # authenticated
127
    app.authorization = ('Basic', ('john.doe', 'password'))
128
    resp = app.post_json(url, payload, status=200)
129
    assert resp.json['data'] is True
130

    
131
    # with wrong method
132
    resp = app.get('/api/subscribe/?email=john@example.net', status=405)
133
    assert resp.json['detail'] == 'Method "GET" not allowed.'
134

    
135
    # right method on right url
136
    resp = app.get('/api/subscriptions/?email=john@example.net', status=200)
137

    
138
    data = resp.json['data']
139
    assert len(data) == 1
140
    assert data[0]['id'] == 'alerts'
141
    assert data[0]['text'] == 'Alerts'
142
    assert len(data[0]['transports']) == 1
143
    transport = data[0]['transports'][0]
144
    assert transport['id'] == 'mailto'
145
    assert transport['text'] == 'mailto'
(3-3/4)