def test_route_with_all(self): from c2cgeoportal_geoportal.views.dynamic import DynamicView with testConfig( settings=self._get_settings( { "test": { "routes": { "XTest": { "name": "route_with_all", "kw": {"key1": "v1", "key2": "v2"}, "elements": ["s1", "s2"], "params": {"test": "value"}, } } } } ) ) as config: config.add_static_view(name="static", path="/etc/geomapfish/static") config.add_route("base", "/", static=True) config.add_route("test", "/test") config.add_route("route_with_all", "/test/{key1}/{key2}") request = DummyRequest({"interface": "test"}) request.get_organization_interface = lambda interface: interface dynamic = DynamicView(request).dynamic() assert "XTest" in dynamic["constants"], dynamic assert dynamic["constants"]["XTest"] == "http://example.com/test/v1/v2/s1/s2?test=value"
def test_shortener_create_1(self): from tests import DummyRequest from pyramid.httpexceptions import HTTPFound from c2cgeoportal_geoportal.views.shortener import Shortener def route_url(name, *elements, **kw): return "https://example.com/short/" + kw["ref"] request = DummyRequest() request.user = None request.host = "example.com:443" request.server_name = "example.com" request.route_url = route_url request.registry.settings["shortener"] = { "base_url": "https://example.com/s/" } shortener = Shortener(request) request.params = {"url": "https://example.com/hi"} result = shortener.create() index = result["short_url"].rfind("/") self.assertEqual(result["short_url"][:index], "https://example.com/s") request.params = {} # fmt: off request.matchdict = {"ref": result["short_url"][index + 1:]} # fmt: on result = shortener.get() self.assertEqual(type(result), HTTPFound) self.assertEqual(result.location, "https://example.com/hi")
def test_theme(self): from c2cgeoportal_geoportal.views.theme import Theme request = DummyRequest() request.static_url = lambda url: "http://example.com/dummy/static/url" request.route_url = lambda url, _query={}: mapserv_url request.user = None theme_view = Theme(request) _, errors = theme_view._themes("desktop2", True, 2) self.assertEqual( len([e for e in errors if e == "Too many recursions with group '__test_layer_group'"]), 1 )
def _request(query={}): query_ = {"interface": "test"} query_.update(query) request = DummyRequest(query_) request.route_url = ( lambda url, _query=None: "/dummy/route/url/{}".format(url) if _query is None else "/dummy/route/url/{}?{}".format( url, "&".join(["=".join(e) for e in _query.items()]))) request.static_url = ( lambda url, _query=None: "/dummy/static/url/{}".format(url) if _query is None else "/dummy/static/url/{}?{}".format( url, "&".join(["=".join(e) for e in _query.items()]))) return request
def _request(query={}): query_ = {"interface": "test"} query_.update(query) request = DummyRequest(query_) request.route_url = ( lambda url, _query=None: "/dummy/route/url/{}".format(url) if _query is None else "/dummy/route/url/{}?{}".format(url, pyramid.url.urlencode(_query)) ) request.static_url = ( lambda url, _query=None: "/dummy/static/url/{}".format(url) if _query is None else "/dummy/static/url/{}?{}".format(url, pyramid.url.urlencode(_query)) ) return request
def _request(query={}): query_ = {"interface": "test"} query_.update(query) request = DummyRequest(query_) request.route_url = ( lambda url, _query=None: f"/dummy/route/url/{url}" if _query is None else f"/dummy/route/url/{url}?{pyramid.url.urlencode(_query)}" ) request.static_url = ( lambda url, _query=None: f"/dummy/static/url/{url}" if _query is None else f"/dummy/static/url/{url}?{pyramid.url.urlencode(_query)}" ) request.get_organization_interface = lambda interface: interface return request
def test_route_with_keywords(self): from c2cgeoportal_geoportal.views.dynamic import DynamicView with testConfig(settings=self._get_settings({ "test": { "routes": { "XTest": { "name": "route_with_keywords", "kw": { "key1": "v1", "key2": "v2" } } } } })) as config: config.add_static_view(name="static", path="/etc/geomapfish/static") config.add_route("test", "/test") config.add_route("route_with_keywords", "/test/{key1}/{key2}") request = DummyRequest({"interface": "test"}) dynamic = DynamicView(request).dynamic() assert "XTest" in dynamic["constants"], dynamic assert dynamic["constants"]["XTest"] == "http://example.com/test/v1/v2"
def test_shortener_dev(self): from tests import DummyRequest from c2cgeoportal_geoportal.views.shortener import Shortener def route_url(name, *elements, **kw): return "https://localhost:8484/s/" + kw["ref"] request = DummyRequest() request.user = None request.host = "localhost:8484" request.server_name = "0.0.0.0" request.route_url = route_url request.registry.settings["shortener"] = { "base_url": "https://localhost:8484/s/", "allowed_hosts": [ "localhost:8484", ], } shortener = Shortener(request) request.params = {"url": "https://localhost:8484/theme/Demo"} result = shortener.create() index = result["short_url"].rfind("/") self.assertEqual(result["short_url"][:index], "https://localhost:8484/s")
def _do(method, headers, credentials=False, settings=SETTINGS): request = DummyRequest({}, method=method, headers=headers) if settings is not None: request.registry.settings = {"headers": {"foo": settings}} else: request.registry.settings = {"headers": {}} set_common_headers(request, "foo", Cache.NO, credentials=credentials) return dict(request.response.headers)
def test_build_url_forward_headers(self): request = DummyRequest() request.registry.settings = { "checker": { "base_internal_url": "http://localhost", "forward_headers": ["Cookie"] } } request.headers["Cookie"] = "test" self.assertEqual( build_url("Test", "/toto?titi#tutu", request), { "url": "http://localhost/toto?titi#tutu", "headers": { "Cache-Control": "no-cache", "Cookie": "test" }, }, )
def test_raster(self): from decimal import Decimal from pyramid.httpexceptions import HTTPNotFound from tests import DummyRequest from c2cgeoportal_geoportal.views.raster import Raster request = DummyRequest() request.registry.settings = { "raster": { "dem1": { "file": "/opt/c2cgeoportal/geoportal/tests/data/dem.shp", "round": 0.1 }, "dem2": { "file": "/opt/c2cgeoportal/geoportal/tests/data/dem.shp", "round": 1 }, "dem3": { "file": "/opt/c2cgeoportal/geoportal/tests/data/dem.shp" }, } } raster = Raster(request) request.params["lon"] = "565000" request.params["lat"] = "218000" result = raster.raster() self.assertEqual(result["dem1"], None) self.assertEqual(result["dem2"], None) self.assertEqual(result["dem3"], None) request.params["lon"] = "548000" request.params["lat"] = "216000" result = raster.raster() self.assertAlmostEqual(result["dem1"], Decimal("1171.6")) self.assertAlmostEqual(result["dem2"], Decimal("1172")) self.assertAlmostEqual(result["dem3"], Decimal("1171.62")) request.params["layers"] = "dem2" result = raster.raster() self.assertFalse("dem1" in result) self.assertFalse("dem3" in result) self.assertAlmostEqual(result["dem2"], Decimal("1172")) # test wrong layer name request.params["layers"] = "wrong" self.assertRaises(HTTPNotFound, raster.raster)
def test_raster_vrt(self): from decimal import Decimal from tests import DummyRequest from c2cgeoportal_geoportal.views.raster import Raster request = DummyRequest() request.registry.settings = { "raster": { "dem6": { "file": "/opt/c2cgeoportal/geoportal/tests/data/dem4.vrt", "type": "gdal", "round": 0.01, } } } raster = Raster(request) # Upper left request.params["lon"] = "547990.4" request.params["lat"] = "216009.5" result = raster.raster() self.assertEqual(result["dem6"], Decimal("1164.2"))
def test_build_url_docker(self): request = DummyRequest() request.registry.settings = { "checker": { "base_internal_url": "http://localhost:8080" } } self.assertEqual( build_url("Test", "/toto?titi#tutu", request), { "url": "http://localhost:8080/toto?titi#tutu", "headers": { "Cache-Control": "no-cache" } }, )
def test_build_url_https(self): request = DummyRequest() request.registry.settings = { "checker": { "base_internal_url": "https://localhost", "forward_host": True } } self.assertEqual( build_url("Test", "/toto?titi#tutu", request), { "url": "https://localhost/toto?titi#tutu", "headers": { "Cache-Control": "no-cache", "Host": "example.com:80" }, }, )
def test_shortener_noreplace_2(self): from tests import DummyRequest from c2cgeoportal_geoportal.views.shortener import Shortener def route_url(name, *elements, **kw): return "https://example.com/short/" + kw["ref"] request = DummyRequest() request.user = None request.host = "example.com:443" request.server_name = "example.com" request.route_url = route_url request.registry.settings["shortener"] = { "base_url": "https://example.com/s/" } shortener = Shortener(request) request.params = {"url": "https://example.com/s/truite"} result = shortener.create() self.assertEqual(result["short_url"], "https://example.com/s/truite")
def match(reference, value, expected): r = DummyRequest() r.referer = value self.assertEqual(is_valid_referer(r, {"authorized_referers": [reference]}), expected)
def test_lang_param(self): from c2cgeoportal_geoportal import locale_negotiator request = DummyRequest(params=dict(lang="fr")) lang = locale_negotiator(request) self.assertEqual(lang, "fr")
def test_login_0(self): from tests import DummyRequest from c2cgeoportal_geoportal.views.login import Login request = DummyRequest() request.is_valid_referer = True request.user = None login = Login(request) request.path = "/for_test" responce = login.loginform403() assert responce["came_from"] == "/for_test" request.params = {"came_from": "/for_a_second_test"} login = Login(request) responce = login.loginform() assert responce["came_from"] == "/for_a_second_test" login = Login(request) request.params = {} responce = login.loginform() assert responce["came_from"] == "/" request.registry.settings = { "functionalities": { "available_in_templates": ["func"] }, "admin_interface": { "available_functionalities": [{ "name": "func" }] }, } fill_tech_user_functionality("anonymous", (("func", "anon"), ("toto", "anon_value2"))) fill_tech_user_functionality("registered", (("func", "reg"), )) login = Login(request) self.assertEqual(login.loginuser()["functionalities"], {"func": ["anon"]}) class R: id = 123 def __init__(self, name, functionalities): self.name = name self.functionalities = functionalities class U: username = "******" is_password_changed = True email = "*****@*****.**" settings_role = None def __init__(self, role="__test_role", functionalities=None): if functionalities is None: functionalities = [] self.roles = [R(role, functionalities)] request.user = U() login = Login(request) expected = { "username": "******", "email": "*****@*****.**", "is_intranet": False, "two_factor_enable": False, "roles": [{ "name": "__test_role", "id": 123 }], "functionalities": { "func": ["reg"] }, } self.assertEqual(login.loginuser(), expected) class F: name = "func" value = "value" request.user = U("__test_role2", [F()]) login = Login(request) expected = { "username": "******", "email": "*****@*****.**", "is_intranet": False, "two_factor_enable": False, "roles": [{ "name": "__test_role2", "id": 123 }], "functionalities": { "func": ["value"] }, } self.assertEqual(login.loginuser(), expected)
def test_intranet(self): from tests import DummyRequest from c2cgeoportal_geoportal.views.login import Login request = DummyRequest() request.registry.settings = { "intranet": { "networks": ["192.168.1.0/255.255.255.0"] } } request.user = None login = Login(request) self.assertEqual( login.loginuser(), { "is_intranet": False, "functionalities": {}, "two_factor_enable": False }) request.client_addr = "192.168.1.20" login = Login(request) self.assertEqual(login.loginuser(), { "is_intranet": True, "functionalities": {}, "two_factor_enable": False }) class G: id = 123 def __init__(self, name, functionalities): self.name = name self.functionalities = functionalities class U: username = "******" is_password_changed = True email = "*****@*****.**" def __init__(self, role="__test_role", functionalities=None): if functionalities is None: functionalities = [] self.roles = [G(role, functionalities)] request.user = U() login = Login(request) request.client_addr = "192.168.2.20" self.assertEqual( login.loginuser(), { "email": "*****@*****.**", "functionalities": {}, "is_intranet": False, "roles": [{ "id": 123, "name": "__test_role" }], "two_factor_enable": False, "username": "******", }, ) login = Login(request) request.client_addr = "192.168.1.20" self.assertEqual( login.loginuser(), { "email": "*****@*****.**", "functionalities": {}, "is_intranet": True, "roles": [{ "id": 123, "name": "__test_role" }], "two_factor_enable": False, "username": "******", }, )
def test_shortener(self): from tests import DummyRequest from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest from c2cgeoportal_geoportal.views.shortener import Shortener def route_url(name, *args, **kwargs): del name # Unused del args # Unused return "https://example.com/short/" + kwargs["ref"] request = DummyRequest() request.user = None request.host = "example.com:443" request.server_name = "example.com" request.route_url = route_url request.registry.settings["shortener"] = { "base_url": "https://example.com/s/" } shortener = Shortener(request) request.params = {} request.matchdict = {"ref": "AAAAAA"} self.assertRaises(HTTPNotFound, shortener.get) request.params = {} request.matchdict = {} self.assertRaises(HTTPBadRequest, shortener.create) request.params = {"url": "https://other-site.com/hi"} self.assertRaises(HTTPBadRequest, shortener.create)
def test_raster_angle(self): from decimal import Decimal from tests import DummyRequest from c2cgeoportal_geoportal.views.raster import Raster request = DummyRequest() request.registry.settings = { "raster": { "dem5": { "file": "/opt/c2cgeoportal/geoportal/tests/data/dem4.bt", "type": "gdal", "round": 0.01, } } } raster = Raster(request) # Upper left request.params["lon"] = "547990.0" request.params["lat"] = "216009.1" result = raster.raster() self.assertEqual(result["dem5"], Decimal("1164.2")) request.params["lon"] = "547990.9" request.params["lat"] = "216010.0" result = raster.raster() self.assertEqual(result["dem5"], Decimal("1164.2")) # Lower right request.params["lon"] = "547996.0" request.params["lat"] = "216003.1" result = raster.raster() self.assertEqual(result["dem5"], Decimal("1180.77")) request.params["lon"] = "547996.9" request.params["lat"] = "216004.0" result = raster.raster() self.assertEqual(result["dem5"], Decimal("1180.77")) # Out request.params["lon"] = "547997.4" request.params["lat"] = "216003.5" result = raster.raster() self.assertEqual(result["dem5"], None)
def test_profile_json(self): from decimal import Decimal from pyramid.httpexceptions import HTTPNotFound from tests import DummyRequest from c2cgeoportal_geoportal.views.profile import Profile request = DummyRequest() request.registry.settings = { "raster": { "dem": {"file": "/opt/c2cgeoportal/geoportal/tests/data/dem.shp", "round": 4}, "dem2": {"file": "/opt/c2cgeoportal/geoportal/tests/data/dem.shp", "round": 4}, } } profile = Profile(request) request.params["nbPoints"] = "3" request.params["geom"] = ( '{"type":"LineString",' '"coordinates":[[548009.5,215990],[547990,216009.5]]}' ) result = profile.json() self.assertEqual(len(result["profile"]), 4) self.assertAlmostEqual(result["profile"][0]["y"], 215990) self.assertAlmostEqual(result["profile"][0]["values"]["dem2"], None) self.assertAlmostEqual(result["profile"][0]["values"]["dem"], None) self.assertAlmostEqual(result["profile"][0]["dist"], Decimal("0.0")) self.assertAlmostEqual(result["profile"][0]["x"], 548009.5) self.assertAlmostEqual(result["profile"][1]["y"], 215996.5) self.assertAlmostEqual(result["profile"][1]["values"]["dem2"], 1181) self.assertAlmostEqual(result["profile"][1]["values"]["dem"], 1181) self.assertAlmostEqual(result["profile"][1]["dist"], Decimal("9.2")) self.assertAlmostEqual(result["profile"][1]["x"], 548003.0) self.assertAlmostEqual(result["profile"][2]["y"], 216003.0) self.assertAlmostEqual(result["profile"][2]["values"]["dem"], 1180) self.assertAlmostEqual(result["profile"][2]["values"]["dem2"], 1180) self.assertAlmostEqual(result["profile"][2]["dist"], Decimal("18.4")) self.assertAlmostEqual(result["profile"][2]["x"], 547996.5) self.assertAlmostEqual(result["profile"][3]["y"], 216009.5) self.assertAlmostEqual(result["profile"][3]["values"]["dem"], 1164) self.assertAlmostEqual(result["profile"][3]["values"]["dem2"], 1164) self.assertAlmostEqual(result["profile"][3]["dist"], Decimal("27.6")) self.assertAlmostEqual(result["profile"][3]["x"], 547990.0) request.params["layers"] = "dem" result = profile.json() self.assertEqual(len(result["profile"]), 4) self.assertAlmostEqual(result["profile"][0]["y"], 215990) self.assertAlmostEqual(result["profile"][0]["values"]["dem"], None) self.assertAlmostEqual(result["profile"][0]["dist"], Decimal("0.0")) self.assertAlmostEqual(result["profile"][0]["x"], 548009.5) self.assertAlmostEqual(result["profile"][1]["y"], 215996.5) self.assertAlmostEqual(result["profile"][1]["values"]["dem"], 1181) self.assertAlmostEqual(result["profile"][1]["dist"], Decimal("9.2")) self.assertAlmostEqual(result["profile"][1]["x"], 548003.0) self.assertAlmostEqual(result["profile"][2]["y"], 216003.0) self.assertAlmostEqual(result["profile"][2]["values"]["dem"], 1180) self.assertAlmostEqual(result["profile"][2]["dist"], Decimal("18.4")) self.assertAlmostEqual(result["profile"][2]["x"], 547996.5) self.assertAlmostEqual(result["profile"][3]["y"], 216009.5) self.assertAlmostEqual(result["profile"][3]["values"]["dem"], 1164) self.assertAlmostEqual(result["profile"][3]["dist"], Decimal("27.6")) self.assertAlmostEqual(result["profile"][3]["x"], 547990.0) # test length = 0 request.params["geom"] = '{"type":"LineString",' '"coordinates":[[548000,216000]]}' result = profile.json() self.assertEqual(len(result["profile"]), 1) self.assertAlmostEqual(result["profile"][0]["y"], 216000) self.assertAlmostEqual(result["profile"][0]["values"]["dem"], 1172) self.assertAlmostEqual(result["profile"][0]["dist"], Decimal("0.0")) self.assertAlmostEqual(result["profile"][0]["x"], 548000) # test cur_nb_points < 1 request.params["geom"] = ( '{"type":"LineString",' '"coordinates":[[548000,216000],[548001,216001],[548009,216009]]}' ) result = profile.json() self.assertEqual(len(result["profile"]), 5) self.assertAlmostEqual(result["profile"][0]["y"], 216000) self.assertAlmostEqual(result["profile"][0]["values"]["dem"], 1172) self.assertAlmostEqual(result["profile"][0]["dist"], Decimal("0.0")) self.assertAlmostEqual(result["profile"][0]["x"], 548000) self.assertAlmostEqual(result["profile"][1]["y"], 216001.0) self.assertAlmostEqual(result["profile"][1]["values"]["dem"], 1167) self.assertAlmostEqual(result["profile"][1]["dist"], Decimal("1.4")) self.assertEqual(result["profile"][1]["x"], 548001.0) self.assertAlmostEqual(result["profile"][2]["y"], 216003.66666666666) self.assertAlmostEqual(result["profile"][2]["values"]["dem"], 1155) self.assertAlmostEqual(result["profile"][2]["dist"], Decimal("5.2")) self.assertAlmostEqual(result["profile"][2]["x"], 548003.6666666666) self.assertAlmostEqual(result["profile"][3]["y"], 216006.33333333334) self.assertAlmostEqual(result["profile"][3]["values"]["dem"], 1154) self.assertAlmostEqual(result["profile"][3]["dist"], Decimal("9")) self.assertAlmostEqual(result["profile"][3]["x"], 548006.3333333334) self.assertAlmostEqual(result["profile"][4]["y"], 216009.0) self.assertAlmostEqual(result["profile"][4]["values"]["dem"], 1158) self.assertAlmostEqual(result["profile"][4]["dist"], Decimal("12.7")) self.assertAlmostEqual(result["profile"][4]["x"], 548009.0) # test wrong layer name request.params["layers"] = "wrong" self.assertRaises(HTTPNotFound, profile.json)