Example #1
0
 def test_get_addresses(self):
     client = Client(api_key, api_secret)
     addresses = client.get_addresses('foo')
     self.assertIsInstance(addresses, APIObject)
     self.assertEqual(addresses.data, mock_collection)
     for address in addresses.data:
         self.assertIsInstance(address, Address)
Example #2
0
 def test_get_transactions(self):
     client = Client(api_key, api_secret)
     transactions = client.get_transactions('foo')
     self.assertIsInstance(transactions, APIObject)
     self.assertEqual(transactions.data, mock_collection)
     for transaction in transactions.data:
         self.assertIsInstance(transaction, Transaction)
Example #3
0
 def test_get_sells(self):
     client = Client(api_key, api_secret)
     sells = client.get_sells('foo')
     self.assertIsInstance(sells, APIObject)
     self.assertEqual(sells.data, mock_collection)
     for sell in sells.data:
         self.assertIsInstance(sell, Sell)
Example #4
0
 def test_get_accounts(self):
     client = Client(api_key, api_secret)
     accounts = client.get_accounts()
     self.assertIsInstance(accounts, APIObject)
     self.assertEqual(accounts.data, mock_collection)
     for account in accounts.data:
         self.assertIsInstance(account, Account)
Example #5
0
 def test_get_withdrawals(self):
     client = Client(api_key, api_secret)
     withdrawals = client.get_withdrawals('foo')
     self.assertIsInstance(withdrawals, APIObject)
     self.assertEqual(withdrawals.data, mock_collection)
     for withdrawal in withdrawals.data:
         self.assertIsInstance(withdrawal, Withdrawal)
Example #6
0
 def test_create_report(self):
     client = Client(api_key, api_secret)
     report = client.create_report(email='*****@*****.**',
                                   type='transactions')
     self.assertIsInstance(report, APIObject)
     self.assertIsInstance(report, Report)
     self.assertEqual(report, mock_item)
Example #7
0
 def test_get_deposits(self):
     client = Client(api_key, api_secret)
     deposits = client.get_deposits('foo')
     self.assertIsInstance(deposits, APIObject)
     self.assertEqual(deposits.data, mock_collection)
     for deposit in deposits.data:
         self.assertIsInstance(deposit, Deposit)
Example #8
0
 def test_get_payment_methods(self):
     client = Client(api_key, api_secret)
     payment_methods = client.get_payment_methods()
     self.assertIsInstance(payment_methods, APIObject)
     self.assertEqual(payment_methods.data, mock_collection)
     for payment_method in payment_methods.data:
         self.assertIsInstance(payment_method, PaymentMethod)
Example #9
0
 def test_get_checkouts(self):
     client = Client(api_key, api_secret)
     checkouts = client.get_checkouts()
     self.assertIsInstance(checkouts, APIObject)
     self.assertEqual(checkouts.data, mock_collection)
     for checkout in checkouts.data:
         self.assertIsInstance(checkout, Checkout)
Example #10
0
 def test_get_checkout_orders(self):
     client = Client(api_key, api_secret)
     orders = client.get_checkout_orders('foo')
     self.assertIsInstance(orders, APIObject)
     self.assertEqual(orders.data, mock_collection)
     for order in orders.data:
         self.assertIsInstance(order, Order)
Example #11
0
 def test_get_reports(self):
     client = Client(api_key, api_secret)
     reports = client.get_reports()
     self.assertIsInstance(reports, APIObject)
     self.assertEqual(reports.data, mock_collection)
     for report in reports.data:
         self.assertIsInstance(report, Report)
Example #12
0
 def test_get_buys(self):
     client = Client(api_key, api_secret)
     buys = client.get_buys('foo')
     self.assertIsInstance(buys, APIObject)
     self.assertEqual(buys.data, mock_collection)
     for buy in buys.data:
         self.assertIsInstance(buy, Buy)
Example #13
0
    def test_request_helper_automatically_encodes_data(self):
        client = Client(api_key, api_secret)

        def server_response(request, uri, headers):
            self.assertIsInstance(request.body, six.binary_type)
            return 200, headers, '{}'

        hp.register_uri(hp.POST, re.compile('.*foo$'), server_response)
        self.assertEqual(
            client._post('foo', data={
                'name': 'example'
            }).status_code, 200)
Example #14
0
 def test_sell(self):
     client = Client(api_key, api_secret)
     with self.assertRaises(ValueError):
         client.sell('foo')
     for valid_kwargs in [{
             'amount': '1.0',
             'currency': 'USD'
     }, {
             'total': '1.0',
             'currency': 'USD'
     }]:
         sell = client.sell('foo', **valid_kwargs)
         self.assertIsInstance(sell, Sell)
         self.assertEqual(sell, mock_item)
Example #15
0
    def test_response_handling(self):
        client = Client(api_key, api_secret)
        # Check that 2XX responses always return the response
        error_response = {
            'errors': [{
                'id': 'fakeid',
                'message': 'some error message',
            }],
            'data': mock_item,
        }
        error_str = json.dumps(error_response)
        for code in [200, 201, 204]:
            hp.register_uri(hp.GET, re.compile('.*' + str(code) + '$'),
                            lambda r, u, h: (code, h, error_str))
            response = client._get(str(code))
            self.assertEqual(response.status_code, code)

        # Check that when the error data is in the response, that's what is used.
        import pycoinbase.wallet.error
        for eid, eclass in six.iteritems(
                pycoinbase.wallet.error._error_id_to_class):
            error_response = {
                'errors': [{
                    'id': eid,
                    'message': 'some message',
                }],
                'data': mock_item,
            }
            error_str = json.dumps(error_response)
            hp.reset()
            hp.register_uri(hp.GET, re.compile('.*test$'), lambda r, u, h:
                            (400, h, error_str))
            with self.assertRaises(eclass):
                client._get('test')

        # Check that when the error data is missing, the status code is used
        # instead.
        error_response = {'data': mock_item}
        for code, eclass in six.iteritems(
                pycoinbase.wallet.error._status_code_to_class):
            hp.reset()
            hp.register_uri(
                hp.GET, re.compile('.*test$'), lambda r, u, h:
                (code, h, json.dumps(error_response)))
            with self.assertRaises(eclass):
                client._get('test')

        # Check that when the response code / error id is unrecognized, a generic
        # APIError is returned
        hp.reset()
        hp.register_uri(hp.GET, re.compile('.*test$'), lambda r, u, h:
                        (418, h, '{}'))
        with self.assertRaises(APIError):
            client._get('test')
Example #16
0
    def test_request_includes_auth_headers(self):
        client = Client(api_key, api_secret)

        def server_response(request, uri, response_headers):
            keys = [
                'CB-VERSION', 'CB-ACCESS-KEY', 'CB-ACCESS-SIGN',
                'CB-ACCESS-TIMESTAMP', 'Accept', 'Content-Type', 'User-Agent'
            ]
            for key in keys:
                self.assertIn(key, request.headers)
                self.assertNotEqual(request.headers[key], '')
            return 200, response_headers, '{}'

        hp.register_uri(hp.GET, re.compile('.*test$'), server_response)
        self.assertEqual(client._get('test').status_code, 200)
Example #17
0
 def test_set_primary(self):
     client = Client(api_key, api_secret)
     account = new_api_object(client, mock_account, Account)
     data = account.set_primary()
     self.assertEqual(data, mock_account_updated)
     for key, value in six.iteritems(mock_account_updated):
         self.assertEqual(account[key], value)
Example #18
0
 def test_modify(self):
     client = Client(api_key, api_secret)
     account = new_api_object(client, mock_account, Account)
     data = account.modify(name='New Account Name')
     self.assertEqual(data, mock_account_updated)
     for key, value in six.iteritems(mock_account_updated):
         self.assertEqual(account[key], value)
Example #19
0
 def test_create_report(self):
     client = Client(api_key, api_secret)
     account = new_api_object(client, mock_account, Account)
     report = account.create_report(type='transactions', email='*****@*****.**')
     self.assertIsInstance(report, APIObject)
     self.assertIsInstance(report, Report)
     self.assertEqual(report, mock_item)
Example #20
0
 def test_request_money(self):
     client = Client(api_key, api_secret)
     # Start with none of the required arguments, and slowly make requests with
     # an additional required argument, expecting failure until all arguments
     # are present.
     send_kwargs = {}
     required_kwargs = {'to': 'bar', 'amount': '1.0', 'currency': 'USD'}
     while required_kwargs:
         with self.assertRaises(ValueError):
             transaction = client.request_money('foo', **send_kwargs)
         for key in required_kwargs:
             send_kwargs[key] = required_kwargs.pop(key)
             break
     transaction = client.request_money('foo', **send_kwargs)
     self.assertIsInstance(transaction, Transaction)
     self.assertEqual(transaction, mock_item)
Example #21
0
 def test_get_address(self):
     client = Client(api_key, api_secret)
     account = new_api_object(client, mock_account, Account)
     address = account.get_address('bar')
     self.assertIsInstance(address, Address)
     self.assertEqual(address, mock_item)
     pass
Example #22
0
 def test_create_checkout(self):
     client = Client(api_key, api_secret)
     # Start with none of the required arguments, and slowly make requests with
     # an additional required argument, expecting failure until all arguments
     # are present.
     send_kwargs = {}
     required_kwargs = {'name': 'bar', 'amount': '1.0', 'currency': 'USD'}
     while required_kwargs:
         with self.assertRaises(ValueError):
             client.create_checkout(**send_kwargs)
         for key in required_kwargs:
             send_kwargs[key] = required_kwargs.pop(key)
             break
     checkout = client.create_checkout(**send_kwargs)
     self.assertIsInstance(checkout, Checkout)
     self.assertEqual(checkout, mock_item)
Example #23
0
 def test_refund_order(self):
     client = Client(api_key, api_secret)
     # Start with none of the required arguments, and slowly make requests with
     # an additional required argument, expecting failure until all arguments
     # are present.
     send_kwargs = {}
     required_kwargs = {'currency': 'USD'}
     while required_kwargs:
         with self.assertRaises(ValueError):
             client.refund_order('foo', **send_kwargs)
         for key in required_kwargs:
             send_kwargs[key] = required_kwargs.pop(key)
             break
     order = client.refund_order('foo', **send_kwargs)
     self.assertIsInstance(order, Order)
     self.assertEqual(order, mock_item)
Example #24
0
 def test_get_withdrawals(self):
     client = Client(api_key, api_secret)
     account = new_api_object(client, mock_account, Account)
     withdrawals = account.get_withdrawals()
     self.assertIsInstance(withdrawals, APIObject)
     self.assertEqual(withdrawals.data, mock_collection)
     for withdrawal in withdrawals.data:
         self.assertIsInstance(withdrawal, Withdrawal)
Example #25
0
 def test_get_orders(self):
     client = Client(api_key, api_secret)
     checkout = new_api_object(client, mock_checkout, Checkout)
     orders = checkout.get_orders()
     self.assertIsInstance(orders, APIObject)
     self.assertEqual(orders.data, mock_collection)
     for order in orders.data:
         self.assertIsInstance(order, Order)
Example #26
0
 def test_commit(self):
     client = Client(api_key, api_secret)
     buy = new_api_object(client, mock_buy, Buy)
     buy2 = buy.commit()
     self.assertIsInstance(buy2, Buy)
     self.assertEqual(buy2, mock_buy_updated)
     for key, value in six.iteritems(mock_buy_updated):
         self.assertEqual(buy[key], value)
Example #27
0
 def test_commit(self):
     client = Client(api_key, api_secret)
     sell = new_api_object(client, mock_sell, Sell)
     sell2 = sell.commit()
     self.assertIsInstance(sell2, Sell)
     self.assertEqual(sell2, mock_sell_updated)
     for key, value in six.iteritems(mock_sell_updated):
         self.assertEqual(sell[key], value)
Example #28
0
 def test_commit(self):
     client = Client(api_key, api_secret)
     deposit = new_api_object(client, mock_deposit, Deposit)
     deposit2 = deposit.commit()
     self.assertIsInstance(deposit2, Deposit)
     self.assertEqual(deposit2, mock_deposit_updated)
     for key, value in six.iteritems(mock_deposit_updated):
         self.assertEqual(deposit[key], value)
Example #29
0
 def test_modify(self):
     client = Client(api_key, api_secret)
     user = new_api_object(client, mock_item, CurrentUser)
     user2 = user.modify(name='New Name')
     self.assertIsInstance(user2, CurrentUser)
     self.assertEqual(user2, mock_item_updated)
     for key, value in six.iteritems(mock_item_updated):
         self.assertEqual(user[key], value)
Example #30
0
 def test_buy(self):
     client = Client(api_key, api_secret)
     with self.assertRaises(ValueError):
         client.buy('foo')
     kwargs_list = [{
         'amount': '1.0',
         'payment_method': 'bar',
         'currency': 'USD'
     }, {
         'total': '1.0',
         'payment_method': 'bar',
         'currency': 'USD'
     }]
     for valid_kwargs in kwargs_list:
         buy = client.buy('foo', **valid_kwargs)
         self.assertIsInstance(buy, Buy)
         self.assertEqual(buy, mock_item)