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.
Implement throttling based on participant
- Loading branch information
1 parent
271607d
commit a204a43
Showing
14 changed files
with
116 additions
and
25 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 |
---|---|---|
|
@@ -3,8 +3,10 @@ | |
import json | ||
import sys | ||
|
||
from pytest import raises | ||
|
||
from gratipay.exceptions import CannotRemovePrimaryEmail, EmailTaken, EmailNotVerified | ||
from gratipay.exceptions import TooManyEmailAddresses | ||
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 | ||
|
@@ -34,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]') | ||
|
@@ -66,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'] | ||
|
@@ -206,12 +211,15 @@ def test_remove_email(self): | |
|
||
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]') | ||
|
@@ -225,12 +233,15 @@ 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]') | ||
|
@@ -241,6 +252,30 @@ def test_html_escaping(self): | |
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): | ||
|
||
|
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