[Pkg-mozext-commits] [requestpolicy] 142/257: [tst][imp] RuleRow: implement `create_rule()`

David Prévot taffit at moszumanska.debian.org
Thu Jan 28 03:20:06 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit 02e7e39fa24382fe10140d272478771a8e630392
Author: Martin Kimmerle <dev at 256k.de>
Date:   Tue Nov 17 15:41:45 2015 +0100

    [tst][imp] RuleRow: implement `create_rule()`
    
    Implement `RuleRow.create_rule()`. The same function in
    `YourPolicyRuleRow` is not needed anymore.
    
    The benefit: `create_rule()` can now be called on any rule row
    which doesn't have a "delete" button, that is, subscription
    rules on "Your Policy" as well as the rules on the "Old Rules"
    import page.
---
 .../rp_puppeteer/tests/test_rules_table.py         |  49 ++++++----
 .../rp_puppeteer/ui/settings/rules_table.py        | 102 ++++++++++++++++-----
 2 files changed, 111 insertions(+), 40 deletions(-)

diff --git a/tests/marionette/rp_puppeteer/tests/test_rules_table.py b/tests/marionette/rp_puppeteer/tests/test_rules_table.py
index 537350f..d1d34e0 100644
--- a/tests/marionette/rp_puppeteer/tests/test_rules_table.py
+++ b/tests/marionette/rp_puppeteer/tests/test_rules_table.py
@@ -171,6 +171,38 @@ class TestRuleRow(RulesTableTestCase):
         dest_string = self.table.user_rule_rows[0].dest
         self.assertEqual(dest_string, "")
 
+    def test_create_rule(self):
+        def test_pre_path_spec(spec_id, allow=True, temp=True):
+            def create_rule():
+                spec = self.data.pre_path_specs[spec_id]
+                rule_data = {"o": spec["spec"], "d": spec["spec"]}
+                return self.rules.create_rule(rule_data, allow=allow, temp=temp)
+
+            # Create and add the rule.
+            rule = create_rule()
+            rule.add()
+
+            rule_row = self.table.user_rule_rows[0]
+            returned_rule = rule_row.create_rule()
+            self.assertIsInstance(returned_rule, Rule)
+            # The returned rule should be identical to what has been added.
+            self.assertEqual(returned_rule, rule)
+
+            rule.remove()
+
+        # Test all possible pre-path specs.
+        test_pre_path_spec("s")
+        test_pre_path_spec("h")
+        test_pre_path_spec("p")
+        test_pre_path_spec("sh")
+        test_pre_path_spec("sp")
+        test_pre_path_spec("hp")
+        test_pre_path_spec("shp")
+
+        # Test rules with all origin/dest fields specified.
+        test_pre_path_spec("shp", True, False)
+        test_pre_path_spec("shp", False, True)
+
 
 class TestYourPolicyRuleRow(RulesTableTestCase):
 
@@ -184,23 +216,6 @@ class TestYourPolicyRuleRow(RulesTableTestCase):
         test(self.data.allow_rule, "User")
         test(self.data.temp_allow_rule, "Temporary")
 
-    def test_create_rule(self):
-        def test(rule):
-            rule.add()
-            rule_row = self.table.user_rule_rows[0]
-            returned_rule = rule_row.create_rule()
-            self.assertIsInstance(returned_rule, Rule,
-                                  "`create_rule()` has returned a `Rule` "
-                                  "instance.")
-            self.assertEqual(returned_rule, rule,
-                             msg=("The returned rule is identical to what "
-                                  "has been added."))
-            rule.remove()
-
-        # Test rules with all origin/dest fields specified.
-        test(self.data.allow_rule_shp_shp)
-        test(self.data.temp_deny_rule_shp_shp)
-
     def test_remove_rule(self):
         for rule in self.data.some_rules:
             rule.add()
diff --git a/tests/marionette/rp_puppeteer/ui/settings/rules_table.py b/tests/marionette/rp_puppeteer/ui/settings/rules_table.py
index 738324d..438cfed 100644
--- a/tests/marionette/rp_puppeteer/ui/settings/rules_table.py
+++ b/tests/marionette/rp_puppeteer/ui/settings/rules_table.py
@@ -4,6 +4,8 @@
 
 from rp_puppeteer.base import BaseLib, ElementBaseLib
 from rp_puppeteer.api.rules import Rule
+from rp_puppeteer.api.l10n import L10n
+import re
 
 
 class RuleRow(ElementBaseLib):
@@ -17,6 +19,18 @@ class RuleRow(ElementBaseLib):
     origin = property(lambda self: self._get_cell_text_content(1))
     dest = property(lambda self: self._get_cell_text_content(2))
 
+    def create_rule(self):
+        """Create a `Rule` instance for this rule row."""
+
+        allow_string = L10n(lambda: self.marionette).get_rp_property("allow")
+        allow = self.policy == allow_string
+        temp = (self.is_temporary()
+                if hasattr(self, "is_temporary")
+                else False)
+        rule_data = RuleRow._get_rule_data_from_strings(self.origin, self.dest)
+
+        return Rule(lambda: self.marionette, rule_data, allow, temp)
+
     ##################################
     # Private Properties and Methods #
     ##################################
@@ -28,6 +42,71 @@ class RuleRow(ElementBaseLib):
     def _get_cell_text_content(self, column_index):
         return self._cells[column_index].get_attribute("textContent")
 
+    @staticmethod
+    def _get_rule_data_from_strings(origin_string, dest_string):
+        origin = RuleRow._get_endpoint_spec_from_string(origin_string)
+        dest = RuleRow._get_endpoint_spec_from_string(dest_string)
+        rule_data = {}
+        assert origin or dest
+        if origin:
+            rule_data["o"] = origin
+        if dest:
+            rule_data["d"] = dest
+        return rule_data
+
+    @staticmethod
+    def _get_endpoint_spec_from_string(string):
+        # Case: Endpoint not specified.
+        if string == "":
+            return None
+
+        # Case: Only a scheme is specified.
+        match = re.match('^scheme "([^"]+)"$', string)
+        if match:
+            return {"s": match.group(1)}
+
+        # Case: URI without host, but with path
+        match = re.match(r"""^
+                             ([^:]+):  # scheme
+                             ([^/]+)   # path
+                             $""", string, flags=re.X)
+        if match:
+            scheme = match.group(1)
+            path = match.group(2)
+            spec = {}
+            if scheme != "*":
+                spec["s"] = scheme
+            if path != "*":
+                # Path support is not implemented yet.
+                raise NotImplementedError
+            if spec == {}:
+                raise SyntaxError
+            return spec
+
+        # Case: An URI with host, optionally with scheme and port.
+        match = re.match(r"""^
+                             (?:   ([^:]+)  :// )?  # scheme (optional)
+                                   ([^:/]+)         # host
+                             (?: : ([^/]+)      )?  # port (optional)
+                             $""", string, flags=re.X)
+        if match:
+            spec = {}
+            scheme = match.group(1)
+            host = match.group(2)
+            port = match.group(3)
+            if scheme and scheme != "*":
+                spec["s"] = scheme
+            if host != "*":
+                spec["h"] = host
+            if port and port != "*":
+                spec["port"] = port
+            if spec == {}:
+                raise SyntaxError
+            return spec
+
+        # Nothing matched.
+        raise SyntaxError
+
 
 class YourPolicyRuleRow(RuleRow):
 
@@ -38,29 +117,6 @@ class YourPolicyRuleRow(RuleRow):
     # Properties to get the strings of the corresponding table cells.
     rule_set = property(lambda self: self._get_cell_text_content(3))
 
-    def create_rule(self):
-        """Create a `Rule` instance for this rule row."""
-
-        if not self.is_user_rule():
-            # Getting non-user (e.g. subscription) rules is not implemented yet.
-            raise NotImplementedError
-
-        # The rule details are retained by RequestPolicy via `jQuery.data()`
-        # on the "<a>x</a>" HTML anchor element, the element being clicked to
-        # remove the rule.
-        [rule_action, rule_data] = self.marionette.execute_script("""
-          var anchor = $(arguments[0]);
-          return [
-            anchor.data('requestpolicyRuleAction'),
-            anchor.data('requestpolicyRuleData')
-          ];
-        """, script_args=[self._remove_anchor])
-
-        allow = True if rule_action == "allow" else False
-        temp = self.is_temporary()
-
-        return Rule(lambda: self.marionette, rule_data, allow, temp)
-
     def remove(self):
         self._remove_anchor.click()
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git



More information about the Pkg-mozext-commits mailing list