freqtrade_origin/tests/rpc/test_rpc_apiserver.py

690 lines
24 KiB
Python
Raw Normal View History

2019-04-04 05:12:58 +00:00
"""
Unit test file for rpc/api_server.py
"""
2019-05-11 11:31:48 +00:00
from datetime import datetime
2019-05-11 12:05:25 +00:00
from unittest.mock import ANY, MagicMock, PropertyMock
2019-04-04 05:12:58 +00:00
2019-05-10 05:07:38 +00:00
import pytest
from flask import Flask
from requests.auth import _basic_auth_str
2019-05-10 05:07:38 +00:00
2019-05-11 07:44:39 +00:00
from freqtrade.__init__ import __version__
2019-05-14 05:07:51 +00:00
from freqtrade.persistence import Trade
from freqtrade.rpc.api_server import BASE_URI, ApiServer
2019-04-04 05:12:58 +00:00
from freqtrade.state import State
2020-04-07 17:50:13 +00:00
from tests.conftest import get_patched_freqtradebot, log_has, patch_get_signal, create_mock_trades
2019-04-04 05:12:58 +00:00
2019-05-25 12:13:59 +00:00
_TEST_USER = "FreqTrader"
_TEST_PASS = "SuperSecurePassword1!"
2019-05-10 05:07:38 +00:00
@pytest.fixture
2019-05-11 06:55:21 +00:00
def botclient(default_conf, mocker):
2019-05-11 11:31:48 +00:00
default_conf.update({"api_server": {"enabled": True,
2019-05-11 11:40:30 +00:00
"listen_ip_address": "127.0.0.1",
2019-11-23 14:20:53 +00:00
"listen_port": 8080,
2019-05-25 12:13:59 +00:00
"username": _TEST_USER,
"password": _TEST_PASS,
}})
2019-05-11 06:55:21 +00:00
ftbot = get_patched_freqtradebot(mocker, default_conf)
2019-05-11 07:44:39 +00:00
mocker.patch('freqtrade.rpc.api_server.ApiServer.run', MagicMock())
2019-05-11 06:55:21 +00:00
apiserver = ApiServer(ftbot)
yield ftbot, apiserver.app.test_client()
2019-05-10 05:07:38 +00:00
# Cleanup ... ?
2019-05-25 12:13:59 +00:00
def client_post(client, url, data={}):
return client.post(url,
content_type="application/json",
data=data,
2020-05-20 17:43:52 +00:00
headers={'Authorization': _basic_auth_str(_TEST_USER, _TEST_PASS),
'Origin': 'example.com'})
2019-05-25 12:13:59 +00:00
def client_get(client, url):
2020-05-20 17:43:52 +00:00
# Add fake Origin to ensure CORS kicks in
return client.get(url, headers={'Authorization': _basic_auth_str(_TEST_USER, _TEST_PASS),
'Origin': 'example.com'})
2019-05-25 12:13:59 +00:00
2020-05-20 17:43:52 +00:00
def assert_response(response, expected_code=200, needs_cors=True):
2019-05-11 12:05:25 +00:00
assert response.status_code == expected_code
2019-05-10 05:07:38 +00:00
assert response.content_type == "application/json"
2020-05-20 17:43:52 +00:00
if needs_cors:
assert ('Access-Control-Allow-Credentials', 'true') in response.headers._list
2019-05-10 05:07:38 +00:00
2019-05-11 11:18:11 +00:00
def test_api_not_found(botclient):
ftbot, client = botclient
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/invalid_url")
2019-05-11 12:05:25 +00:00
assert_response(rc, 404)
2019-05-15 05:12:33 +00:00
assert rc.json == {"status": "error",
"reason": f"There's no API call for http://localhost{BASE_URI}/invalid_url.",
"code": 404
2019-05-11 11:18:11 +00:00
}
2019-05-25 12:13:59 +00:00
def test_api_unauthorized(botclient):
ftbot, client = botclient
2019-11-11 19:09:58 +00:00
rc = client.get(f"{BASE_URI}/ping")
2020-05-20 17:43:52 +00:00
assert_response(rc, needs_cors=False)
2019-11-11 19:09:58 +00:00
assert rc.json == {'status': 'pong'}
2019-05-25 12:13:59 +00:00
# Don't send user/pass information
rc = client.get(f"{BASE_URI}/version")
2020-05-20 17:43:52 +00:00
assert_response(rc, 401, needs_cors=False)
2019-05-25 12:13:59 +00:00
assert rc.json == {'error': 'Unauthorized'}
# Change only username
ftbot.config['api_server']['username'] = "Ftrader"
rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401)
assert rc.json == {'error': 'Unauthorized'}
# Change only password
ftbot.config['api_server']['username'] = _TEST_USER
ftbot.config['api_server']['password'] = "WrongPassword"
rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401)
assert rc.json == {'error': 'Unauthorized'}
ftbot.config['api_server']['username'] = "Ftrader"
ftbot.config['api_server']['password'] = "WrongPassword"
rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401)
assert rc.json == {'error': 'Unauthorized'}
2020-05-10 08:43:13 +00:00
def test_api_token_login(botclient):
ftbot, client = botclient
rc = client_post(client, f"{BASE_URI}/token/login")
assert_response(rc)
assert 'access_token' in rc.json
assert 'refresh_token' in rc.json
# test Authentication is working with JWT tokens too
rc = client.get(f"{BASE_URI}/count",
content_type="application/json",
2020-05-20 17:43:52 +00:00
headers={'Authorization': f'Bearer {rc.json["access_token"]}',
'Origin': 'example.com'})
2020-05-10 08:43:13 +00:00
assert_response(rc)
def test_api_token_refresh(botclient):
ftbot, client = botclient
rc = client_post(client, f"{BASE_URI}/token/login")
assert_response(rc)
rc = client.post(f"{BASE_URI}/token/refresh",
content_type="application/json",
data=None,
2020-05-20 17:43:52 +00:00
headers={'Authorization': f'Bearer {rc.json["refresh_token"]}',
'Origin': 'example.com'})
2020-05-10 08:43:13 +00:00
assert_response(rc)
assert 'access_token' in rc.json
assert 'refresh_token' not in rc.json
2019-05-11 06:55:21 +00:00
def test_api_stop_workflow(botclient):
ftbot, client = botclient
assert ftbot.state == State.RUNNING
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/stop")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'stopping trader ...'}
assert ftbot.state == State.STOPPED
2019-05-10 05:07:38 +00:00
2019-05-11 06:55:21 +00:00
# Stop bot again
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/stop")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'already stopped'}
# Start bot
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/start")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'starting trader ...'}
assert ftbot.state == State.RUNNING
# Call start again
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/start")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'already running'}
2019-05-10 05:07:38 +00:00
2019-05-11 06:55:21 +00:00
def test_api__init__(default_conf, mocker):
2019-04-04 05:12:58 +00:00
"""
Test __init__() method
"""
2020-05-10 18:00:19 +00:00
default_conf.update({"api_server": {"enabled": True,
"listen_ip_address": "127.0.0.1",
"listen_port": 8080,
"username": "TestUser",
"password": "testPass",
}})
2019-04-04 05:12:58 +00:00
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock())
mocker.patch('freqtrade.rpc.api_server.ApiServer.run', MagicMock())
apiserver = ApiServer(get_patched_freqtradebot(mocker, default_conf))
assert apiserver._config == default_conf
2019-05-14 05:07:51 +00:00
def test_api_run(default_conf, mocker, caplog):
default_conf.update({"api_server": {"enabled": True,
"listen_ip_address": "127.0.0.1",
2019-11-23 14:20:53 +00:00
"listen_port": 8080,
"username": "TestUser",
"password": "testPass",
}})
2019-05-14 05:07:51 +00:00
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock())
mocker.patch('freqtrade.rpc.api_server.threading.Thread', MagicMock())
2019-05-18 07:54:36 +00:00
server_mock = MagicMock()
mocker.patch('freqtrade.rpc.api_server.make_server', server_mock)
apiserver = ApiServer(get_patched_freqtradebot(mocker, default_conf))
2019-05-14 05:07:51 +00:00
assert apiserver._config == default_conf
apiserver.run()
2019-05-18 07:54:36 +00:00
assert server_mock.call_count == 1
assert server_mock.call_args_list[0][0][0] == "127.0.0.1"
2019-11-23 14:20:53 +00:00
assert server_mock.call_args_list[0][0][1] == 8080
2019-05-18 07:54:36 +00:00
assert isinstance(server_mock.call_args_list[0][0][2], Flask)
assert hasattr(apiserver, "srv")
2019-05-14 05:07:51 +00:00
2019-08-11 18:17:22 +00:00
assert log_has("Starting HTTP Server at 127.0.0.1:8080", caplog)
assert log_has("Starting Local Rest Server.", caplog)
2019-05-14 05:07:51 +00:00
# Test binding to public
caplog.clear()
2019-05-18 07:54:36 +00:00
server_mock.reset_mock()
2019-05-14 05:07:51 +00:00
apiserver._config.update({"api_server": {"enabled": True,
"listen_ip_address": "0.0.0.0",
2019-11-23 14:20:53 +00:00
"listen_port": 8089,
"password": "",
}})
2019-05-14 05:07:51 +00:00
apiserver.run()
2019-05-18 07:54:36 +00:00
assert server_mock.call_count == 1
assert server_mock.call_args_list[0][0][0] == "0.0.0.0"
2019-11-23 14:20:53 +00:00
assert server_mock.call_args_list[0][0][1] == 8089
2019-05-18 07:54:36 +00:00
assert isinstance(server_mock.call_args_list[0][0][2], Flask)
2019-08-11 18:17:22 +00:00
assert log_has("Starting HTTP Server at 0.0.0.0:8089", caplog)
assert log_has("Starting Local Rest Server.", caplog)
2019-05-14 05:07:51 +00:00
assert log_has("SECURITY WARNING - Local Rest Server listening to external connections",
2019-08-11 18:17:22 +00:00
caplog)
2019-05-14 05:07:51 +00:00
assert log_has("SECURITY WARNING - This is insecure please set to your loopback,"
2019-08-11 18:17:22 +00:00
"e.g 127.0.0.1 in config.json", caplog)
assert log_has("SECURITY WARNING - No password for local REST Server defined. "
2019-08-11 18:17:22 +00:00
"Please make sure that this is intentional!", caplog)
2019-05-14 05:07:51 +00:00
2019-05-15 04:24:22 +00:00
# Test crashing flask
caplog.clear()
mocker.patch('freqtrade.rpc.api_server.make_server', MagicMock(side_effect=Exception))
2019-05-15 04:24:22 +00:00
apiserver.run()
2019-08-11 18:17:22 +00:00
assert log_has("Api server failed to start.", caplog)
2019-05-15 04:24:22 +00:00
2019-05-14 05:07:51 +00:00
2019-05-18 07:54:36 +00:00
def test_api_cleanup(default_conf, mocker, caplog):
default_conf.update({"api_server": {"enabled": True,
"listen_ip_address": "127.0.0.1",
2019-11-23 14:20:53 +00:00
"listen_port": 8080,
"username": "TestUser",
"password": "testPass",
}})
2019-05-18 07:54:36 +00:00
mocker.patch('freqtrade.rpc.telegram.Updater', MagicMock())
mocker.patch('freqtrade.rpc.api_server.threading.Thread', MagicMock())
mocker.patch('freqtrade.rpc.api_server.make_server', MagicMock())
apiserver = ApiServer(get_patched_freqtradebot(mocker, default_conf))
apiserver.run()
stop_mock = MagicMock()
stop_mock.shutdown = MagicMock()
apiserver.srv = stop_mock
apiserver.cleanup()
assert stop_mock.shutdown.call_count == 1
2019-08-11 18:17:22 +00:00
assert log_has("Stopping API Server", caplog)
2019-05-18 07:54:36 +00:00
2019-05-11 06:55:21 +00:00
def test_api_reloadconf(botclient):
ftbot, client = botclient
2019-04-04 05:12:58 +00:00
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/reload_conf")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'reloading config ...'}
assert ftbot.state == State.RELOAD_CONF
2019-04-04 05:12:58 +00:00
2019-05-11 06:55:21 +00:00
def test_api_stopbuy(botclient):
ftbot, client = botclient
assert ftbot.config['max_open_trades'] != 0
2019-04-04 05:12:58 +00:00
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/stopbuy")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 06:55:21 +00:00
assert rc.json == {'status': 'No more buy will occur from now. Run /reload_conf to reset.'}
assert ftbot.config['max_open_trades'] == 0
2019-05-11 07:10:54 +00:00
def test_api_balance(botclient, mocker, rpc_balance):
ftbot, client = botclient
ftbot.config['dry_run'] = False
2019-05-11 07:10:54 +00:00
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance)
2019-07-03 18:07:26 +00:00
mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination',
side_effect=lambda a, b: f"{a}/{b}")
ftbot.wallets.update()
2019-05-11 07:10:54 +00:00
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/balance")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 07:10:54 +00:00
assert "currencies" in rc.json
assert len(rc.json["currencies"]) == 5
assert rc.json['currencies'][0] == {
'currency': 'BTC',
'free': 12.0,
2019-05-11 07:10:54 +00:00
'balance': 12.0,
'used': 0.0,
2019-11-15 05:33:07 +00:00
'est_stake': 12.0,
'stake': 'BTC',
2019-05-11 07:10:54 +00:00
}
2019-05-11 07:44:39 +00:00
def test_api_count(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
2019-12-18 15:34:30 +00:00
fetch_ticker=ticker,
2019-05-11 07:44:39 +00:00
get_fee=fee,
markets=PropertyMock(return_value=markets)
)
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/count")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 07:44:39 +00:00
assert rc.json["current"] == 0
assert rc.json["max"] == 1.0
# Create some test data
ftbot.enter_positions()
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/count")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2019-05-11 07:44:39 +00:00
assert rc.json["current"] == 1.0
assert rc.json["max"] == 1.0
2019-05-11 11:31:48 +00:00
2019-11-17 13:56:08 +00:00
def test_api_show_config(botclient, mocker):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
rc = client_get(client, f"{BASE_URI}/show_config")
assert_response(rc)
assert 'dry_run' in rc.json
assert rc.json['exchange'] == 'bittrex'
assert rc.json['ticker_interval'] == '5m'
2020-05-05 19:19:35 +00:00
assert rc.json['state'] == 'running'
2019-11-17 13:56:08 +00:00
assert not rc.json['trailing_stop']
2019-05-11 11:31:48 +00:00
def test_api_daily(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
2019-12-18 15:34:30 +00:00
fetch_ticker=ticker,
2019-05-11 11:31:48 +00:00
get_fee=fee,
markets=PropertyMock(return_value=markets)
)
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/daily")
2019-05-11 12:05:25 +00:00
assert_response(rc)
2020-05-17 18:12:01 +00:00
assert len(rc.json['data']) == 7
assert rc.json['stake_currency'] == 'BTC'
assert rc.json['fiat_display_currency'] == 'USD'
assert rc.json['data'][0]['date'] == str(datetime.utcnow().date())
2019-05-11 12:05:25 +00:00
2020-04-07 17:50:13 +00:00
def test_api_trades(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets)
)
rc = client_get(client, f"{BASE_URI}/trades")
assert_response(rc)
assert len(rc.json) == 2
assert rc.json['trades_count'] == 0
create_mock_trades(fee)
rc = client_get(client, f"{BASE_URI}/trades")
assert_response(rc)
assert len(rc.json['trades']) == 3
assert rc.json['trades_count'] == 3
rc = client_get(client, f"{BASE_URI}/trades?limit=2")
assert_response(rc)
assert len(rc.json['trades']) == 2
assert rc.json['trades_count'] == 2
2019-05-11 12:05:25 +00:00
def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
2019-12-18 15:34:30 +00:00
fetch_ticker=ticker,
2019-05-11 12:05:25 +00:00
get_fee=fee,
markets=PropertyMock(return_value=markets)
)
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/edge")
2019-05-11 12:05:25 +00:00
assert_response(rc, 502)
assert rc.json == {"error": "Error querying _edge: Edge is not enabled."}
def test_api_profit(botclient, mocker, ticker, fee, markets, limit_buy_order, limit_sell_order):
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
2019-12-18 15:34:30 +00:00
fetch_ticker=ticker,
2019-05-11 12:05:25 +00:00
get_fee=fee,
markets=PropertyMock(return_value=markets)
)
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/profit")
assert_response(rc, 200)
assert rc.json['trade_count'] == 0
2019-05-11 12:05:25 +00:00
ftbot.enter_positions()
2019-05-11 12:05:25 +00:00
trade = Trade.query.first()
# Simulate fulfilled LIMIT_BUY order for trade
trade.update(limit_buy_order)
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/profit")
assert_response(rc, 200)
# One open trade
assert rc.json['trade_count'] == 1
assert rc.json['best_pair'] == ''
assert rc.json['best_rate'] == 0
2019-05-11 12:05:25 +00:00
trade.update(limit_sell_order)
trade.close_date = datetime.utcnow()
trade.is_open = False
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/profit")
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert rc.json == {'avg_duration': '0:00:00',
'best_pair': 'ETH/BTC',
'best_rate': 6.2,
'first_trade_date': 'just now',
2020-05-24 07:07:24 +00:00
'first_trade_timestamp': ANY,
2019-05-11 12:05:25 +00:00
'latest_trade_date': 'just now',
2020-05-24 07:07:24 +00:00
'latest_trade_timestamp': ANY,
2019-05-11 12:05:25 +00:00
'profit_all_coin': 6.217e-05,
'profit_all_fiat': 0,
'profit_all_percent': 6.2,
'profit_closed_coin': 6.217e-05,
'profit_closed_fiat': 0,
'profit_closed_percent': 6.2,
'trade_count': 1,
'closed_trade_count': 1,
2019-05-11 12:05:25 +00:00
}
2019-05-15 04:51:23 +00:00
def test_api_performance(botclient, mocker, ticker, fee):
2019-05-11 12:05:25 +00:00
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
trade = Trade(
pair='LTC/ETH',
amount=1,
exchange='binance',
stake_amount=1,
open_rate=0.245441,
open_order_id="123456",
is_open=False,
fee_close=fee.return_value,
fee_open=fee.return_value,
close_rate=0.265441,
)
trade.close_profit = trade.calc_profit_ratio()
2019-05-11 12:05:25 +00:00
Trade.session.add(trade)
trade = Trade(
pair='XRP/ETH',
amount=5,
stake_amount=1,
exchange='binance',
open_rate=0.412,
open_order_id="123456",
is_open=False,
fee_close=fee.return_value,
fee_open=fee.return_value,
close_rate=0.391
)
trade.close_profit = trade.calc_profit_ratio()
2019-05-11 12:05:25 +00:00
Trade.session.add(trade)
Trade.session.flush()
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/performance")
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert len(rc.json) == 2
assert rc.json == [{'count': 1, 'pair': 'LTC/ETH', 'profit': 7.61},
{'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57}]
2019-05-15 04:51:23 +00:00
def test_api_status(botclient, mocker, ticker, fee, markets):
2019-05-11 12:05:25 +00:00
ftbot, client = botclient
patch_get_signal(ftbot, (True, False))
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
2019-12-18 15:34:30 +00:00
fetch_ticker=ticker,
2019-05-11 12:05:25 +00:00
get_fee=fee,
markets=PropertyMock(return_value=markets)
)
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/status")
assert_response(rc, 200)
assert rc.json == []
2019-05-11 12:05:25 +00:00
ftbot.enter_positions()
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/status")
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert len(rc.json) == 1
assert rc.json == [{'amount': 91.07468124,
2019-05-11 12:05:25 +00:00
'base_currency': 'BTC',
'close_date': None,
'close_date_hum': None,
2020-05-24 06:47:10 +00:00
'close_timestamp': None,
2019-05-11 12:05:25 +00:00
'close_profit': None,
2020-05-26 17:25:03 +00:00
'close_profit_pct': None,
'close_profit_abs': None,
2019-05-11 12:05:25 +00:00
'close_rate': None,
2020-05-24 06:47:10 +00:00
'current_profit': -0.00408133,
2020-05-26 17:25:03 +00:00
'current_profit_pct': -0.41,
'current_rate': 1.099e-05,
2019-05-11 12:05:25 +00:00
'initial_stop_loss': 0.0,
'initial_stop_loss_pct': None,
'open_date': ANY,
'open_date_hum': 'just now',
2020-05-24 06:47:10 +00:00
'open_timestamp': ANY,
2019-05-11 12:05:25 +00:00
'open_order': '(limit buy rem=0.00000000)',
'open_rate': 1.098e-05,
2019-05-11 12:05:25 +00:00
'pair': 'ETH/BTC',
'stake_amount': 0.001,
'stop_loss': 0.0,
'stop_loss_pct': None,
'stoploss_order_id': None,
'stoploss_last_update': None,
'stoploss_last_update_timestamp': None,
2020-04-06 13:49:24 +00:00
'trade_id': 1,
'close_rate_requested': None,
'current_rate': 1.099e-05,
'fee_close': 0.0025,
2020-04-30 04:58:43 +00:00
'fee_close_cost': None,
'fee_close_currency': None,
2020-04-06 13:49:24 +00:00
'fee_open': 0.0025,
2020-04-30 04:58:43 +00:00
'fee_open_cost': None,
'fee_open_currency': None,
2020-04-06 13:49:24 +00:00
'open_date': ANY,
'is_open': True,
'max_rate': 0.0,
'min_rate': None,
'open_order_id': ANY,
'open_rate_requested': 1.098e-05,
'open_trade_price': 0.0010025,
'sell_reason': None,
'sell_order_status': None,
2020-04-06 13:49:24 +00:00
'strategy': 'DefaultStrategy',
'ticker_interval': 5,
'timeframe': 5,
'exchange': 'bittrex',
}]
2019-05-11 12:05:25 +00:00
def test_api_version(botclient):
ftbot, client = botclient
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/version")
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert rc.json == {"version": __version__}
2019-05-15 04:51:23 +00:00
def test_api_blacklist(botclient, mocker):
2019-05-11 12:05:25 +00:00
ftbot, client = botclient
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/blacklist")
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert rc.json == {"blacklist": ["DOGE/BTC", "HOT/BTC"],
"length": 2,
2019-11-09 13:00:32 +00:00
"method": ["StaticPairList"]}
2019-05-11 12:05:25 +00:00
# Add ETH/BTC to blacklist
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/blacklist",
data='{"blacklist": ["ETH/BTC"]}')
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert rc.json == {"blacklist": ["DOGE/BTC", "HOT/BTC", "ETH/BTC"],
"length": 3,
2019-11-09 13:00:32 +00:00
"method": ["StaticPairList"]}
2019-05-11 12:05:25 +00:00
2019-05-15 04:51:23 +00:00
def test_api_whitelist(botclient):
2019-05-11 12:05:25 +00:00
ftbot, client = botclient
2019-05-25 12:13:59 +00:00
rc = client_get(client, f"{BASE_URI}/whitelist")
2019-05-11 12:05:25 +00:00
assert_response(rc)
assert rc.json == {"whitelist": ['ETH/BTC', 'LTC/BTC', 'XRP/BTC', 'NEO/BTC'],
"length": 4,
2019-11-09 13:00:32 +00:00
"method": ["StaticPairList"]}
2019-05-15 04:51:23 +00:00
def test_api_forcebuy(botclient, mocker, fee):
ftbot, client = botclient
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/forcebuy",
2019-05-15 04:51:23 +00:00
data='{"pair": "ETH/BTC"}')
assert_response(rc, 502)
assert rc.json == {"error": "Error querying _forcebuy: Forcebuy not enabled."}
# enable forcebuy
ftbot.config["forcebuy_enable"] = True
fbuy_mock = MagicMock(return_value=None)
mocker.patch("freqtrade.rpc.RPC._rpc_forcebuy", fbuy_mock)
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/forcebuy",
2019-05-15 04:51:23 +00:00
data='{"pair": "ETH/BTC"}')
assert_response(rc)
assert rc.json == {"status": "Error buying pair ETH/BTC."}
# Test creating trae
fbuy_mock = MagicMock(return_value=Trade(
pair='ETH/ETH',
amount=1,
exchange='bittrex',
stake_amount=1,
open_rate=0.245441,
open_order_id="123456",
open_date=datetime.utcnow(),
is_open=False,
fee_close=fee.return_value,
fee_open=fee.return_value,
close_rate=0.265441,
))
mocker.patch("freqtrade.rpc.RPC._rpc_forcebuy", fbuy_mock)
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/forcebuy",
2019-05-15 04:51:23 +00:00
data='{"pair": "ETH/BTC"}')
assert_response(rc)
assert rc.json == {'amount': 1,
'close_date': None,
'close_date_hum': None,
2020-05-24 06:47:10 +00:00
'close_timestamp': None,
2019-05-15 04:51:23 +00:00
'close_rate': 0.265441,
'initial_stop_loss': None,
'initial_stop_loss_pct': None,
'open_date': ANY,
'open_date_hum': 'just now',
2020-05-24 06:47:10 +00:00
'open_timestamp': ANY,
2019-05-15 04:51:23 +00:00
'open_rate': 0.245441,
'pair': 'ETH/ETH',
'stake_amount': 1,
'stop_loss': None,
'stop_loss_pct': None,
'stoploss_order_id': None,
'stoploss_last_update': None,
'stoploss_last_update_timestamp': None,
2020-04-06 13:49:24 +00:00
'trade_id': None,
'close_profit': None,
'close_profit_abs': None,
2020-04-06 13:49:24 +00:00
'close_rate_requested': None,
'fee_close': 0.0025,
2020-04-30 04:58:43 +00:00
'fee_close_cost': None,
'fee_close_currency': None,
2020-04-06 13:49:24 +00:00
'fee_open': 0.0025,
2020-04-30 04:58:43 +00:00
'fee_open_cost': None,
'fee_open_currency': None,
2020-04-06 13:49:24 +00:00
'is_open': False,
'max_rate': None,
'min_rate': None,
'open_order_id': '123456',
'open_rate_requested': None,
'open_trade_price': 0.2460546025,
'sell_reason': None,
'sell_order_status': None,
2020-04-06 13:49:24 +00:00
'strategy': None,
'ticker_interval': None,
'timeframe': None,
'exchange': 'bittrex',
2020-04-06 13:49:24 +00:00
}
2019-05-15 04:51:23 +00:00
2019-05-15 05:00:17 +00:00
def test_api_forcesell(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker),
2019-12-18 15:34:30 +00:00
fetch_ticker=ticker,
2019-05-15 05:00:17 +00:00
get_fee=fee,
markets=PropertyMock(return_value=markets)
)
patch_get_signal(ftbot, (True, False))
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/forcesell",
2019-05-15 05:00:17 +00:00
data='{"tradeid": "1"}')
assert_response(rc, 502)
assert rc.json == {"error": "Error querying _forcesell: invalid argument"}
2019-05-15 04:51:23 +00:00
ftbot.enter_positions()
2019-05-15 05:00:17 +00:00
2019-05-25 12:13:59 +00:00
rc = client_post(client, f"{BASE_URI}/forcesell",
2019-05-15 05:00:17 +00:00
data='{"tradeid": "1"}')
assert_response(rc)
assert rc.json == {'result': 'Created sell order for trade 1.'}