150 lines
6.7 KiB
Python
150 lines
6.7 KiB
Python
import pytest
|
|
import random
|
|
|
|
from backend.models.permission.constants import DefaultPermissionEnum
|
|
from tests.base.authentication_web import AuthenticationWeb
|
|
|
|
|
|
class TestUpdatePermission:
|
|
@pytest.mark.asyncio
|
|
async def test_update_permission_success(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a permission successfully with valid and unique fields."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
perm_data = {
|
|
"permission_key": f"update_perm_key_{suffix}",
|
|
"permission_name": f"Update Permission {suffix}",
|
|
"description": "desc"
|
|
}
|
|
create_resp = await authentication_web.create_permission(perm_data)
|
|
perm_id = create_resp.json()["id"]
|
|
update_data = {
|
|
"permission_id": perm_id,
|
|
"permission_key": f"update_perm_key_{suffix}_new",
|
|
"permission_name": f"Update Permission {suffix} New",
|
|
"description": "desc new"
|
|
}
|
|
resp = await authentication_web.update_permission(update_data)
|
|
assert resp.status_code == 200
|
|
json = resp.json()
|
|
assert json["permission_key"] == update_data["permission_key"]
|
|
assert json["permission_name"] == update_data["permission_name"]
|
|
assert json["description"] == update_data["description"]
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_permission_fail_not_found(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a permission fails when permission_id does not exist."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
update_data = {
|
|
"permission_id": "000000000000000000000000",
|
|
"permission_key": f"notfound_key_{suffix}",
|
|
"permission_name": f"NotFound Permission {suffix}",
|
|
"description": "desc"
|
|
}
|
|
resp = await authentication_web.update_permission(update_data)
|
|
assert resp.status_code == 422 or resp.status_code == 400
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_permission_fail_duplicate_key(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a permission fails when permission_key is duplicated."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
perm1 = await authentication_web.create_permission({
|
|
"permission_key": f"dupkey1_{suffix}",
|
|
"permission_name": f"dupkey1_{suffix}",
|
|
"description": "desc"
|
|
})
|
|
perm2 = await authentication_web.create_permission({
|
|
"permission_key": f"dupkey2_{suffix}",
|
|
"permission_name": f"dupkey2_{suffix}",
|
|
"description": "desc"
|
|
})
|
|
perm2_id = perm2.json()["id"]
|
|
update_data = {
|
|
"permission_id": perm2_id,
|
|
"permission_key": f"dupkey1_{suffix}",
|
|
"permission_name": f"dupkey2_{suffix}_new",
|
|
"description": "desc"
|
|
}
|
|
resp = await authentication_web.update_permission(update_data)
|
|
assert resp.status_code == 422 or resp.status_code == 400
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_permission_fail_duplicate_name(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a permission fails when permission_name is duplicated."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
perm1 = await authentication_web.create_permission({
|
|
"permission_key": f"dupname1_{suffix}",
|
|
"permission_name": f"dupname1_{suffix}",
|
|
"description": "desc"
|
|
})
|
|
perm2 = await authentication_web.create_permission({
|
|
"permission_key": f"dupname2_{suffix}",
|
|
"permission_name": f"dupname2_{suffix}",
|
|
"description": "desc"
|
|
})
|
|
perm2_id = perm2.json()["id"]
|
|
update_data = {
|
|
"permission_id": perm2_id,
|
|
"permission_key": f"dupname2_{suffix}_new",
|
|
"permission_name": f"dupname1_{suffix}",
|
|
"description": "desc"
|
|
}
|
|
resp = await authentication_web.update_permission(update_data)
|
|
assert resp.status_code == 422 or resp.status_code == 400
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_permission_fail_empty_key(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a permission fails when permission_key is empty."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
perm = await authentication_web.create_permission({
|
|
"permission_key": f"emptykey_{suffix}",
|
|
"permission_name": f"emptykey_{suffix}",
|
|
"description": "desc"
|
|
})
|
|
perm_id = perm.json()["id"]
|
|
update_data = {
|
|
"permission_id": perm_id,
|
|
"permission_key": "",
|
|
"permission_name": f"emptykey_{suffix}_new",
|
|
"description": "desc"
|
|
}
|
|
resp = await authentication_web.update_permission(update_data)
|
|
assert resp.status_code == 422 or resp.status_code == 400
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_permission_fail_empty_name(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a permission fails when permission_name is empty."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
perm = await authentication_web.create_permission({
|
|
"permission_key": f"emptyname_{suffix}",
|
|
"permission_name": f"emptyname_{suffix}",
|
|
"description": "desc"
|
|
})
|
|
perm_id = perm.json()["id"]
|
|
update_data = {
|
|
"permission_id": perm_id,
|
|
"permission_key": f"emptyname_{suffix}_new",
|
|
"permission_name": "",
|
|
"description": "desc"
|
|
}
|
|
resp = await authentication_web.update_permission(update_data)
|
|
assert resp.status_code == 422 or resp.status_code == 400
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_default_permission_fail(self, authentication_web: AuthenticationWeb):
|
|
"""Test updating a default permission fails. Default permission cannot be updated."""
|
|
suffix = str(random.randint(10000, 99999))
|
|
# Query a default role
|
|
resp = await authentication_web.query_permissions(
|
|
params={"page": 1, "page_size": 2, "permission_key": DefaultPermissionEnum.CHANGE_PERMISSIONS.value.permission_key})
|
|
json = resp.json()
|
|
default_permission = json["items"][0]
|
|
resp = await authentication_web.update_permission(perm_data={
|
|
"permission_id": default_permission["id"],
|
|
"permission_key": f"{default_permission['permission_key']}_{suffix}_update",
|
|
"permission_name": f"{default_permission['permission_name']}_{suffix}_update",
|
|
"description": "desc",
|
|
})
|
|
assert resp.status_code == 422 or resp.status_code == 400
|
|
|
|
if __name__ == '__main__':
|
|
pytest.main([__file__]) |