<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , unicode_literals <EOL> import itertools <EOL> import unittest <EOL> from os . path import dirname , join , realpath <EOL> from aspen import resources , Response <EOL> from aspen . utils import utcnow <EOL> from aspen . testing . client import Client <EOL> from psycopg2 import IntegrityError , InternalError <EOL> from liberapay . billing import exchanges <EOL> from liberapay . billing . exchanges import ( <EOL> record_exchange , record_exchange_result , _record_transfer_result <EOL> ) <EOL> from liberapay . constants import SESSION <EOL> from liberapay . elsewhere . _base import UserInfo <EOL> from liberapay . main import website <EOL> from liberapay . models . account_elsewhere import AccountElsewhere <EOL> from liberapay . models . exchange_route import ExchangeRoute <EOL> from liberapay . models . participant import Participant <EOL> from liberapay . testing . vcr import use_cassette <EOL> TOP = realpath ( join ( dirname ( dirname ( __file__ ) ) , '<STR_LIT:..>' ) ) <EOL> WWW_ROOT = str ( realpath ( join ( TOP , '<STR_LIT>' ) ) ) <EOL> PROJECT_ROOT = str ( TOP ) <EOL> class ClientWithAuth ( Client ) : <EOL> def __init__ ( self , * a , ** kw ) : <EOL> Client . __init__ ( self , * a , ** kw ) <EOL> Client . website = website <EOL> def build_wsgi_environ ( self , * a , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . cookie . clear ( ) <EOL> csrf_token = kw . get ( '<STR_LIT>' , b'<STR_LIT>' ) <EOL> if csrf_token : <EOL> self . cookie [ b'<STR_LIT>' ] = csrf_token <EOL> kw [ b'<STR_LIT>' ] = csrf_token <EOL> auth_as = kw . pop ( '<STR_LIT>' , None ) <EOL> if auth_as : <EOL> assert auth_as . session_token <EOL> self . cookie [ SESSION ] = '<STR_LIT>' % ( auth_as . id , auth_as . session_token ) <EOL> for k , v in kw . pop ( '<STR_LIT>' , { } ) . items ( ) : <EOL> self . cookie [ k ] = v <EOL> return Client . build_wsgi_environ ( self , * a , ** kw ) <EOL> def hit ( self , * a , ** kw ) : <EOL> if kw . pop ( '<STR_LIT>' , False ) : <EOL> kw [ b'<STR_LIT>' ] = b'<STR_LIT>' <EOL> if not kw . pop ( '<STR_LIT>' , True ) : <EOL> env = self . website . env <EOL> old_reraise , env . sentry_reraise = env . sentry_reraise , False <EOL> try : <EOL> return super ( ClientWithAuth , self ) . hit ( * a , ** kw ) <EOL> finally : <EOL> env . sentry_reraise = old_reraise <EOL> return super ( ClientWithAuth , self ) . hit ( * a , ** kw ) <EOL> def decode_body ( self ) : <EOL> body = self . body <EOL> return body . decode ( self . charset ) if isinstance ( body , bytes ) else body <EOL> Response . text = property ( decode_body ) <EOL> class Harness ( unittest . TestCase ) : <EOL> QUARANTINE = exchanges . QUARANTINE <EOL> client = ClientWithAuth ( www_root = WWW_ROOT , project_root = PROJECT_ROOT ) <EOL> db = client . website . db <EOL> platforms = client . website . platforms <EOL> website = client . website <EOL> tablenames = db . all ( """<STR_LIT>""" ) <EOL> seq = itertools . count ( <NUM_LIT:0> ) <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls_name = cls . __name__ <EOL> if cls_name [ <NUM_LIT:4> : <NUM_LIT:5> ] . isupper ( ) : <EOL> cls_id = sum ( ord ( c ) - <NUM_LIT> << ( i * <NUM_LIT:5> ) for i , c in enumerate ( cls_name [ <NUM_LIT:4> : <NUM_LIT:10> ] . lower ( ) ) ) <EOL> else : <EOL> cls_id = <NUM_LIT:1> <EOL> cls . db . run ( "<STR_LIT>" , ( cls_id , ) ) <EOL> cls . db . run ( "<STR_LIT>" , ( cls_id , ) ) <EOL> cls . setUpVCR ( ) <EOL> exchanges . QUARANTINE = '<STR_LIT>' <EOL> @ classmethod <EOL> def setUpVCR ( cls ) : <EOL> """<STR_LIT>""" <EOL> cls . vcr_cassette = use_cassette ( cls . __name__ ) <EOL> cls . vcr_cassette . __enter__ ( ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . vcr_cassette . __exit__ ( None , None , None ) <EOL> exchanges . QUARANTINE = cls . QUARANTINE <EOL> def setUp ( self ) : <EOL> self . clear_tables ( ) <EOL> def tearDown ( self ) : <EOL> resources . __cache__ = { } <EOL> self . clear_tables ( ) <EOL> def clear_tables ( self ) : <EOL> tablenames = self . tablenames [ : ] <EOL> while tablenames : <EOL> tablename = tablenames . pop ( ) <EOL> try : <EOL> self . db . run ( "<STR_LIT>" % tablename ) <EOL> except ( IntegrityError , InternalError ) : <EOL> if not tablenames : <EOL> raise <EOL> tablenames . insert ( <NUM_LIT:0> , tablename ) <EOL> self . tablenames . remove ( tablename ) <EOL> self . tablenames . insert ( <NUM_LIT:0> , tablename ) <EOL> self . db . run ( "<STR_LIT>" ) <EOL> self . db . run ( "<STR_LIT>" ) <EOL> def make_elsewhere ( self , platform , user_id , user_name , ** kw ) : <EOL> info = UserInfo ( platform = platform <EOL> , user_id = str ( user_id ) <EOL> , user_name = user_name <EOL> , ** kw <EOL> ) <EOL> return AccountElsewhere . upsert ( info ) <EOL> def make_participant ( self , username , ** kw ) : <EOL> platform = kw . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> kw2 = { } <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if key in kw : <EOL> kw2 [ key ] = kw . pop ( key ) <EOL> kind = kw . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if kind not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> kw . setdefault ( '<STR_LIT:password>' , '<STR_LIT:x>' ) <EOL> kw . setdefault ( '<STR_LIT>' , username ) <EOL> i = next ( self . seq ) <EOL> kw . setdefault ( '<STR_LIT>' , - i ) <EOL> kw . setdefault ( '<STR_LIT>' , - i ) <EOL> kw . setdefault ( '<STR_LIT:status>' , '<STR_LIT>' ) <EOL> if not '<STR_LIT>' in kw : <EOL> kw [ '<STR_LIT>' ] = utcnow ( ) <EOL> cols , vals = zip ( * kw . items ( ) ) <EOL> cols = '<STR_LIT:U+002CU+0020>' . join ( cols ) <EOL> placeholders = '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT:%s>' ] * len ( vals ) ) <EOL> participant = self . db . one ( """<STR_LIT>""" . format ( cols , placeholders ) , ( username , ) + vals ) <EOL> self . db . run ( """<STR_LIT>""" , ( platform , participant . id , username , participant . id ) ) <EOL> if '<STR_LIT>' in kw2 : <EOL> ExchangeRoute . insert ( participant , '<STR_LIT>' , '<STR_LIT>' , kw2 [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in kw2 and kw2 [ '<STR_LIT>' ] != <NUM_LIT:0> : <EOL> self . make_exchange ( '<STR_LIT>' , kw2 [ '<STR_LIT>' ] , <NUM_LIT:0> , participant ) <EOL> return participant <EOL> def make_stub ( self , ** kw ) : <EOL> return Participant . make_stub ( ** kw ) <EOL> def fetch_payday ( self ) : <EOL> return self . db . one ( "<STR_LIT>" , back_as = dict ) <EOL> def make_exchange ( self , route , amount , fee , participant , status = '<STR_LIT>' , error = '<STR_LIT>' , vat = <NUM_LIT:0> ) : <EOL> if not isinstance ( route , ExchangeRoute ) : <EOL> network = route <EOL> route = ExchangeRoute . from_network ( participant , network ) <EOL> if not route : <EOL> from . mangopay import MangopayHarness <EOL> route = ExchangeRoute . insert ( participant , network , MangopayHarness . card_id ) <EOL> assert route <EOL> e_id = record_exchange ( self . db , route , amount , fee , vat , participant , '<STR_LIT>' ) <EOL> record_exchange_result ( self . db , e_id , status , error , participant ) <EOL> return e_id <EOL> def make_transfer ( self , tipper , tippee , amount , context = '<STR_LIT>' , team = None , status = '<STR_LIT>' ) : <EOL> t_id = self . db . one ( """<STR_LIT>""" , ( tipper , tippee , amount , context , team ) ) <EOL> _record_transfer_result ( self . db , t_id , status ) <EOL> return t_id <EOL> class Foobar ( Exception ) : pass </s>
