2014-01-31 18:53:33 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import
|
|
|
|
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
|
|
|
from django.test import TestCase
|
|
|
|
|
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 (
|
|
|
|
add_ratelimit_rule,
|
|
|
|
clear_user_history,
|
|
|
|
remove_ratelimit_rule,
|
|
|
|
)
|
|
|
|
|
|
|
|
from zerver.lib.actions import compute_mit_user_fullname
|
2016-11-10 19:30:09 +01:00
|
|
|
from zerver.lib.test_classes import (
|
|
|
|
ZulipTestCase,
|
|
|
|
)
|
2014-01-31 18:53:33 +01:00
|
|
|
from zerver.models import get_user_profile_by_email
|
|
|
|
from zerver.lib.test_runner import slow
|
|
|
|
|
2016-04-28 07:17:10 +02:00
|
|
|
import DNS
|
|
|
|
import mock
|
2014-01-31 18:53:33 +01:00
|
|
|
import time
|
|
|
|
import ujson
|
|
|
|
|
2016-06-28 22:20:38 +02:00
|
|
|
from six.moves import urllib
|
2015-11-01 17:15:05 +01:00
|
|
|
from six.moves import range
|
2016-12-04 18:38:56 +01:00
|
|
|
from typing import Text
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
class MITNameTest(TestCase):
|
|
|
|
def test_valid_hesiod(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2016-04-28 07:17:10 +02:00
|
|
|
with mock.patch('DNS.dnslookup', return_value=[['starnine:*:84233:101:Athena Consulting Exchange User,,,:/mit/starnine:/bin/bash']]):
|
|
|
|
self.assertEquals(compute_mit_user_fullname("starnine@mit.edu"), "Athena Consulting Exchange User")
|
|
|
|
with mock.patch('DNS.dnslookup', return_value=[['sipbexch:*:87824:101:Exch Sipb,,,:/mit/sipbexch:/bin/athena/bash']]):
|
|
|
|
self.assertEquals(compute_mit_user_fullname("sipbexch@mit.edu"), "Exch Sipb")
|
|
|
|
|
2014-01-31 18:53:33 +01:00
|
|
|
def test_invalid_hesiod(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2016-04-28 07:17:10 +02:00
|
|
|
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
|
|
|
self.assertEquals(compute_mit_user_fullname("1234567890@mit.edu"), "1234567890@mit.edu")
|
|
|
|
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
|
|
|
self.assertEquals(compute_mit_user_fullname("ec-discuss@mit.edu"), "ec-discuss@mit.edu")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
def test_mailinglist(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2016-04-28 07:17:10 +02:00
|
|
|
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
2016-11-06 00:29:55 +01:00
|
|
|
self.assertRaises(ValidationError, email_is_not_mit_mailing_list, "1234567890@mit.edu")
|
2016-04-28 07:17:10 +02:00
|
|
|
with mock.patch('DNS.dnslookup', side_effect=DNS.Base.ServerError('DNS query status: NXDOMAIN', 3)):
|
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
|
|
|
|
2014-01-31 18:53:33 +01:00
|
|
|
def test_notmailinglist(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2016-04-28 07:17:10 +02: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
|
|
|
|
2016-08-23 02:08:42 +02:00
|
|
|
class RateLimitTests(ZulipTestCase):
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 18:53:33 +01:00
|
|
|
settings.RATE_LIMITING = True
|
|
|
|
add_ratelimit_rule(1, 5)
|
|
|
|
|
|
|
|
def tearDown(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 18:53:33 +01:00
|
|
|
settings.RATE_LIMITING = False
|
|
|
|
remove_ratelimit_rule(1, 5)
|
|
|
|
|
2016-09-27 23:27:36 +02:00
|
|
|
def send_api_message(self, email, content):
|
2016-12-04 18:38:56 +01:00
|
|
|
# type: (Text, Text) -> HttpResponse
|
2016-09-27 21:41:42 +02:00
|
|
|
return self.client_post("/api/v1/messages", {"type": "stream",
|
|
|
|
"to": "Verona",
|
|
|
|
"client": "test suite",
|
|
|
|
"content": content,
|
|
|
|
"subject": "Test subject"},
|
|
|
|
**self.api_auth(email))
|
|
|
|
|
2014-01-31 18:53:33 +01:00
|
|
|
def test_headers(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 18:53:33 +01:00
|
|
|
email = "hamlet@zulip.com"
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
clear_user_history(user)
|
|
|
|
|
2016-09-27 23:27:36 +02:00
|
|
|
result = self.send_api_message(email, "some stuff")
|
2014-01-31 18:53:33 +01:00
|
|
|
self.assertTrue('X-RateLimit-Remaining' in result)
|
|
|
|
self.assertTrue('X-RateLimit-Limit' in result)
|
|
|
|
self.assertTrue('X-RateLimit-Reset' in result)
|
|
|
|
|
|
|
|
def test_ratelimit_decrease(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 18:53:33 +01:00
|
|
|
email = "hamlet@zulip.com"
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
clear_user_history(user)
|
2016-09-27 23:27:36 +02:00
|
|
|
result = self.send_api_message(email, "some stuff")
|
2014-01-31 18:53:33 +01:00
|
|
|
limit = int(result['X-RateLimit-Remaining'])
|
|
|
|
|
2016-09-27 23:27:36 +02:00
|
|
|
result = self.send_api_message(email, "some stuff 2")
|
2014-01-31 18:53:33 +01:00
|
|
|
newlimit = int(result['X-RateLimit-Remaining'])
|
|
|
|
self.assertEqual(limit, newlimit + 1)
|
|
|
|
|
2016-07-29 21:06:22 +02:00
|
|
|
@slow('has to sleep to work')
|
2014-01-31 18:53:33 +01:00
|
|
|
def test_hit_ratelimits(self):
|
2016-06-04 20:14:05 +02:00
|
|
|
# type: () -> None
|
2014-01-31 18:53:33 +01:00
|
|
|
email = "cordelia@zulip.com"
|
|
|
|
user = get_user_profile_by_email(email)
|
|
|
|
clear_user_history(user)
|
|
|
|
|
|
|
|
for i in range(6):
|
2016-09-27 23:27:36 +02:00
|
|
|
result = self.send_api_message(email, "some stuff %s" % (i,))
|
2014-01-31 18:53:33 +01:00
|
|
|
|
2014-03-07 16:47:30 +01:00
|
|
|
self.assertEqual(result.status_code, 429)
|
2014-01-31 18:53:33 +01:00
|
|
|
json = ujson.loads(result.content)
|
|
|
|
self.assertEqual(json.get("result"), "error")
|
|
|
|
self.assertIn("API usage exceeded rate limit, try again in", json.get("msg"))
|
2014-03-07 16:47:30 +01:00
|
|
|
self.assertTrue('Retry-After' in result)
|
|
|
|
self.assertIn(result['Retry-After'], json.get("msg"))
|
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.
|
|
|
|
time.sleep(1)
|
|
|
|
|
2016-09-27 23:27:36 +02:00
|
|
|
result = self.send_api_message(email, "Good message")
|
2014-01-31 18:53:33 +01:00
|
|
|
|
|
|
|
self.assert_json_success(result)
|