0002-add-simple-subscription-API-13284.patch
corbo/api_urls.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
from django.conf.urls import patterns, url |
18 | 18 | |
19 |
from .api_views import NewslettersView, SubscriptionsView |
|
19 |
from .api_views import NewslettersView, SubscriptionsView, SubscribeView |
|
20 | ||
20 | 21 | |
21 | 22 |
urlpatterns = patterns('', |
22 | 23 |
url(r'^newsletters/', NewslettersView.as_view(), name='newsletters'), |
23 | 24 |
url(r'^subscriptions/', SubscriptionsView.as_view(), name='subscriptions'), |
25 |
url(r'^subscribe/', SubscribeView.as_view(), name='subscribe'), |
|
24 | 26 |
) |
corbo/api_views.py | ||
---|---|---|
106 | 106 |
for subscription in self.get_subscriptions(email): |
107 | 107 |
self.update_subscriptions(subscription['id'], [], email, uuid) |
108 | 108 |
return Response({'data': True}) |
109 | ||
110 | ||
111 |
class SubscribeView(SubscriptionsView): |
|
112 |
http_method_names = ['post'] |
|
113 | ||
114 |
def post(self, request): |
|
115 |
email = request.GET.get('email') |
|
116 |
uuid = request.GET.get('uuid') |
|
117 |
if not email: |
|
118 |
raise PermissionDenied('Email parameter required') |
|
119 |
data = json.loads(request.body) |
|
120 | ||
121 |
category_id = data['category_id'] |
|
122 |
transport = ('mailto',) |
|
123 | ||
124 |
self.update_subscriptions(category_id, transport, email, uuid) |
|
125 | ||
126 |
return Response({'data': True}) |
tests/test_api.py | ||
---|---|---|
111 | 111 |
if resp.json['data']: |
112 | 112 |
resp = app.get(subscriptions_url) |
113 | 113 |
assert resp.json['data'] == [] |
114 | ||
115 | ||
116 |
def test_simple_subscription(app, categories, user): |
|
117 |
payload = {'category_id': 1} |
|
118 |
url = '/api/subscribe/?email=john@example.net' |
|
119 | ||
120 |
# anonymous |
|
121 |
resp = app.post_json(url, payload, status=403) |
|
122 |
assert resp.json['detail'] == 'Authentication credentials were not provided.' |
|
123 | ||
124 |
# authenticated |
|
125 |
app.authorization = ('Basic', ('john.doe', 'password')) |
|
126 |
resp = app.post_json(url, payload, status=200) |
|
127 |
assert resp.json['data'] is True |
|
128 | ||
129 |
# with wrong method |
|
130 |
resp = app.get('/api/subscribe/?email=john@example.net', status=405) |
|
131 |
assert resp.json['detail'] == 'Method "GET" not allowed.' |
|
132 | ||
133 |
# right method on right url |
|
134 |
resp = app.get('/api/subscriptions/?email=john@example.net', status=200) |
|
135 | ||
136 |
data = resp.json['data'] |
|
137 |
assert len(data) == 1 |
|
138 |
assert data[0]['id'] == '1' |
|
139 |
assert data[0]['text'] == 'Alerts' |
|
140 |
assert len(data[0]['transports']) == 1 |
|
141 |
transport = data[0]['transports'][0] |
|
142 |
assert transport['id'] == 'mailto' |
|
143 |
assert transport['text'] == 'mailto' |
|
114 |
- |