def test_web_client_functionalities(self):
        from tests.functional import create_dummy_request

        from c2cgeoportal_commons.models import DBSession
        from c2cgeoportal_commons.models.static import User

        request = create_dummy_request()
        request.static_url = lambda url: "http://example.com/dummy/static/url"
        request1 = create_dummy_request()
        request1.static_url = lambda url: "http://example.com/dummy/static/url"
        request1.user = DBSession.query(User).filter(User.username == "__test_user1").one()
        request2 = create_dummy_request()
        request2.static_url = lambda url: "http://example.com/dummy/static/url"
        request2.user = DBSession.query(User).filter(User.username == "__test_user2").one()

        fill_tech_user_functionality(
            "registered", (("__test_s", "registered"), ("__test_a", "r1"), ("__test_a", "r2"))
        )
        fill_tech_user_functionality(
            "anonymous", (("__test_s", "anonymous"), ("__test_a", "a1"), ("__test_a", "a2"))
        )
        settings = {
            "functionalities": {"available_in_templates": ["__test_s", "__test_a"]},
            "admin_interface": {"available_functionalities": [{"name": "__test_a"}, {"name": "__test_s"}]},
        }
        request.registry.settings.update(settings)
        request1.registry.settings.update(settings)
        request2.registry.settings.update(settings)
Esempio n. 2
0
    def test_functionalities(self):
        from tests.functional import create_dummy_request

        from c2cgeoportal_commons.models import DBSession
        from c2cgeoportal_commons.models.static import User
        from c2cgeoportal_geoportal.lib.functionality import get_functionality

        request = create_dummy_request()
        request.user = None
        request1 = create_dummy_request()
        request1.user = DBSession.query(User).filter(
            User.username == "__test_user1").one()
        request2 = create_dummy_request()
        request2.user = DBSession.query(User).filter(
            User.username == "__test_user2").one()
        request3 = create_dummy_request()
        request3.user = DBSession.query(User).filter(
            User.username == "__test_user3").one()

        settings = {
            "admin_interface": {
                "available_functionalities": [
                    {
                        "name": "__test_a"
                    },
                    {
                        "name": "__test_b",
                        "single": True
                    },
                    {
                        "name": "__test_s",
                        "single": True
                    },
                ]
            }
        }
        request.registry.settings.update(settings)
        request1.registry.settings.update(settings)
        request2.registry.settings.update(settings)
        request3.registry.settings.update(settings)
        self.assertEqual(get_functionality("__test_s", request, False), [])
        self.assertEqual(get_functionality("__test_a", request, False), [])
        self.assertEqual(get_functionality("__test_s", request1, False), [])
        self.assertEqual(get_functionality("__test_a", request1, False), [])
        self.assertEqual(get_functionality("__test_s", request2, False),
                         ["db"])
        self.assertEqual(set(get_functionality("__test_a", request2, False)),
                         {"db1", "db2"})
        self.assertEqual(get_functionality("__test_s", request3, False),
                         ["db"])
        self.assertEqual(set(get_functionality("__test_a", request3, False)),
                         {"db1", "db2"})
        self.assertEqual(get_functionality("__test_b", request3, False), [])

        fill_tech_user_functionality("registered",
                                     (("__test_s", "registered"),
                                      ("__test_a", "r1"), ("__test_a", "r2")))
        settings = {
            "admin_interface": {
                "available_functionalities": [{
                    "name": "__test_a"
                }, {
                    "name": "__test_s"
                }]
            }
        }
        request.registry.settings.update(settings)
        request1.registry.settings.update(settings)
        request2.registry.settings.update(settings)
        request1.user = DBSession.query(User).filter(
            User.username == "__test_user1").one()
        request2.user = DBSession.query(User).filter(
            User.username == "__test_user2").one()
        request3.user = DBSession.query(User).filter(
            User.username == "__test_user3").one()
        self.assertEqual(get_functionality("__test_s", request, False), [])
        self.assertEqual(get_functionality("__test_a", request, False), [])
        self.assertEqual(get_functionality("__test_s", request1, False),
                         ["registered"])
        self.assertEqual(set(get_functionality("__test_a", request1, False)),
                         {"r1", "r2"})
        self.assertEqual(get_functionality("__test_s", request2, False),
                         ["db"])
        self.assertEqual(set(get_functionality("__test_a", request2, False)),
                         {"db1", "db2"})

        fill_tech_user_functionality("registered", [])
        fill_tech_user_functionality("anonymous",
                                     (("__test_s", "anonymous"),
                                      ("__test_a", "a1"), ("__test_a", "a2")))
        settings = {
            "admin_interface": {
                "available_functionalities": [{
                    "name": "__test_a"
                }, {
                    "name": "__test_s"
                }]
            }
        }
        request.registry.settings.update(settings)
        request1.registry.settings.update(settings)
        request2.registry.settings.update(settings)
        request1.user = DBSession.query(User).filter(
            User.username == "__test_user1").one()
        request2.user = DBSession.query(User).filter(
            User.username == "__test_user2").one()
        request3.user = DBSession.query(User).filter(
            User.username == "__test_user3").one()
        self.assertEqual(get_functionality("__test_s", request, False),
                         ["anonymous"])
        self.assertEqual(set(get_functionality("__test_a", request, False)),
                         {"a1", "a2"})
        self.assertEqual(get_functionality("__test_s", request1, False),
                         ["anonymous"])
        self.assertEqual(set(get_functionality("__test_a", request1, False)),
                         {"a1", "a2"})
        self.assertEqual(get_functionality("__test_s", request2, False),
                         ["db"])
        self.assertEqual(set(get_functionality("__test_a", request2, False)),
                         {"db1", "db2"})

        fill_tech_user_functionality("registered",
                                     (("__test_s", "registered"),
                                      ("__test_a", "r1"), ("__test_a", "r2")))
        fill_tech_user_functionality("anonymous",
                                     (("__test_s", "anonymous"),
                                      ("__test_a", "a1"), ("__test_a", "a2")))
        settings = {
            "admin_interface": {
                "available_functionalities": [{
                    "name": "__test_a"
                }, {
                    "name": "__test_s"
                }]
            }
        }
        request.registry.settings.update(settings)
        request1.registry.settings.update(settings)
        request2.registry.settings.update(settings)
        request1.user = DBSession.query(User).filter(
            User.username == "__test_user1").one()
        request2.user = DBSession.query(User).filter(
            User.username == "__test_user2").one()
        request3.user = DBSession.query(User).filter(
            User.username == "__test_user3").one()
        self.assertEqual(get_functionality("__test_s", request, False),
                         ["anonymous"])
        self.assertEqual(set(get_functionality("__test_a", request, False)),
                         {"a1", "a2"})
        self.assertEqual(get_functionality("__test_s", request1, False),
                         ["registered"])
        self.assertEqual(set(get_functionality("__test_a", request1, False)),
                         {"r1", "r2"})
        self.assertEqual(get_functionality("__test_s", request2, False),
                         ["db"])
        self.assertEqual(set(get_functionality("__test_a", request2, False)),
                         {"db1", "db2"})
    def test_substitution(self):
        from c2cgeoportal_geoportal.views.mapserverproxy import MapservProxy

        request = self._create_dummy_request()
        request.method = "POST"
        request.headers["Content-Type"] = "application/xml; charset=UTF-8"
        request.body = SUBSTITUTION_GETFEATURE_REQUEST

        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "foo" in response.body.decode("utf-8")
        assert "bar" not in response.body.decode("utf-8")
        assert "éàè" not in response.body.decode("utf-8")
        assert "123" not in response.body.decode("utf-8")

        request.params.update(dict(s_name="bar"))
        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "foo" in response.body.decode("utf-8")
        assert "bar" not in response.body.decode("utf-8")
        assert "éàè" not in response.body.decode("utf-8")
        assert "123" not in response.body.decode("utf-8")

        request = self._create_dummy_request()
        request.method = "POST"
        request.body = SUBSTITUTION_GETFEATURE_REQUEST
        request.params.update(dict(S_NAME="bar"))
        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "foo" in response.body.decode("utf-8")
        assert "bar" not in response.body.decode("utf-8")
        assert "éàè" not in response.body.decode("utf-8")
        assert "123" not in response.body.decode("utf-8")

        request = self._create_dummy_request()
        request.method = "POST"
        request.body = SUBSTITUTION_GETFEATURE_REQUEST
        fill_tech_user_functionality(
            "anonymous", (("mapserver_substitution", "name=bar"), ))

        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "foo" not in response.body.decode("utf-8")
        assert "bar" in response.body.decode("utf-8")
        assert "éàè" not in response.body.decode("utf-8")
        assert "123" not in response.body.decode("utf-8")

        request.body = COLUMN_RESTRICTION_GETFEATURE_REQUEST
        fill_tech_user_functionality(
            "anonymous", [("mapserver_substitution", e)
                          for e in ["cols=name", "cols=city", "cols=country"]])
        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "Lausanne" in response.body.decode("utf-8")
        assert "Swiss" in response.body.decode("utf-8")

        fill_tech_user_functionality("anonymous",
                                     [("mapserver_substitution", e)
                                      for e in ["cols=name", "cols=city"]])
        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "Lausanne" in response.body.decode("utf-8")
        assert "Swiss" not in response.body.decode("utf-8")

        fill_tech_user_functionality("anonymous",
                                     [("mapserver_substitution", e)
                                      for e in ["cols=name", "cols=country"]])
        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "Lausanne" not in response.body.decode("utf-8")
        assert "Swiss" in response.body.decode("utf-8")

        fill_tech_user_functionality("anonymous",
                                     [("mapserver_substitution", e)
                                      for e in ["cols=name"]])
        response = MapservProxy(request).proxy()
        self.assertTrue(response.status_int, 200)
        assert "Lausanne" not in response.body.decode("utf-8")
        assert "Swiss" not in response.body.decode("utf-8")

        request = self._create_dummy_request()
        request.registry.settings["admin_interface"] = {
            "available_functionalities": [{
                "name": "mapserver_substitution"
            }]
        }
        request.method = "POST"
        request.body = SUBSTITUTION_GETFEATURE_REQUEST
        fill_tech_user_functionality("anonymous",
                                     (("mapserver_substitution", "foo_bar"), ))

        request.params.update(
            dict(s_test1="to be removed", S_TEST2="to be removed"))
        # just pass in the log message
        response = MapservProxy(request).proxy()
Esempio n. 4
0
    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)