From d4a22dbaadf0c58090743e0e61e2fb6e247e76e1 Mon Sep 17 00:00:00 2001 From: Paul Kuruvilla Date: Sun, 9 Jul 2017 15:37:03 +0530 Subject: [PATCH] Add Participant::from_email --- gratipay/models/participant/__init__.py | 14 +++++++++++ tests/py/test_participant_emails.py | 31 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/py/test_participant_emails.py diff --git a/gratipay/models/participant/__init__.py b/gratipay/models/participant/__init__.py index 90f8256c62..c17a5c031a 100644 --- a/gratipay/models/participant/__init__.py +++ b/gratipay/models/participant/__init__.py @@ -102,6 +102,20 @@ def from_session_token(cls, token): return participant + @classmethod + def from_email(cls, email_address): + """Return an existing participant based on email. + """ + return cls.db.one(""" + + SELECT participants.*::participants + FROM participants + JOIN emails ON emails.participant_id = participants.id + WHERE emails.address=%s + AND emails.verified IS true + + """, (email_address, )) + @classmethod def _from_thing(cls, thing, value): assert thing in ("id", "username_lower", "session_token", "api_key") diff --git a/tests/py/test_participant_emails.py b/tests/py/test_participant_emails.py new file mode 100644 index 0000000000..be9550a69c --- /dev/null +++ b/tests/py/test_participant_emails.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +from gratipay.testing import Harness +from gratipay.models.participant import Participant + + +class TestFromEmail(Harness): + + def test_returns_participant_by_primary_email(self): + alice = self.make_participant('alice') + self.add_and_verify_email(alice, 'alice@gratipay.com') + + assert Participant.from_email('alice@gratipay.com').username == 'alice' + + def test_returns_participant_by_non_primary_email(self): + alice = self.make_participant('alice') + self.add_and_verify_email(alice, 'alice@gratipay.com') + self.add_and_verify_email(alice, 'alice_non_primary@gratipay.com') + + assert Participant.from_username('alice').email_address == 'alice@gratipay.com' + assert Participant.from_email('alice_non_primary@gratipay.com').username == 'alice' + + def test_returns_none_for_unverified_email(self): + alice = self.make_participant('alice') + alice.start_email_verification('alice@gratipay.com') + + assert Participant.from_email('alice@gratipay.com') is None + + def test_returns_none_if_no_email_exists(self): + assert Participant.from_email('dummy@gratipay.com') is None