import pytest import random from tests.base.authentication_web import AuthenticationWeb class TestCreatePermission: @pytest.mark.asyncio async def test_create_permission_success(self, authentication_web: AuthenticationWeb): """Test creating a permission successfully with valid and unique permission_key and permission_name.""" suffix = str(random.randint(10000, 99999)) perm_data = { "permission_key": f"test_perm_key_success_{suffix}", "permission_name": f"Test Permission Success {suffix}", "description": "Permission for testing success" } response = await authentication_web.create_permission(perm_data) assert response.status_code == 200 json = response.json() assert json["permission_key"] == perm_data["permission_key"] assert json["permission_name"] == perm_data["permission_name"] assert json["description"] == perm_data["description"] assert json["id"] is not None assert json["created_at"] is not None assert json["updated_at"] is not None @pytest.mark.asyncio async def test_create_permission_fail_duplicate_key(self, authentication_web: AuthenticationWeb): """Test creating a permission fails when permission_key is duplicated.""" suffix = str(random.randint(10000, 99999)) perm_data = { "permission_key": f"test_perm_key_dup_{suffix}", "permission_name": f"Test Permission DupKey {suffix}", "description": "desc" } await authentication_web.create_permission(perm_data) perm_data2 = { "permission_key": f"test_perm_key_dup_{suffix}", "permission_name": f"Test Permission DupKey2 {suffix}", "description": "desc2" } response = await authentication_web.create_permission(perm_data2) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_permission_fail_duplicate_name(self, authentication_web: AuthenticationWeb): """Test creating a permission fails when permission_name is duplicated.""" suffix = str(random.randint(10000, 99999)) perm_data = { "permission_key": f"test_perm_key_dupname1_{suffix}", "permission_name": f"Test Permission DupName {suffix}", "description": "desc" } await authentication_web.create_permission(perm_data) perm_data2 = { "permission_key": f"test_perm_key_dupname2_{suffix}", "permission_name": f"Test Permission DupName {suffix}", "description": "desc2" } response = await authentication_web.create_permission(perm_data2) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_permission_fail_empty_key(self, authentication_web: AuthenticationWeb): """Test creating a permission fails when permission_key is empty.""" suffix = str(random.randint(10000, 99999)) perm_data = { "permission_key": "", "permission_name": f"Test Permission EmptyKey {suffix}", "description": "desc" } response = await authentication_web.create_permission(perm_data) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_permission_fail_empty_name(self, authentication_web: AuthenticationWeb): """Test creating a permission fails when permission_name is empty.""" suffix = str(random.randint(10000, 99999)) perm_data = { "permission_key": f"test_perm_key_emptyname_{suffix}", "permission_name": "", "description": "desc" } response = await authentication_web.create_permission(perm_data) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_permission_success_empty_description(self, authentication_web: AuthenticationWeb): """Test creating a permission successfully when description is None (optional field).""" suffix = str(random.randint(10000, 99999)) perm_data = { "permission_key": f"test_perm_key_emptydesc_{suffix}", "permission_name": f"Test Permission EmptyDesc {suffix}", "description": None } response = await authentication_web.create_permission(perm_data) assert response.status_code == 200 json = response.json() assert json["permission_key"] == perm_data["permission_key"] assert json["permission_name"] == perm_data["permission_name"] assert json["description"] is None or json["description"] == "" if __name__ == '__main__': pytest.main([__file__])