commit 0ffadaaa3da6dbc7592dcf82a4d91efaa0bde228 Author: hiro hiro@torproject.org Date: Mon Oct 14 18:08:47 2019 +0200
Add test for email limit --- gettor/parse/email.py | 54 +++++++++++++++++++++++++++------------------ share/locale/es.json | 2 +- tests/test_email_service.py | 11 +++++++++ 3 files changed, 45 insertions(+), 22 deletions(-)
diff --git a/gettor/parse/email.py b/gettor/parse/email.py index 3980763..74156b0 100644 --- a/gettor/parse/email.py +++ b/gettor/parse/email.py @@ -198,6 +198,23 @@ class EmailParser(object): return request
+ def check_num_request(self, request_id, request_service, limit): + now_str = datetime.now().strftime("%Y%m%d%H%M%S") + dbname = self.settings.get("dbname") + conn = SQLite3(dbname) + + hid = hashlib.sha256(request['id'].encode('utf-8')) + # check limits first + num_requests = yield conn.get_num_requests( + id=hid.hexdigest(), service=request_service + ) + + if num_requests[0][0] < email_requests_limit: + return 1 + else: + return 0 + + @defer.inlineCallbacks def parse_callback(self, request): """ @@ -212,30 +229,13 @@ class EmailParser(object): execution details. """ email_requests_limit = self.settings.get("email_requests_limit") - log.msg( - "Found request for {}.".format(request['command']), - system="email parser" - )
if request["command"]: - now_str = datetime.now().strftime("%Y%m%d%H%M%S") - dbname = self.settings.get("dbname") - conn = SQLite3(dbname) - - hid = hashlib.sha256(request['id'].encode('utf-8')) - # check limits first - num_requests = yield conn.get_num_requests( - id=hid.hexdigest(), service=request['service'] - ) - - if num_requests[0][0] > email_requests_limit: - log.msg( - "Discarded. Too many requests from {}.".format( - hid.hexdigest - ), system="email parser" + log.msg( + "Found request for {}.".format(request['command']), + system="email parser" ) - - else: + if check_num_request(request['id'], request['service'], email_request_limit): conn.new_request( id=request['id'], command=request['command'], @@ -245,6 +245,18 @@ class EmailParser(object): date=now_str, status="ONHOLD", ) + else: + log.msg( + "Discarded. Too many requests from {}.".format( + hid.hexdigest + ), system="email parser" + ) + + else: + log.msg( + "Request not found", + system="email parser" + )
def parse_errback(self, error): """ diff --git a/share/locale/es.json b/share/locale/es.json index 8ed6722..4d18f61 100644 --- a/share/locale/es.json +++ b/share/locale/es.json @@ -7,7 +7,7 @@ "help_config": "Custom config file location (optional)", "smtp_links_subject": "[GetTor] Links for your request", "smtp_mirrors_subject": "[GetTor] Mirrors", - "smtp_help_subject": "[GetTor] Help", + "smtp_help_subject": "[GetTor] Ayuda", "smtp_unsupported_locale_subject": "[GetTor] Unsupported locale", "smtp_unsupported_locale_msg": "The locale you requested '{}' is not supported.", "smtp_vlinks_msg": "You requested Tor Browser for {}.\n\nYou will need only one of the links below to download the bundle. If a link does not work for you, try the next one.\n\n{}\n\nShould you have issues with any of the links above you can access the following Google Drive folder: https://drive.google.com/open?id=13CADQTsCwrGsIID09YQbNz2DfRMUoxUU%5Cn%5Cn Download the file: {}\n\n \n--\nGetTor", diff --git a/tests/test_email_service.py b/tests/test_email_service.py index 9d50f5f..f81bd17 100644 --- a/tests/test_email_service.py +++ b/tests/test_email_service.py @@ -58,6 +58,17 @@ class EmailServiceTests(unittest.TestCase): self.assertEqual(request["platform"], "osx") self.assertEqual(request["language"], "es")
+ def test_remove_limits(self): + ep = conftests.EmailParser(self.settings, "gettor@torproject.org") + msg_str = "From: "GetTor Test" test.gettor.browser@gmail.com\n Subject: \r\n Reply-To: test.gettor.browser@gmail.com \nTo: gettor@torproject.org\r\n osx es" + msg = conftests.message_from_string(msg_str) + languages = [*self.locales.keys()] + platforms = self.settings.get('platforms') + request = ep.build_request(msg_str, "hiro@torproject.org", languages, platforms) + email_requests_limit = self.settings.get("email_requests_limit") + check = check_num_request(request['id'], request['service'], email_request_limit) + assert check + def test_language_email_parser(self): ep = conftests.EmailParser(self.settings, "gettor@torproject.org") request = ep.parse("From: "silvia [hiro]" hiro@torproject.org\n Subject: \r\n Reply-To: hiro@torproject.org \nTo: gettor@torproject.org\n osx en")
tor-commits@lists.torproject.org