Commit 8953e38e authored by Hiro's avatar Hiro 🏄
Browse files

Update check for too_many_requests

parent f08007fa
......@@ -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
return True
......@@ -224,28 +224,30 @@ class EmailParser(object):
email_requests_limit = self.settings.get("email_requests_limit")
now_str ="%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']
"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:
"Discarded. Too many requests from {}.".format(
), system="email parser"
#!/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, "")
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, "")
request = ep.parse("From: \"silvia [hiro]\" <>\n Subject: \r\n Reply-To: \nTo:\n osx en")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment