1
|
import mock
|
2
|
import pytest
|
3
|
|
4
|
from django.conf import settings
|
5
|
from django.core.urlresolvers import reverse
|
6
|
|
7
|
from mandayejs.mandaye.models import UserCredentials
|
8
|
from utils import create_user, create_credentials, get_uuid, get_user
|
9
|
|
10
|
pytestmark = pytest.mark.django_db
|
11
|
|
12
|
|
13
|
def test_api_get(client, url):
|
14
|
response = client.get(url)
|
15
|
if client.session.values():
|
16
|
status_code = 200
|
17
|
else:
|
18
|
status_code = 403
|
19
|
|
20
|
assert response.status_code == status_code
|
21
|
|
22
|
if status_code == 200:
|
23
|
assert {'login': '', 'password': ''} == response.data
|
24
|
|
25
|
|
26
|
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
|
27
|
def test_signed_api_get(client_service, url_signed):
|
28
|
response = client_service.get(url_signed.url)
|
29
|
if url_signed.orig == 'testserver':
|
30
|
status_codes = (200,)
|
31
|
else:
|
32
|
status_codes = (401, 403)
|
33
|
|
34
|
assert response.status_code in status_codes
|
35
|
|
36
|
if response.status_code == 200:
|
37
|
assert {'login': '', 'password': ''} == response.data
|
38
|
|
39
|
|
40
|
# POST
|
41
|
@mock.patch('mandayejs.mandaye.api.exec_phantom')
|
42
|
def test_api_post(mock_phantomjs_result, client, url, payload):
|
43
|
|
44
|
if client.session.values():
|
45
|
status_code = {'success': 200, 'failure': 401}
|
46
|
else:
|
47
|
status_code = {'success': 403, 'failure': 403}
|
48
|
|
49
|
if payload.get('name_id_content') == '12345':
|
50
|
response = client.post(url, data=payload, format='json')
|
51
|
|
52
|
assert response.status_code == status_code['failure']
|
53
|
|
54
|
if client.session.values():
|
55
|
kevin = get_user(first_name='kevin')
|
56
|
assert kevin.username == payload['name_id_content']
|
57
|
|
58
|
kevin_uuid = get_uuid(name_id=payload['name_id_content'])
|
59
|
assert kevin_uuid.name_id == '12345'
|
60
|
else:
|
61
|
mock_phantomjs_result.return_value = {"result": "ok"}
|
62
|
|
63
|
response = client.post(url, data=payload, format='json')
|
64
|
|
65
|
assert response.status_code == status_code['success']
|
66
|
|
67
|
if client.session.values():
|
68
|
josh = get_user(username='77777')
|
69
|
josh_creds = UserCredentials.objects.filter(user=josh)[0]
|
70
|
|
71
|
assert josh_creds.to_login_info()['#login'] == 'josh'
|
72
|
assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password'
|
73
|
|
74
|
|
75
|
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
|
76
|
@mock.patch('mandayejs.mandaye.api.exec_phantom')
|
77
|
def test_signed_api_post(mock_phantomjs_result, client_service, url_signed, payload):
|
78
|
if url_signed.orig == 'testserver':
|
79
|
status_code = {'success': 200, 'failure': 401}
|
80
|
else:
|
81
|
status_code = {'success': 403, 'failure': 403}
|
82
|
|
83
|
if payload.get('name_id_content') == '12345':
|
84
|
response = client_service.post(url_signed.url, data=payload, format='json')
|
85
|
|
86
|
assert response.status_code == status_code['failure']
|
87
|
|
88
|
if url_signed.orig == 'testserver':
|
89
|
kevin = get_user(first_name='kevin')
|
90
|
assert kevin.username == payload['name_id_content']
|
91
|
|
92
|
kevin_uuid = get_uuid(name_id=payload['name_id_content'])
|
93
|
assert kevin_uuid.name_id == '12345'
|
94
|
else:
|
95
|
mock_phantomjs_result.return_value = {"result": "ok"}
|
96
|
|
97
|
response = client_service.post(url_signed.url, data=payload, format='json')
|
98
|
|
99
|
assert response.status_code == status_code['success']
|
100
|
|
101
|
if url_signed.orig == 'testserver':
|
102
|
josh = get_user(username='77777')
|
103
|
josh_creds = UserCredentials.objects.filter(user=josh)[0]
|
104
|
|
105
|
assert josh_creds.to_login_info()['#login'] == 'josh'
|
106
|
assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password'
|
107
|
|
108
|
|
109
|
# DELETE
|
110
|
def test_api_delete(client, url):
|
111
|
if client.session.values():
|
112
|
status_code = {'success': 200, 'failure': 404}
|
113
|
else:
|
114
|
status_code = {'success': 403, 'failure': 403}
|
115
|
|
116
|
kevin = get_user(first_name='kevin')
|
117
|
assert UserCredentials.objects.filter(user=kevin).exists() is False
|
118
|
response = client.delete(url, data={'name_id_content': '12345'}, format='json')
|
119
|
assert response.status_code == status_code['failure']
|
120
|
|
121
|
josh = create_user(username='77777')
|
122
|
create_credentials(josh, {
|
123
|
'login': 'josh',
|
124
|
'password': 'josh password'})
|
125
|
|
126
|
assert UserCredentials.objects.filter(user=josh).exists() is True
|
127
|
response = client.delete(url, data={'name_id_content': '77777'}, format='json')
|
128
|
assert response.status_code == status_code['success']
|
129
|
if client.session.values():
|
130
|
assert UserCredentials.objects.filter(user=josh).exists() is False
|
131
|
|
132
|
|
133
|
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
|
134
|
def test_signed_api_delete(client_service, url_signed):
|
135
|
if url_signed.orig == 'testserver':
|
136
|
status_code = {'success': 200, 'failure': 404}
|
137
|
else:
|
138
|
status_code = {'success': 403, 'failure': 403}
|
139
|
|
140
|
kevin = get_user(first_name='kevin')
|
141
|
assert UserCredentials.objects.filter(user=kevin).exists() is False
|
142
|
response = client_service.delete(url_signed.url, data={'name_id_content': '12345'}, format='json')
|
143
|
assert response.status_code == status_code['failure']
|
144
|
|
145
|
josh = create_user(username='77777')
|
146
|
create_credentials(josh, {
|
147
|
'login': 'josh',
|
148
|
'password': 'josh password'
|
149
|
})
|
150
|
|
151
|
assert UserCredentials.objects.filter(user=josh).exists() is True
|
152
|
response = client_service.delete(url_signed.url, data={'name_id_content': '77777'}, format='json')
|
153
|
assert response.status_code == status_code['success']
|
154
|
if url_signed.orig == 'testserver':
|
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
|