forked from kernelci/kernelci-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
318 lines (261 loc) · 8.86 KB
/
conftest.py
File metadata and controls
318 lines (261 loc) · 8.86 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2022 Jeny Sadadia
# Author: Jeny Sadadia <[email protected]>
#
# Copyright (C) 2022, 2023 Collabora Limited
# Author: Jeny Sadadia <[email protected]>
# pylint: disable=protected-access
"""pytest fixtures for KernelCI API"""
from unittest.mock import AsyncMock
import fakeredis.aioredis
from fastapi.testclient import TestClient
from fastapi import Request, HTTPException, status
import pytest
from mongomock_motor import AsyncMongoMockClient
from beanie import init_beanie
from httpx import AsyncClient
from api.main import (
app,
versioned_app,
get_current_user,
get_current_superuser,
)
from api.models import User, Subscription
from api.pubsub import PubSub
BEARER_TOKEN = "Bearer \
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJib2IifQ.\
ci1smeJeuX779PptTkuaG1SEdkp5M1S1AgYvX8VdB20"
ADMIN_BEARER_TOKEN = 'Bearer \
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.\
eyJzdWIiOiJib2IiLCJzY29wZXMiOlsiYWRtaW4iXX0.\
t3bAE-pHSzZaSHp7FMlImqgYvL6f_0xDUD-nQwxEm3k'
API_VERSION = 'latest'
BASE_URL = 'http://testserver/' + API_VERSION + '/'
def mock_get_current_user(request: Request):
"""
Get current active user
"""
token = request.headers.get('authorization')
if not token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing token",
)
return User(
id='65265305c74695807499037f',
username='bob',
hashed_password='$2b$12$CpJZx5ooxM11bCFXT76/z.o6HWs2sPJy4iP8.'
'xCZGmM8jWXUXJZ4L',
email='[email protected]',
is_active=True,
is_superuser=False,
is_verified=True
)
def mock_get_current_admin_user(request: Request):
"""
Get current active admin user
"""
token = request.headers.get('authorization')
if not token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing token",
)
if token != ADMIN_BEARER_TOKEN:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Forbidden",
)
return User(
id='653a5e1a7e9312c86f8f86e1',
username='admin',
hashed_password='$2b$12$CpJZx5ooxM11bCFXT76/z.o6HWs2sPJy4iP8.'
'xCZGmM8jWXUXJZ4K',
email='[email protected]',
groups=[],
is_active=True,
is_superuser=True,
is_verified=True
)
# Mock dependency callables for getting current user
app.dependency_overrides[get_current_user] = mock_get_current_user
app.dependency_overrides[get_current_superuser] = mock_get_current_admin_user
@pytest.fixture(scope='session')
def test_client():
"""Fixture to get FastAPI Test client instance"""
with TestClient(app=versioned_app, base_url=BASE_URL) as client:
yield client
@pytest.fixture
async def test_async_client():
"""Fixture to get Test client for asynchronous tests"""
async with AsyncClient(app=versioned_app, base_url=BASE_URL) as client:
await versioned_app.router.startup()
yield client
await versioned_app.router.shutdown()
@pytest.fixture
def mock_db_create(mocker):
"""Mocks async call to Database class method used to create object"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.create',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_db_count(mocker):
"""Mocks async call to Database class method used to count objects"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.count',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_db_find_by_attributes(mocker):
"""
Mocks async call to Database class method
used to find a list of objects by attributes
"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.find_by_attributes',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_db_find_by_id(mocker):
"""
Mocks async call to Database class method
used to find an object by id
"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.find_by_id',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_db_delete_by_id(mocker):
"""Mocks async call to Database class method used to delete an object"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.delete_by_id',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_db_find_one(mocker):
"""Mocks async call to database method used to find one object"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.find_one',
side_effect=async_mock)
return async_mock
@pytest.fixture(autouse=True)
def mock_init_sub_id(mocker):
"""Mocks async call to PubSub method to initialize subscription id"""
async_mock = AsyncMock()
mocker.patch('api.pubsub.PubSub._init_sub_id',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_listen(mocker):
"""Mocks async call to listen method of PubSub"""
async_mock = AsyncMock()
mocker.patch('api.pubsub.PubSub.listen',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_publish_cloudevent(mocker):
"""
Mocks async call to PubSub class method
used to publish cloud event
"""
async_mock = AsyncMock()
mocker.patch('api.pubsub.PubSub.publish_cloudevent',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_pubsub(mocker):
"""Mocks `_redis` member of PubSub class instance"""
pubsub = PubSub()
redis_mock = fakeredis.aioredis.FakeRedis()
mocker.patch.object(pubsub, '_redis', redis_mock)
return pubsub
@pytest.fixture
def mock_pubsub_subscriptions(mocker):
"""Mocks `_redis` and `_subscriptions` member of PubSub class instance"""
pubsub = PubSub()
redis_mock = fakeredis.aioredis.FakeRedis()
sub = Subscription(id=1, channel='test', user='test')
mocker.patch.object(pubsub, '_redis', redis_mock)
subscriptions_mock = dict(
{1: {'sub': sub, 'redis_sub': pubsub._redis.pubsub()}})
mocker.patch.object(pubsub, '_subscriptions', subscriptions_mock)
return pubsub
@pytest.fixture()
def mock_pubsub_publish(mocker):
"""
Mocks execution of publish_cloudevent
from PubSub class.
"""
pubsub = PubSub()
redis_mock = fakeredis.aioredis.FakeRedis()
mocker.patch.object(pubsub, '_redis', redis_mock)
mocker.patch.object(pubsub._redis, 'execute_command')
return pubsub
@pytest.fixture
def mock_subscribe(mocker):
"""Mocks async call to subscribe method of PubSub"""
async_mock = AsyncMock()
mocker.patch('api.pubsub.PubSub.subscribe',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_unsubscribe(mocker):
"""Mocks async call to unsubscribe method of PubSub"""
async_mock = AsyncMock()
mocker.patch('api.pubsub.PubSub.unsubscribe',
side_effect=async_mock)
return async_mock
@pytest.fixture(autouse=True)
async def mock_init_beanie(mocker):
"""Mocks async call to Database method to initialize Beanie"""
async_mock = AsyncMock()
client = AsyncMongoMockClient()
init = await init_beanie(
document_models=[User], database=client.get_database(name="db"))
mocker.patch('api.db.Database.initialize_beanie',
side_effect=async_mock, return_value=init)
return async_mock
@pytest.fixture
def mock_db_update(mocker):
"""
Mocks async call to Database class method used to update object
"""
async_mock = AsyncMock()
mocker.patch('api.db.Database.update',
side_effect=async_mock)
return async_mock
@pytest.fixture
async def mock_beanie_get_user_by_id(mocker):
"""Mocks async call to external method to get model by id"""
async_mock = AsyncMock()
mocker.patch('fastapi_users_db_beanie.BeanieUserDatabase.get',
side_effect=async_mock)
return async_mock
@pytest.fixture
async def mock_beanie_user_update(mocker):
"""Mocks async call to external method to update user"""
async_mock = AsyncMock()
mocker.patch('fastapi_users_db_beanie.BeanieUserDatabase.update',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_auth_current_user(mocker):
"""
Mocks async call to external method to get authenticated user
"""
async_mock = AsyncMock()
mocker.patch('fastapi_users.authentication.Authenticator._authenticate',
side_effect=async_mock)
return async_mock
@pytest.fixture
def mock_user_find(mocker):
"""
Mocks async call to external method to find user model using Beanie
"""
async_mock = AsyncMock()
mocker.patch('api.models.User.find_one',
side_effect=async_mock)
return async_mock