Skip to content

Commit fc1eb03

Browse files
authored
[Tests] Messages API tests (#261)
Added basic tests for the /api/v0/messages.json endpoint. Improved the app creation flow: we now create the app inside the `create_app` function. This is required to run several API unit tests, as pytest resets the event loop on each run.
1 parent ea83599 commit fc1eb03

File tree

5 files changed

+381
-50
lines changed

5 files changed

+381
-50
lines changed

src/aleph/commands.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from aleph.services import p2p
3434
from aleph.services.keys import generate_keypair, save_keys
3535
from aleph.services.p2p import singleton
36-
from aleph.web import app, init_cors
36+
from aleph.web import app
3737

3838
__author__ = "Moshe Malawach"
3939
__copyright__ = "Moshe Malawach"
@@ -49,9 +49,6 @@ async def run_server(
4949
from aiohttp import web
5050
from aleph.web.controllers.listener import broadcast
5151

52-
LOGGER.debug("Initializing CORS")
53-
init_cors()
54-
5552
LOGGER.debug("Setup of runner")
5653

5754
app["config"] = config
@@ -175,7 +172,6 @@ async def main(args):
175172
model.init_db(config, ensure_indexes=True)
176173
LOGGER.info("Database initialized.")
177174

178-
init_cors() # FIXME: This is stateful and process-dependent
179175
set_start_method("spawn")
180176

181177
with Manager() as shared_memory_manager:

src/aleph/web/__init__.py

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,67 @@
88
import pkg_resources
99
import socketio
1010
from aiohttp import web
11+
1112
from aleph.web.controllers.routes import register_routes
1213

13-
app = web.Application(client_max_size=1024 ** 2 * 64)
14-
sio = socketio.AsyncServer(async_mode="aiohttp", cors_allowed_origins="*")
15-
auth = None
16-
17-
# Configure default CORS settings.
18-
cors = aiohttp_cors.setup(
19-
app,
20-
defaults={
21-
"*": aiohttp_cors.ResourceOptions(
22-
allow_methods=["GET", "POST"],
23-
allow_credentials=True,
24-
expose_headers="*",
25-
allow_headers="*",
26-
)
27-
},
28-
)
29-
sio.attach(app)
30-
31-
tpl_path = pkg_resources.resource_filename("aleph.web", "templates")
32-
JINJA_LOADER = jinja2.ChoiceLoader(
33-
[
34-
jinja2.FileSystemLoader(tpl_path),
35-
]
36-
)
37-
aiohttp_jinja2.setup(app, loader=JINJA_LOADER)
38-
env = aiohttp_jinja2.get_env(app)
39-
env.globals.update(
40-
{
41-
"app": app,
42-
"date": date,
43-
"datetime": datetime,
44-
"time": time,
45-
"timedelta": timedelta,
46-
"int": int,
47-
"float": float,
48-
"len": len,
49-
"pprint": pprint,
50-
}
51-
)
52-
53-
register_routes(app)
54-
55-
56-
def init_cors():
14+
15+
def init_cors(app: web.Application):
16+
# Configure default CORS settings.
17+
cors = aiohttp_cors.setup(
18+
app,
19+
defaults={
20+
"*": aiohttp_cors.ResourceOptions(
21+
allow_methods=["GET", "POST"],
22+
allow_credentials=True,
23+
expose_headers="*",
24+
allow_headers="*",
25+
)
26+
},
27+
)
28+
5729
# Configure CORS on all routes.
5830
for route in list(app.router.routes()):
5931
if "/socket.io/" not in repr(route.resource):
6032
cors.add(route)
33+
34+
35+
def init_sio(app: web.Application) -> socketio.AsyncServer:
36+
sio = socketio.AsyncServer(async_mode="aiohttp", cors_allowed_origins="*")
37+
sio.attach(app)
38+
return sio
39+
40+
41+
def create_app() -> web.Application:
42+
app = web.Application(client_max_size=1024 ** 2 * 64)
43+
44+
tpl_path = pkg_resources.resource_filename("aleph.web", "templates")
45+
jinja_loader = jinja2.ChoiceLoader(
46+
[
47+
jinja2.FileSystemLoader(tpl_path),
48+
]
49+
)
50+
aiohttp_jinja2.setup(app, loader=jinja_loader)
51+
env = aiohttp_jinja2.get_env(app)
52+
env.globals.update(
53+
{
54+
"app": app,
55+
"date": date,
56+
"datetime": datetime,
57+
"time": time,
58+
"timedelta": timedelta,
59+
"int": int,
60+
"float": float,
61+
"len": len,
62+
"pprint": pprint,
63+
}
64+
)
65+
66+
register_routes(app)
67+
68+
init_cors(app)
69+
70+
return app
71+
72+
73+
app = create_app()
74+
sio = init_sio(app)
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
[
2+
{
3+
"chain": "ETH",
4+
"channel": "unit-tests",
5+
"sender": "0x696879aE4F6d8DaDD5b8F1cbb1e663B89b08f106",
6+
"type": "POST",
7+
"time": 1652126646.5008686,
8+
"item_content": "{\"address\":\"0x696879aE4F6d8DaDD5b8F1cbb1e663B89b08f106\",\"time\":1652126646.5007327,\"content\":{\"title\":\"My first blog post using Aleph.im\",\"body\":\"Using Aleph.im, we can make a decentralized blog.\"},\"type\":\"test\"}",
9+
"item_hash": "4c33dd1ebf61bbb4342d8258b591fcd52cca73fd7c425542f78311d8f45ba274",
10+
"signature": "0x999ab556b92351e6edf894b4a67f01f0344c7023883eb5bafdf4cd0b98ca91781692ac6b95246c1bf940eedcedfd6dc04751accfbc417ee1b1ae13893634e7eb1c",
11+
"confirmed": false,
12+
"content": {
13+
"address": "0x696879aE4F6d8DaDD5b8F1cbb1e663B89b08f106",
14+
"time": 1652126646.5007327,
15+
"content": {
16+
"title": "My first blog post using Aleph.im",
17+
"body": "Using Aleph.im, we can make a decentralized blog."
18+
},
19+
"type": "test"
20+
}
21+
},
22+
{
23+
"chain": "ETH",
24+
"channel": "unit-tests",
25+
"sender": "0x696879aE4F6d8DaDD5b8F1cbb1e663B89b08f106",
26+
"type": "STORE",
27+
"time": 1652126721.497669,
28+
"item_content": "{\"address\":\"0x696879aE4F6d8DaDD5b8F1cbb1e663B89b08f106\",\"time\":1652126721.4974446,\"item_type\":\"storage\",\"item_hash\":\"5ccdd7bccfbc5955e2e40166dd0cdea0b093154fd87bc2bea57e7c768cde2f21\",\"mime_type\":\"text/plain\"}",
29+
"item_hash": "2953f0b52beb79fc0ed1bc455346fdcb530611605e16c636778a0d673d7184af",
30+
"signature": "0xa10129dd561c1bc93e8655daf09520e9f1694989263e25f330b403ad33563f4b64c9ae18f6cbfb33e8a47a095be7a181b140a369e6205fd04eef55397624a7121b",
31+
"content": {
32+
"address": "0x696879aE4F6d8DaDD5b8F1cbb1e663B89b08f106",
33+
"time": 1652126721.4974446,
34+
"item_type": "storage",
35+
"item_hash": "5ccdd7bccfbc5955e2e40166dd0cdea0b093154fd87bc2bea57e7c768cde2f21",
36+
"mime_type": "text/plain"
37+
}
38+
},
39+
{
40+
"chain": "ETH",
41+
"item_hash": "88a045b90b48c590748a607690fdf85b94b7be4d63940bf4835828b3254b4265",
42+
"sender": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
43+
"type": "POST",
44+
"channel": "INTEGRATION_TESTS",
45+
"confirmed": true,
46+
"content": {
47+
"address": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
48+
"time": 1648215810.245091,
49+
"type": "POST"
50+
},
51+
"item_content": "{\"address\":\"0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4\",\"time\":1648215810.245091,\"type\":\"POST\"}",
52+
"item_type": "inline",
53+
"signature": "0x2acfc49c4709a97bb63fd63277304e54b474912785a48efdcd0ee4571f4b42a8730fda00a6c7bdaf20c42f92c8ff5449824a668ab1897359f4a0e503c335b8a21b",
54+
"size": 95,
55+
"time": 1648215810.2451403,
56+
"confirmations": [
57+
{
58+
"chain": "ETH",
59+
"height": 14474458,
60+
"hash": "0xbb123b07933ce8d1d49c2ba287fb7942f272359a7f220b7fc2b662c65c960ccc"
61+
}
62+
]
63+
},
64+
{
65+
"chain": "ETH",
66+
"item_hash": "bc411ae2ba89289458d0168714457e7c9394a29ca83159240585591f4f46444a",
67+
"sender": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
68+
"type": "POST",
69+
"channel": "INTEGRATION_TESTS",
70+
"confirmed": true,
71+
"content": {
72+
"address": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
73+
"time": 1648215810.1763275,
74+
"type": "POST"
75+
},
76+
"item_content": "{\"address\":\"0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4\",\"time\":1648215810.1763275,\"type\":\"POST\"}",
77+
"item_type": "inline",
78+
"signature": "0x1fdb9f188490ad82c093dcf44dbc4bae7665b11c85d55b6e2e6d78565a1967de0a7dc03720fe56f340154f8d5d361ba95bca9e40f910f2834f62d2c4416c77a31c",
79+
"size": 96,
80+
"time": 1648215810.1763897,
81+
"confirmations": [
82+
{
83+
"chain": "ETH",
84+
"height": 14474458,
85+
"hash": "0xbb123b07933ce8d1d49c2ba287fb7942f272359a7f220b7fc2b662c65c960ccc"
86+
}
87+
]
88+
},
89+
{
90+
"chain": "ETH",
91+
"item_hash": "3cbb3e09a61429e8ecd21ee392a1543ef79934ef39338d01a6c771b17a46c274",
92+
"sender": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
93+
"type": "FORGET",
94+
"channel": "INTEGRATION_TESTS",
95+
"confirmed": true,
96+
"content": {
97+
"address": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
98+
"time": 1648215810.1041584,
99+
"hashes": [
100+
"448b3c6f6455e6f4216b01b43522bddc3564a14c04799ed0ce8af4857c7ba15f"
101+
],
102+
"reason": "I want to remember this post. Maybe I can forget I forgot it?"
103+
},
104+
"item_content": "{\"address\":\"0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4\",\"time\":1648215810.1041584,\"hashes\":[\"448b3c6f6455e6f4216b01b43522bddc3564a14c04799ed0ce8af4857c7ba15f\"],\"reason\":\"I want to remember this post. Maybe I can forget I forgot it?\"}",
105+
"item_type": "inline",
106+
"signature": "0x03563d1fbec46358af7ec9d577a57f348dd57a9d7f84b63e5bfb8112a9aca48b120d2913c96a89c98745ffb6f65a4c63c71ae16d017f0ae75c4a5a956182e25a1b",
107+
"size": 233,
108+
"time": 1648215810.104279,
109+
"confirmations": [
110+
{
111+
"chain": "ETH",
112+
"height": 14474458,
113+
"hash": "0xbb123b07933ce8d1d49c2ba287fb7942f272359a7f220b7fc2b662c65c960ccc"
114+
}
115+
]
116+
},
117+
{
118+
"chain": "ETH",
119+
"item_hash": "448b3c6f6455e6f4216b01b43522bddc3564a14c04799ed0ce8af4857c7ba15f",
120+
"sender": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
121+
"type": "FORGET",
122+
"channel": "INTEGRATION_TESTS",
123+
"confirmed": true,
124+
"content": {
125+
"address": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
126+
"time": 1648215809.0270267,
127+
"hashes": [
128+
"fea0e00f73102aa951794a3ea85f6f1bbfd3decb804fb73232f2a645a379ae54"
129+
],
130+
"reason": "This well thought-out content offends me!"
131+
},
132+
"item_content": "{\"address\":\"0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4\",\"time\":1648215809.0270267,\"hashes\":[\"fea0e00f73102aa951794a3ea85f6f1bbfd3decb804fb73232f2a645a379ae54\"],\"reason\":\"This well thought-out content offends me!\"}",
133+
"item_type": "inline",
134+
"signature": "0x3619c016987c4221c85842ce250f3e50a9b8e42c04d4f9fbdfdfad9941d6c5195a502a4f63289429513bf152d24d0a7bb0533701ec3c7bbca91b18ce7eaa7dee1b",
135+
"size": 213,
136+
"time": 1648215809.027116,
137+
"confirmations": [
138+
{
139+
"chain": "ETH",
140+
"height": 14474458,
141+
"hash": "0xbb123b07933ce8d1d49c2ba287fb7942f272359a7f220b7fc2b662c65c960ccc"
142+
}
143+
]
144+
},
145+
{
146+
"chain": "ETH",
147+
"item_hash": "fea0e00f73102aa951794a3ea85f6f1bbfd3decb804fb73232f2a645a379ae54",
148+
"sender": "0xaC033C1cA5C49Eff98A1D9a56BeDBC4840010BA4",
149+
"type": "POST",
150+
"channel": "INTEGRATION_TESTS",
151+
"confirmed": true,
152+
"content": null,
153+
"item_content": null,
154+
"item_type": "inline",
155+
"signature": "0x1a2bddb50e94e4491dec6b555faf70716d0b18ffa48176857d0d7ca3ff9292be1e619679fda95d50e58250a703c295c02bb5d90833d3345d3238b173f19fea2c1c",
156+
"size": 151,
157+
"time": 1648215808.9630325,
158+
"confirmations": [
159+
{
160+
"chain": "ETH",
161+
"height": 14474458,
162+
"hash": "0xbb123b07933ce8d1d49c2ba287fb7942f272359a7f220b7fc2b662c65c960ccc"
163+
}
164+
],
165+
"forgotten_by": [
166+
"448b3c6f6455e6f4216b01b43522bddc3564a14c04799ed0ce8af4857c7ba15f"
167+
]
168+
},
169+
{
170+
"chain": "ETH",
171+
"item_hash": "de4499ec1c0d7e60380d6b538c1ca58220b34224272b8164d144712d5a334097",
172+
"sender": "0x720F319A9c3226dCDd7D8C49163D79EDa1084E98",
173+
"type": "AGGREGATE",
174+
"channel": "aggregate-tests",
175+
"confirmed": true,
176+
"content": {
177+
"address": "0x720F319A9c3226dCDd7D8C49163D79EDa1084E98",
178+
"time": 1648818372.840815,
179+
"key": "test_od",
180+
"content": {
181+
"a": 2,
182+
"b": 3
183+
}
184+
},
185+
"item_content": "{\"address\":\"0x720F319A9c3226dCDd7D8C49163D79EDa1084E98\",\"time\":1648818372.840815,\"key\":\"test_od\",\"content\":{\"a\":2,\"b\":3}}",
186+
"item_type": "inline",
187+
"signature": "0xbccfd10ca6da8e2959783a689cb46c77cbe9fe0b3407f18cd7a5bc6ba0da2bae57158b7c837080daa9e62b67ecccb12910297d749e9cec63ea58220c2dcd91221c",
188+
"size": 121,
189+
"time": 1648818372.8409374,
190+
"confirmations": [
191+
{
192+
"chain": "ETH",
193+
"height": 14500712,
194+
"hash": "0x39e581f14409008fb7d3663c0a24bb5035aff1edbc30d024168ce9426b631e7c"
195+
}
196+
]
197+
},
198+
{
199+
"chain": "ETH",
200+
"item_hash": "9200cfab5950e5d173f07d7c61bb0524675d0305e808590e7d0a0752ce65f791",
201+
"sender": "0x720F319A9c3226dCDd7D8C49163D79EDa1084E98",
202+
"type": "AGGREGATE",
203+
"channel": "aggregate-tests",
204+
"confirmed": false,
205+
"content": {
206+
"address": "0x720F319A9c3226dCDd7D8C49163D79EDa1084E98",
207+
"time": 1648818417.7640934,
208+
"key": "test_od",
209+
"content": {
210+
"a": 1,
211+
"b": 23
212+
}
213+
},
214+
"item_content": "{\"address\":\"0x720F319A9c3226dCDd7D8C49163D79EDa1084E98\",\"time\":1648818417.7640934,\"key\":\"test_od\",\"content\":{\"a\":1,\"b\":23}}",
215+
"item_type": "inline",
216+
"signature": "0x94e4e6e121c88401b5ea55c2ec4a94ebb98856fe6775fb30b3ac62d183798d7b4323fed7aaead6cbfd0d4e5b1409f15df188ed7f3be969506af40fc0e8d2e8261b",
217+
"size": 123,
218+
"time": 1648818417.7642117,
219+
"confirmations": [
220+
{
221+
"chain": "ETH",
222+
"height": 14500712,
223+
"hash": "0x39e581f14409008fb7d3663c0a24bb5035aff1edbc30d024168ce9426b631e7c"
224+
}
225+
]
226+
}
227+
]

0 commit comments

Comments
 (0)