Exemplo n.º 1
0
    def test_performance(self):
        def mkdataset(depth=3):
            depth = depth - 1
            if depth <= 0:
                return
            return dict([(str(k), mkdataset(depth=depth))
                         for k in range(1, 1000)])

        data = {"a": mkdataset(3), "b": mkdataset(3)}

        ruleset = {"require": {"a.100.100": 0x01}}

        ruleset["require"].update(**dict([("b.100.%d" % i, 0x01)
                                          for i in range(1, 100)]))

        import time

        perms_struct = util.perms_structure(self.perms)

        t = time.time()
        result = util.permissions_apply(data, perms_struct, debug=False)
        diff = time.time() - t
        print("\n\nPerformance test took: %.5f seconds" % (diff))

        t = time.time()
        result = util.permissions_apply(data,
                                        perms_struct,
                                        debug=False,
                                        ruleset=ruleset)
        diff = time.time() - t
        print("\n\nPerformance (w/ Explicit ruleset) test took: %.5f seconds" %
              (diff))
Exemplo n.º 2
0
    def test_perms_apply(self):
        data = {
            "a": {
                "b": {
                    "c": "This should be here",
                    "d": "This should be gone"
                },
                "100": "This should be here",
            },
            "b": "This should be here",
            "c": {
                "a": {
                    "b": "This should be gone",
                    "a": "This should be here"
                }
            },
        }
        expected = {
            "a": {
                "100": data["a"]["100"],
                "b": {
                    "c": data["a"]["b"]["c"]
                }
            },
            "b": data["b"],
            "c": {
                "a": {
                    "a": data["c"]["a"]["a"]
                }
            },
        }

        perms_struct = util.perms_structure(self.perms)
        result = util.permissions_apply(data, perms_struct)
        self.assertEqual(expected, result)
Exemplo n.º 3
0
    def test_apply_with_permset(self):
        data = {
            "x": {
                "a": "This should be here",
                "b": "This should be gone",
                "c": "This should be here",
                "d": "This should be here",
                "y": {
                    "y": {
                        "a": "This should be here",
                        "b": "This should be gone"
                    },
                    "z": {
                        "a": "This should be here",
                        "b": "This should be gone",
                        "c": "This should be here",
                        "d": "This should be here",
                    },
                },
            }
        }

        expected = {
            "x": {
                "a": data["x"]["a"],
                "c": data["x"]["c"],
                "d": data["x"]["d"],
                "y": {
                    "y": {
                        "a": data["x"]["y"]["y"]["a"]
                    },
                    "z": {
                        "a": data["x"]["y"]["z"]["a"],
                        "c": data["x"]["y"]["z"]["c"],
                        "d": data["x"]["y"]["z"]["d"],
                    },
                },
            }
        }

        ruleset = {
            "require": {
                "x.b": 0x01,
                "x.c": 0x01,
                "x.y.*.b": 0x01,
                "x.y.z.c": 0x01,
                "x.y.z.d": 0x01,
            }
        }

        perms_struct = util.perms_structure(self.perms)
        result = util.permissions_apply(data,
                                        perms_struct,
                                        debug=False,
                                        ruleset=ruleset)
        self.assertEqual(expected, result)
Exemplo n.º 4
0
    def apply_permissions(self, ns, data, ruleset={}):
        """
        Wrapper function to apply permissions to a data row and
        return the sanitized result
        """
        if type(ns) != list:
            ns = ns.split(".")

        # prepare ruleset
        if ruleset:
            _ruleset = {}
            namespace_str = ".".join(ns)
            for section, rules in ruleset.items():
                _ruleset[section] = {}
                for rule, perms in rules.items():
                    _ruleset[section]["%s.%s" % (namespace_str, rule)] = perms
                    ruleset = _ruleset

        return nsp.dict_get_path(
            nsp.permissions_apply(nsp.dict_from_namespace(ns, data),
                                  self.request.user,
                                  ruleset=ruleset), ns)
Exemplo n.º 5
0
    def apply_permissions(self, ns, data, ruleset={}):
        """
        Wrapper function to apply permissions to a data row and
        return the sanitized result
        """
        if type(ns) != list:
            ns = ns.split(".")

        # prepare ruleset
        if ruleset:
            _ruleset = {}
            namespace_str = ".".join(ns)
            for section, rules in ruleset.items():
                _ruleset[section] = {}
                for rule, perms in rules.items():
                    _ruleset[section]["%s.%s" % (namespace_str, rule)] = perms
                    ruleset = _ruleset

        return nsp.dict_get_path(
            nsp.permissions_apply(
                nsp.dict_from_namespace(ns, data), self.request.user,
                ruleset=ruleset), ns)
Exemplo n.º 6
0
    def test_list_handlers(self):

        data = {
            "f": [{
                "a": 1,
                "b": "should be here"
            }, {
                "a": 2,
                "b": "should be gone"
            }],
            "g": {
                "a": [{
                    "a": 1,
                    "b": "should be here"
                }, {
                    "a": 2,
                    "b": "should be gone"
                }],
                "b":
                "should be here",
                "c": [
                    {
                        "a": 1,
                        "b": "should be gone"
                    },
                    {
                        "a": 2,
                        "b": "should be gone"
                    },
                    {
                        "a": 3,
                        "b": "should be here"
                    },
                ],
            },
            "h": {
                "a": [{
                    "a": 1,
                    "b": "should be here"
                }, {
                    "a": 2,
                    "b": "should be gone"
                }],
                "b": [{
                    "a": 1,
                    "b": "should be here"
                }, {
                    "a": 2,
                    "b": "should be gone"
                }],
            },
            "l": {
                "o": {
                    "1": {
                        "n": {
                            "1": {
                                "p": ["should be here"],
                                "a": "should be here",
                                "b": "should be here",
                            }
                        }
                    }
                }
            },
        }

        expected = {
            "f": [data["f"][0]],
            "g": {
                "a": [data["g"]["a"][0]],
                "c": [data["g"]["c"][2]],
                "b": data["g"]["b"],
            },
            "h": {
                "a": [data["h"]["a"][0]],
                "b": [data["h"]["b"][0]]
            },
            "l": {
                "o": {
                    "1": {
                        "n": {
                            "1": {
                                "p": ["should be here"],
                                "a": "should be here",
                                "b": "should be here",
                            }
                        }
                    }
                }
            },
        }

        def namespace_builder(**kwargs):
            return str(kwargs.get("a"))

        def namespace_builder_absolute(**kwargs):
            return "g.a.%s" % kwargs.get("a")

        def namespace_builder_complex(**kwargs):
            return "p"

        ruleset = {
            "require": {
                "g.c.1": 0x01,
                "g.c.2": 0x01,
                "g.c.3": 0x01,
                "h.*.2": 0x01,
                "l.o.1.n.p.p": 0x01,
            },
            "list-handlers": {
                "f": {
                    "namespace": namespace_builder
                },
                "g.a": {
                    "namespace": namespace_builder_absolute,
                    "absolute": True
                },
                "g.c": {
                    "namespace": namespace_builder
                },
                "h.*": {
                    "namespace": namespace_builder
                },
                "l.o.1.n.1.p": {
                    "namespace": namespace_builder_complex
                },
            },
        }

        perms_struct = util.perms_structure(self.perms)
        result = util.permissions_apply(data,
                                        perms_struct,
                                        debug=False,
                                        ruleset=ruleset)

        self.assertEqual(expected, result)