Révision bf13a765
Ajouté par Josué Kouka il y a environ 7 ans
mandayejs/views.py | ||
---|---|---|
20 | 20 |
import requests |
21 | 21 |
|
22 | 22 |
from django.contrib.auth.models import User |
23 |
from django.shortcuts import get_object_or_404 |
|
24 |
|
|
25 | 23 |
from rest_framework import status |
26 | 24 |
from rest_framework.views import APIView |
27 | 25 |
from rest_framework.response import Response |
... | ... | |
41 | 39 |
app_settings.SITE_WS_ENDPOINT['account_details']) |
42 | 40 |
|
43 | 41 |
username = kwargs['username'] |
44 |
user = get_object_or_404(User, username=username) |
|
45 |
credentials = get_object_or_404(UserCredentials, user=user) |
|
42 |
|
|
43 |
try: |
|
44 |
user = User.objects.get(username=username) |
|
45 |
except (User.DoesNotExist,): |
|
46 |
return Response({'message': 'User %s does not exist' % username, |
|
47 |
'success': False}, status=status.HTTP_404_NOT_FOUND) |
|
48 |
|
|
49 |
try: |
|
50 |
credentials = UserCredentials.objects.get(user=user) |
|
51 |
except (UserCredentials.DoesNotExist,): |
|
52 |
return Response({ |
|
53 |
'message': 'User %s is not associated' % username, |
|
54 |
'success': False}, status=status.HTTP_404_NOT_FOUND) |
|
46 | 55 |
|
47 | 56 |
login_url = request.build_absolute_uri( |
48 | 57 |
'/DEFAULT/Ermes/Recherche/logon.svc/logon') |
... | ... | |
53 | 62 |
response = session.post(login_url, data=login_info) |
54 | 63 |
logger.debug("Archimed login response {}".format(response.json())) |
55 | 64 |
if not response.json()['success']: |
56 |
return Response('Authentication failed', status=status.HTTP_401_UNAUTHORIZED) |
|
65 |
return Response({'message': 'Authentication failed', |
|
66 |
'success': False}, status=status.HTTP_401_UNAUTHORIZED) |
|
57 | 67 |
|
58 | 68 |
content = { |
59 | 69 |
'codeConfig': '', |
tests/test_archimed.py | ||
---|---|---|
1 |
import os |
|
2 |
import json |
|
3 |
|
|
4 |
import pytest |
|
5 |
import mock |
|
6 |
from rest_framework.test import APIClient as Client |
|
7 |
|
|
8 |
from mandayejs.applications import Archimed |
|
9 |
from utils import create_user, create_credentials |
|
10 |
|
|
11 |
pytestmark = pytest.mark.django_db |
|
12 |
|
|
13 |
|
|
14 |
def get_base_dir(filename): |
|
15 |
return file(os.path.join(os.path.dirname(__file__), 'data', filename)).read() |
|
16 |
|
|
17 |
|
|
18 |
class MokcedRequestsResponse(mock.Mock): |
|
19 |
|
|
20 |
def json(self): |
|
21 |
return json.loads(self.content) |
|
22 |
|
|
23 |
|
|
24 |
MOCKED_RESPONSES_LIST = [ |
|
25 |
MokcedRequestsResponse(content=get_base_dir('archimed_auth_failure_response.json')), |
|
26 |
MokcedRequestsResponse(content=get_base_dir('archimed_auth_success_response.json')), |
|
27 |
MokcedRequestsResponse(content=get_base_dir('archimed_account_detail_response.json')) |
|
28 |
] |
|
29 |
|
|
30 |
|
|
31 |
@mock.patch('mandayejs.views.requests.Session.post') |
|
32 |
@mock.patch('mandayejs.applications.get_app_settings') |
|
33 |
def test_archimed_ws(mocked_get_app_settings, mocked_requests_post): |
|
34 |
mocked_get_app_settings.return_value = Archimed |
|
35 |
mocked_requests_post.side_effect = MOCKED_RESPONSES_LIST |
|
36 |
|
|
37 |
user = create_user(username='kevin', password='kevin') |
|
38 |
|
|
39 |
# test with invalid username |
|
40 |
client = Client() |
|
41 |
client.login(username='kevin', password='kevin') |
|
42 |
response = client.get('/_mandaye/ws/account/whatever/') |
|
43 |
assert response.status_code == 404 |
|
44 |
assert json.loads(response.content)['message'] == 'User whatever does not exist' |
|
45 |
assert json.loads(response.content)['success'] is False |
|
46 |
|
|
47 |
# test with unlinked user |
|
48 |
client = Client() |
|
49 |
client.login(username='kevin', password='kevin') |
|
50 |
response = client.get('/_mandaye/ws/account/kevin/') |
|
51 |
assert response.status_code == 404 |
|
52 |
assert json.loads(response.content)['message'] == 'User kevin is not associated' |
|
53 |
assert json.loads(response.content)['success'] is False |
|
54 |
|
|
55 |
create_credentials(user, {'carte': 'kevin', 'code': 'whatever'}) |
|
56 |
# test with wrong credentials |
|
57 |
client = Client() |
|
58 |
client.login(username='kevin', password='kevin') |
|
59 |
response = client.get('/_mandaye/ws/account/kevin/') |
|
60 |
assert response.status_code == 401 |
|
61 |
assert json.loads(response.content)['message'] == 'Authentication failed' |
|
62 |
assert json.loads(response.content)['success'] is False |
|
63 |
|
|
64 |
# test with good credentials |
|
65 |
client.login(username='kevin', password='kevin') |
|
66 |
response = client.get('/_mandaye/ws/account/kevin/') |
|
67 |
assert response.status_code == 200 |
|
68 |
assert json.loads(response.content)['message'] == 'Whatever is whatever' |
|
69 |
assert json.loads(response.content)['success'] is True |
Formats disponibles : Unified diff
return explicite errors (#14967)