[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [gettor/master] Update check for too_many_requests



commit 8953e38e9dd5527507c3a3de5d1c9ea4b5541d43
Author: hiro <hiro@xxxxxxxxxxxxxx>
Date:   Fri Oct 18 12:11:24 2019 +0200

    Update check for too_many_requests
---
 gettor/parse/email.py       | 18 ++++++++++--------
 tests/test_email_service.py | 12 ++++++++++++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/gettor/parse/email.py b/gettor/parse/email.py
index 0768356..31afb1e 100644
--- a/gettor/parse/email.py
+++ b/gettor/parse/email.py
@@ -151,10 +151,10 @@ class EmailParser(object):
         return request
 
 
-    def too_many_requests(self, request_id, hid, request_service, num_requests, limit):
-        if hid.hexdigest() == self.settings.get('test_hid'):
+    def too_many_requests(self, hid, num_requests, limit):
+        if hid == self.settings.get('test_hid'):
             return False
-        elif num_requests[0][0] < limit:
+        elif num_requests < limit:
             return False
         else:
             return True
@@ -224,28 +224,30 @@ class EmailParser(object):
         email_requests_limit = self.settings.get("email_requests_limit")
         now_str = datetime.now().strftime("%Y%m%d%H%M%S")
         dbname = self.settings.get("dbname")
-        hid = hashlib.sha256(request['id'].encode('utf-8'))
-        request_service = request['service']
         conn = SQLite3(dbname)
 
         if request["command"]:
+
+            hid = hashlib.sha256(request['id'].encode('utf-8')).hexdigest()
+            request_service = request['service']
+
             log.msg(
                 "Found request for {}.".format(request['command']),
                 system="email parser"
             )
 
             num_requests = yield conn.get_num_requests(
-                id=hid.hexdigest(), service=request_service
+                id=hid, service=request_service
             )
 
             check = self.too_many_requests(
-                request['id'], hid, request_service, num_requests, email_requests_limit
+                hid, num_requests[0][0], email_requests_limit
             )
 
             if check:
                 log.msg(
                     "Discarded. Too many requests from {}.".format(
-                        hid.hexdigest()
+                        hid
                     ), system="email parser"
                 )
             else:
diff --git a/tests/test_email_service.py b/tests/test_email_service.py
index 9d50f5f..8e7adf0 100644
--- a/tests/test_email_service.py
+++ b/tests/test_email_service.py
@@ -1,5 +1,7 @@
 #!/usr/bin/env python3
 import pytest
+import hashlib
+from datetime import datetime
 from twisted.trial import unittest
 from twisted.internet import defer, reactor
 from twisted.internet import task
@@ -58,6 +60,16 @@ class EmailServiceTests(unittest.TestCase):
         self.assertEqual(request["platform"], "osx")
         self.assertEqual(request["language"], "es")
 
+    def test_too_many_request_exclude(self):
+        ep = conftests.EmailParser(self.settings, "gettor@xxxxxxxxxxxxxx")
+        hid = "80d7054da0d3826563c7babb5453e18f3e42f932e562c5ab0434aec9df7b0625"
+        request_service = "osx"
+        limit = self.settings.get("email_requests_limit")
+        num_requests = 300
+        check = ep.too_many_requests(hid, num_requests, limit)
+        self.assertEqual(hid, self.settings.get('test_hid'))
+        self.assertEqual(check, False)
+
     def test_language_email_parser(self):
         ep = conftests.EmailParser(self.settings, "gettor@xxxxxxxxxxxxxx")
         request = ep.parse("From: \"silvia [hiro]\" <hiro@xxxxxxxxxxxxxx>\n Subject: \r\n Reply-To: hiro@xxxxxxxxxxxxxx \nTo: gettor@xxxxxxxxxxxxxx\n osx en")

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits