[tor-commits] [oonib/master] Move validation logic into policy object

art at torproject.org art at torproject.org
Wed Sep 11 09:13:52 UTC 2013


commit 912a4d3f1bb479865c3f3890901c2855cd61b266
Author: Arturo Filastò <art at fuffa.org>
Date:   Mon Aug 19 15:19:42 2013 +0200

    Move validation logic into policy object
---
 oonib/policy/handlers.py |   20 ++++++++++++++++++++
 oonib/report/handlers.py |    6 ++----
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/oonib/policy/handlers.py b/oonib/policy/handlers.py
index 93f9f82..9b92403 100644
--- a/oonib/policy/handlers.py
+++ b/oonib/policy/handlers.py
@@ -1,3 +1,4 @@
+from oonib import errors as e
 from oonib.handlers import OONIBHandler
 
 from oonib import config
@@ -15,6 +16,25 @@ class Policy(object):
             self.nettest = list(p['nettest'])
             self.input = list(p['input'])
 
+    def validateInputHash(self, input_hash):
+        valid = False
+        for i in self.input:
+            if input_hash == i['id']:
+                valid = True
+                break
+        if not valid:
+            raise e.InvalidInputHash
+
+    def validateNettest(self, nettest_name):
+        # XXX add support for version checking too.
+        valid = False
+        for nt in self.nettest:
+            if nettest_name == nt['name']:
+                valid = True
+                break
+        if not valid:
+            raise e.InvalidNettestName
+
 class PolicyHandler(OONIBHandler):
     def initialize(self):
         self.policy = Policy()
diff --git a/oonib/report/handlers.py b/oonib/report/handlers.py
index 6dd5ec4..75ec55d 100644
--- a/oonib/report/handlers.py
+++ b/oonib/report/handlers.py
@@ -125,10 +125,8 @@ class NewReportHandlerFile(OONIBHandler):
 
     def checkPolicy(self):
         policy = Policy()
-        if not self.inputHash in policy.input.values():
-            raise e.InvalidInputHash
-        if self.testName not in policy.nettest.keys():
-            raise e.InvalidNettestName
+        policy.validateInputHash(self.inputHash)
+        policy.validateNettest(self.testName)
         # XXX add support for version checking too.
 
     def post(self):





More information about the tor-commits mailing list