コード例 #1
0
    def test_commit(self):
        account = Account(Client(api_key, api_secret))
        account.id = 'fakeaccountid'
        transfer = account.load({'transfer': {'id': '1'}}).transfer

        def server_response(request, uri, headers):
            try:
                json.loads(request.body.decode())
            except ValueError:
                raise AssertionError("request body was malformed.")
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.POST, re.compile('.*'), body=server_response)

        with self.assertRaises(APIError):
            data = {'success': False, 'transfer': {'id': '1'}}
            transfer.commit()
        with self.assertRaises(UnexpectedDataFormatError):
            data = {'success': True, 'transfer': 'wrong-type'}
            transfer.commit()
        with self.assertRaises(UnexpectedDataFormatError):
            data = {'success': True, 'missing-transfer-key': True}
            transfer.commit()

        data = {'success': True, 'transfer': {'id': '1'}}
        tx = transfer.commit()
        self.assertIsInstance(tx, Transfer)
コード例 #2
0
ファイル: test_model.py プロジェクト: OspreyX/coinbase-python
  def test_refund(self):
    account = Account(Client(api_key, api_secret))
    account.id = 'fakeaccountid'
    order = account.load({
      'order': {
        'id': '1',
        'custom': 'custom',
        'button': {
          'id': 'fakeid',
          'code': 'acode'
        },
      },
    }).order
    def server_response(request, uri, headers):
      try: req_data = json.loads(request.body.decode())
      except ValueError: raise AssertionError("request body was malformed.")
      order_data = req_data.get('order')
      self.assertIsInstance(order_data, dict)
      return (200, headers, json.dumps(data))
    hp.register_uri(hp.POST, re.compile('.*'), body=server_response)

    with self.assertRaises(UnexpectedDataFormatError):
      data = {'order': 'wrong-type'}
      order.refund('USD')
    with self.assertRaises(UnexpectedDataFormatError):
      data = {'missing-order-key': True}
      order.refund('USD')

    data = {'order': {'id': '1'}}
    refunded = order.refund('USD')
    self.assertEqual(refunded, data['order'])
    self.assertIsInstance(refunded, Order)
コード例 #3
0
ファイル: test_model.py プロジェクト: OspreyX/coinbase-python
  def test_create_order(self):
    account = Account(Client(api_key, api_secret))
    account.id = 'fakeaccountid'
    initial_name = 'name'
    initial_price_string = '12.0'
    initial_price_currency_iso = 'USD'

    button = account.load({
      'button': {
        'id': '1',
        'name': initial_name,
        'price_string': initial_price_string,
        'price_currency_iso': initial_price_currency_iso,
        'code': 'buttoncode',
      },
    }).button

    def server_response(request, uri, headers):
      self.assertEqual(request.body.decode(), '')
      return (200, headers, json.dumps(data))

    hp.register_uri(hp.POST, re.compile('.*'), body=server_response)

    name = 'b-name'
    price_string = 'b-price'
    price_currency_iso = 'BTC'
    with self.assertRaises(APIError):
     data = {
         'success': False,
         'order': {
           'name': name,
           'price_string': price_string,
           'price_currency_iso': price_currency_iso,

         },
       }
     button.create_order()

    with self.assertRaises(UnexpectedDataFormatError):
     data = {'success': True, 'order': 'wrong-type'}
     button.create_order()

    with self.assertRaises(UnexpectedDataFormatError):
     data = {'success': True, 'missing-order-key': True}
     button.create_order()

    data = {
        'success': True,
        'order': {
          'name': name,
          'price_string': price_string,
          'price_currency_iso': price_currency_iso,

        },
      }
    order = button.create_order()
    self.assertIsInstance(order, Order)
コード例 #4
0
    def test_get_orders(self):
        account = Account(Client(api_key, api_secret))
        account.id = 'fakeaccountid'
        initial_name = 'name'
        initial_price_string = '12.0'
        initial_price_currency_iso = 'USD'

        button = account.load({
            'button': {
                'id': '1',
                'name': initial_name,
                'price_string': initial_price_string,
                'price_currency_iso': initial_price_currency_iso,
                'code': 'buttoncode',
            },
        }).button

        def server_response(request, uri, headers):
            try:
                json.loads(request.body.decode())
            except ValueError:
                raise AssertionError("request body was malformed.")
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.GET, re.compile('.*'), body=server_response)

        data = {
            'total_count':
            3,
            'current_page':
            1,
            'num_pages':
            1,
            'orders': [
                {
                    'order': {
                        'id': '1'
                    }
                },
                {
                    'order': {
                        'id': '2'
                    }
                },
                {
                    'order': {
                        'id': '3'
                    }
                },
            ],
        }
        response = button.get_orders()
        self.assertIsInstance(response, APIObject)
        self.assertEqual(len(response.orders), 3)
        for order in response.orders:
            self.assertIsInstance(order, Order)
コード例 #5
0
    def test_create_order(self):
        account = Account(Client(api_key, api_secret))
        account.id = 'fakeaccountid'
        initial_name = 'name'
        initial_price_string = '12.0'
        initial_price_currency_iso = 'USD'

        button = account.load({
            'button': {
                'id': '1',
                'name': initial_name,
                'price_string': initial_price_string,
                'price_currency_iso': initial_price_currency_iso,
                'code': 'buttoncode',
            },
        }).button

        def server_response(request, uri, headers):
            self.assertEqual(request.body.decode(), '')
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.POST, re.compile('.*'), body=server_response)

        name = 'b-name'
        price_string = 'b-price'
        price_currency_iso = 'BTC'
        with self.assertRaises(APIError):
            data = {
                'success': False,
                'order': {
                    'name': name,
                    'price_string': price_string,
                    'price_currency_iso': price_currency_iso,
                },
            }
            button.create_order()

        with self.assertRaises(UnexpectedDataFormatError):
            data = {'success': True, 'order': 'wrong-type'}
            button.create_order()

        with self.assertRaises(UnexpectedDataFormatError):
            data = {'success': True, 'missing-order-key': True}
            button.create_order()

        data = {
            'success': True,
            'order': {
                'name': name,
                'price_string': price_string,
                'price_currency_iso': price_currency_iso,
            },
        }
        order = button.create_order()
        self.assertIsInstance(order, Order)
コード例 #6
0
ファイル: test_model.py プロジェクト: OspreyX/coinbase-python
  def test_resend(self):
    account = Account(Client(api_key, api_secret))
    account.id = 'fakeaccountid'
    transaction = account.load({'transaction': {'id': '1' }}).transaction

    def server_response(request, uri, headers):
      try: json.loads(request.body.decode())
      except ValueError: raise AssertionError("request body was malformed.")
      return (200, headers, json.dumps(data))
    hp.register_uri(hp.PUT, re.compile('.*'), body=server_response)

    with self.assertRaises(APIError):
      data = {'success': False}
      transaction.resend()

    data = {'success': True}
    self.assertTrue(transaction.resend())
コード例 #7
0
    def test_cancel(self):
        account = Account(Client(api_key, api_secret))
        account.id = 'fakeaccountid'
        transaction = account.load({'transaction': {'id': '1'}}).transaction

        def server_response(request, uri, headers):
            try:
                json.loads(request.body.decode())
            except ValueError:
                raise AssertionError("request body was malformed.")
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.DELETE, re.compile('.*'), body=server_response)

        with self.assertRaises(APIError):
            data = {'success': False}
            transaction.cancel()

        data = {'success': True}
        self.assertTrue(transaction.cancel())
コード例 #8
0
ファイル: test_model.py プロジェクト: OspreyX/coinbase-python
  def test_get_orders(self):
    account = Account(Client(api_key, api_secret))
    account.id = 'fakeaccountid'
    initial_name = 'name'
    initial_price_string = '12.0'
    initial_price_currency_iso = 'USD'

    button = account.load({
      'button': {
        'id': '1',
        'name': initial_name,
        'price_string': initial_price_string,
        'price_currency_iso': initial_price_currency_iso,
        'code': 'buttoncode',
      },
    }).button

    def server_response(request, uri, headers):
      try: json.loads(request.body.decode())
      except ValueError: raise AssertionError("request body was malformed.")
      return (200, headers, json.dumps(data))
    hp.register_uri(hp.GET, re.compile('.*'), body=server_response)

    data = {
        'total_count': 3,
        'current_page': 1,
        'num_pages': 1,
        'orders': [
          {'order': {'id': '1'}},
          {'order': {'id': '2'}},
          {'order': {'id': '3'}},
        ],
      }
    response = button.get_orders()
    self.assertIsInstance(response, APIObject)
    self.assertEqual(len(response.orders), 3)
    for order in response.orders:
      self.assertIsInstance(order, Order)
コード例 #9
0
    def test_refund(self):
        account = Account(Client(api_key, api_secret))
        account.id = 'fakeaccountid'
        order = account.load({
            'order': {
                'id': '1',
                'custom': 'custom',
                'button': {
                    'id': 'fakeid',
                    'code': 'acode'
                },
            },
        }).order

        def server_response(request, uri, headers):
            try:
                req_data = json.loads(request.body.decode())
            except ValueError:
                raise AssertionError("request body was malformed.")
            order_data = req_data.get('order')
            self.assertIsInstance(order_data, dict)
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.POST, re.compile('.*'), body=server_response)

        with self.assertRaises(UnexpectedDataFormatError):
            data = {'order': 'wrong-type'}
            order.refund('USD')
        with self.assertRaises(UnexpectedDataFormatError):
            data = {'missing-order-key': True}
            order.refund('USD')

        data = {'order': {'id': '1'}}
        refunded = order.refund('USD')
        self.assertEqual(refunded, data['order'])
        self.assertIsInstance(refunded, Order)
コード例 #10
0
ファイル: test_model.py プロジェクト: OspreyX/coinbase-python
  def test_complete(self):
    account = Account(Client(api_key, api_secret))
    account.id = 'fakeaccountid'
    transaction = account.load({'transaction': {'id': '1' }}).transaction

    def server_response(request, uri, headers):
      try: json.loads(request.body.decode())
      except ValueError: raise AssertionError("request body was malformed.")
      return (200, headers, json.dumps(data))
    hp.register_uri(hp.PUT, re.compile('.*'), body=server_response)

    with self.assertRaises(APIError):
      data = {'success': False, 'transaction': {'id': '1'}}
      transaction.complete()
    with self.assertRaises(UnexpectedDataFormatError):
      data = {'success': True, 'transaction': 'wrong-type'}
      transaction.complete()
    with self.assertRaises(UnexpectedDataFormatError):
      data = {'success': True, 'missing-transaction-key': True}
      transaction.complete()

    data = {'success': True, 'transaction': {'id': '1'}}
    tx = transaction.complete()
    self.assertIsInstance(tx, Transaction)
コード例 #11
0
    def test_modify(self):
        account = Account(Client(api_key, api_secret))
        account.id = 'fakeaccountid'
        initial_native_currency = 'USD',
        initial_time_zone = 'Pacific Time (US & Canada)'
        initial_name = 'Test User'
        user = account.load({
            'user': {
                'id': '1',
                'name': initial_name,
                'native_currency': initial_native_currency,
                'time_zone': initial_time_zone,
            },
        }).user

        with self.assertRaises(ValueError):
            user.modify()

        def server_response(request, uri, headers):
            self.assertTrue(uri.endswith(user.id))
            try:
                request_data = json.loads(request.body.decode())
            except ValueError:
                raise AssertionError("request body was malformed.")
            user_data = request_data.get('user')
            self.assertIsInstance(user_data, dict)
            return (200, headers, json.dumps(data))

        hp.register_uri(hp.PUT, re.compile('.*'), body=server_response)

        with self.assertRaises(APIError):
            new_name = 'Fake Name'
            data = {
                'success': False,
                'user': {
                    'id': user.id,
                    'name': new_name,
                    'native_currency': initial_native_currency,
                    'time_zone': initial_time_zone,
                },
            }
            user.modify(name=new_name)
        self.assertEqual(user.name, initial_name)
        self.assertEqual(user.native_currency, initial_native_currency)
        self.assertEqual(user.time_zone, initial_time_zone)

        with self.assertRaises(UnexpectedDataFormatError):
            new_name = 'Fake Name'
            data = {'success': True, 'user': '******'}
            user.modify(name=new_name)
        self.assertEqual(user.name, initial_name)
        self.assertEqual(user.native_currency, initial_native_currency)
        self.assertEqual(user.time_zone, initial_time_zone)

        with self.assertRaises(UnexpectedDataFormatError):
            new_name = 'Fake Name'
            data = {'success': True, 'missing-user-key': True}
            user.modify(name=new_name)
        self.assertEqual(user.name, initial_name)
        self.assertEqual(user.native_currency, initial_native_currency)
        self.assertEqual(user.time_zone, initial_time_zone)

        new_name = 'Fake Name'
        new_native_currency = 'CAD'
        new_time_zone = 'Eastern'
        data = {
            'success': True,
            'user': {
                'id': user.id,
                'name': new_name,
                'native_currency': new_native_currency,
                'time_zone': new_time_zone,
            },
        }
        user.modify(name=new_name,
                    time_zone=new_time_zone,
                    native_currency=new_native_currency)
        self.assertEqual(user.name, new_name)
        self.assertEqual(user.native_currency, new_native_currency)
        self.assertEqual(user.time_zone, new_time_zone)
コード例 #12
0
ファイル: test_model.py プロジェクト: OspreyX/coinbase-python
  def test_modify(self):
    account = Account(Client(api_key, api_secret))
    account.id = 'fakeaccountid'
    initial_native_currency = 'USD',
    initial_time_zone = 'Pacific Time (US & Canada)'
    initial_name = 'Test User'
    user = account.load({
      'user': {
        'id': '1',
        'name': initial_name,
        'native_currency': initial_native_currency,
        'time_zone': initial_time_zone,
      },
    }).user

    with self.assertRaises(ValueError):
      user.modify()

    def server_response(request, uri, headers):
      self.assertTrue(uri.endswith(user.id))
      try: request_data = json.loads(request.body.decode())
      except ValueError: raise AssertionError("request body was malformed.")
      user_data = request_data.get('user')
      self.assertIsInstance(user_data, dict)
      return (200, headers, json.dumps(data))
    hp.register_uri(hp.PUT, re.compile('.*'), body=server_response)

    with self.assertRaises(APIError):
      new_name = 'Fake Name'
      data = {
          'success': False,
          'user': {
            'id': user.id,
            'name': new_name,
            'native_currency': initial_native_currency,
            'time_zone': initial_time_zone,
          },
        }
      user.modify(name=new_name)
    self.assertEqual(user.name, initial_name)
    self.assertEqual(user.native_currency, initial_native_currency)
    self.assertEqual(user.time_zone, initial_time_zone)

    with self.assertRaises(UnexpectedDataFormatError):
      new_name = 'Fake Name'
      data = {'success': True, 'user': '******'}
      user.modify(name=new_name)
    self.assertEqual(user.name, initial_name)
    self.assertEqual(user.native_currency, initial_native_currency)
    self.assertEqual(user.time_zone, initial_time_zone)

    with self.assertRaises(UnexpectedDataFormatError):
      new_name = 'Fake Name'
      data = {'success': True, 'missing-user-key': True}
      user.modify(name=new_name)
    self.assertEqual(user.name, initial_name)
    self.assertEqual(user.native_currency, initial_native_currency)
    self.assertEqual(user.time_zone, initial_time_zone)

    new_name = 'Fake Name'
    new_native_currency = 'CAD'
    new_time_zone = 'Eastern'
    data = {
        'success': True,
        'user': {
          'id': user.id,
          'name': new_name,
          'native_currency': new_native_currency,
          'time_zone': new_time_zone,
        },
      }
    user.modify(name=new_name,
                time_zone=new_time_zone,
                native_currency=new_native_currency)
    self.assertEqual(user.name, new_name)
    self.assertEqual(user.native_currency, new_native_currency)
    self.assertEqual(user.time_zone, new_time_zone)