1
|
import os
|
2
|
import json
|
3
|
import uuid
|
4
|
|
5
|
import pytest
|
6
|
import mock
|
7
|
from rest_framework.test import APIClient as Client
|
8
|
|
9
|
from mandayejs.applications import Archimed
|
10
|
from utils import create_user, create_credentials
|
11
|
|
12
|
pytestmark = pytest.mark.django_db
|
13
|
|
14
|
|
15
|
def get_base_dir(filename):
|
16
|
return file(os.path.join(os.path.dirname(__file__), 'data', filename)).read()
|
17
|
|
18
|
|
19
|
class MokcedRequestsResponse(mock.Mock):
|
20
|
|
21
|
def json(self):
|
22
|
return json.loads(self.content)
|
23
|
|
24
|
|
25
|
MOCKED_RESPONSES_LIST = [
|
26
|
MokcedRequestsResponse(content=get_base_dir('archimed_auth_failure_response.json')),
|
27
|
MokcedRequestsResponse(content=get_base_dir('archimed_auth_success_response.json')),
|
28
|
MokcedRequestsResponse(content=get_base_dir('archimed_account_detail_response.json'))
|
29
|
]
|
30
|
|
31
|
|
32
|
@mock.patch('mandayejs.applications.views.requests.Session.post')
|
33
|
@mock.patch('mandayejs.applications.get_app_settings')
|
34
|
def test_archimed_ws(mocked_get_app_settings, mocked_requests_post):
|
35
|
mocked_get_app_settings.return_value = Archimed
|
36
|
mocked_requests_post.side_effect = MOCKED_RESPONSES_LIST
|
37
|
|
38
|
user_uuid = uuid.uuid4().hex
|
39
|
|
40
|
user = create_user(username=user_uuid[:30], password='kevin')
|
41
|
|
42
|
# test with invalid username
|
43
|
client = Client()
|
44
|
client.login(username=user_uuid[:30], password='kevin')
|
45
|
response = client.get('/_mandaye/ws/account/whatever/')
|
46
|
assert response.status_code == 404
|
47
|
assert json.loads(response.content)['err_desc'] == 'User whatever does not exist'
|
48
|
assert json.loads(response.content)['err'] == 1
|
49
|
|
50
|
# test with unlinked user
|
51
|
response = client.get('/_mandaye/ws/account/%s/' % user_uuid)
|
52
|
assert response.status_code == 404
|
53
|
assert json.loads(response.content)['err_desc'] == 'User %s is not associated' % user_uuid[:30]
|
54
|
assert json.loads(response.content)['err'] == 1
|
55
|
|
56
|
create_credentials(user, {'carte': 'kevin', 'code': 'whatever'})
|
57
|
# test with wrong credentials
|
58
|
response = client.get('/_mandaye/ws/account/%s/' % user_uuid)
|
59
|
assert response.status_code == 401
|
60
|
assert json.loads(response.content)['err_desc'] == 'Authentication failed'
|
61
|
assert json.loads(response.content)['err'] == 1
|
62
|
|
63
|
# test with good credentials
|
64
|
response = client.get('/_mandaye/ws/account/%s/' % user_uuid)
|
65
|
assert response.status_code == 200
|
66
|
assert json.loads(response.content)['data']['message'] == 'Whatever is whatever'
|
67
|
assert json.loads(response.content)['data']['success'] is True
|