Skip to content

Commit

Permalink
Tests for form submit actions
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Dec 7, 2021
1 parent c62a255 commit 3c71405
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/qgsaction.cpp
Expand Up @@ -100,6 +100,14 @@ void QgsAction::run( const QgsExpressionContext &expressionContext ) const
url.setQuery( QString( ) );

QNetworkRequest req { url };

// Specific code for testing, produces an invalid POST but we can still listen to
// signals and examine the request
if ( url.toString().contains( QLatin1String( "fake_qgis_http_endpoint" ) ) )
{
req.setUrl( QStringLiteral( "file://%1" ).arg( url.path() ) );
}

QNetworkReply *reply = nullptr;

if ( mType != QgsAction::SubmitUrlMultipart )
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -1984,6 +1984,7 @@ class TestQgsExpression: public QObject
// Form encoding tests
QTest::newRow( "url_encode" ) << QStringLiteral( "url_encode(map())" ).arg( testDataDir ) << false << QVariant( "" );
QTest::newRow( "url_encode" ) << QStringLiteral( "url_encode(map('a b', 'a b', 'c &% d', 'c &% d'))" ).arg( testDataDir ) << false << QVariant( "a%20b=a%20b&c%20%26%25%20d=c%20%26%25%20d" );
QTest::newRow( "url_encode" ) << QStringLiteral( "url_encode(map('a&+b', 'a and plus b', 'a=b', 'a equals b'))" ).arg( testDataDir ) << false << QVariant( "a%26+b=a%20and%20plus%20b&a%3Db=a%20equals%20b" );
}

void run_evaluation_test( QgsExpression &exp, bool evalError, QVariant &expected )
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -13,6 +13,7 @@ ADD_PYTHON_TEST(PyCoreAdditions test_core_additions.py)
ADD_PYTHON_TEST(PyPythonRepr test_python_repr.py)
ADD_PYTHON_TEST(PyPythonUtils test_python_utils.py)
ADD_PYTHON_TEST(PyQgsActionManager test_qgsactionmanager.py)
ADD_PYTHON_TEST(PyQgsAction test_qgsaction.py)
ADD_PYTHON_TEST(PyQgsAFSProvider test_provider_afs.py)
ADD_PYTHON_TEST(PyQgsAggregateMappingWidget test_qgsaggregatemappingwidget.py)
ADD_PYTHON_TEST(PyQgsAlignmentComboBox test_qgsalignmentcombobox.py)
Expand Down
102 changes: 102 additions & 0 deletions tests/src/python/test_qgsaction.py
@@ -0,0 +1,102 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsAction.
From build dir, run: ctest -R PyQgsAction -V
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Alessandro Pasotti'
__date__ = '24/11/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'

import qgis # NOQA switch sip api

from qgis.core import (
QgsExpressionContext,
QgsAction,
QgsNetworkAccessManager,
QgsNetworkRequestParameters,
QgsApplication,
)

from qgis.PyQt.QtCore import QTemporaryDir

from qgis.testing import start_app, unittest

import os
import re
import time
import platform
from functools import partial

start_app()


class TestQgsAction(unittest.TestCase):

def setUp(self):
self.body = None

def _req_logger(self, params):
self.body = bytes(params.content())

def test_post_urlencoded_action(self):
"""Test form www urlencoded"""

def _req_logger(self, params):
self.body = bytes(params.content())

QgsNetworkAccessManager.instance().requestAboutToBeCreated[QgsNetworkRequestParameters].connect(partial(_req_logger, self))

temp_dir = QTemporaryDir()
temp_path = temp_dir.path()
temp_file = os.path.join(temp_path, 'urlencoded.txt')

action = QgsAction(QgsAction.SubmitUrlEncoded, 'url_encoded', "http://fake_qgis_http_endpoint" + temp_file + r"?[% url_encode(map('a&+b', 'a and plus b', 'a=b', 'a equals b')) %]")
ctx = QgsExpressionContext()
action.run(ctx)

while not self.body:
QgsApplication.instance().processEvents()

self.assertEqual(self.body, br"a%26%2Bb=a%20and%20plus%20b&a%3Db=a%20equals%20b")

def test_post_multipart_action(self):
"""Test multipart"""

self.body = None

def _req_logger(self, params):
self.body = bytes(params.content())

QgsNetworkAccessManager.instance().requestAboutToBeCreated[QgsNetworkRequestParameters].connect(partial(_req_logger, self))

temp_dir = QTemporaryDir()
temp_path = temp_dir.path()
temp_file = os.path.join(temp_path, 'multipart.txt')

action = QgsAction(QgsAction.SubmitUrlMultipart, 'url_encoded', "http://fake_qgis_http_endpoint" + temp_file + r"?[% url_encode(map('a&+b', 'a and plus b', 'a=b', 'a equals b')) %]")
ctx = QgsExpressionContext()
action.run(ctx)

while not self.body:
QgsApplication.instance().processEvents()

self.assertEqual(re.sub(r'\.oOo\.[^\r]*', '.oOo.UUID', self.body.decode('utf8')), '\r\n'.join([
'--boundary_.oOo.UUID',
'Content-Disposition: form-data; name="a&+b"',
'',
'a and plus b',
'--boundary_.oOo.UUID',
'Content-Disposition: form-data; name="a=b"',
'',
'a equals b',
'--boundary_.oOo.UUID',
'']))


if __name__ == '__main__':
unittest.main()

0 comments on commit 3c71405

Please sign in to comment.