Projet

Général

Profil

0001-only-get-stdout-enclosed-by-mandayejs-tag-14881.patch

Josué Kouka, 07 février 2017 16:15

Télécharger (6,4 ko)

Voir les différences:

Subject: [PATCH] only get stdout enclosed by <mandayejs> tag (#14881)

 mandayejs/do_login.js      | 21 ++++++++++-----------
 mandayejs/do_logout.js     | 13 ++++++++-----
 mandayejs/mandaye/utils.py |  5 +++++
 tests/test_mandayejs.py    | 21 +++++++++++++++++++--
 4 files changed, 42 insertions(+), 18 deletions(-)
mandayejs/do_login.js
24 24

  
25 25
var output = {'stderr': null, 'result': null };
26 26

  
27
function mandaye_exit(message){
28
    console.log('<mandayejs>'+message+'</mandayejs>')
29
    phantom.exit()
30
}
31

  
27 32
page.onResourceReceived = function(response){
28 33
    if (response.url === input.address && response.status > 399){
29 34
      output['result'] = 'page not found';
30 35
      output['status_code'] = response.status
31
      console.log(JSON.stringify(output));
32
      phantom.exit()
36
      mandaye_exit(JSON.stringify(output));
33 37
    }
34 38
    for (var i=0; i < response.headers.length; i++){
35 39
        var c_header = response.headers[i];
......
56 60
page.open(input.address, function(status) {
57 61
  if (status !== 'success'){
58 62
      output['result'] = 'failed to open resource';
59
      console.log(JSON.stringify(output));
60
      phantom.exit();
63
      mandaye_exit(JSON.stringify(output));
61 64
  }
62 65
  page.onLoadFinished = function() {
63 66
     if (page.injectJs(input.auth_checker)){
......
77 80
        output['result'] = 'redirect';
78 81
        output['reason'] = 'password_change_required';
79 82
        output['url'] = page.url;
80
        console.log(JSON.stringify(output));
81
        phantom.exit();
83
        mandaye_exit(JSON.stringify(output));
82 84
    }
83 85

  
84 86
    if (!input.auth_success){
85 87
        output['result'] = 'failure';
86 88
        output['reason'] = 'authentication';
87
        console.log(JSON.stringify(output));
88
        phantom.exit();
89
        mandaye_exit(JSON.stringify(output));
89 90
    }
90 91

  
91 92
    output['result'] = 'ok';
92 93
    output['url'] = page.frameUrl;
93 94

  
94
    console.log(JSON.stringify(output));
95

  
96
    phantom.exit();
95
    mandaye_exit(JSON.stringify(output));
97 96
  }
98 97

  
99 98
  page.evaluate(function(input) {
mandayejs/do_logout.js
31 31

  
32 32
page.viewportSize = {width: 1280, height: 1024};
33 33

  
34
function mandaye_exit(message){
35
    console.log('<mandayejs>'+message+'</mandayejs>')
36
    phantom.exit()
37
}
38

  
34 39
page.onError = function(msg, trace){
35 40
    var err_stack = ['ERROR: ' + msg];
36 41

  
......
46 51
page.open(input.address, function(status){
47 52
    if (status !== 'success'){
48 53
        output['result'] = 'failed to open resource';
49
        console.log(JSON.stringify(output));
54
        mandaye_exit(JSON.stringify(output));
50 55
        phantom.exit();
51 56
    }
52 57

  
......
56 61
        output['cookies'] = page.cookies;
57 62
        output['url'] = page.url;
58 63

  
59
        console.log(JSON.stringify(output));
60
        phantom.exit();
64
        mandaye_exit(JSON.stringify(output));
61 65
    };
62 66

  
63 67
    var logout = page.evaluate(function(input){
......
72 76

  
73 77
    if (logout == false){
74 78
        output['result'] = 'logout failed';
75
        console.log(JSON.stringify(output));
76
        phantom.exit();
79
        mandaye_exit(JSON.stringify(output));
77 80
    }
78 81
});
mandayejs/mandaye/utils.py
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16
import os
17
import re
17 18
import json
18 19
import subprocess
19 20
import logging
......
42 43
        stdout, stderr = phantom.communicate(json.dumps(data))
43 44

  
44 45
        try:
46
            output = re.search('<mandayejs>(.*)</mandayejs>', stdout)
47
            if not output:
48
                raise ValueError
49
            stdout = output.group(1)
45 50
            result = json.loads(stdout)
46 51
        except (ValueError,):
47 52
            result = {"result": "json_error"}
tests/test_mandayejs.py
278 278
        "result": "ok"
279 279
    }
280 280

  
281
    expected_output = (json.dumps(stdout), None)
281
    expected_output = ('<mandayejs>%s</mandayejs>' % json.dumps(stdout), None)
282 282

  
283 283
    mocked_popen.return_value = MockedPopen(expected_output=expected_output)
284 284
    result = exec_phantom(LOGIN_INFO)
......
340 340
        "reason": "password change required",
341 341
        "url": "http://mydomain.com/update_password.aspx"
342 342
    }
343
    mocked_popen.return_value = MockedPopen(expected_output=(json.dumps(expected_output), None))
343
    expected_output = '<mandayejs>%s</mandayejs>' % json.dumps(expected_output)
344
    mocked_popen.return_value = MockedPopen(expected_output=(expected_output, None))
344 345

  
345 346
    UserCredentials.objects.create(user=user_john,
346 347
                                   locators={
......
352 353
    request.user = user_john
353 354
    response = post_login_do(request)
354 355
    assert 'window.top.location = "/update_password.aspx"' in response.content
356

  
357

  
358
@mock.patch('mandayejs.mandaye.utils.subprocess.Popen')
359
@mock.patch('mandayejs.applications.Test.SITE_LOCATORS', MOCKED_SITE_LOCATORS)
360
def test_enclosed_response(mocked_popen):
361
    output = {'result': 'ok'}
362
    output = '<mandayejs>%s</mandayejs> this is just a random error' % json.dumps(output)
363

  
364
    mocked_popen.return_value = MockedPopen(expected_output=(output, None))
365
    result = exec_phantom(LOGIN_INFO)
366
    assert result['result'] == 'ok'
367

  
368
    # with no match
369
    mocked_popen.return_value = MockedPopen(expected_output=('<mandayejs></mandayejs>', None))
370
    result = exec_phantom(LOGIN_INFO)
371
    assert result['result'] == 'json_error'
355
-