Skip to content

Commit d24b68a

Browse files
committed
Fix bug with validationerror not being raised
Added tests to check that all requests returns an error when the data is incorrect
1 parent 521a15f commit d24b68a

File tree

10 files changed

+498
-92
lines changed

10 files changed

+498
-92
lines changed

_ucoinpy_test/api/bma/test_blockchain.py

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import unittest
22
import jsonschema
3+
from _ucoinpy_test.api.webserver import WebFunctionalSetupMixin, web, asyncio
34
from ucoinpy.api.bma.blockchain import Parameters, Block, Current, Hardship, Membership, Newcomers, \
45
Certifications, Joiners, Actives, Leavers, UD, TX
56

67

7-
class Test_BMA_Blockchain(unittest.TestCase):
8+
class Test_BMA_Blockchain(WebFunctionalSetupMixin, unittest.TestCase):
89
def test_parameters(self):
910
json_sample = {
1011
"currency": "meta_brouzouf",
@@ -25,6 +26,22 @@ def test_parameters(self):
2526
}
2627
jsonschema.validate(json_sample, Parameters.schema)
2728

29+
def test_parameters_bad(self):
30+
@asyncio.coroutine
31+
def handler(request):
32+
yield from request.read()
33+
return web.Response(body=b'{}', content_type='application/json')
34+
35+
@asyncio.coroutine
36+
def go():
37+
_, srv, url = yield from self.create_server('GET', '/', handler)
38+
params = Parameters(None)
39+
params.reverse_url = lambda path: url
40+
with self.assertRaises(jsonschema.exceptions.ValidationError):
41+
yield from params.get()
42+
43+
self.loop.run_until_complete(go())
44+
2845
def test_schema_block_0(self):
2946
json_sample = {
3047
"version": 1,
@@ -79,6 +96,38 @@ def test_schema_block_0(self):
7996
jsonschema.validate(json_sample, Block.schema)
8097
jsonschema.validate(json_sample, Current.schema)
8198

99+
def test_block_bad(self):
100+
@asyncio.coroutine
101+
def handler(request):
102+
yield from request.read()
103+
return web.Response(body=b'{}', content_type='application/json')
104+
105+
@asyncio.coroutine
106+
def go():
107+
_, srv, url = yield from self.create_server('GET', '/100', handler)
108+
block = Block(None, 100)
109+
block.reverse_url = lambda path: url
110+
with self.assertRaises(jsonschema.exceptions.ValidationError):
111+
yield from block.get()
112+
113+
self.loop.run_until_complete(go())
114+
115+
def test_current_bad(self):
116+
@asyncio.coroutine
117+
def handler(request):
118+
yield from request.read()
119+
return web.Response(body=b'{}', content_type='application/json')
120+
121+
@asyncio.coroutine
122+
def go():
123+
_, srv, url = yield from self.create_server('GET', '/', handler)
124+
current = Current(None)
125+
current.reverse_url = lambda path: url
126+
with self.assertRaises(jsonschema.exceptions.ValidationError):
127+
yield from current.get()
128+
129+
self.loop.run_until_complete(go())
130+
82131
def test_schema_block(self):
83132
json_sample = {
84133
"version": 1,
@@ -125,6 +174,22 @@ def test_schema_hardship(self):
125174
}
126175
jsonschema.validate(json_sample, Hardship.schema)
127176

177+
def test_hardship_bad(self):
178+
@asyncio.coroutine
179+
def handler(request):
180+
yield from request.read()
181+
return web.Response(body=b'{}', content_type='application/json')
182+
183+
@asyncio.coroutine
184+
def go():
185+
_, srv, url = yield from self.create_server('GET', '/fingerprint', handler)
186+
hardship = Hardship(None, "fingerprint")
187+
hardship.reverse_url = lambda path: url
188+
with self.assertRaises(jsonschema.exceptions.ValidationError):
189+
yield from hardship.get()
190+
191+
self.loop.run_until_complete(go())
192+
128193
def test_schema_membership(self):
129194
json_sample = {
130195
"pubkey": "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU",
@@ -149,6 +214,22 @@ def test_schema_membership(self):
149214
}
150215
jsonschema.validate(json_sample, Membership.schema)
151216

217+
def test_membership_bad(self):
218+
@asyncio.coroutine
219+
def handler(request):
220+
yield from request.read()
221+
return web.Response(body=b'{}', content_type='application/json')
222+
223+
@asyncio.coroutine
224+
def go():
225+
_, srv, url = yield from self.create_server('GET', '/pubkey', handler)
226+
membership = Membership(None, "pubkey")
227+
membership.reverse_url = lambda path: url
228+
with self.assertRaises(jsonschema.exceptions.ValidationError):
229+
yield from membership.get()
230+
231+
self.loop.run_until_complete(go())
232+
152233
def test_schema_newcomers(self):
153234
json_sample = {
154235
"result": {
@@ -157,6 +238,22 @@ def test_schema_newcomers(self):
157238
}
158239
jsonschema.validate(json_sample, Newcomers.schema)
159240

241+
def test_newcomers_bad(self):
242+
@asyncio.coroutine
243+
def handler(request):
244+
yield from request.read()
245+
return web.Response(body=b'{}', content_type='application/json')
246+
247+
@asyncio.coroutine
248+
def go():
249+
_, srv, url = yield from self.create_server('GET', '/', handler)
250+
newcomers = Newcomers(None)
251+
newcomers.reverse_url = lambda path: url
252+
with self.assertRaises(jsonschema.exceptions.ValidationError):
253+
yield from newcomers.get()
254+
255+
self.loop.run_until_complete(go())
256+
160257
def test_schema_certifications(self):
161258
json_sample = {
162259
"result": {
@@ -165,6 +262,22 @@ def test_schema_certifications(self):
165262
}
166263
jsonschema.validate(json_sample, Certifications.schema)
167264

265+
def test_certifications_bad(self):
266+
@asyncio.coroutine
267+
def handler(request):
268+
yield from request.read()
269+
return web.Response(body=b'{}', content_type='application/json')
270+
271+
@asyncio.coroutine
272+
def go():
273+
_, srv, url = yield from self.create_server('GET', '/', handler)
274+
certs = Certifications(None)
275+
certs.reverse_url = lambda path: url
276+
with self.assertRaises(jsonschema.exceptions.ValidationError):
277+
yield from certs.get()
278+
279+
self.loop.run_until_complete(go())
280+
168281
def test_schema_joiners(self):
169282
json_sample = {
170283
"result": {
@@ -173,6 +286,22 @@ def test_schema_joiners(self):
173286
}
174287
jsonschema.validate(json_sample, Joiners.schema)
175288

289+
def test_joiners_bad(self):
290+
@asyncio.coroutine
291+
def handler(request):
292+
yield from request.read()
293+
return web.Response(body=b'{}', content_type='application/json')
294+
295+
@asyncio.coroutine
296+
def go():
297+
_, srv, url = yield from self.create_server('GET', '/', handler)
298+
joiners = Joiners(None)
299+
joiners.reverse_url = lambda path: url
300+
with self.assertRaises(jsonschema.exceptions.ValidationError):
301+
yield from joiners.get()
302+
303+
self.loop.run_until_complete(go())
304+
176305
def test_schema_actives(self):
177306
json_sample = {
178307
"result": {
@@ -181,6 +310,22 @@ def test_schema_actives(self):
181310
}
182311
jsonschema.validate(json_sample, Actives.schema)
183312

313+
def test_actives_bad(self):
314+
@asyncio.coroutine
315+
def handler(request):
316+
yield from request.read()
317+
return web.Response(body=b'{}', content_type='application/json')
318+
319+
@asyncio.coroutine
320+
def go():
321+
_, srv, url = yield from self.create_server('GET', '/', handler)
322+
actives = Actives(None)
323+
actives.reverse_url = lambda path: url
324+
with self.assertRaises(jsonschema.exceptions.ValidationError):
325+
yield from actives.get()
326+
327+
self.loop.run_until_complete(go())
328+
184329
def test_schema_leavers(self):
185330
json_sample = {
186331
"result": {
@@ -189,6 +334,22 @@ def test_schema_leavers(self):
189334
}
190335
jsonschema.validate(json_sample, Leavers.schema)
191336

337+
def test_leavers_bad(self):
338+
@asyncio.coroutine
339+
def handler(request):
340+
yield from request.read()
341+
return web.Response(body=b'{}', content_type='application/json')
342+
343+
@asyncio.coroutine
344+
def go():
345+
_, srv, url = yield from self.create_server('GET', '/', handler)
346+
leavers = Leavers(None)
347+
leavers.reverse_url = lambda path: url
348+
with self.assertRaises(jsonschema.exceptions.ValidationError):
349+
yield from leavers.get()
350+
351+
self.loop.run_until_complete(go())
352+
192353
def test_schema_ud(self):
193354
json_sample = {
194355
"result": {
@@ -197,10 +358,42 @@ def test_schema_ud(self):
197358
}
198359
jsonschema.validate(json_sample, UD.schema)
199360

361+
def test_ud_bad(self):
362+
@asyncio.coroutine
363+
def handler(request):
364+
yield from request.read()
365+
return web.Response(body=b'{}', content_type='application/json')
366+
367+
@asyncio.coroutine
368+
def go():
369+
_, srv, url = yield from self.create_server('GET', '/', handler)
370+
ud = UD(None)
371+
ud.reverse_url = lambda path: url
372+
with self.assertRaises(jsonschema.exceptions.ValidationError):
373+
yield from ud.get()
374+
375+
self.loop.run_until_complete(go())
376+
200377
def test_schema_tx(self):
201378
json_sample = {
202379
"result": {
203380
"blocks": [223, 813]
204381
}
205382
}
206383
jsonschema.validate(json_sample, TX.schema)
384+
385+
def test_tx_bad(self):
386+
@asyncio.coroutine
387+
def handler(request):
388+
yield from request.read()
389+
return web.Response(body=b'{}', content_type='application/json')
390+
391+
@asyncio.coroutine
392+
def go():
393+
_, srv, url = yield from self.create_server('GET', '/', handler)
394+
tx = TX(None)
395+
tx.reverse_url = lambda path: url
396+
with self.assertRaises(jsonschema.exceptions.ValidationError):
397+
yield from tx.get()
398+
399+
self.loop.run_until_complete(go())

_ucoinpy_test/api/bma/test_network.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import unittest
22
import jsonschema
33
from ucoinpy.api.bma.network import Peering
4-
from ucoinpy.api.bma.network.peering import Peers, Status
4+
from _ucoinpy_test.api.webserver import WebFunctionalSetupMixin, web, asyncio
5+
from ucoinpy.api.bma.network.peering import Peers
56

67

7-
class Test_BMA_Network(unittest.TestCase):
8-
8+
class Test_BMA_Network(WebFunctionalSetupMixin, unittest.TestCase):
9+
910
def test_peering(self):
1011
json_sample = {
1112
"version": "1",
@@ -20,6 +21,21 @@ def test_peering(self):
2021
}
2122
jsonschema.validate(json_sample, Peering.schema)
2223

24+
def test_peering_bad(self):
25+
@asyncio.coroutine
26+
def handler(request):
27+
yield from request.read()
28+
return web.Response(body=b'{}', content_type='application/json')
29+
30+
@asyncio.coroutine
31+
def go():
32+
_, srv, url = yield from self.create_server('GET', '/', handler)
33+
peering = Peering(None)
34+
peering.reverse_url = lambda path: url
35+
with self.assertRaises(jsonschema.exceptions.ValidationError):
36+
yield from peering.get()
37+
38+
self.loop.run_until_complete(go())
2339

2440
def test_peers_root(self):
2541
json_sample = {
@@ -45,4 +61,20 @@ def test_peers_leaf(self):
4561
"signature": "42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r"
4662
}
4763
}
48-
jsonschema.validate(json_sample, Peers.schema)
64+
jsonschema.validate(json_sample, Peers.schema)
65+
66+
def test_peers_bad(self):
67+
@asyncio.coroutine
68+
def handler(request):
69+
yield from request.read()
70+
return web.Response(body=b'{}', content_type='application/json')
71+
72+
@asyncio.coroutine
73+
def go():
74+
_, srv, url = yield from self.create_server('GET', '/', handler)
75+
peers = Peers(None)
76+
peers.reverse_url = lambda path: url
77+
with self.assertRaises(jsonschema.exceptions.ValidationError):
78+
yield from peers.get()
79+
80+
self.loop.run_until_complete(go())

0 commit comments

Comments
 (0)