0001-api-add-franceconnect-information-to-user-s-API-when.patch
src/authentic2_auth_fc/__init__.py | ||
---|---|---|
42 | 42 |
from authentic2.api_views import UsersAPI |
43 | 43 |
UsersAPI.fc_unlink = fc_unlink |
44 | 44 | |
45 |
def a2_hook_api_modify_serializer(self, view, serializer): |
|
46 |
from rest_framework import serializers |
|
47 |
from authentic2.utils import make_url |
|
48 |
from . import app_settings |
|
49 | ||
50 |
if not app_settings.enable: |
|
51 |
return |
|
52 | ||
53 |
request = view.request |
|
54 | ||
55 |
if 'full' not in request.GET: |
|
56 |
return |
|
57 | ||
58 |
if view.__class__.__name__ == 'UsersAPI': |
|
59 |
def get_franceconnect(user): |
|
60 |
linked = user.fc_accounts.exists() |
|
61 |
return { |
|
62 |
'linked': linked, |
|
63 |
'link_url': make_url('fc-login-or-link', request=request, absolute=True), |
|
64 |
'unlink_url': make_url('fc-unlink', request=request, absolute=True), |
|
65 |
} |
|
66 |
serializer.get_franceconnect = get_franceconnect |
|
67 |
serializer.fields['franceconnect'] = serializers.SerializerMethodField() |
|
68 | ||
45 | 69 | |
46 | 70 |
default_app_config = '%s.%s' % (__name__, 'AppConfig') |
tests/test_api.py | ||
---|---|---|
12 | 12 |
# test success |
13 | 13 |
app.delete(url, status=204) |
14 | 14 |
assert FcAccount.objects.filter(user=user_cartman).exists() is False |
15 | ||
16 | ||
17 |
def test_api_user_franceconnect(settings, app, admin, user_cartman): |
|
18 |
settings.A2_FC_ENABLE = True |
|
19 | ||
20 |
url = '/api/users/%s/' % user_cartman.uuid |
|
21 |
# test unauthorized method |
|
22 |
app.authorization = ('Basic', (admin.username, admin.username)) |
|
23 |
response = app.get(url + '?full') |
|
24 |
assert 'franceconnect' in response.json, 'missing franceconnect field in user API' |
|
25 |
content = response.json['franceconnect'] |
|
26 |
assert isinstance(content, dict), 'franceconnect field is not a dict' |
|
27 |
assert content.get('linked') is True |
|
28 |
assert content.get('link_url').startswith('http://') |
|
29 |
assert content.get('link_url').endswith('/callback/') |
|
30 |
assert content.get('unlink_url').startswith('http://') |
|
31 |
assert content.get('unlink_url').endswith('/unlink/') |
|
32 | ||
33 |
unlink_url = '/api/users/%s/fc-unlink/' % user_cartman.uuid |
|
34 |
app.delete(unlink_url, status=204) |
|
35 | ||
36 |
response = app.get(url + '?full') |
|
37 |
assert response.json['franceconnect']['linked'] is False |
|
15 |
- |