revert back test_rpc_api_server

This commit is contained in:
kevinjulian 2021-07-23 18:34:18 +07:00
parent 8032257fdf
commit acfaa39e54

View File

@ -46,13 +46,13 @@ def botclient(default_conf, mocker):
"password": _TEST_PASS, "password": _TEST_PASS,
}}) }})
freqtrade = get_patched_freqtradebot(mocker, default_conf) ftbot = get_patched_freqtradebot(mocker, default_conf)
rpc = RPC(freqtrade) rpc = RPC(ftbot)
mocker.patch('freqtrade.rpc.api_server.ApiServer.start_api', MagicMock()) mocker.patch('freqtrade.rpc.api_server.ApiServer.start_api', MagicMock())
try: try:
apiserver = ApiServer(default_conf) apiserver = ApiServer(default_conf)
apiserver.add_rpc_handler(rpc) apiserver.add_rpc_handler(rpc)
yield freqtrade, TestClient(apiserver.app) yield ftbot, TestClient(apiserver.app)
# Cleanup ... ? # Cleanup ... ?
finally: finally:
ApiServer.shutdown() ApiServer.shutdown()
@ -88,7 +88,7 @@ def assert_response(response, expected_code=200, needs_cors=True):
def test_api_not_found(botclient): def test_api_not_found(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/invalid_url") rc = client_get(client, f"{BASE_URI}/invalid_url")
assert_response(rc, 404) assert_response(rc, 404)
@ -96,7 +96,7 @@ def test_api_not_found(botclient):
def test_api_ui_fallback(botclient): def test_api_ui_fallback(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, "/favicon.ico") rc = client_get(client, "/favicon.ico")
assert rc.status_code == 200 assert rc.status_code == 200
@ -140,7 +140,7 @@ def test_api_auth():
def test_api_unauthorized(botclient): def test_api_unauthorized(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client.get(f"{BASE_URI}/ping") rc = client.get(f"{BASE_URI}/ping")
assert_response(rc, needs_cors=False) assert_response(rc, needs_cors=False)
assert rc.json() == {'status': 'pong'} assert rc.json() == {'status': 'pong'}
@ -151,20 +151,20 @@ def test_api_unauthorized(botclient):
assert rc.json() == {'detail': 'Unauthorized'} assert rc.json() == {'detail': 'Unauthorized'}
# Change only username # Change only username
freqtrade.config['api_server']['username'] = 'Ftrader' ftbot.config['api_server']['username'] = 'Ftrader'
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401) assert_response(rc, 401)
assert rc.json() == {'detail': 'Unauthorized'} assert rc.json() == {'detail': 'Unauthorized'}
# Change only password # Change only password
freqtrade.config['api_server']['username'] = _TEST_USER ftbot.config['api_server']['username'] = _TEST_USER
freqtrade.config['api_server']['password'] = 'WrongPassword' ftbot.config['api_server']['password'] = 'WrongPassword'
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401) assert_response(rc, 401)
assert rc.json() == {'detail': 'Unauthorized'} assert rc.json() == {'detail': 'Unauthorized'}
freqtrade.config['api_server']['username'] = 'Ftrader' ftbot.config['api_server']['username'] = 'Ftrader'
freqtrade.config['api_server']['password'] = 'WrongPassword' ftbot.config['api_server']['password'] = 'WrongPassword'
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc, 401) assert_response(rc, 401)
@ -172,7 +172,7 @@ def test_api_unauthorized(botclient):
def test_api_token_login(botclient): def test_api_token_login(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client.post(f"{BASE_URI}/token/login", rc = client.post(f"{BASE_URI}/token/login",
data=None, data=None,
headers={'Authorization': _basic_auth_str('WRONG_USER', 'WRONG_PASS'), headers={'Authorization': _basic_auth_str('WRONG_USER', 'WRONG_PASS'),
@ -191,7 +191,7 @@ def test_api_token_login(botclient):
def test_api_token_refresh(botclient): def test_api_token_refresh(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_post(client, f"{BASE_URI}/token/login") rc = client_post(client, f"{BASE_URI}/token/login")
assert_response(rc) assert_response(rc)
rc = client.post(f"{BASE_URI}/token/refresh", rc = client.post(f"{BASE_URI}/token/refresh",
@ -204,12 +204,12 @@ def test_api_token_refresh(botclient):
def test_api_stop_workflow(botclient): def test_api_stop_workflow(botclient):
freqtrade, client = botclient ftbot, client = botclient
assert freqtrade.state == State.RUNNING assert ftbot.state == State.RUNNING
rc = client_post(client, f"{BASE_URI}/stop") rc = client_post(client, f"{BASE_URI}/stop")
assert_response(rc) assert_response(rc)
assert rc.json() == {'status': 'stopping trader ...'} assert rc.json() == {'status': 'stopping trader ...'}
assert freqtrade.state == State.STOPPED assert ftbot.state == State.STOPPED
# Stop bot again # Stop bot again
rc = client_post(client, f"{BASE_URI}/stop") rc = client_post(client, f"{BASE_URI}/stop")
@ -220,7 +220,7 @@ def test_api_stop_workflow(botclient):
rc = client_post(client, f"{BASE_URI}/start") rc = client_post(client, f"{BASE_URI}/start")
assert_response(rc) assert_response(rc)
assert rc.json() == {'status': 'starting trader ...'} assert rc.json() == {'status': 'starting trader ...'}
assert freqtrade.state == State.RUNNING assert ftbot.state == State.RUNNING
# Call start again # Call start again
rc = client_post(client, f"{BASE_URI}/start") rc = client_post(client, f"{BASE_URI}/start")
@ -399,32 +399,32 @@ def test_api_cleanup(default_conf, mocker, caplog):
def test_api_reloadconf(botclient): def test_api_reloadconf(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_post(client, f"{BASE_URI}/reload_config") rc = client_post(client, f"{BASE_URI}/reload_config")
assert_response(rc) assert_response(rc)
assert rc.json() == {'status': 'Reloading config ...'} assert rc.json() == {'status': 'Reloading config ...'}
assert freqtrade.state == State.RELOAD_CONFIG assert ftbot.state == State.RELOAD_CONFIG
def test_api_stopbuy(botclient): def test_api_stopbuy(botclient):
freqtrade, client = botclient ftbot, client = botclient
assert freqtrade.config['max_open_trades'] != 0 assert ftbot.config['max_open_trades'] != 0
rc = client_post(client, f"{BASE_URI}/stopbuy") rc = client_post(client, f"{BASE_URI}/stopbuy")
assert_response(rc) assert_response(rc)
assert rc.json() == {'status': 'No more buy will occur from now. Run /reload_config to reset.'} assert rc.json() == {'status': 'No more buy will occur from now. Run /reload_config to reset.'}
assert freqtrade.config['max_open_trades'] == 0 assert ftbot.config['max_open_trades'] == 0
def test_api_balance(botclient, mocker, rpc_balance): def test_api_balance(botclient, mocker, rpc_balance):
freqtrade, client = botclient ftbot, client = botclient
freqtrade.config['dry_run'] = False ftbot.config['dry_run'] = False
mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance) mocker.patch('freqtrade.exchange.Exchange.get_balances', return_value=rpc_balance)
mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination', mocker.patch('freqtrade.exchange.Exchange.get_valid_pair_combination',
side_effect=lambda a, b: f"{a}/{b}") side_effect=lambda a, b: f"{a}/{b}")
freqtrade.wallets.update() ftbot.wallets.update()
rc = client_get(client, f"{BASE_URI}/balance") rc = client_get(client, f"{BASE_URI}/balance")
assert_response(rc) assert_response(rc)
@ -441,8 +441,8 @@ def test_api_balance(botclient, mocker, rpc_balance):
def test_api_count(botclient, mocker, ticker, fee, markets): def test_api_count(botclient, mocker, ticker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -463,13 +463,13 @@ def test_api_count(botclient, mocker, ticker, fee, markets):
assert rc.json()["current"] == 4 assert rc.json()["current"] == 4
assert rc.json()["max"] == 1 assert rc.json()["max"] == 1
freqtrade.config['max_open_trades'] = float('inf') ftbot.config['max_open_trades'] = float('inf')
rc = client_get(client, f"{BASE_URI}/count") rc = client_get(client, f"{BASE_URI}/count")
assert rc.json()["max"] == -1 assert rc.json()["max"] == -1
def test_api_locks(botclient): def test_api_locks(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/locks") rc = client_get(client, f"{BASE_URI}/locks")
assert_response(rc) assert_response(rc)
@ -503,8 +503,8 @@ def test_api_locks(botclient):
def test_api_show_config(botclient, mocker): def test_api_show_config(botclient, mocker):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
rc = client_get(client, f"{BASE_URI}/show_config") rc = client_get(client, f"{BASE_URI}/show_config")
assert_response(rc) assert_response(rc)
@ -521,8 +521,8 @@ def test_api_show_config(botclient, mocker):
def test_api_daily(botclient, mocker, ticker, fee, markets): def test_api_daily(botclient, mocker, ticker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -539,8 +539,8 @@ def test_api_daily(botclient, mocker, ticker, fee, markets):
def test_api_trades(botclient, mocker, fee, markets): def test_api_trades(botclient, mocker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets) markets=PropertyMock(return_value=markets)
@ -567,8 +567,8 @@ def test_api_trades(botclient, mocker, fee, markets):
def test_api_trade_single(botclient, mocker, fee, ticker, markets): def test_api_trade_single(botclient, mocker, fee, ticker, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets), markets=PropertyMock(return_value=markets),
@ -587,8 +587,8 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets):
def test_api_delete_trade(botclient, mocker, fee, markets): def test_api_delete_trade(botclient, mocker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
stoploss_mock = MagicMock() stoploss_mock = MagicMock()
cancel_mock = MagicMock() cancel_mock = MagicMock()
mocker.patch.multiple( mocker.patch.multiple(
@ -603,7 +603,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets):
create_mock_trades(fee) create_mock_trades(fee)
Trade.query.session.flush() Trade.query.session.flush()
freqtrade.strategy.order_types['stoploss_on_exchange'] = True ftbot.strategy.order_types['stoploss_on_exchange'] = True
trades = Trade.query.all() trades = Trade.query.all()
trades[1].stoploss_order_id = '1234' trades[1].stoploss_order_id = '1234'
assert len(trades) > 2 assert len(trades) > 2
@ -629,7 +629,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets):
def test_api_logs(botclient): def test_api_logs(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/logs") rc = client_get(client, f"{BASE_URI}/logs")
assert_response(rc) assert_response(rc)
assert len(rc.json()) == 2 assert len(rc.json()) == 2
@ -661,8 +661,8 @@ def test_api_logs(botclient):
def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -677,8 +677,8 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_api_profit(botclient, mocker, ticker, fee, markets): def test_api_profit(botclient, mocker, ticker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -728,8 +728,8 @@ def test_api_profit(botclient, mocker, ticker, fee, markets):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_api_stats(botclient, mocker, ticker, fee, markets,): def test_api_stats(botclient, mocker, ticker, fee, markets,):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -756,8 +756,8 @@ def test_api_stats(botclient, mocker, ticker, fee, markets,):
def test_api_performance(botclient, fee): def test_api_performance(botclient, fee):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
trade = Trade( trade = Trade(
pair='LTC/ETH', pair='LTC/ETH',
@ -802,8 +802,8 @@ def test_api_performance(botclient, fee):
def test_api_status(botclient, mocker, ticker, fee, markets): def test_api_status(botclient, mocker, ticker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -891,7 +891,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
def test_api_version(botclient): def test_api_version(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/version") rc = client_get(client, f"{BASE_URI}/version")
assert_response(rc) assert_response(rc)
@ -899,7 +899,7 @@ def test_api_version(botclient):
def test_api_blacklist(botclient, mocker): def test_api_blacklist(botclient, mocker):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/blacklist") rc = client_get(client, f"{BASE_URI}/blacklist")
assert_response(rc) assert_response(rc)
@ -934,7 +934,7 @@ def test_api_blacklist(botclient, mocker):
def test_api_whitelist(botclient): def test_api_whitelist(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/whitelist") rc = client_get(client, f"{BASE_URI}/whitelist")
assert_response(rc) assert_response(rc)
@ -946,7 +946,7 @@ def test_api_whitelist(botclient):
def test_api_forcebuy(botclient, mocker, fee): def test_api_forcebuy(botclient, mocker, fee):
freqtrade, client = botclient ftbot, client = botclient
rc = client_post(client, f"{BASE_URI}/forcebuy", rc = client_post(client, f"{BASE_URI}/forcebuy",
data='{"pair": "ETH/BTC"}') data='{"pair": "ETH/BTC"}')
@ -954,7 +954,7 @@ def test_api_forcebuy(botclient, mocker, fee):
assert rc.json() == {"error": "Error querying /api/v1/forcebuy: Forcebuy not enabled."} assert rc.json() == {"error": "Error querying /api/v1/forcebuy: Forcebuy not enabled."}
# enable forcebuy # enable forcebuy
freqtrade.config['forcebuy_enable'] = True ftbot.config['forcebuy_enable'] = True
fbuy_mock = MagicMock(return_value=None) fbuy_mock = MagicMock(return_value=None)
mocker.patch("freqtrade.rpc.RPC._rpc_forcebuy", fbuy_mock) mocker.patch("freqtrade.rpc.RPC._rpc_forcebuy", fbuy_mock)
@ -1037,7 +1037,7 @@ def test_api_forcebuy(botclient, mocker, fee):
def test_api_forcesell(botclient, mocker, ticker, fee, markets): def test_api_forcesell(botclient, mocker, ticker, fee, markets):
freqtrade, client = botclient ftbot, client = botclient
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
get_balances=MagicMock(return_value=ticker), get_balances=MagicMock(return_value=ticker),
@ -1046,14 +1046,14 @@ def test_api_forcesell(botclient, mocker, ticker, fee, markets):
markets=PropertyMock(return_value=markets), markets=PropertyMock(return_value=markets),
_is_dry_limit_order_filled=MagicMock(return_value=False), _is_dry_limit_order_filled=MagicMock(return_value=False),
) )
patch_get_signal(freqtrade, (True, False, None)) patch_get_signal(ftbot, (True, False, None))
rc = client_post(client, f"{BASE_URI}/forcesell", rc = client_post(client, f"{BASE_URI}/forcesell",
data='{"tradeid": "1"}') data='{"tradeid": "1"}')
assert_response(rc, 502) assert_response(rc, 502)
assert rc.json() == {"error": "Error querying /api/v1/forcesell: invalid argument"} assert rc.json() == {"error": "Error querying /api/v1/forcesell: invalid argument"}
freqtrade.enter_positions() ftbot.enter_positions()
rc = client_post(client, f"{BASE_URI}/forcesell", rc = client_post(client, f"{BASE_URI}/forcesell",
data='{"tradeid": "1"}') data='{"tradeid": "1"}')
@ -1062,7 +1062,7 @@ def test_api_forcesell(botclient, mocker, ticker, fee, markets):
def test_api_pair_candles(botclient, ohlcv_history): def test_api_pair_candles(botclient, ohlcv_history):
freqtrade, client = botclient ftbot, client = botclient
timeframe = '5m' timeframe = '5m'
amount = 3 amount = 3
@ -1090,7 +1090,7 @@ def test_api_pair_candles(botclient, ohlcv_history):
ohlcv_history.loc[1, 'buy'] = 1 ohlcv_history.loc[1, 'buy'] = 1
ohlcv_history['sell'] = 0 ohlcv_history['sell'] = 0
freqtrade.dataprovider._set_cached_df("XRP/BTC", timeframe, ohlcv_history) ftbot.dataprovider._set_cached_df("XRP/BTC", timeframe, ohlcv_history)
rc = client_get(client, rc = client_get(client,
f"{BASE_URI}/pair_candles?limit={amount}&pair=XRP%2FBTC&timeframe={timeframe}") f"{BASE_URI}/pair_candles?limit={amount}&pair=XRP%2FBTC&timeframe={timeframe}")
@ -1128,7 +1128,7 @@ def test_api_pair_candles(botclient, ohlcv_history):
def test_api_pair_history(botclient, ohlcv_history): def test_api_pair_history(botclient, ohlcv_history):
freqtrade, client = botclient ftbot, client = botclient
timeframe = '5m' timeframe = '5m'
# No pair # No pair
@ -1181,23 +1181,23 @@ def test_api_pair_history(botclient, ohlcv_history):
def test_api_plot_config(botclient): def test_api_plot_config(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/plot_config") rc = client_get(client, f"{BASE_URI}/plot_config")
assert_response(rc) assert_response(rc)
assert rc.json() == {} assert rc.json() == {}
freqtrade.strategy.plot_config = { ftbot.strategy.plot_config = {
'main_plot': {'sma': {}}, 'main_plot': {'sma': {}},
'subplots': {'RSI': {'rsi': {'color': 'red'}}} 'subplots': {'RSI': {'rsi': {'color': 'red'}}}
} }
rc = client_get(client, f"{BASE_URI}/plot_config") rc = client_get(client, f"{BASE_URI}/plot_config")
assert_response(rc) assert_response(rc)
assert rc.json() == freqtrade.strategy.plot_config assert rc.json() == ftbot.strategy.plot_config
assert isinstance(rc.json()['main_plot'], dict) assert isinstance(rc.json()['main_plot'], dict)
assert isinstance(rc.json()['subplots'], dict) assert isinstance(rc.json()['subplots'], dict)
freqtrade.strategy.plot_config = {'main_plot': {'sma': {}}} ftbot.strategy.plot_config = {'main_plot': {'sma': {}}}
rc = client_get(client, f"{BASE_URI}/plot_config") rc = client_get(client, f"{BASE_URI}/plot_config")
assert_response(rc) assert_response(rc)
@ -1206,7 +1206,7 @@ def test_api_plot_config(botclient):
def test_api_strategies(botclient): def test_api_strategies(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/strategies") rc = client_get(client, f"{BASE_URI}/strategies")
@ -1219,7 +1219,7 @@ def test_api_strategies(botclient):
def test_api_strategy(botclient): def test_api_strategy(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/strategy/DefaultStrategy") rc = client_get(client, f"{BASE_URI}/strategy/DefaultStrategy")
@ -1234,7 +1234,7 @@ def test_api_strategy(botclient):
def test_list_available_pairs(botclient): def test_list_available_pairs(botclient):
freqtrade, client = botclient ftbot, client = botclient
rc = client_get(client, f"{BASE_URI}/available_pairs") rc = client_get(client, f"{BASE_URI}/available_pairs")