Project

General

Profile

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

root / tests / test_api.py @ e01c978b

1
import pytest
2

    
3
import urllib
4
from uuid import uuid4
5

    
6

    
7
from django.urls import reverse
8
from django.utils.http import urlencode
9
from django.contrib.auth import get_user_model
10
from django.utils.text import slugify
11

    
12
from corbo.models import Category, Announce, Broadcast, Subscription
13
from corbo.models import channel_choices
14

    
15
pytestmark = pytest.mark.django_db
16

    
17
CATEGORIES = (u'Alerts', u'News')
18

    
19

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

    
28

    
29
@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
        Broadcast.objects.create(announce=a)
35
        announces.append(a)
36
        a = Announce.objects.create(category=category, title='Another one')
37
        Broadcast.objects.create(announce=a)
38
        announces.append(a)
39
    return announces
40

    
41

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

    
51

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

    
66

    
67
def test_get_subscriptions(app, categories, announces, user):
68
    resp = app.get(reverse('subscriptions'), status=(401, 403))
69
    uuid = str(uuid4())
70
    resp = app.get(reverse('subscriptions'), params={'uuid': uuid}, status=(401, 403))
71
    app.authorization = ('Basic', ('john.doe', 'password'))
72

    
73
    for identifier, name in channel_choices:
74
        for category in categories:
75
            uri = '%s:%s' % (identifier, '00339988776655')
76
            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

    
84

    
85
def test_update_subscriptions(app, categories, announces, user):
86
    uuid = uuid4()
87
    app.authorization = ('Basic', ('john.doe', 'password'))
88
    subscriptions_url = '/api/subscriptions/?uuid=%s&email=user@example.com&mobile=0607080900' % uuid
89
    for category in categories:
90
        transports = []
91
        for identifier, name in channel_choices:
92
            transports.append(identifier)
93
            subscriptions = [{'id': category.slug,
94
                              'text': category.name,
95
                              'transports': transports}]
96
            resp = app.post_json(subscriptions_url, params=subscriptions)
97
            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

    
104

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

    
115

    
116
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

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

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

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

    
134
    # right method on right url
135
    resp = app.get('/api/subscriptions/?uuid=%s' % uuid)
136

    
137
    data = resp.json['data']
138
    assert len(data) == 1
139
    assert data[0]['id'] == 'alerts'
140
    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

    
146
def test_simple_sms_subscription(app, categories, user, phonenumber):
147
    payload = {'category_id': 'alerts', 'transports': ['sms']}
148
    uuid = uuid4()
149
    url_params = urlencode({'uuid': uuid, 'mobile': phonenumber})
150
    url = '/api/subscribe/?' + url_params
151
    app.authorization = ('Basic', ('john.doe', 'password'))
152
    resp = app.post_json(url, params=payload, status=200)
153
    assert Subscription.objects.get(uuid=uuid).identifier in ['sms:0610203040', 'sms:+33610203040', 'sms:0033610203040']
154
    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'
(4-4/10)