Révision b69ac82e
Ajouté par Josué Kouka il y a environ 6 ans
tests/test_mandayejs.py | ||
---|---|---|
16 | 16 |
from mandayejs.mandaye.models import UserCredentials |
17 | 17 |
from mandayejs.mandaye.utils import exec_phantom, get_login_info |
18 | 18 |
from mandayejs.mandaye.forms import FormFactory |
19 |
from mandayejs.mandaye.views import associate, post_login_do
|
|
19 |
from mandayejs.mandaye.views import post_login_do |
|
20 | 20 |
|
21 |
from utils import create_user, create_credentials, get_uuid, get_user |
|
22 | 21 |
|
23 | 22 |
pytestmark = pytest.mark.django_db |
24 | 23 |
|
... | ... | |
98 | 97 |
assert cred.to_login_info(decrypt=True)['#password'] == cred.to_login_info()['#username'] |
99 | 98 |
|
100 | 99 |
|
101 |
# MANDAYE API |
|
102 |
|
|
103 |
# GET |
|
104 |
def test_api_get(client, url): |
|
105 |
response = client.get(url) |
|
106 |
if client.session.values(): |
|
107 |
status_code = 200 |
|
108 |
else: |
|
109 |
status_code = 403 |
|
110 |
|
|
111 |
assert response.status_code == status_code |
|
112 |
|
|
113 |
if status_code == 200: |
|
114 |
assert {'login': '', 'password': ''} == response.data |
|
115 |
|
|
116 |
|
|
117 |
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") |
|
118 |
def test_signed_api_get(client_service, url_signed): |
|
119 |
response = client_service.get(url_signed.url) |
|
120 |
if url_signed.orig == 'testserver': |
|
121 |
status_code = 200 |
|
122 |
else: |
|
123 |
status_code = 403 |
|
124 |
|
|
125 |
assert response.status_code == status_code |
|
126 |
|
|
127 |
if status_code == 200: |
|
128 |
assert {'login': '', 'password': ''} == response.data |
|
129 |
|
|
130 |
|
|
131 |
# POST |
|
132 |
@mock.patch('mandayejs.mandaye.api.exec_phantom') |
|
133 |
def test_api_post(mock_phantomjs_result, client, url, payload): |
|
134 |
|
|
135 |
if client.session.values(): |
|
136 |
status_code = {'success': 200, 'failure': 401} |
|
137 |
else: |
|
138 |
status_code = {'success': 403, 'failure': 403} |
|
139 |
|
|
140 |
if payload.get('name_id_content') == '12345': |
|
141 |
response = client.post(url, data=payload, format='json') |
|
142 |
|
|
143 |
assert response.status_code == status_code['failure'] |
|
144 |
|
|
145 |
if client.session.values(): |
|
146 |
kevin = get_user(first_name='kevin') |
|
147 |
assert kevin.username == payload['name_id_content'] |
|
148 |
|
|
149 |
kevin_uuid = get_uuid(name_id=payload['name_id_content']) |
|
150 |
assert kevin_uuid.name_id == '12345' |
|
151 |
else: |
|
152 |
mock_phantomjs_result.return_value = {"result": "ok"} |
|
153 |
|
|
154 |
response = client.post(url, data=payload, format='json') |
|
155 |
|
|
156 |
assert response.status_code == status_code['success'] |
|
157 |
|
|
158 |
if client.session.values(): |
|
159 |
josh = get_user(username='77777') |
|
160 |
josh_creds = UserCredentials.objects.filter(user=josh)[0] |
|
161 |
|
|
162 |
assert josh_creds.to_login_info()['#login'] == 'josh' |
|
163 |
assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password' |
|
164 |
|
|
165 |
|
|
166 |
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") |
|
167 |
@mock.patch('mandayejs.mandaye.api.exec_phantom') |
|
168 |
def test_signed_api_post(mock_phantomjs_result, client_service, url_signed, payload): |
|
169 |
if url_signed.orig == 'testserver': |
|
170 |
status_code = {'success': 200, 'failure': 401} |
|
171 |
else: |
|
172 |
status_code = {'success': 403, 'failure': 403} |
|
173 |
|
|
174 |
if payload.get('name_id_content') == '12345': |
|
175 |
response = client_service.post(url_signed.url, data=payload, format='json') |
|
176 |
|
|
177 |
assert response.status_code == status_code['failure'] |
|
178 |
|
|
179 |
if url_signed.orig == 'testserver': |
|
180 |
kevin = get_user(first_name='kevin') |
|
181 |
assert kevin.username == payload['name_id_content'] |
|
182 |
|
|
183 |
kevin_uuid = get_uuid(name_id=payload['name_id_content']) |
|
184 |
assert kevin_uuid.name_id == '12345' |
|
185 |
else: |
|
186 |
mock_phantomjs_result.return_value = {"result": "ok"} |
|
187 |
|
|
188 |
response = client_service.post(url_signed.url, data=payload, format='json') |
|
189 |
|
|
190 |
assert response.status_code == status_code['success'] |
|
191 |
|
|
192 |
if url_signed.orig == 'testserver': |
|
193 |
josh = get_user(username='77777') |
|
194 |
josh_creds = UserCredentials.objects.filter(user=josh)[0] |
|
195 |
|
|
196 |
assert josh_creds.to_login_info()['#login'] == 'josh' |
|
197 |
assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password' |
|
198 |
|
|
199 |
|
|
200 |
# DELETE |
|
201 |
def test_api_delete(client, url): |
|
202 |
if client.session.values(): |
|
203 |
status_code = {'success': 200, 'failure': 404} |
|
204 |
else: |
|
205 |
status_code = {'success': 403, 'failure': 403} |
|
206 |
|
|
207 |
kevin = get_user(first_name='kevin') |
|
208 |
assert UserCredentials.objects.filter(user=kevin).exists() is False |
|
209 |
response = client.delete(url, data={'name_id_content': '12345'}, format='json') |
|
210 |
assert response.status_code == status_code['failure'] |
|
211 |
|
|
212 |
josh = create_user(username='77777') |
|
213 |
create_credentials(josh, { |
|
214 |
'login': 'josh', |
|
215 |
'password': 'josh password'}) |
|
216 |
|
|
217 |
assert UserCredentials.objects.filter(user=josh).exists() is True |
|
218 |
response = client.delete(url, data={'name_id_content': '77777'}, format='json') |
|
219 |
assert response.status_code == status_code['success'] |
|
220 |
if client.session.values(): |
|
221 |
assert UserCredentials.objects.filter(user=josh).exists() is False |
|
222 |
|
|
223 |
|
|
224 |
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required") |
|
225 |
def test_signed_api_delete(client_service, url_signed): |
|
226 |
if url_signed.orig == 'testserver': |
|
227 |
status_code = {'success': 200, 'failure': 404} |
|
228 |
else: |
|
229 |
status_code = {'success': 403, 'failure': 403} |
|
230 |
|
|
231 |
kevin = get_user(first_name='kevin') |
|
232 |
assert UserCredentials.objects.filter(user=kevin).exists() is False |
|
233 |
response = client_service.delete(url_signed.url, data={'name_id_content': '12345'}, format='json') |
|
234 |
assert response.status_code == status_code['failure'] |
|
235 |
|
|
236 |
josh = create_user(username='77777') |
|
237 |
create_credentials(josh, { |
|
238 |
'login': 'josh', |
|
239 |
'password': 'josh password' |
|
240 |
}) |
|
241 |
|
|
242 |
assert UserCredentials.objects.filter(user=josh).exists() is True |
|
243 |
response = client_service.delete(url_signed.url, data={'name_id_content': '77777'}, format='json') |
|
244 |
assert response.status_code == status_code['success'] |
|
245 |
if url_signed.orig == 'testserver': |
|
246 |
assert UserCredentials.objects.filter(user=josh).exists() is False |
|
247 |
|
|
248 |
|
|
249 | 100 |
@mock.patch('mandayejs.mandaye.utils.subprocess.Popen') |
250 | 101 |
@mock.patch('mandayejs.applications.Test.SITE_LOCATORS', MOCKED_SITE_LOCATORS) |
251 | 102 |
def test_phantom_invalid_json(mocked_popen, caplog, user_john): |
Formats disponibles : Unified diff
misc: move api's tests into dedicated file