Projet

Général

Profil

0001-misc-move-api-s-tests-into-dedicated-file.patch

Josué Kouka, 06 mars 2018 14:21

Télécharger (12,4 ko)

Voir les différences:

Subject: [PATCH 1/2] misc: move api's tests into dedicated file

 tests/test_api.py       | 152 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_mandayejs.py | 151 +----------------------------------------------
 2 files changed, 153 insertions(+), 150 deletions(-)
 create mode 100644 tests/test_api.py
tests/test_api.py
1
import mock
2
import pytest
3

  
4
from django.conf import settings
5

  
6
from mandayejs.mandaye.models import UserCredentials
7
from utils import create_user, create_credentials, get_uuid, get_user
8

  
9

  
10
def test_api_get(client, url):
11
    response = client.get(url)
12
    if client.session.values():
13
        status_code = 200
14
    else:
15
        status_code = 403
16

  
17
    assert response.status_code == status_code
18

  
19
    if status_code == 200:
20
        assert {'login': '', 'password': ''} == response.data
21

  
22

  
23
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
24
def test_signed_api_get(client_service, url_signed):
25
    response = client_service.get(url_signed.url)
26
    if url_signed.orig == 'testserver':
27
        status_code = 200
28
    else:
29
        status_code = 403
30

  
31
    assert response.status_code == status_code
32

  
33
    if status_code == 200:
34
        assert {'login': '', 'password': ''} == response.data
35

  
36

  
37
# POST
38
@mock.patch('mandayejs.mandaye.api.exec_phantom')
39
def test_api_post(mock_phantomjs_result, client, url, payload):
40

  
41
    if client.session.values():
42
        status_code = {'success': 200, 'failure': 401}
43
    else:
44
        status_code = {'success': 403, 'failure': 403}
45

  
46
    if payload.get('name_id_content') == '12345':
47
        response = client.post(url, data=payload, format='json')
48

  
49
        assert response.status_code == status_code['failure']
50

  
51
        if client.session.values():
52
            kevin = get_user(first_name='kevin')
53
            assert kevin.username == payload['name_id_content']
54

  
55
            kevin_uuid = get_uuid(name_id=payload['name_id_content'])
56
            assert kevin_uuid.name_id == '12345'
57
    else:
58
        mock_phantomjs_result.return_value = {"result": "ok"}
59

  
60
        response = client.post(url, data=payload, format='json')
61

  
62
        assert response.status_code == status_code['success']
63

  
64
        if client.session.values():
65
            josh = get_user(username='77777')
66
            josh_creds = UserCredentials.objects.filter(user=josh)[0]
67

  
68
            assert josh_creds.to_login_info()['#login'] == 'josh'
69
            assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password'
70

  
71

  
72
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
73
@mock.patch('mandayejs.mandaye.api.exec_phantom')
74
def test_signed_api_post(mock_phantomjs_result, client_service, url_signed, payload):
75
    if url_signed.orig == 'testserver':
76
        status_code = {'success': 200, 'failure': 401}
77
    else:
78
        status_code = {'success': 403, 'failure': 403}
79

  
80
    if payload.get('name_id_content') == '12345':
81
        response = client_service.post(url_signed.url, data=payload, format='json')
82

  
83
        assert response.status_code == status_code['failure']
84

  
85
        if url_signed.orig == 'testserver':
86
            kevin = get_user(first_name='kevin')
87
            assert kevin.username == payload['name_id_content']
88

  
89
            kevin_uuid = get_uuid(name_id=payload['name_id_content'])
90
            assert kevin_uuid.name_id == '12345'
91
    else:
92
        mock_phantomjs_result.return_value = {"result": "ok"}
93

  
94
        response = client_service.post(url_signed.url, data=payload, format='json')
95

  
96
        assert response.status_code == status_code['success']
97

  
98
        if url_signed.orig == 'testserver':
99
            josh = get_user(username='77777')
100
            josh_creds = UserCredentials.objects.filter(user=josh)[0]
101

  
102
            assert josh_creds.to_login_info()['#login'] == 'josh'
103
            assert josh_creds.to_login_info(decrypt=True)['#password'] == 'josh password'
104

  
105

  
106
# DELETE
107
def test_api_delete(client, url):
108
    if client.session.values():
109
        status_code = {'success': 200, 'failure': 404}
110
    else:
111
        status_code = {'success': 403, 'failure': 403}
112

  
113
    kevin = get_user(first_name='kevin')
114
    assert UserCredentials.objects.filter(user=kevin).exists() is False
115
    response = client.delete(url, data={'name_id_content': '12345'}, format='json')
116
    assert response.status_code == status_code['failure']
117

  
118
    josh = create_user(username='77777')
119
    create_credentials(josh, {
120
        'login': 'josh',
121
        'password': 'josh password'})
122

  
123
    assert UserCredentials.objects.filter(user=josh).exists() is True
124
    response = client.delete(url, data={'name_id_content': '77777'}, format='json')
125
    assert response.status_code == status_code['success']
126
    if client.session.values():
127
        assert UserCredentials.objects.filter(user=josh).exists() is False
128

  
129

  
130
@pytest.mark.skipif(settings.HOBO is None, reason="hobo is required")
131
def test_signed_api_delete(client_service, url_signed):
132
    if url_signed.orig == 'testserver':
133
        status_code = {'success': 200, 'failure': 404}
134
    else:
135
        status_code = {'success': 403, 'failure': 403}
136

  
137
    kevin = get_user(first_name='kevin')
138
    assert UserCredentials.objects.filter(user=kevin).exists() is False
139
    response = client_service.delete(url_signed.url, data={'name_id_content': '12345'}, format='json')
140
    assert response.status_code == status_code['failure']
141

  
142
    josh = create_user(username='77777')
143
    create_credentials(josh, {
144
        'login': 'josh',
145
        'password': 'josh password'
146
    })
147

  
148
    assert UserCredentials.objects.filter(user=josh).exists() is True
149
    response = client_service.delete(url_signed.url, data={'name_id_content': '77777'}, format='json')
150
    assert response.status_code == status_code['success']
151
    if url_signed.orig == 'testserver':
152
        assert UserCredentials.objects.filter(user=josh).exists() is False
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):
252
-