0003-archimed-return-Response-in-Publik-Api-Response-form.patch
mandayejs/views.py | ||
---|---|---|
32 | 32 |
"""Archimed user's account details |
33 | 33 |
""" |
34 | 34 | |
35 |
def dispatch(self, request, *args, **kwargs): |
|
36 |
response = super(ArchimedAccountDetails, self).dispatch(request, *args, **kwargs) |
|
37 |
if response.status_code == 200: |
|
38 |
response.data = {'data': response.data, 'err': 0} |
|
39 |
else: |
|
40 |
response.data = {'data': None, 'err': 1, 'err_desc': response.data} |
|
41 |
return response |
|
42 | ||
35 | 43 |
def get(self, request, *args, **kwargs): |
36 | 44 |
logger = logging.getLogger(__name__) |
37 | 45 |
app_settings = get_app_settings() |
... | ... | |
43 | 51 |
try: |
44 | 52 |
user = User.objects.get(username=username) |
45 | 53 |
except (User.DoesNotExist,): |
46 |
return Response({'message': 'User %s does not exist' % username, |
|
47 |
'success': False}, status=status.HTTP_404_NOT_FOUND) |
|
54 |
return Response('User %s does not exist' % username, status=status.HTTP_404_NOT_FOUND) |
|
48 | 55 | |
49 | 56 |
try: |
50 | 57 |
credentials = UserCredentials.objects.get(user=user) |
51 | 58 |
except (UserCredentials.DoesNotExist,): |
52 |
return Response({ |
|
53 |
'message': 'User %s is not associated' % username, |
|
54 |
'success': False}, status=status.HTTP_404_NOT_FOUND) |
|
59 |
return Response('User %s is not associated' % username, status=status.HTTP_404_NOT_FOUND) |
|
55 | 60 | |
56 | 61 |
login_url = request.build_absolute_uri( |
57 | 62 |
'/DEFAULT/Ermes/Recherche/logon.svc/logon') |
... | ... | |
59 | 64 |
with requests.Session() as session: |
60 | 65 |
login_info = credentials.to_login_info(decrypt=True) |
61 | 66 |
login_info = {'username': login_info['#carte'], 'password': login_info['#code']} |
62 |
response = session.post(login_url, data=login_info, verify=False)
|
|
67 |
response = session.post(login_url, data=login_info) |
|
63 | 68 |
logger.debug("Archimed login response {}".format(response.json())) |
64 | 69 |
if not response.json()['success']: |
65 |
return Response({'message': 'Authentication failed', |
|
66 |
'success': False}, status=status.HTTP_401_UNAUTHORIZED) |
|
70 |
return Response('Authentication failed', status=status.HTTP_401_UNAUTHORIZED) |
|
67 | 71 | |
68 | 72 |
content = { |
69 | 73 |
'codeConfig': '', |
70 | 74 |
'xslPath': 'Services/LectorShortAccount.xslt' |
71 | 75 |
} |
72 |
response = session.post(ws_uri, json=content, verify=False)
|
|
76 |
response = session.post(ws_uri, json=content) |
|
73 | 77 |
logger.debug("Archimed ws response {}".format(response.json())) |
74 | 78 |
return Response(response.json()) |
75 | 79 |
tests/test_archimed.py | ||
---|---|---|
41 | 41 |
client.login(username='kevin', password='kevin') |
42 | 42 |
response = client.get('/_mandaye/ws/account/whatever/') |
43 | 43 |
assert response.status_code == 404 |
44 |
assert response.data['message'] == 'User whatever does not exist'
|
|
45 |
assert response.data['success'] is False
|
|
44 |
assert response.data['err_desc'] == 'User whatever does not exist'
|
|
45 |
assert response.data['err'] == 1
|
|
46 | 46 | |
47 | 47 |
# test with unlinked user |
48 | 48 |
client = Client() |
49 | 49 |
client.login(username='kevin', password='kevin') |
50 | 50 |
response = client.get('/_mandaye/ws/account/kevin/') |
51 | 51 |
assert response.status_code == 404 |
52 |
assert response.data['message'] == 'User kevin is not associated'
|
|
53 |
assert response.data['success'] is False
|
|
52 |
assert response.data['err_desc'] == 'User kevin is not associated'
|
|
53 |
assert response.data['err'] == 1
|
|
54 | 54 | |
55 | 55 |
create_credentials(user, {'carte': 'kevin', 'code': 'whatever'}) |
56 | 56 |
# test with wrong credentials |
... | ... | |
58 | 58 |
client.login(username='kevin', password='kevin') |
59 | 59 |
response = client.get('/_mandaye/ws/account/kevin/') |
60 | 60 |
assert response.status_code == 401 |
61 |
assert response.data['message'] == 'Authentication failed'
|
|
62 |
assert response.data['success'] is False
|
|
61 |
assert response.data['err_desc'] == 'Authentication failed'
|
|
62 |
assert response.data['err'] == 1
|
|
63 | 63 | |
64 | 64 |
# test with good credentials |
65 | 65 |
client.login(username='kevin', password='kevin') |
66 | 66 |
response = client.get('/_mandaye/ws/account/kevin/') |
67 | 67 |
assert response.status_code == 200 |
68 |
assert response.data['message'] == 'Whatever is whatever' |
|
69 |
assert response.data['success'] is True |
|
68 |
assert response.data['data']['message'] == 'Whatever is whatever' |
|
69 |
assert response.data['data']['success'] is True |
|
70 |
- |