-
-
Notifications
You must be signed in to change notification settings - Fork 554
/
Copy pathtest_exchanges.py
163 lines (145 loc) · 6.3 KB
/
test_exchanges.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from unittest.mock import patch
from rotkehlchen.api.server import APIServer
from rotkehlchen.db.settings import ModifiableDBSettings
from rotkehlchen.exchanges.binance import Binance
from rotkehlchen.tests.utils.factories import make_api_key, make_api_secret
from rotkehlchen.tests.utils.kraken import MockKraken
from rotkehlchen.types import ApiKey, ApiSecret, ExchangeApiCredentials, Location
def test_exchanges_filtering(database, exchange_manager, function_scope_messages_aggregator):
kraken1 = MockKraken(
name='mockkraken_1',
api_key=make_api_key(),
secret=make_api_secret(),
database=database,
msg_aggregator=function_scope_messages_aggregator,
)
kraken2 = MockKraken(
name='mockkraken_2',
api_key=make_api_key(),
secret=make_api_secret(),
database=database,
msg_aggregator=function_scope_messages_aggregator,
)
binance1 = Binance(
name='mockbinance_1',
api_key=make_api_key(),
secret=make_api_secret(),
database=database,
msg_aggregator=function_scope_messages_aggregator,
)
binance2 = Binance(
name='mockbinance_2',
api_key=make_api_key(),
secret=make_api_secret(),
database=database,
msg_aggregator=function_scope_messages_aggregator,
)
exchange_manager.initialize_exchanges({}, database)
exchange_manager.connected_exchanges[Location.KRAKEN].append(kraken1)
exchange_manager.connected_exchanges[Location.KRAKEN].append(kraken2)
exchange_manager.connected_exchanges[Location.BINANCE].append(binance1)
exchange_manager.connected_exchanges[Location.BINANCE].append(binance2)
assert set(exchange_manager.iterate_exchanges()) == {kraken1, kraken2, binance1, binance2}
with database.user_write() as cursor:
database.set_settings(cursor, ModifiableDBSettings(
non_syncing_exchanges=[kraken1.location_id(), kraken2.location_id()],
))
assert set(exchange_manager.iterate_exchanges()) == {binance1, binance2}
database.set_settings(cursor, ModifiableDBSettings(
non_syncing_exchanges=[binance1.location_id()],
))
assert set(exchange_manager.iterate_exchanges()) == {binance2, kraken1, kraken2}
TEST_CREDENTIALS_1 = ExchangeApiCredentials(
name='KuCoin',
location=Location.KUCOIN,
api_key=ApiKey('api-key-1'),
api_secret=ApiSecret(b'api-secret-1'),
passphrase='passphrase-1',
)
TEST_CREDENTIALS_2 = ExchangeApiCredentials(
name='KuCoin',
location=Location.KUCOIN,
api_key=ApiKey('api-key-2'),
api_secret=ApiSecret(b'api-secret-2'),
passphrase='passphrase-2',
)
TEST_CREDENTIALS_3 = ExchangeApiCredentials(
name='KuCoin',
location=Location.KUCOIN,
api_key=ApiKey('api-key-3'),
api_secret=ApiSecret(b'api-secret-3'),
passphrase='passphrase-3',
)
def test_change_credentials(rotkehlchen_api_server: APIServer) -> None:
"""
Test that chaning exchange credentials works as expected and if incorrect credentials
were provided then the old credentials are restored.
"""
rotki = rotkehlchen_api_server.rest_api.rotkehlchen
def mock_kucoin_validate_api_key(kucoin):
if kucoin.api_passphrase in (TEST_CREDENTIALS_1.passphrase, TEST_CREDENTIALS_3.passphrase):
return True, ''
return False, 'Invalid passphrase' # For TEST_KUCOIN_PASSPHRASE_2
def get_current_credentials(kucoin) -> ExchangeApiCredentials:
return ExchangeApiCredentials(
name=kucoin.name,
location=Location.KUCOIN,
api_key=kucoin.api_key,
api_secret=kucoin.secret,
passphrase=kucoin.api_passphrase,
)
with patch('rotkehlchen.exchanges.kucoin.Kucoin.validate_api_key', mock_kucoin_validate_api_key): # noqa: E501
# Setup with correct credentials
rotki.setup_exchange(
name='KuCoin',
location=Location.KUCOIN,
api_key=TEST_CREDENTIALS_1.api_key,
api_secret=TEST_CREDENTIALS_1.api_secret,
passphrase=TEST_CREDENTIALS_1.passphrase,
)
kucoin = rotki.exchange_manager.connected_exchanges[Location.KUCOIN][0]
with rotki.data.db.conn.read_ctx() as cursor:
credentials_in_db = rotki.data.db.get_exchange_credentials(
cursor=cursor,
location=Location.KUCOIN,
name='KuCoin',
)[Location.KUCOIN][0]
assert credentials_in_db == get_current_credentials(kucoin) == TEST_CREDENTIALS_1
# Try to change credentials to incorrect ones
success, _ = rotki.exchange_manager.edit_exchange(
name='KuCoin',
location=Location.KUCOIN,
new_name=None,
api_key=TEST_CREDENTIALS_2.api_key,
api_secret=TEST_CREDENTIALS_2.api_secret,
passphrase=TEST_CREDENTIALS_2.passphrase,
kraken_account_type=None,
binance_selected_trade_pairs=None,
)
assert success is False, 'Should not have been able to change credentials'
with rotki.data.db.conn.read_ctx() as cursor:
credentials_in_db = rotki.data.db.get_exchange_credentials(
cursor=cursor,
location=Location.KUCOIN,
name='KuCoin',
)[Location.KUCOIN][0]
assert credentials_in_db == get_current_credentials(kucoin) == TEST_CREDENTIALS_1, 'Credentials should not have changed' # noqa: E501
# Change credentials to correct ones
success, _ = rotki.exchange_manager.edit_exchange(
name='KuCoin',
location=Location.KUCOIN,
new_name=None,
api_key=TEST_CREDENTIALS_3.api_key,
api_secret=TEST_CREDENTIALS_3.api_secret,
passphrase=TEST_CREDENTIALS_3.passphrase,
kraken_account_type=None,
binance_selected_trade_pairs=None,
)
assert success is True, 'Should have been able to change credentials'
with rotki.data.db.conn.read_ctx() as cursor:
credentials_in_db = rotki.data.db.get_exchange_credentials(
cursor=cursor,
location=Location.KUCOIN,
name='KuCoin',
)[Location.KUCOIN][0]
assert credentials_in_db == get_current_credentials(kucoin) == TEST_CREDENTIALS_3