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

Commit

Permalink
Allow deleting last elsewhere if email attached
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Jul 16, 2017
1 parent 13f0eff commit 114f36c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
15 changes: 11 additions & 4 deletions gratipay/models/participant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,16 @@ def delete_elsewhere(self, platform, user_id):
user_id = unicode(user_id)
with self.db.get_cursor() as c:
accounts = self.get_elsewhere_logins(c)
assert len(accounts) > 0
if len(accounts) == 1 and accounts[0] == (platform, user_id):
raise LastElsewhere()

# A user who signed up via a third-party provider might not have
# and email attached. They must maintain at least one elsewhere
# account until they provide an email.
assert self.email_address or (len(accounts) > 0)

is_last = len(accounts) == 1 and accounts[0] == (platform, user_id)
if is_last and not self.email_address:
raise LastElsewhereAndNoEmail()

c.one("""
DELETE FROM elsewhere
WHERE participant=%s
Expand Down Expand Up @@ -1432,7 +1439,7 @@ def __nonzero__(self):
A, B, C = self._all
return A or C

class LastElsewhere(Exception): pass
class LastElsewhereAndNoEmail(Exception): pass

class NonexistingElsewhere(Exception): pass

Expand Down
10 changes: 7 additions & 3 deletions tests/py/test_participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import (
LastElsewhere, NeedConfirmation, NonexistingElsewhere, Participant
LastElsewhereAndNoEmail, NeedConfirmation, NonexistingElsewhere, Participant
)
from gratipay.testing import Harness, D,P,T

Expand Down Expand Up @@ -69,12 +69,16 @@ def test_comparison(self):
assert not (self.alice == None)

def test_delete_elsewhere_last(self):
with pytest.raises(LastElsewhere):
with pytest.raises(LastElsewhereAndNoEmail):
self.alice.delete_elsewhere('twitter', self.alice.id)

def test_delete_elsewhere_last_works_if_email_present(self):
self.add_and_verify_email(self.alice, '[email protected]')
self.alice.delete_elsewhere('twitter', self.alice.id)

def test_delete_elsewhere_last_signin(self):
self.make_elsewhere('bountysource', self.alice.id, 'alice')
with pytest.raises(LastElsewhere):
with pytest.raises(LastElsewhereAndNoEmail):
self.alice.delete_elsewhere('twitter', self.alice.id)

def test_delete_elsewhere_nonsignin(self):
Expand Down
14 changes: 10 additions & 4 deletions www/~/%username/delete-elsewhere.json.spt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from aspen import Response
from gratipay.models.participant import LastElsewhere, NonexistingElsewhere
from gratipay.models.participant import LastElsewhereAndNoEmail, NonexistingElsewhere
[-----------------------------------------------------------------------------]
if user.ANON:
raise Response(403)
Expand All @@ -10,10 +10,16 @@ user_id = request.body["user_id"]

try:
user.participant.delete_elsewhere(platform, user_id)
except LastElsewhere:
raise Response(400, "Cannot delete last login account. If you want to merge this login account to a different Gratipay account: log out of this account; log into the other account using another method; and then add this authentication provider to it. The two Gratipay accounts will be merged.")
except LastElsewhereAndNoEmail:
msg = _("Cannot delete last login account since you don't have an email "
"address attached. If you want to merge this login account to a "
"different Gratipay account: log out of this account; log into the "
"other account using another method; and then add this "
"authentication provider to it. The two Gratipay accounts "
"will be merged.")
raise Response(400, msg)
except NonexistingElsewhere:
raise Response(400, "Account does not exist.")
raise Response(400, _("Account does not exist."))

[---] application/json via json_dump
{ "msg": "OK" }

0 comments on commit 114f36c

Please sign in to comment.