-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet
More file actions
237 lines (219 loc) · 7.74 KB
/
Copy pathwallet
File metadata and controls
237 lines (219 loc) · 7.74 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Copyright (c) 2018 Andrey Valyaev <dron.valyaev@gmail.com>
# Copyright (c) 2018 Alexey Tsurkan
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
''' Тестирование Работы со списком задач '''
from test.zold.test_score import FakeScore
from test.zold.test_transaction import BadPrefixTransaction, FakeTransaction
from test.zold.transaction import IncomingTransaction
from test.zold.wallet import FakeWallet, RootWallet, IncomeWallet
from flask_api import status
from node.app import APP
from node.db import DB, Score
from zold.wallet import TransactionWallet, WalletString
from .test_wallet import FullWallet
class WalletScore:
''' Score для кошeлька '''
def __init__(self, wallet):
self.wallet = wallet
def __str__(self):
return str(
FakeScore(
3,
APP.config,
prefix=self.wallet.prefix(),
id=self.wallet.id()
)
)
class TestGetTasks:
''' Тестирование GET /tasks'''
def test_mining_tasks(self):
''' В списке задач присутствуют задачи майнинга '''
with APP.app_context():
DB.session.query(Score).delete()
response = APP.test_client().get('/tasks')
assert response.status_code == status.HTTP_200_OK
assert any(t['type'] == 'mining' for t in response.json['tasks'])
def test_remotes_tasks(self):
''' В списке задач присутствуют задачи поиска кошелька '''
wallet = FakeWallet()
APP.test_client().get(
'/',
headers={'X-Zold-Score': '3/3: %s' % WalletScore(wallet)}
)
response = APP.test_client().get('/tasks')
assert response.status_code == status.HTTP_200_OK
assert any(
t['id'] == wallet.id() and t['prefix'] in wallet.public()
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_remotes_not_need_tasks(self):
''' В списке задач отсутствую задачи поиска, если кошелек присутствует '''
wallet = FakeWallet()
APP.test_client().get(
'/',
headers={'X-Zold-Score': '3/3: %s' % WalletScore(wallet)}
)
APP.test_client().put(
'/wallet/%s' % wallet.id(),
data=str(WalletString(wallet))
)
response = APP.test_client().get('/tasks')
assert response.status_code == status.HTTP_200_OK
assert not any(
t['id'] == wallet.id()
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_dst_wallet_to_wanted(self):
''' Кошельки получатели помещаются в список tasks '''
root = RootWallet()
wallet = FullWallet(root, 1000, APP.test_client())
response = APP.test_client().get('/tasks')
assert any(
all((
t['id'] == wallet.id(),
t['prefix'] in wallet.public(),
t.get('who', None) == root.id()
))
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_dst_wallet_from_wanted(self):
''' Кошельки получатели удаляются из списка tasks '''
wallet = FullWallet(RootWallet(), 1000, APP.test_client())
APP.test_client().put(
'/wallet/%s' % wallet.id(),
data=str(WalletString(wallet))
)
response = APP.test_client().get('/tasks')
assert not any(
t['id'] == wallet.id()
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_dst_wallet_from_wanted_if_not_match(self):
'''
Кошельки получатели удаляются из списка tasks,
даже если не совпадает префикс
'''
src = FullWallet(RootWallet(), 1000, APP.test_client())
dst = FakeWallet()
transaction = BadPrefixTransaction(src, dst, -100)
APP.test_client().put(
'/wallet/%s' % src.id(),
data=str(WalletString(TransactionWallet(src, transaction)))
)
APP.test_client().put('/wallet/%s' % dst.id(), data=str(WalletString(dst)))
response = APP.test_client().get('/tasks')
assert not any(
t['id'] in [src.id(), dst.id()]
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_src_wallet_to_wanted(self):
''' Кошельки отправители помещаются в список tasks '''
src = FakeWallet()
dst = IncomeWallet(src, 1500)
APP.test_client().put('/wallet/%s' % dst.id(), data=str(WalletString(dst)))
response = APP.test_client().get('/tasks')
assert any(
t['id'] == src.id() and t.get('who', None) == dst.id()
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_src_wallet_wanted_once(self):
'''
Известные отправители отправители помещаются в список tasks
только один раз
'''
src = FakeWallet()
dst = IncomeWallet(src, 777)
APP.test_client().put('/wallet/%s' % dst.id(), data=str(WalletString(dst)))
APP.test_client().put('/wallet/%s' % dst.id(), data=str(WalletString(dst)))
response = APP.test_client().get('/tasks')
assert len([
t
for t in response.json['tasks']
if t['type'] == 'wanted' and t['id'] == src.id()
]) == 1
def test_src_wallet_to_wanted_alone(self):
'''
Ищем кошелек только один раз, не зависимо от того,
для каких еще кошельков он нужен
'''
src = FakeWallet()
dst1 = IncomeWallet(src, 1500)
APP.test_client().put('/wallet/%s' % dst1.id(), data=str(WalletString(dst1)))
dst2 = IncomeWallet(src, 100)
APP.test_client().put('/wallet/%s' % dst2.id(), data=str(WalletString(dst2)))
response = APP.test_client().get('/tasks')
assert len([
t
for t in response.json['tasks']
if t['type'] == 'wanted' and t['id'] == src.id()
]) == 1
def test_known_src_wallet_not_wanted(self):
''' Известные отправители отправители не помещаются в список tasks '''
src_wallet = FullWallet(RootWallet(), 1000, APP.test_client())
wallet = FakeWallet()
dst_wallet = FakeWallet()
src_transaction = FakeTransaction(src_wallet, dst_wallet, -777)
transaction = FakeTransaction(wallet, dst_wallet, -1500)
APP.test_client().put(
'/wallet/%s' % src_wallet.id(),
data=str(WalletString(TransactionWallet(src_wallet, src_transaction)))
)
APP.test_client().put(
'/wallet/%s' % dst_wallet.id(),
data=str(WalletString(TransactionWallet(
dst_wallet,
IncomingTransaction(src_wallet, src_transaction),
IncomingTransaction(wallet, transaction),
)))
)
response = APP.test_client().get('/tasks')
assert not any(
t['id'] == src_wallet.id()
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_src_wallet_from_wanted_trivial(self):
''' Кошелек убирается из поиска, простой случай '''
src = FullWallet(RootWallet(), 3000, APP.test_client())
dst = FakeWallet()
transaction = FakeTransaction(src, dst, -777)
APP.test_client().put(
'/wallet/%s' % dst.id(),
data=str(WalletString(TransactionWallet(
dst,
IncomingTransaction(src, transaction)
)))
)
APP.test_client().put(
'/wallet/%s' % src.id(),
data=str(WalletString(TransactionWallet(src, transaction)))
)
response = APP.test_client().get('/tasks')
assert not any(
t['id'] == src.id()
for t in response.json['tasks']
if t['type'] == 'wanted'
)
def test_src_wallet_from_wanted_anyway(self):
'''
В случае загрузки кошелька он удаляется из поиска,
даже если искомых транзакций там нет.
'''
src = FakeWallet()
dst = IncomeWallet(src, 555)
APP.test_client().put('/wallet/%s' % dst.id(), data=str(WalletString(dst)))
APP.test_client().put('/wallet/%s' % src.id(), data=str(WalletString(src)))
response = APP.test_client().get('/tasks')
assert not any(
t['id'] == src.id()
for t in response.json['tasks']
if t['type'] == 'wanted'
)