Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move invalid size test to its own class so it can reread os.env
Signed-off-by: Marco Bernasocchi <marco@opengis.ch>

add init_server method

Signed-off-by: Marco Bernasocchi <marco@opengis.ch>

Use own qgs application so we can change the env settings

Signed-off-by: Marco Bernasocchi <marco@opengis.ch>
  • Loading branch information
mbernasocchi committed Apr 5, 2019
1 parent 64e92c1 commit 29a15ad
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 38 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -286,6 +286,7 @@ IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsServerPlugins test_qgsserver_plugins.py)
ADD_PYTHON_TEST(PyQgsServerWMS test_qgsserver_wms.py)
ADD_PYTHON_TEST(PyQgsServerWMSGetMap test_qgsserver_wms_getmap.py)
ADD_PYTHON_TEST(PyQgsServerWMSGetMapSize test_qgsserver_wms_getmap_size.py)
ADD_PYTHON_TEST(PyQgsServerWMSGetFeatureInfo test_qgsserver_wms_getfeatureinfo.py)
ADD_PYTHON_TEST(PyQgsServerWMSGetLegendGraphic test_qgsserver_wms_getlegendgraphic.py)
ADD_PYTHON_TEST(PyQgsServerWMSGetPrint test_qgsserver_wms_getprint.py)
Expand Down
38 changes: 0 additions & 38 deletions tests/src/python/test_qgsserver_wms_getmap.py
Expand Up @@ -615,44 +615,6 @@ def test_wms_getmap_background(self):
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_Background_Hex")

def test_wms_getmap_invalid_size(self):
project = os.path.join(self.testdata_path, "test_project_with_size.qgs")
expected = self.strip_version_xmlns(b'<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc">\n <ServiceException code="InvalidParameterValue">The requested map size is too large</ServiceException>\n</ServiceExceptionReport>\n')

os.environ['QGIS_SERVER_WMS_MAX_WIDTH'] = '6000'
os.environ['QGIS_SERVER_WMS_MAX_HEIGHT'] = '6000'
# test the 6000 limit from server is overridden by the more conservative 5000 in the project
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(project),
"SERVICE": "WMS",
"VERSION": "1.3.0",
"REQUEST": "GetMap",
"LAYERS": "",
"STYLES": "",
"FORMAT": "image/png",
"HEIGHT": "5001",
"WIDTH": "5000"
}.items())])
r, h = self._result(self._execute_request(qs))
self.assertEqual(self.strip_version_xmlns(r), expected)

# test the QGIS_SERVER_WMS_MAX_XXX env vars
os.environ['QGIS_SERVER_WMS_MAX_WIDTH'] = '3000'
os.environ['QGIS_SERVER_WMS_MAX_HEIGHT'] = '3000'
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(project),
"SERVICE": "WMS",
"VERSION": "1.3.0",
"REQUEST": "GetMap",
"LAYERS": "",
"STYLES": "",
"FORMAT": "image/png",
"HEIGHT": "3000",
"WIDTH": "3001"
}.items())])
r, h = self._result(self._execute_request(qs))
self.assertEqual(self.strip_version_xmlns(r), expected)

def test_wms_getmap_order(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
Expand Down
130 changes: 130 additions & 0 deletions tests/src/python/test_qgsserver_wms_getmap_size.py
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsServer MaxHeight and MaxWidth Override Options.
From build dir, run: ctest -R PyQgsServerGetMapSize -V
.. note:: This test needs env vars to be set before the server is
configured for the first time, for this
reason it cannot run as a test case of another server
test.
.. 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__ = 'Marco Bernasocchi'
__date__ = '01/04/2019'
__copyright__ = 'Copyright 2019, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

# Needed on Qt 5 so that the serialization of XML is consistent among all
# executions
os.environ['QT_HASH_SEED'] = '1'


import urllib.request
import urllib.parse
import urllib.error

from utilities import (
unitTestDataPath,
)
from qgis.testing import unittest

from test_qgsserver import QgsServerTestBase
from qgis.core import QgsProject, QgsFontUtils, QgsApplication
from qgis.server import QgsServer


class TestQgsServerWMSGetMapSize(QgsServerTestBase):
"""QGIS Server WMS Tests for GetFeatureInfo request"""

# Set to True to re-generate reference files for this class
regenerate_reference = False

@classmethod
def setUpClass(cls):
# override the QgsServerTestBase method
pass

@classmethod
def tearDownClass(cls):
# override the QgsServerTestBase method
pass

def setUp(self):
# override the QgsServerTestBase method
pass

def tearDown(self):
self.app.exitQgis()
del self.app

def init_server(self):
"""Create our own server instance so it can be called after setting up different os.environ settings"""
self.app = QgsApplication([], False)
self.fontFamily = QgsFontUtils.standardTestFontFamily()
QgsFontUtils.loadStandardTestFonts(['All'])

self.testdata_path = unitTestDataPath('qgis_server') + '/'

d = unitTestDataPath('qgis_server') + '/'
self.projectPath = os.path.join(d, "project.qgs")

# Clean env just to be sure
env_vars = ['QUERY_STRING', 'QGIS_PROJECT_FILE']
for ev in env_vars:
try:
del os.environ[ev]
except KeyError:
pass

self.server = QgsServer()

self.project = os.path.join(self.testdata_path, "test_project_with_size.qgs")
self.expected_too_big = self.strip_version_xmlns(b'<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc">\n <ServiceException code="InvalidParameterValue">The requested map size is too large</ServiceException>\n</ServiceExceptionReport>\n')

def test_wms_getmap_invalid_size_project(self):

os.environ['QGIS_SERVER_WMS_MAX_WIDTH'] = '6000'
os.environ['QGIS_SERVER_WMS_MAX_HEIGHT'] = '6000'
self.init_server()

# test the 6000 limit from server is overridden by the more conservative 5000 in the project
r = self._make_request(5001, 5000)
self.assertEqual(self.strip_version_xmlns(r), self.expected_too_big)

def test_wms_getmap_invalid_size_server(self):

# test the QGIS_SERVER_WMS_MAX_XXX env vars
os.environ['QGIS_SERVER_WMS_MAX_WIDTH'] = '3000'
os.environ['QGIS_SERVER_WMS_MAX_HEIGHT'] = '3000'
self.init_server()

# test the 3000 limit from server is overriding the less conservative 5000 in the project
r = self._make_request(3001, 3000)
self.assertEqual(self.strip_version_xmlns(r), self.expected_too_big)

def _make_request(self, height, width):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.project),
"SERVICE": "WMS",
"VERSION": "1.3.0",
"REQUEST": "GetMap",
"LAYERS": "",
"STYLES": "",
"FORMAT": "image/png",
"HEIGHT": str(height),
"WIDTH": str(width)
}.items())])
r, h = self._result(self._execute_request(qs))
return self.strip_version_xmlns(r)


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

0 comments on commit 29a15ad

Please sign in to comment.