Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Add Participant::from_email
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Jul 13, 2017
1 parent 43c4343 commit b2d6fe1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gratipay/models/participant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
31 changes: 31 additions & 0 deletions tests/py/test_participant_emails.py
Original file line number Diff line number Diff line change
@@ -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, '[email protected]')

assert Participant.from_email('[email protected]').username == 'alice'

def test_returns_participant_by_non_primary_email(self):
alice = self.make_participant('alice')
self.add_and_verify_email(alice, '[email protected]')
self.add_and_verify_email(alice, '[email protected]')

assert Participant.from_username('alice').email_address == '[email protected]'
assert Participant.from_email('[email protected]').username == 'alice'

def test_returns_none_for_unverified_email(self):
alice = self.make_participant('alice')
alice.start_email_verification('[email protected]')

assert Participant.from_email('[email protected]') is None

def test_returns_none_if_no_email_exists(self):
assert Participant.from_email('[email protected]') is None

0 comments on commit b2d6fe1

Please sign in to comment.