only get stdout enclosed by <mandayejs> tag (#14881)

This commit is contained in:
Josue Kouka 2017-02-07 15:07:44 +01:00
parent 25613946c5
commit 7030f3e5e7
4 changed files with 43 additions and 18 deletions

View File

@ -24,12 +24,16 @@ headers_list = [];
var output = {'stderr': null, 'result': null };
function mandaye_exit(message){
console.log('<mandayejs>'+message+'</mandayejs>')
phantom.exit()
}
page.onResourceReceived = function(response){
if (response.url === input.address && response.status > 399){
output['result'] = 'page not found';
output['status_code'] = response.status
console.log(JSON.stringify(output));
phantom.exit()
mandaye_exit(JSON.stringify(output));
}
for (var i=0; i < response.headers.length; i++){
var c_header = response.headers[i];
@ -56,8 +60,7 @@ page.onError = function(msg, trace){
page.open(input.address, function(status) {
if (status !== 'success'){
output['result'] = 'failed to open resource';
console.log(JSON.stringify(output));
phantom.exit();
mandaye_exit(JSON.stringify(output));
}
page.onLoadFinished = function() {
if (page.injectJs(input.auth_checker)){
@ -77,23 +80,19 @@ page.open(input.address, function(status) {
output['result'] = 'redirect';
output['reason'] = 'password_change_required';
output['url'] = page.url;
console.log(JSON.stringify(output));
phantom.exit();
mandaye_exit(JSON.stringify(output));
}
if (!input.auth_success){
output['result'] = 'failure';
output['reason'] = 'authentication';
console.log(JSON.stringify(output));
phantom.exit();
mandaye_exit(JSON.stringify(output));
}
output['result'] = 'ok';
output['url'] = page.frameUrl;
console.log(JSON.stringify(output));
phantom.exit();
mandaye_exit(JSON.stringify(output));
}
page.evaluate(function(input) {

View File

@ -31,6 +31,11 @@ var output = {'stderr': null, 'result': null };
page.viewportSize = {width: 1280, height: 1024};
function mandaye_exit(message){
console.log('<mandayejs>'+message+'</mandayejs>')
phantom.exit()
}
page.onError = function(msg, trace){
var err_stack = ['ERROR: ' + msg];
@ -46,7 +51,7 @@ page.onError = function(msg, trace){
page.open(input.address, function(status){
if (status !== 'success'){
output['result'] = 'failed to open resource';
console.log(JSON.stringify(output));
mandaye_exit(JSON.stringify(output));
phantom.exit();
}
@ -56,8 +61,7 @@ page.open(input.address, function(status){
output['cookies'] = page.cookies;
output['url'] = page.url;
console.log(JSON.stringify(output));
phantom.exit();
mandaye_exit(JSON.stringify(output));
};
var logout = page.evaluate(function(input){
@ -72,7 +76,6 @@ page.open(input.address, function(status){
if (logout == false){
output['result'] = 'logout failed';
console.log(JSON.stringify(output));
phantom.exit();
mandaye_exit(JSON.stringify(output));
}
});

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import json
import subprocess
import logging
@ -42,6 +43,10 @@ def exec_phantom(data, script='do_login.js'):
stdout, stderr = phantom.communicate(json.dumps(data))
try:
output = re.search('<mandayejs>(.*?)</mandayejs>', stdout, re.DOTALL)
if not output:
raise ValueError
stdout = output.group(1)
result = json.loads(stdout)
except (ValueError,):
result = {"result": "json_error"}

View File

@ -278,7 +278,7 @@ def test_phantom_js_errors(mocked_popen, caplog):
"result": "ok"
}
expected_output = (json.dumps(stdout), None)
expected_output = ('<mandayejs>%s</mandayejs>' % json.dumps(stdout), None)
mocked_popen.return_value = MockedPopen(expected_output=expected_output)
result = exec_phantom(LOGIN_INFO)
@ -340,7 +340,8 @@ def test_post_login_do(mocked_popen, user_john):
"reason": "password change required",
"url": "http://mydomain.com/update_password.aspx"
}
mocked_popen.return_value = MockedPopen(expected_output=(json.dumps(expected_output), None))
expected_output = '<mandayejs>%s</mandayejs>' % json.dumps(expected_output)
mocked_popen.return_value = MockedPopen(expected_output=(expected_output, None))
UserCredentials.objects.create(user=user_john,
locators={
@ -352,3 +353,20 @@ def test_post_login_do(mocked_popen, user_john):
request.user = user_john
response = post_login_do(request)
assert 'window.top.location = "/update_password.aspx"' in response.content
@mock.patch('mandayejs.mandaye.utils.subprocess.Popen')
@mock.patch('mandayejs.applications.Test.SITE_LOCATORS', MOCKED_SITE_LOCATORS)
def test_enclosed_response(mocked_popen):
output = """<mandayejs>{"result": "ok",
"authentication": "success"} </mandayejs>
this is just a random error"""
mocked_popen.return_value = MockedPopen(expected_output=(output, None))
result = exec_phantom(LOGIN_INFO)
assert result['result'] == 'ok'
# with no match
mocked_popen.return_value = MockedPopen(expected_output=('<mandayejs></mandayejs>', None))
result = exec_phantom(LOGIN_INFO)
assert result['result'] == 'json_error'