2020-06-11 00:54:34 +02:00
|
|
|
import time
|
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import DNS
|
2014-01-31 18:53:33 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
2016-06-04 20:14:05 +02:00
|
|
|
from django.http import HttpResponse
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2016-11-06 00:29:55 +01:00
|
|
|
from zerver.forms import email_is_not_mit_mailing_list
|
2014-01-31 18:53:33 +01:00
|
|
|
from zerver.lib.rate_limiter import (
|
2017-07-31 07:03:52 +02:00
|
|
|
RateLimitedUser,
|
2019-03-16 15:46:50 +01:00
|
|
|
RateLimiterLockingException,
|
2020-06-11 00:54:34 +02:00
|
|
|
add_ratelimit_rule,
|
|
|
|
remove_ratelimit_rule,
|
2014-01-31 18:53:33 +01:00
|
|
|
)
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-08-11 16:26:46 +02:00
|
|
|
from zerver.lib.zephyr import compute_mit_user_fullname
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import UserProfile
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
|
2017-05-23 02:33:04 +02:00
|
|
|
class MITNameTest(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_valid_hesiod(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup",
|
2021-02-12 08:19:30 +01:00
|
|
|
return_value=[
|
2021-02-12 08:20:45 +01:00
|
|
|
["starnine:*:84233:101:Athena Consulting Exchange User,,,:/mit/starnine:/bin/bash"]
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
):
|
|
|
|
self.assertEqual(
|
|
|
|
compute_mit_user_fullname(self.mit_email("starnine")),
|
|
|
|
"Athena Consulting Exchange User",
|
|
|
|
)
|
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup",
|
|
|
|
return_value=[["sipbexch:*:87824:101:Exch Sipb,,,:/mit/sipbexch:/bin/athena/bash"]],
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(compute_mit_user_fullname("sipbexch@mit.edu"), "Exch Sipb")
|
2016-04-28 07:17:10 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_invalid_hesiod(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup", side_effect=DNS.Base.ServerError("DNS query status: NXDOMAIN", 3)
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(compute_mit_user_fullname("1234567890@mit.edu"), "1234567890@mit.edu")
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup", side_effect=DNS.Base.ServerError("DNS query status: NXDOMAIN", 3)
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2016-12-16 02:01:34 +01:00
|
|
|
self.assertEqual(compute_mit_user_fullname("ec-discuss@mit.edu"), "ec-discuss@mit.edu")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_mailinglist(self) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup", side_effect=DNS.Base.ServerError("DNS query status: NXDOMAIN", 3)
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2016-11-06 00:29:55 +01:00
|
|
|
self.assertRaises(ValidationError, email_is_not_mit_mailing_list, "1234567890@mit.edu")
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"DNS.dnslookup", side_effect=DNS.Base.ServerError("DNS query status: NXDOMAIN", 3)
|
2021-02-12 08:19:30 +01:00
|
|
|
):
|
2016-11-06 00:29:55 +01:00
|
|
|
self.assertRaises(ValidationError, email_is_not_mit_mailing_list, "ec-discuss@mit.edu")
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_notmailinglist(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("DNS.dnslookup", return_value=[["POP IMAP.EXCHANGE.MIT.EDU starnine"]]):
|
2016-11-06 00:29:55 +01:00
|
|
|
email_is_not_mit_mailing_list("sipbexch@mit.edu")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
class RateLimitTests(ZulipTestCase):
|
2017-11-05 10:51:25 +01:00
|
|
|
def setUp(self) -> None:
|
2019-10-19 20:47:00 +02:00
|
|
|
super().setUp()
|
2020-08-22 15:32:37 +02:00
|
|
|
|
|
|
|
# Some tests here can be somewhat timing-sensitive in a way
|
|
|
|
# that can't be eliminated, e.g. due to testing things that rely
|
2020-10-23 02:43:28 +02:00
|
|
|
# on Redis' internal timing mechanism which we can't mock.
|
2020-08-22 15:32:37 +02:00
|
|
|
# The first API request when running a suite of tests is slow
|
|
|
|
# and can take multiple seconds. This is not a problem when running
|
|
|
|
# multiple tests, but if an individual, time-sensitive test from this class
|
|
|
|
# is run, the first API request it makes taking a lot of time can throw things off
|
|
|
|
# and cause the test to fail. Thus we do a dummy API request here to warm up
|
|
|
|
# the system and allow the tests to assume their requests won't take multiple seconds.
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-08-22 15:32:37 +02:00
|
|
|
self.api_get(user, "/api/v1/messages")
|
|
|
|
|
2014-01-31 18:53:33 +01:00
|
|
|
settings.RATE_LIMITING = True
|
|
|
|
add_ratelimit_rule(1, 5)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def tearDown(self) -> None:
|
2014-01-31 18:53:33 +01:00
|
|
|
settings.RATE_LIMITING = False
|
|
|
|
remove_ratelimit_rule(1, 5)
|
|
|
|
|
2019-10-18 16:11:48 +02:00
|
|
|
super().tearDown()
|
|
|
|
|
2020-03-10 11:48:26 +01:00
|
|
|
def send_api_message(self, user: UserProfile, content: str) -> HttpResponse:
|
2021-02-12 08:19:30 +01:00
|
|
|
return self.api_post(
|
|
|
|
user,
|
|
|
|
"/api/v1/messages",
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": "Verona",
|
|
|
|
"client": "test suite",
|
|
|
|
"content": content,
|
|
|
|
"topic": "whatever",
|
|
|
|
},
|
|
|
|
)
|
2016-09-27 21:41:42 +02:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_headers(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-03-04 14:05:25 +01:00
|
|
|
RateLimitedUser(user).clear_history()
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.send_api_message(user, "some stuff")
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertTrue("X-RateLimit-Remaining" in result)
|
|
|
|
self.assertTrue("X-RateLimit-Limit" in result)
|
|
|
|
self.assertTrue("X-RateLimit-Reset" in result)
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_ratelimit_decrease(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("hamlet")
|
2020-03-04 14:05:25 +01:00
|
|
|
RateLimitedUser(user).clear_history()
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.send_api_message(user, "some stuff")
|
2021-02-12 08:20:45 +01:00
|
|
|
limit = int(result["X-RateLimit-Remaining"])
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.send_api_message(user, "some stuff 2")
|
2021-02-12 08:20:45 +01:00
|
|
|
newlimit = int(result["X-RateLimit-Remaining"])
|
2014-01-31 18:53:33 +01:00
|
|
|
self.assertEqual(limit, newlimit + 1)
|
|
|
|
|
2017-11-05 10:51:25 +01:00
|
|
|
def test_hit_ratelimits(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("cordelia")
|
2020-03-04 14:05:25 +01:00
|
|
|
RateLimitedUser(user).clear_history()
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2017-05-06 13:56:02 +02:00
|
|
|
start_time = time.time()
|
2014-01-31 18:53:33 +01:00
|
|
|
for i in range(6):
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("time.time", return_value=(start_time + i * 0.1)):
|
2020-06-10 06:41:04 +02:00
|
|
|
result = self.send_api_message(user, f"some stuff {i}")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2014-03-07 16:47:30 +01:00
|
|
|
self.assertEqual(result.status_code, 429)
|
2017-08-17 08:40:19 +02:00
|
|
|
json = result.json()
|
2014-01-31 18:53:33 +01:00
|
|
|
self.assertEqual(json.get("result"), "error")
|
2017-05-22 20:12:59 +02:00
|
|
|
self.assertIn("API usage exceeded rate limit", json.get("msg"))
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(json.get("retry-after"), 0.5)
|
|
|
|
self.assertTrue("Retry-After" in result)
|
|
|
|
self.assertEqual(result["Retry-After"], "0.5")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
# We actually wait a second here, rather than force-clearing our history,
|
|
|
|
# to make sure the rate-limiting code automatically forgives a user
|
|
|
|
# after some time has passed.
|
2021-02-12 08:20:45 +01:00
|
|
|
with mock.patch("time.time", return_value=(start_time + 1.01)):
|
2020-03-10 11:48:26 +01:00
|
|
|
result = self.send_api_message(user, "Good message")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2016-12-08 23:08:39 +01:00
|
|
|
self.assert_json_success(result)
|
2019-03-16 15:46:50 +01:00
|
|
|
|
2020-07-11 14:17:25 +02:00
|
|
|
def test_hit_ratelimiterlockingexception(self) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
user = self.example_user("cordelia")
|
2020-03-04 14:05:25 +01:00
|
|
|
RateLimitedUser(user).clear_history()
|
2019-03-16 15:46:50 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
with mock.patch(
|
2021-02-12 08:20:45 +01:00
|
|
|
"zerver.lib.rate_limiter.RedisRateLimiterBackend.incr_ratelimit",
|
2021-02-12 08:19:30 +01:00
|
|
|
side_effect=RateLimiterLockingException,
|
|
|
|
):
|
2020-07-11 14:17:25 +02:00
|
|
|
with self.assertLogs("zerver.lib.rate_limiter", level="WARNING") as m:
|
|
|
|
result = self.send_api_message(user, "some stuff")
|
|
|
|
self.assertEqual(result.status_code, 429)
|
|
|
|
self.assertEqual(
|
|
|
|
m.output,
|
2021-02-12 08:19:30 +01:00
|
|
|
[
|
|
|
|
"WARNING:zerver.lib.rate_limiter:Deadlock trying to incr_ratelimit for {}".format(
|
|
|
|
f"RateLimitedUser:{user.id}:api_by_user"
|
|
|
|
)
|
|
|
|
],
|
2020-05-02 08:44:14 +02:00
|
|
|
)
|