def test_logout(self, retrying):
     ig_service = IGService(config.username, config.password,
                            config.api_key, config.acc_type, retryer=retrying)
     ig_service.create_session()
     ig_service.logout()
     with pytest.raises(Exception) as error:
         print(error)
         ig_service.fetch_accounts()
Example #2
0
    def test_fetch_accounts_happy(self):

        with open('tests/data/accounts_balances.json', 'r') as file:
            response_body = json.loads(file.read())

        responses.add(responses.GET,
                      'https://demo-api.ig.com/gateway/deal/accounts',
                      headers={
                          'CST': 'abc123',
                          'X-SECURITY-TOKEN': 'xyz987'
                      },
                      json=response_body,
                      status=200)

        ig_service = IGService('username', 'password', 'api_key', 'DEMO')
        ig_service.crud_session.HEADERS["LOGGED_IN"] = {}
        result = ig_service.fetch_accounts()

        pd.set_option('display.max_columns', 13)
        print(result)

        assert isinstance(result, pd.DataFrame)
        assert result.iloc[0]['accountId'] == 'XYZ987'
        assert result.iloc[0]['balance'] == 1000.0
        assert result.iloc[1]['accountId'] == 'ABC123'
        assert pd.isna(result.iloc[1]['balance'])
        assert pd.isna(result.iloc[1]['deposit'])
 def test_create_session_v3(self, retrying):
     ig_service = IGService(config.username, config.password, config.api_key, config.acc_type,
                            acc_number=config.acc_number, retryer=retrying)
     ig_service.create_session(version='3')
     assert 'X-IG-API-KEY' in ig_service.session.headers
     assert 'Authorization' in ig_service.session.headers
     assert 'IG-ACCOUNT-ID' in ig_service.session.headers
     assert len(ig_service.fetch_accounts()) == 2
    def test_retry(self):

        with open('tests/data/accounts_balances.json', 'r') as file:
            response_body = json.loads(file.read())

        url = 'https://demo-api.ig.com/gateway/deal/accounts'
        headers = {'CST': 'abc123', 'X-SECURITY-TOKEN': 'xyz987'}
        api_exceeded = {
            'errorCode': 'error.public-api.exceeded-api-key-allowance'
        }
        account_exceeded = {
            'errorCode': 'error.public-api.exceeded-account-allowance'
        }
        trading_exceeded = {
            'errorCode': 'error.public-api.exceeded-account-trading-allowance'
        }

        responses.add(
            Response(method='GET',
                     url=url,
                     headers={},
                     json=api_exceeded,
                     status=403))
        responses.add(
            Response(method='GET',
                     url=url,
                     headers={},
                     json=account_exceeded,
                     status=403))
        responses.add(
            Response(method='GET',
                     url=url,
                     headers={},
                     json=trading_exceeded,
                     status=403))
        responses.add(
            Response(method='GET',
                     url=url,
                     headers=headers,
                     json=response_body,
                     status=200))

        ig_service = IGService(
            'username',
            'password',
            'api_key',
            'DEMO',
            retryer=Retrying(
                wait=tenacity.wait_exponential(),
                retry=tenacity.retry_if_exception_type(ApiExceededException)))

        result = ig_service.fetch_accounts()

        assert responses.assert_call_count(url, 4) is True
        assert isinstance(result, pd.DataFrame)
        assert result.iloc[0]['accountId'] == 'XYZ987'
 def test_session_v3_refresh(self, retrying):
     """
     Tests refresh capability of v3 sessions. It makes repeated calls to the 'fetch_accounts'
     endpoint, with random sleep times in between, to show/test the different scenarios. Will take
     a long time to run
     """
     ig_service = IGService(config.username, config.password, config.api_key, config.acc_type,
                            acc_number=config.acc_number, retryer=retrying)
     ig_service.create_session(version='3')
     delay_choice = [(1, 59), (60, 650)]
     for count in range(1, 20):
         data = ig_service.fetch_accounts()
         logging.info(f"Account count: {len(data)}")
         option = choice(delay_choice)
         wait = randint(option[0], option[1])
         logging.info(f"Waiting for {wait} seconds...")
         time.sleep(wait)
 def test_fetch_accounts(self, ig_service: IGService):
     response = ig_service.fetch_accounts()
     preferred = response.loc[response["preferred"]]
     assert all(preferred["balance"] > 0)