-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_operation.py
More file actions
278 lines (215 loc) · 9.16 KB
/
test_operation.py
File metadata and controls
278 lines (215 loc) · 9.16 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import typing
import pytest
from ellar.socket_io import TestGateway
from ellar.socket_io.testing.module import RunWithServerContext
from ..utils import pydantic_error_url
from .sample import EventGateway, GatewayOthers, GatewayWithGuards
# @pytest.fixture()
# from asgi_lifespan import LifespanManager
# from httpx import AsyncClient
# async def client() -> AsyncIterator[AsyncClient]:
# async with LifespanManager(app):
# async with AsyncClient(app=app, base_url="http://localhost") as client:
# yield client
@pytest.mark.asyncio
class TestEventGateway:
test_client = TestGateway.create_test_module(controllers=[EventGateway])
async def test_socket_connection_work(self, unused_tcp_port):
my_response_message = []
connected_called = False
disconnected_called = False
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
@ctx.sio.event
async def my_response(message):
my_response_message.append(message)
@ctx.sio.event
async def disconnect():
nonlocal disconnected_called
disconnected_called = True
@ctx.sio.event
async def connect(*args):
nonlocal connected_called
await ctx.sio.emit("my_event", {"data": "I'm connected!"})
connected_called = True
await ctx.connect(socketio_path="/ws/")
await ctx.wait()
assert len(my_response_message) == 2
assert my_response_message == [
{"data": "Connected", "count": 0},
{"data": "I'm connected!"},
]
assert disconnected_called and connected_called
async def test_broadcast_work(self, unused_tcp_port):
sio_1_response_message = []
sio_2_response_message = []
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
ctx_2 = ctx.new_socket_client_context()
@ctx.sio.on("my_response")
async def my_response_case_1(message):
sio_1_response_message.append(message)
@ctx_2.sio.on("my_response")
async def my_response_case_2(message):
sio_2_response_message.append(message)
await ctx.connect(socketio_path="/ws/")
await ctx_2.connect(socketio_path="/ws/")
await ctx.sio.emit(
"my_broadcast_event", {"data": "Testing Broadcast"}
) # both sio_1 and sio_2 would receive this message
await ctx.wait()
await ctx_2.wait()
assert len(sio_1_response_message) == 2
assert sio_1_response_message == [
{"data": "Connected", "count": 0},
{"data": "Testing Broadcast"},
]
assert len(sio_2_response_message) == 2
assert sio_2_response_message == [
{"data": "Connected", "count": 0},
{"data": "Testing Broadcast"},
]
@pytest.mark.asyncio
class TestGatewayWithGuards:
test_client = TestGateway.create_test_module(controllers=[GatewayWithGuards])
async def test_socket_connection_work(self, unused_tcp_port):
my_response_message = []
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
@ctx.sio.event
async def my_response(message):
my_response_message.append(message)
await ctx.connect(
socketio_path="/ws-guard/", headers={"x-auth-key": "supersecret"}
)
await ctx.sio.emit("my_event", {"data": "Testing Broadcast"})
await ctx.wait()
assert my_response_message == [
{"auth-key": "supersecret", "data": "Testing Broadcast"}
]
async def test_event_with_header_work(self, unused_tcp_port):
my_response_message = []
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
@ctx.sio.event
async def my_response(message):
my_response_message.append(message)
await ctx.connect(
socketio_path="/ws-guard/", headers={"x-auth-key": "supersecret"}
)
await ctx.sio.emit("my_event_header", {"data": "Testing Broadcast"})
await ctx.wait()
assert my_response_message == [
{"data": "Testing Broadcast", "x_auth_key": "supersecret"}
]
async def test_event_with_plain_response(self, unused_tcp_port):
my_response_message = []
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
@ctx.sio.on("my_plain_response")
async def message_receive(message):
my_response_message.append(message)
await ctx.connect(
socketio_path="/ws-guard/", headers={"x-auth-key": "supersecret"}
)
await ctx.sio.emit("my_plain_response", {"data": "Testing Broadcast"})
await ctx.wait()
assert my_response_message == [
{"data": "Testing Broadcast", "x_auth_key": "supersecret"}
]
async def test_failed_to_connect(self, unused_tcp_port):
my_response_message = []
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
ctx = typing.cast(RunWithServerContext, ctx)
@ctx.sio.on("error")
async def error(message):
my_response_message.append(message)
await ctx.connect(
socketio_path="/ws-guard/", headers={"x-auth-key": "supersecre"}
)
await ctx.sio.emit("my_event_header", {"data": "Testing Broadcast"})
await ctx.wait()
assert my_response_message == [{"code": 1011, "reason": "Authorization Failed"}]
async def test_failed_process_message_sent(self, unused_tcp_port):
my_response_message = []
async with self.test_client.run_with_server(port=unused_tcp_port) as ctx:
ctx = typing.cast(RunWithServerContext, ctx)
@ctx.sio.on("error")
async def error(message):
my_response_message.append(message)
await ctx.connect(
socketio_path="/ws-guard/", headers={"x-auth-key": "supersecret"}
)
await ctx.sio.emit(
"my_event_header", {"doesnt_exist_key": "Testing Broadcast"}
)
await ctx.wait()
assert my_response_message == [
{
"code": 1007,
"reason": [
{
"input": None,
"loc": ["body", "data"],
"msg": "Field required",
"type": "missing",
"url": pydantic_error_url("missing"),
}
],
}
]
@pytest.mark.asyncio
class TestGatewayExceptions:
@pytest.mark.parametrize(
"debug, result",
[
(
True,
[
{"code": 1011, "reason": "I dont have anything to run."},
{"code": 1009, "reason": "Message is too big"},
],
),
(
False,
[
{"code": 1011, "reason": "Something went wrong"},
{"code": 1009, "reason": "Message is too big"},
],
),
],
)
async def test_exception_handling_works_debug_true_or_false(
self, debug, result, unused_tcp_port
):
test_client = TestGateway.create_test_module(
controllers=[GatewayOthers], config_module={"DEBUG": debug}
)
my_response_message = []
async with test_client.run_with_server(port=unused_tcp_port) as ctx:
ctx = typing.cast(RunWithServerContext, ctx)
ctx2 = ctx.new_socket_client_context()
@ctx.sio.on("error")
async def error(message):
my_response_message.append(message)
@ctx2.sio.on("error")
async def error_2(message):
my_response_message.append(message)
await ctx.connect(socketio_path="/ws-others/")
await ctx2.connect(socketio_path="/ws-others/")
await ctx.sio.emit("my_event", {})
await ctx.wait()
await ctx2.sio.emit("my_event_raise", {})
await ctx2.wait()
assert my_response_message == result
async def test_message_with_extra_args(self, unused_tcp_port):
test_client = TestGateway.create_test_module(controllers=[GatewayOthers])
my_response_message = []
async with test_client.run_with_server(port=unused_tcp_port) as ctx:
ctx = typing.cast(RunWithServerContext, ctx)
@ctx.sio.on("error")
async def error(message):
my_response_message.append(message)
await ctx.connect(
socketio_path="/ws-others/?query1=some-query1&query2=some-query2&?"
)
await ctx.sio.emit("extra_args", {})
await ctx.wait()
assert my_response_message == [
{"code": 1009, "reason": {"query1": "some-query1", "query2": "some-query2"}}
]