Project

General

Profile

Download (6.05 KB) Statistics
| Branch: | Tag: | Revision:

root / tests / test_api.py @ e01c978b

1 e93ea142 Serghei Mihai
import pytest
2 d70894fa Josue Kouka
3
import urllib
4 8759c981 Serghei Mihai
from uuid import uuid4
5 e93ea142 Serghei Mihai
6
7 e01c978b Frédéric Péters
from django.urls import reverse
8 8759c981 Serghei Mihai
from django.utils.http import urlencode
9 b84a6769 Serghei Mihai
from django.contrib.auth import get_user_model
10 40b65753 Serghei Mihai
from django.utils.text import slugify
11 e93ea142 Serghei Mihai
12 8759c981 Serghei Mihai
from corbo.models import Category, Announce, Broadcast, Subscription
13
from corbo.models import channel_choices
14 e93ea142 Serghei Mihai
15
pytestmark = pytest.mark.django_db
16
17 40b65753 Serghei Mihai
CATEGORIES = (u'Alerts', u'News')
18 e93ea142 Serghei Mihai
19
20
@pytest.fixture
21
def categories():
22
    categories = []
23
    for category in CATEGORIES:
24 40b65753 Serghei Mihai
        c, created = Category.objects.get_or_create(name=category, slug=slugify(category))
25 e93ea142 Serghei Mihai
        categories.append(c)
26
    return categories
27
28 c13b4c9b Josue Kouka
29 e93ea142 Serghei Mihai
@pytest.fixture
30
def announces():
31
    announces = []
32
    for category in Category.objects.all():
33
        a = Announce.objects.create(category=category, title='By email')
34 8759c981 Serghei Mihai
        Broadcast.objects.create(announce=a)
35 e93ea142 Serghei Mihai
        announces.append(a)
36 e147eff6 Serghei Mihai
        a = Announce.objects.create(category=category, title='Another one')
37 8759c981 Serghei Mihai
        Broadcast.objects.create(announce=a)
38 e93ea142 Serghei Mihai
        announces.append(a)
39
    return announces
40
41 c13b4c9b Josue Kouka
42 b84a6769 Serghei Mihai
@pytest.fixture
43
def user():
44
    User = get_user_model()
45 c13b4c9b Josue Kouka
    user = User.objects.create(username='john.doe', first_name=u'John',
46
                               last_name=u'Doe', email='john.doe@example.net')
47 b84a6769 Serghei Mihai
    user.set_password('password')
48
    user.save()
49
    return user
50
51
52
def test_get_newsletters(app, categories, announces, user):
53 e01c978b Frédéric Péters
    resp = app.get(reverse('newsletters'), status=(401, 403))
54 b84a6769 Serghei Mihai
    app.authorization = ('Basic', ('john.doe', 'password'))
55
    resp = app.get(reverse('newsletters'))
56 e93ea142 Serghei Mihai
    data = resp.json
57
    assert data['data']
58
    for category in data['data']:
59
        assert 'id' in category
60
        assert 'text' in category
61 40b65753 Serghei Mihai
        assert category['id'] in [slugify(c) for c in CATEGORIES]
62 e93ea142 Serghei Mihai
        assert category['text'] in CATEGORIES
63
        assert 'transports' in category
64 9f832011 Serghei Mihai
        assert category['transports'] == [{'id': 'mailto', 'text': 'Email'}, {'id': 'sms', 'text': 'SMS'}]
65 8759c981 Serghei Mihai
66
67 5591a1dc Serghei Mihai
def test_get_subscriptions(app, categories, announces, user):
68 e01c978b Frédéric Péters
    resp = app.get(reverse('subscriptions'), status=(401, 403))
69 5591a1dc Serghei Mihai
    uuid = str(uuid4())
70 e01c978b Frédéric Péters
    resp = app.get(reverse('subscriptions'), params={'uuid': uuid}, status=(401, 403))
71 b84a6769 Serghei Mihai
    app.authorization = ('Basic', ('john.doe', 'password'))
72 5591a1dc Serghei Mihai
73
    for identifier, name in channel_choices:
74 8759c981 Serghei Mihai
        for category in categories:
75 d70894fa Josue Kouka
            uri = '%s:%s' % (identifier, '00339988776655')
76 5591a1dc Serghei Mihai
            Subscription.objects.create(identifier=uri, uuid=uuid, category=category)
77
        resp = app.get(reverse('subscriptions'), params={'uuid': uuid})
78
        assert len(resp.json['data']) == 2
79
        for d in resp.json['data']:
80
            assert d['id'] in [category.slug for category in categories]
81
            assert d['text'] in [category.name for category in categories]
82
            assert identifier in [transport['id'] for transport in d['transports']]
83 8759c981 Serghei Mihai
84
85 b84a6769 Serghei Mihai
def test_update_subscriptions(app, categories, announces, user):
86 5591a1dc Serghei Mihai
    uuid = uuid4()
87 b84a6769 Serghei Mihai
    app.authorization = ('Basic', ('john.doe', 'password'))
88 5591a1dc Serghei Mihai
    subscriptions_url = '/api/subscriptions/?uuid=%s&email=user@example.com&mobile=0607080900' % uuid
89 8759c981 Serghei Mihai
    for category in categories:
90
        transports = []
91 5591a1dc Serghei Mihai
        for identifier, name in channel_choices:
92 8759c981 Serghei Mihai
            transports.append(identifier)
93 5591a1dc Serghei Mihai
            subscriptions = [{'id': category.slug,
94 8759c981 Serghei Mihai
                              'text': category.name,
95
                              'transports': transports}]
96 a98fc4c0 Frédéric Péters
            resp = app.post_json(subscriptions_url, params=subscriptions)
97 5591a1dc Serghei Mihai
            resp = app.get(subscriptions_url)
98
            assert len(resp.json['data']) >= 1
99
            for cat in resp.json['data']:
100
                if cat['id'] == category.slug:
101
                    sub_transports = [c['id'] for c in cat['transports']]
102
                    assert sub_transports == transports
103 8759c981 Serghei Mihai
104
105 b84a6769 Serghei Mihai
def test_delete_subscriptions(app, categories, announces, user):
106 5591a1dc Serghei Mihai
    params = urlencode({'uuid': str(uuid4())})
107 8759c981 Serghei Mihai
    subscriptions_url = reverse('subscriptions') + '?' + params
108 e01c978b Frédéric Péters
    resp = app.delete(subscriptions_url, status=(401, 403))
109 b84a6769 Serghei Mihai
    app.authorization = ('Basic', ('john.doe', 'password'))
110 8759c981 Serghei Mihai
    resp = app.delete(subscriptions_url)
111
    if resp.json['data']:
112 b84a6769 Serghei Mihai
        resp = app.get(subscriptions_url)
113 8759c981 Serghei Mihai
        assert resp.json['data'] == []
114 77674a4e Josue Kouka
115
116 5591a1dc Serghei Mihai
def test_simple_email_subscription(app, categories, user):
117
    payload = {'category_id': 'alerts', 'transports': ['mailto']}
118
    uuid = uuid4()
119
    url = '/api/subscribe/?uuid=%s&email=john@example.net' % uuid
120 77674a4e Josue Kouka
121
    # anonymous
122 e01c978b Frédéric Péters
    resp = app.post_json(url, params=payload, status=(401, 403))
123 77674a4e Josue Kouka
    assert resp.json['detail'] == 'Authentication credentials were not provided.'
124
125
    # authenticated
126
    app.authorization = ('Basic', ('john.doe', 'password'))
127 a98fc4c0 Frédéric Péters
    resp = app.post_json(url, params=payload, status=200)
128 77674a4e Josue Kouka
    assert resp.json['data'] is True
129
130
    # with wrong method
131 5591a1dc Serghei Mihai
    resp = app.get('/api/subscribe/?uuid=%s&email=john@example.net' % uuid, status=405)
132 77674a4e Josue Kouka
    assert resp.json['detail'] == 'Method "GET" not allowed.'
133
134
    # right method on right url
135 5591a1dc Serghei Mihai
    resp = app.get('/api/subscriptions/?uuid=%s' % uuid)
136 77674a4e Josue Kouka
137
    data = resp.json['data']
138
    assert len(data) == 1
139 40b65753 Serghei Mihai
    assert data[0]['id'] == 'alerts'
140 77674a4e Josue Kouka
    assert data[0]['text'] == 'Alerts'
141
    assert len(data[0]['transports']) == 1
142
    transport = data[0]['transports'][0]
143
    assert transport['id'] == 'mailto'
144
    assert transport['text'] == 'mailto'
145 5591a1dc Serghei Mihai
146 d70894fa Josue Kouka
def test_simple_sms_subscription(app, categories, user, phonenumber):
147 5591a1dc Serghei Mihai
    payload = {'category_id': 'alerts', 'transports': ['sms']}
148
    uuid = uuid4()
149 d57bfbcc Frédéric Péters
    url_params = urlencode({'uuid': uuid, 'mobile': phonenumber})
150 d70894fa Josue Kouka
    url = '/api/subscribe/?' + url_params
151 5591a1dc Serghei Mihai
    app.authorization = ('Basic', ('john.doe', 'password'))
152
    resp = app.post_json(url, params=payload, status=200)
153 d70894fa Josue Kouka
    assert Subscription.objects.get(uuid=uuid).identifier in ['sms:0610203040', 'sms:+33610203040', 'sms:0033610203040']
154 5591a1dc Serghei Mihai
    resp = app.get('/api/subscriptions/?uuid=%s' % uuid)
155
    data = resp.json['data']
156
    assert len(data) == 1
157
    assert data[0]['id'] == 'alerts'
158
    assert data[0]['text'] == 'Alerts'
159
    assert len(data[0]['transports']) == 1
160
    transport = data[0]['transports'][0]
161
    assert transport['id'] == 'sms'
162
    assert transport['text'] == 'sms'