This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4349 from gratipay/throttle-better
Throttle better
- Loading branch information
Showing
16 changed files
with
124 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN; | ||
|
||
ALTER TABLE email_queue ADD COLUMN user_initiated bool NOT NULL DEFAULT TRUE; | ||
|
||
END; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,30 +2,26 @@ | |
|
||
import json | ||
import sys | ||
import time | ||
|
||
from pytest import raises | ||
|
||
from gratipay.exceptions import CannotRemovePrimaryEmail, EmailTaken, EmailNotVerified | ||
from gratipay.exceptions import TooManyEmailAddresses, ResendingTooFast | ||
from gratipay.exceptions import TooManyEmailAddresses, Throttled | ||
from gratipay.testing import P | ||
from gratipay.testing.email import QueuedEmailHarness, SentEmailHarness | ||
from gratipay.models.participant import email as _email | ||
from gratipay.utils import encode_for_querystring | ||
from gratipay.cli import queue_branch_email as _queue_branch_email | ||
|
||
|
||
class AliceAndResend(QueuedEmailHarness): | ||
class Alice(QueuedEmailHarness): | ||
|
||
def setUp(self): | ||
QueuedEmailHarness.setUp(self) | ||
self.alice = self.make_participant('alice', claimed_time='now') | ||
self._old_threshold = self.client.website.env.resend_verification_threshold | ||
self.client.website.env.resend_verification_threshold = '0 seconds' | ||
|
||
def tearDown(self): | ||
self.client.website.env.resend_verification_threshold = self._old_threshold | ||
|
||
|
||
class TestEndpoints(AliceAndResend): | ||
class TestEndpoints(Alice): | ||
|
||
def hit_email_spt(self, action, address, user='alice', should_fail=False): | ||
f = self.client.PxST if should_fail else self.client.POST | ||
|
@@ -40,11 +36,13 @@ def verify_email(self, email, nonce, username='alice', should_fail=False): | |
f = self.client.GxT if should_fail else self.client.GET | ||
return f(url, auth_as=username) | ||
|
||
def verify_and_change_email(self, old_email, new_email, username='alice'): | ||
def verify_and_change_email(self, old_email, new_email, username='alice', _flush=True): | ||
self.hit_email_spt('add-email', old_email) | ||
nonce = P(username).get_email(old_email).nonce | ||
self.verify_email(old_email, nonce) | ||
self.hit_email_spt('add-email', new_email) | ||
if _flush: | ||
self.app.email_queue.flush() | ||
|
||
def test_participant_can_add_email(self): | ||
response = self.hit_email_spt('add-email', '[email protected]') | ||
|
@@ -72,9 +70,10 @@ def test_verification_email_doesnt_contain_unsubscribe(self): | |
assert "To stop receiving" not in last_email['body_text'] | ||
|
||
def test_adding_second_email_sends_verification_notice(self): | ||
self.verify_and_change_email('[email protected]', '[email protected]') | ||
self.verify_and_change_email('[email protected]', '[email protected]', _flush=False) | ||
assert self.count_email_messages() == 3 | ||
last_email = self.get_last_email() | ||
self.app.email_queue.flush() | ||
assert last_email['to'] == 'alice <[email protected]>' | ||
expected = "We are connecting [email protected] to the alice account on Gratipay" | ||
assert expected in last_email['body_text'] | ||
|
@@ -210,14 +209,17 @@ def test_remove_email(self): | |
self.hit_email_spt('remove', '[email protected]') | ||
|
||
|
||
class TestFunctions(AliceAndResend): | ||
class TestFunctions(Alice): | ||
|
||
def add_and_verify(self, participant, address): | ||
participant.add_email('[email protected]') | ||
nonce = participant.get_email('[email protected]').nonce | ||
r = participant.verify_email('[email protected]', nonce) | ||
assert r == _email.VERIFICATION_SUCCEEDED | ||
|
||
def test_cannot_update_email_to_already_verified(self): | ||
bob = self.make_participant('bob', claimed_time='now') | ||
self.alice.add_email('[email protected]') | ||
nonce = self.alice.get_email('[email protected]').nonce | ||
r = self.alice.verify_email('[email protected]', nonce) | ||
assert r == _email.VERIFICATION_SUCCEEDED | ||
self.add_and_verify(self.alice, '[email protected]') | ||
|
||
with self.assertRaises(EmailTaken): | ||
bob.add_email('[email protected]') | ||
|
@@ -231,33 +233,49 @@ def test_cannot_add_too_many_emails(self): | |
self.alice.add_email('[email protected]') | ||
self.alice.add_email('[email protected]') | ||
self.alice.add_email('[email protected]') | ||
self.app.email_queue.flush() | ||
self.alice.add_email('[email protected]') | ||
self.alice.add_email('[email protected]') | ||
self.alice.add_email('[email protected]') | ||
self.app.email_queue.flush() | ||
self.alice.add_email('[email protected]') | ||
self.alice.add_email('[email protected]') | ||
self.alice.add_email('[email protected]') | ||
self.app.email_queue.flush() | ||
self.alice.add_email('[email protected]') | ||
with self.assertRaises(TooManyEmailAddresses): | ||
self.alice.add_email('[email protected]') | ||
|
||
def test_cannot_resend_verification_too_frequently(self): | ||
self.alice.add_email('[email protected]') | ||
time.sleep(0.05) | ||
with self.assertRaises(ResendingTooFast): | ||
self.alice.add_email('[email protected]', '0.1 seconds') | ||
|
||
def test_can_resend_verification_after_a_while(self): | ||
self.alice.add_email('[email protected]') | ||
time.sleep(0.15) | ||
self.alice.add_email('[email protected]', '0.1 seconds') | ||
|
||
def test_html_escaping(self): | ||
self.alice.add_email("foo'[email protected]") | ||
last_email = self.get_last_email() | ||
assert 'foo'bar' in last_email['body_html'] | ||
assert ''' not in last_email['body_text'] | ||
|
||
def test_queueing_email_is_throttled(self): | ||
self.app.email_queue.put(self.alice, "verification") | ||
self.app.email_queue.put(self.alice, "branch") | ||
self.app.email_queue.put(self.alice, "verification_notice") | ||
raises(Throttled, self.app.email_queue.put, self.alice, "branch") | ||
|
||
def test_only_user_initiated_messages_count_towards_throttling(self): | ||
self.app.email_queue.put(self.alice, "verification") | ||
self.app.email_queue.put(self.alice, "verification", _user_initiated=False) | ||
self.app.email_queue.put(self.alice, "branch") | ||
self.app.email_queue.put(self.alice, "branch", _user_initiated=False) | ||
self.app.email_queue.put(self.alice, "verification_notice") | ||
self.app.email_queue.put(self.alice, "verification_notice", _user_initiated=False) | ||
raises(Throttled, self.app.email_queue.put, self.alice, "branch") | ||
|
||
def test_flushing_queue_resets_throttling(self): | ||
self.add_and_verify(self.alice, '[email protected]') | ||
assert self.app.email_queue.flush() == 1 | ||
self.app.email_queue.put(self.alice, "verification") | ||
self.app.email_queue.put(self.alice, "branch") | ||
self.app.email_queue.put(self.alice, "verification_notice") | ||
assert self.app.email_queue.flush() == 3 | ||
self.app.email_queue.put(self.alice, "verification_notice") | ||
|
||
|
||
class FlushEmailQueue(SentEmailHarness): | ||
|
||
|
Oops, something went wrong.