Skip to content

Commit 461d580

Browse files
committed
fix: auto_apis unit bugs
1 parent 0b0309a commit 461d580

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

tests/conftest.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,38 @@ def user(db) -> User:
2828
def easy_api_client(user) -> Callable:
2929
orig_func = copy.deepcopy(JWTAuthAsync.__call__)
3030

31-
orig_has_perm_fuc = copy.deepcopy(user.has_perm)
32-
33-
def mock_has_perm_true(*args, **kwargs):
34-
return True
35-
36-
def mock_has_perm_false(*args, **kwargs):
37-
return False
38-
39-
async def mock_func(self, request):
40-
setattr(request, "user", user)
41-
return True
42-
43-
setattr(JWTAuthAsync, "__call__", mock_func)
44-
4531
def create_client(
4632
api: Union[EasyAPI, Router, Type[ControllerBase]],
33+
api_user=None, # type: ignore
4734
is_staff: bool = False,
4835
is_superuser: bool = False,
4936
has_perm: bool = False,
5037
) -> "EasyTestClient":
51-
setattr(user, "is_staff", is_staff)
52-
setattr(user, "is_superuser", is_superuser)
38+
if api_user is None:
39+
api_user = user
40+
setattr(api_user, "is_staff", is_staff)
41+
setattr(api_user, "is_superuser", is_superuser)
42+
43+
def mock_has_perm_true(*args, **kwargs):
44+
return True
45+
46+
def mock_has_perm_false(*args, **kwargs):
47+
return False
48+
49+
async def mock_func(self, request):
50+
setattr(request, "user", api_user)
51+
return True
52+
53+
setattr(JWTAuthAsync, "__call__", mock_func)
54+
5355
if is_superuser:
54-
setattr(user, "is_staff", True)
56+
setattr(api_user, "is_staff", True)
5557
if has_perm:
56-
setattr(user, "has_perm", mock_has_perm_true)
58+
setattr(api_user, "has_perm", mock_has_perm_true)
5759
else:
58-
setattr(user, "has_perm", mock_has_perm_false)
60+
setattr(api_user, "has_perm", mock_has_perm_false)
5961
client = EasyTestClient(api, auth=jwt_auth_async)
6062
return client
6163

6264
yield create_client
6365
setattr(JWTAuthAsync, "__call__", orig_func)
64-
setattr(user, "has_perm", orig_has_perm_fuc)

tests/test_auto_api_creation.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,33 @@ def test_auto_generate_admin_api():
3232
assert "TypeAdminAPIController" in controller_names
3333

3434

35-
async def test_auto_apis(transactional_db, easy_api_client):
35+
async def test_auto_apis(transactional_db, user, easy_api_client):
3636
for controller_class in controllers:
3737
if not str(controller_class).endswith("ClientAdminAPIController"):
3838
continue
39-
40-
client = easy_api_client(controller_class)
41-
response = await client.get("/")
42-
# TODO: figure out why user.is_authenticated is False in auto created API
43-
39+
client = easy_api_client(controller_class, api_user=user, has_perm=True)
40+
response = await client.get("/", data={}, json={}, user=user)
4441
assert response.status_code == 403
45-
# assert response.json()["data"] == []
4642

43+
client = easy_api_client(
44+
controller_class, api_user=user, has_perm=True, is_staff=True
45+
)
46+
response = await client.get("/", data={}, json={}, user=user)
47+
assert response.status_code == 200
48+
assert response.json()["data"] == []
49+
50+
client = easy_api_client(
51+
controller_class, api_user=user, has_perm=True, is_staff=True
52+
)
4753
response = await client.delete("/20000")
4854
assert response.status_code == 403
49-
# assert response.json()["code"] == 404
55+
56+
client = easy_api_client(
57+
controller_class, api_user=user, has_perm=True, is_staff=True
58+
)
59+
response = await client.delete("/20000", data={}, json={}, user=user)
60+
assert response.status_code == 200
61+
assert response.json()["code"] == 404
5062

5163

5264
async def test_auto_generation_settings(settings):

0 commit comments

Comments
 (0)