Projet

Général

Profil

0001-don-t-associate-user-if-PhantomJS-response-is-errone.patch

Josué Kouka, 31 janvier 2017 18:05

Télécharger (3,72 ko)

Voir les différences:

Subject: [PATCH] don't associate user if PhantomJS response is erroneous
 (#14813)

 mandayejs/mandaye/utils.py |  4 ++--
 mandayejs/mandaye/views.py |  3 +++
 tests/test_mandayejs.py    | 26 +++++++++++++++++++-------
 3 files changed, 24 insertions(+), 9 deletions(-)
mandayejs/mandaye/utils.py
44 44
        try:
45 45
            result = json.loads(stdout)
46 46
        except (ValueError,):
47
            result = {"result": "failure, couldn't decode JSON"}
48
            logger.error(stdout)
47
            result = {"result": "json_error"}
48
            logger.error("invalid json: %s" % stdout)
49 49

  
50 50
        if result.get('stderr'):
51 51
            logger.warning(result['stderr'])
mandayejs/mandaye/views.py
159 159
    elif result.get('result') == 'timeout':
160 160
        messages.error(request, _('server took too long to respond'))
161 161
        url = resolve_url('associate')
162
    elif result.get('result') == 'json_error':
163
        messages.error(request, _('invalid response from server'))
164
        url = resolve_url('associate')
162 165
    elif result.get('result') == 'redirect':
163 166
        url = urlparse(result.get('url', '/'))
164 167
        url = url.path
tests/test_mandayejs.py
9 9
from django.core.management import call_command
10 10
from django.http.request import HttpRequest, QueryDict
11 11
from django.forms.fields import DateField
12
from django.test.client import RequestFactory
12
from django.test.client import RequestFactory, Client
13 13
from django.core.urlresolvers import reverse
14 14

  
15 15
from mandayejs.mandaye.models import UserCredentials
......
246 246

  
247 247

  
248 248
@mock.patch('mandayejs.mandaye.utils.subprocess.Popen')
249
def test_phantom_invalid_json(mocked_popen, caplog):
249
@mock.patch('mandayejs.applications.Test.SITE_LOCATORS', MOCKED_SITE_LOCATORS)
250
def test_phantom_invalid_json(mocked_popen, caplog, user_john):
250 251
    expected_output = ('This is not a valid JSON', None)
251 252
    mocked_popen.return_value = MockedPopen(expected_output=expected_output)
252 253

  
253
    result = exec_phantom(LOGIN_INFO)
254
    for record in caplog.records():
255
        assert record.levelname == 'ERROR'
256
        assert record.message == 'This is not a valid JSON'
254
    UserCredentials.objects.create(user=user_john,
255
                                   locators={
256
                                       'login': 'johnny', 'password': 'jumper',
257
                                       'birth_date': '1995-06-11'})
258

  
259
    client = Client()
260
    client.login(username='john', password='john')
261
    response = client.get(reverse('post-login-do'))
262
    assert 'window.top.location = "/_mandaye/associate/"' in response.content
257 263

  
258
    assert result['result'] == "failure, couldn't decode JSON"
264
    for message in response.context['messages']:
265
        assert message.level_tag == 'error'
266
        assert message.message == 'invalid response from server'
267

  
268
    for record in caplog.records():
269
        if record.levelname == 'ERROR':
270
            assert record.message == 'invalid json: This is not a valid JSON'
259 271

  
260 272

  
261 273
@mock.patch('mandayejs.mandaye.utils.subprocess.Popen')
262
-