0001-api-add-federations-stats-endpoint-14787.patch
mandayejs/mandaye/api.py | ||
---|---|---|
18 | 18 | |
19 | 19 |
from django.shortcuts import get_object_or_404 |
20 | 20 |
from django.contrib.auth.models import User |
21 |
from django.db import models |
|
21 | 22 | |
22 | 23 |
from rest_framework import status |
23 | 24 |
from rest_framework.views import APIView |
... | ... | |
78 | 79 | |
79 | 80 | |
80 | 81 |
api = MandayeAPI.as_view() |
82 | ||
83 | ||
84 |
class MandayeStatsAPI(APIView): |
|
85 | ||
86 |
http_method_names = ['get'] |
|
87 | ||
88 |
def get(self, request, *args, **kwargs): |
|
89 |
data = UserCredentials.objects.aggregate( |
|
90 |
users_linked=models.Sum(models.Case(models.When(linked=True, then=1), output_field=models.IntegerField())), |
|
91 |
users_unlinked=models.Sum(models.Case(models.When(linked=False, then=1), output_field=models.IntegerField())) |
|
92 |
) |
|
93 |
return Response(data) |
|
94 | ||
95 | ||
96 |
api_stats = MandayeStatsAPI.as_view() |
mandayejs/urls.py | ||
---|---|---|
30 | 30 |
url(r'^_mandaye/post-login-do/$', 'mandayejs.mandaye.views.post_login_do', name='post-login-do'), |
31 | 31 |
url(r'^_mandaye/admin/', include(admin.site.urls)), |
32 | 32 |
url(r'^_mandaye/ws/(?P<path>.*)$', app_web_services, name='app-web-services'), |
33 |
url(r'^_mandaye/api/', 'mandayejs.mandaye.api.api', name='api') |
|
33 |
url(r'^_mandaye/api/$', 'mandayejs.mandaye.api.api', name='api'), |
|
34 |
url(r'^_mandaye/api/stats/$', 'mandayejs.mandaye.api.api_stats', name='api-stats') |
|
34 | 35 |
) |
35 | 36 | |
36 | 37 |
if 'mellon' in settings.INSTALLED_APPS: |
tests/test_api.py | ||
---|---|---|
2 | 2 |
import pytest |
3 | 3 | |
4 | 4 |
from django.conf import settings |
5 |
from django.core.urlresolvers import reverse |
|
5 | 6 | |
6 | 7 |
from mandayejs.mandaye.models import UserCredentials |
7 | 8 |
from utils import create_user, create_credentials, get_uuid, get_user |
8 | 9 | |
10 |
pytestmark = pytest.mark.django_db |
|
11 | ||
9 | 12 | |
10 | 13 |
def test_api_get(client, url): |
11 | 14 |
response = client.get(url) |
... | ... | |
150 | 153 |
assert response.status_code == status_code['success'] |
151 | 154 |
if url_signed.orig == 'testserver': |
152 | 155 |
assert UserCredentials.objects.filter(user=josh).exists() is False |
156 | ||
157 | ||
158 |
def test_api_stats(client): |
|
159 |
url = reverse('api-stats') |
|
160 | ||
161 |
if client.session.values(): |
|
162 |
status_code = 200 |
|
163 |
else: |
|
164 |
status_code = 403 |
|
165 | ||
166 |
for username in ('john', 'jane', 'doe'): |
|
167 |
user = create_user(username=username) |
|
168 |
creds = create_credentials(user, { |
|
169 |
'login': username, |
|
170 |
'password': username |
|
171 |
}) |
|
172 |
if username != 'doe': |
|
173 |
creds.linked = True |
|
174 |
creds.save() |
|
175 | ||
176 |
response = client.get(url) |
|
177 |
assert response.status_code == status_code |
|
178 | ||
179 |
if status_code == 200: |
|
180 |
assert {'users_linked': 2, 'users_unlinked': 1} == response.data |
|
153 |
- |