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

Commit

Permalink
Started working through test failures; #287
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Apr 5, 2013
1 parent 28cb48c commit 7894d23
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 65 deletions.
22 changes: 12 additions & 10 deletions gittip/billing/payday.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def genparticipants(self, ts_start, for_payday):
"""
for deets in self.get_participants(ts_start):
participant = Participant(deets['id'])
participant = Participant(deets['username'])
tips, total = participant.get_tips_and_total( for_payday=for_payday
, db=self.db
)
Expand Down Expand Up @@ -200,7 +200,7 @@ def get_participants(self, ts_start):
"""Given a timestamp, return a list of participants dicts.
"""
PARTICIPANTS = """\
SELECT id
SELECT username
, balance
, balanced_account_uri
, stripe_customer_id
Expand Down Expand Up @@ -319,7 +319,7 @@ def tip(self, participant, tip, ts_start):
"""
msg = "$%s from %s to %s."
msg %= (tip['amount'], participant['id'], tip['tippee'])
msg %= (tip['amount'], participant['username'], tip['tippee'])

if tip['amount'] == 0:

Expand All @@ -340,7 +340,8 @@ def tip(self, participant, tip, ts_start):
log("SKIPPED: %s" % msg)
return 0

if not self.transfer(participant['id'], tip['tippee'], tip['amount']):
if not self.transfer(participant['username'], tip['tippee'], \
tip['amount']):

# The transfer failed due to a lack of funds for the participant.
# Don't try any further transfers.
Expand Down Expand Up @@ -507,7 +508,7 @@ def ach_credit(self, ts_start, participant, tips, total):
also_log = " ($%s balance - $%s in obligations)"
also_log %= (balance, total)
log("Minimum payout is $%s. %s is only due $%s%s."
% (MINIMUM_CREDIT, participant['id'], amount, also_log))
% (MINIMUM_CREDIT, participant['username'], amount, also_log))
return # Participant owed too little.

if not is_whitelisted(participant):
Expand All @@ -526,7 +527,7 @@ def ach_credit(self, ts_start, participant, tips, total):
else:
also_log = "$%s" % amount
msg = "Crediting %s %d cents (%s - $%s fee = $%s) on Balanced ... "
msg %= (participant['id'], cents, also_log, fee, credit_amount)
msg %= (participant['username'], cents, also_log, fee, credit_amount)


# Try to dance with Balanced.
Expand All @@ -536,12 +537,13 @@ def ach_credit(self, ts_start, participant, tips, total):

balanced_account_uri = participant['balanced_account_uri']
if balanced_account_uri is None:
log("%s has no balanced_account_uri." % participant['id'])
log("%s has no balanced_account_uri."
% participant['username'])
return # not in Balanced

account = balanced.Account.find(balanced_account_uri)
if 'merchant' not in account.roles:
log("%s is not a merchant." % participant['id'])
log("%s is not a merchant." % participant['username'])
return # not a merchant

account.credit(cents)
Expand All @@ -552,7 +554,7 @@ def ach_credit(self, ts_start, participant, tips, total):
error = err.message
log(msg + "failed: %s" % error)

self.record_credit(credit_amount, fee, error, participant['id'])
self.record_credit(credit_amount, fee, error, participant['username'])


def charge_on_balanced(self, username, balanced_account_uri, amount):
Expand Down Expand Up @@ -670,7 +672,7 @@ def record_charge(self, amount, charge_amount, fee, error, username):
EXCHANGE = """\
INSERT INTO exchanges
(amount, fee, username)
(amount, fee, participant)
VALUES (%s, %s, %s)
"""
Expand Down
6 changes: 3 additions & 3 deletions gittip/elsewhere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ def opt_in(self, desired_username):
"""Given a desired username, return a User object.
"""
self.set_is_locked(False)
user = User.from_username(self.username) # give them a session
user = User.from_username(self.participant) # give them a session
if not self.is_claimed:
user.set_as_claimed()
try:
user.change_id(desired_username)
user.change_username(desired_username)
user.username = self.username = desired_username
except user.ProblemChangingUsername:
pass
Expand Down Expand Up @@ -109,7 +109,7 @@ def upsert(self, user_info):
with gittip.db.get_transaction() as txn:
_username = reserve_a_random_username(txn)
txn.execute( "INSERT INTO elsewhere "
"(platform, user_id, username) "
"(platform, user_id, participant) "
"VALUES (%s, %s, %s)"
, (self.platform, self.user_id, _username)
)
Expand Down
24 changes: 12 additions & 12 deletions gittip/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sqlalchemy.exc import IntegrityError
from sqlalchemy import func
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Column, CheckConstraint, UniqueConstraint
from sqlalchemy.schema import Column, CheckConstraint, UniqueConstraint, Sequence
from sqlalchemy.types import Text, TIMESTAMP, Boolean, Numeric, BigInteger

import gittip
Expand All @@ -31,7 +31,7 @@ class Participant(db.Model):
name="participants_session_token_key"),
)

id = Column(BigInteger, nullable=False, unique=True)
id = Column(BigInteger, Sequence('participants_id_seq'), nullable=False, unique=True)
username = Column(Text, nullable=False, primary_key=True)
statement = Column(Text, default="", nullable=False)
stripe_customer_id = Column(Text)
Expand Down Expand Up @@ -109,7 +109,7 @@ def tips_receiving(self):
@property
def valid_tips_receiving(self):
return self.tips_receiving \
.join(Participant, Tip.tipper == Participant.id) \
.join(Participant, Tip.tipper == Participant.username) \
.filter( 'participants.is_suspicious IS NOT true'
, Participant.last_bill_result == ''
)
Expand Down Expand Up @@ -228,7 +228,7 @@ def get_number_of_backers(self):
return nbackers

def get_og_title(self):
out = self.id
out = self.username
receiving = self.get_dollars_receiving()
giving = self.get_dollars_giving()
if (giving > receiving) and not self.anonymous:
Expand All @@ -249,21 +249,21 @@ def get_age_in_seconds(self):

# TODO: Move these queries into this class.

def set_tip_to(self, tippee_id, amount):
return OldParticipant(self.id).set_tip_to(tippee_id, amount)
def set_tip_to(self, tippee, amount):
return OldParticipant(self.username).set_tip_to(tippee, amount)

def get_dollars_giving(self):
return OldParticipant(self.id).get_dollars_giving()
return OldParticipant(self.username).get_dollars_giving()

def get_tip_distribution(self):
return OldParticipant(self.id).get_tip_distribution()
return OldParticipant(self.username).get_tip_distribution()

def get_giving_for_profile(self, db=None):
return OldParticipant(self.id).get_giving_for_profile(db)
return OldParticipant(self.username).get_giving_for_profile(db)

def get_tips_and_total(self, for_payday=False, db=None):
return OldParticipant(self.id).get_tips_and_total(for_payday, db)
return OldParticipant(self.username).get_tips_and_total(for_payday, db)

def take_over(self, account_elsewhere, have_confirmation=False):
OldParticipant(self.id).take_over(account_elsewhere,
have_confirmation)
OldParticipant(self.username).take_over(account_elsewhere,
have_confirmation)
20 changes: 10 additions & 10 deletions gittip/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from psycopg2 import IntegrityError


ASCII_ALLOWED_IN_PARTICIPANT_ID = set("0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
".,-_;:@ ")
ASCII_ALLOWED_IN_USERNAME = set("0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
".,-_;:@ ")


class NoParticipantId(Exception):
Expand Down Expand Up @@ -68,7 +68,7 @@ def reserve_a_random_username(db=None):

for username in gen_random_usernames():
try:
db.execute( "INSERT INTO participants (id) VALUES (%s)"
db.execute( "INSERT INTO participants (username) VALUES (%s)"
, (username,)
)
except IntegrityError: # Collision, try again with another value.
Expand Down Expand Up @@ -150,7 +150,7 @@ def set_as_claimed(self):


@require_username
def change_id(self, suggested):
def change_username(self, suggested):
"""Raise Response or return None.
We want to be pretty loose with usernames. Unicode is allowed--XXX
Expand All @@ -161,13 +161,13 @@ def change_id(self, suggested):
for i, c in enumerate(suggested):
if i == 32:
raise Response(413) # Request Entity Too Large (more or less)
elif ord(c) < 128 and c not in ASCII_ALLOWED_IN_PARTICIPANT_ID:
elif ord(c) < 128 and c not in ASCII_ALLOWED_IN_USERNAME:
raise Response(400) # Yeah, no.
elif c not in ASCII_ALLOWED_IN_PARTICIPANT_ID:
elif c not in ASCII_ALLOWED_IN_USERNAME:
raise Response(400) # XXX Burned by an Aspen bug. :`-(
# https://github.com/whit537/aspen/issues/102

if suggested in gittip.RESTRICTED_IDS:
if suggested in gittip.RESTRICTED_USERNAMES:
raise Response(400)

if suggested != self.username:
Expand All @@ -178,7 +178,7 @@ def change_id(self, suggested):
(suggested, self.username))

assert rec is not None # sanity check
assert suggested == rec['id'] # sanity check
assert suggested == rec['username'] # sanity check
self.username = suggested


Expand Down
2 changes: 1 addition & 1 deletion gittip/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def populate_db_with_dummy_data(db):
from gittip.participant import Participant
for user_id, login in GITHUB_USERS:
account = GitHubAccount(user_id, {"id": user_id, "login": login})
Participant(account.participant).change_id(login)
Participant(account.participant).change_username(login)


class Harness(unittest.TestCase):
Expand Down
19 changes: 10 additions & 9 deletions tests/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_associate_invalid_card(self, find):
, self.balanced_account_uri
, self.card_uri
)
user = authentication.User.from_id('alice')
user = authentication.User.from_username('alice')
# participant in db should be updated to reflect the error message of
# last update
assert user.last_bill_result == error_message
Expand All @@ -210,7 +210,7 @@ def test_associate_bank_account_valid(self, find):
args, _ = find.return_value.add_bank_account.call_args
assert args == (self.balanced_destination_uri,)

user = authentication.User.from_id('alice')
user = authentication.User.from_username('alice')

# participant in db should be updated
assert user.last_ach_result == ''
Expand All @@ -226,7 +226,7 @@ def test_associate_bank_account_invalid(self, find):
, self.balanced_destination_uri
)

user = authentication.User.from_id('alice')
user = authentication.User.from_username('alice')

# participant in db should be updated
assert user.last_ach_result == 'errrrrror'
Expand All @@ -247,7 +247,7 @@ def test_clear(self, find):
UPDATE participants
SET balanced_account_uri='not null'
, last_bill_result='ooga booga'
WHERE id=%s
WHERE username=%s
"""
gittip.db.execute(MURKY, ('alice',))
Expand All @@ -258,7 +258,7 @@ def test_clear(self, find):
assert valid_card.save.call_count
assert not invalid_card.save.call_count

user = authentication.User.from_id('alice')
user = authentication.User.from_username('alice')
assert not user.last_bill_result
assert user.balanced_account_uri

Expand All @@ -278,7 +278,7 @@ def test_clear_bank_account(self, b_account):
UPDATE participants
SET balanced_account_uri='not null'
, last_ach_result='ooga booga'
WHERE id=%s
WHERE username=%s
"""
gittip.db.execute(MURKY, ('alice',))
Expand All @@ -289,15 +289,16 @@ def test_clear_bank_account(self, b_account):
assert valid_ba.save.call_count
assert not invalid_ba.save.call_count

user = authentication.User.from_id('alice')
user = authentication.User.from_username('alice')
assert not user.last_ach_result
assert user.balanced_account_uri


class TestBillingStoreError(TestBillingBase):
def test_store_error_stores_bill_error(self):
billing.store_error(u"credit card", "alice", "cheese is yummy")
rec = gittip.db.fetchone("select * from participants where id='alice'")
rec = gittip.db.fetchone("select * from participants where "
"username='alice'")
expected = "cheese is yummy"
actual = rec['last_bill_result']
assert actual == expected, actual
Expand All @@ -306,5 +307,5 @@ def test_store_error_stores_ach_error(self):
for message in ['cheese is yummy', 'cheese smells like my vibrams']:
billing.store_error(u"bank account", 'alice', message)
rec = gittip.db.fetchone("select * from participants "
"where id='alice'")
"where username='alice'")
assert rec['last_ach_result'] == message
Loading

0 comments on commit 7894d23

Please sign in to comment.