import pytest import random from tests.base.authentication_web import AuthenticationWeb class TestCreateRole: @pytest.mark.asyncio async def test_create_role_success(self, authentication_web: AuthenticationWeb): """Test creating a role successfully with valid and unique role_key and role_name.""" suffix = str(random.randint(10000, 99999)) role_data = { "role_key": f"test_role_key_success_{suffix}", "role_name": f"Test Role Success {suffix}", "role_description": "Role for testing success", "role_level": 1 } response = await authentication_web.create_role(role_data) assert response.status_code == 200 json = response.json() assert json["role_key"] == role_data["role_key"] assert json["role_name"] == role_data["role_name"] assert json["role_description"] == role_data["role_description"] assert json["role_level"] == role_data["role_level"] 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_role_fail_duplicate_role_key(self, authentication_web: AuthenticationWeb): """Test creating a role fails when role_key is duplicated.""" suffix = str(random.randint(10000, 99999)) role_data = { "role_key": f"test_role_key_dup_{suffix}", "role_name": f"Test Role DupKey {suffix}", "role_description": "desc", "role_level": 1 } await authentication_web.create_role(role_data) role_data2 = { "role_key": f"test_role_key_dup_{suffix}", "role_name": f"Test Role DupKey2 {suffix}", "role_description": "desc2", "role_level": 2 } response = await authentication_web.create_role(role_data2) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_role_fail_duplicate_role_name(self, authentication_web: AuthenticationWeb): """Test creating a role fails when role_name is duplicated.""" suffix = str(random.randint(10000, 99999)) role_data = { "role_key": f"test_role_key_dupname1_{suffix}", "role_name": f"Test Role DupName {suffix}", "role_description": "desc", "role_level": 1 } await authentication_web.create_role(role_data) role_data2 = { "role_key": f"test_role_key_dupname2_{suffix}", "role_name": f"Test Role DupName {suffix}", "role_description": "desc2", "role_level": 2 } response = await authentication_web.create_role(role_data2) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_role_fail_empty_role_key(self, authentication_web: AuthenticationWeb): """Test creating a role fails when role_key is empty.""" suffix = str(random.randint(10000, 99999)) role_data = { "role_key": "", "role_name": f"Test Role EmptyKey {suffix}", "role_description": "desc", "role_level": 1 } response = await authentication_web.create_role(role_data) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_role_fail_empty_role_name(self, authentication_web: AuthenticationWeb): """Test creating a role fails when role_name is empty.""" suffix = str(random.randint(10000, 99999)) role_data = { "role_key": f"test_role_key_emptyname_{suffix}", "role_name": "", "role_description": "desc", "role_level": 1 } response = await authentication_web.create_role(role_data) assert response.status_code == 422 or response.status_code == 400 @pytest.mark.asyncio async def test_create_role_success_empty_description(self, authentication_web: AuthenticationWeb): """Test creating a role successfully when role_description is None (optional field).""" suffix = str(random.randint(10000, 99999)) role_data = { "role_key": f"test_role_key_emptydesc_{suffix}", "role_name": f"Test Role EmptyDesc {suffix}", "role_description": None, "role_level": 1 } response = await authentication_web.create_role(role_data) assert response.status_code == 200 json = response.json() assert json["role_key"] == role_data["role_key"] assert json["role_name"] == role_data["role_name"] assert json["role_description"] is None or json["role_description"] == "" assert json["role_level"] == role_data["role_level"] if __name__ == '__main__': pytest.main([__file__])