第一步:先实现新增用户的单元测试
import unittest
import req
class TestUserAddRoles(unittest.TestCase):
def assertApiSuccess(self, resp):
"""断言API接口的响应是成功的"""
self.assertEqual(200, resp.status_code)
data = resp.json()
self.assertEqual(10000, data["code"])
self.assertEqual(True, data["status"])
return data
@classmethod
def setUpClass(cls):
cls.base_url = "http://127.0.0.1:8888/auth"
def test_user_add_roles(self):
"""测试批量绑定角色"""
target_url = f"{self.base_url}/user/register"
username = "zhangdapeng1"
data = {
"username": username,
"password": username,
"re_password": username,
}
resp = req.post(target_url, json=data)
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/user"
resp = req.get(target_url)
data = self.assertApiSuccess(resp)
users = data.get("data")
self.assertTrue(isinstance(users, list))
user_id = None
for user in users:
if user.get("username") == username:
user_id = user.get("id")
break
self.assertIsNotNone(user_id)
target_url = f"{self.base_url}/user/{user_id}"
resp = req.delete(target_url)
self.assertApiSuccess(resp)
第二步:实现新增角色的单元测试
import unittest
import req
class TestUserAddRoles(unittest.TestCase):
def assertApiSuccess(self, resp):
"""断言API接口的响应是成功的"""
self.assertEqual(200, resp.status_code)
data = resp.json()
self.assertEqual(10000, data["code"])
self.assertEqual(True, data["status"])
return data
@classmethod
def setUpClass(cls):
cls.base_url = "http://127.0.0.1:8888/auth"
def test_user_add_roles(self):
"""测试批量绑定角色"""
target_url = f"{self.base_url}/user/register"
username = "zhangdapeng1"
data = {
"username": username,
"password": username,
"re_password": username,
}
resp = req.post(target_url, json=data)
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/role"
rolename1, rolename2, rolename3 = "testrole1", "testrole2", "testrole3"
rolenames = (rolename1, rolename2, rolename3)
for rolename in rolenames:
data = {
"name": rolename,
"nickname": rolename,
}
resp = req.post(target_url, json=data)
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/user"
resp = req.get(target_url)
data = self.assertApiSuccess(resp)
users = data.get("data")
self.assertTrue(isinstance(users, list))
user_id = None
for user in users:
if user.get("username") == username:
user_id = user.get("id")
break
self.assertIsNotNone(user_id)
target_url = f"{self.base_url}/role"
resp = req.get(target_url)
data = self.assertApiSuccess(resp)
roles = data.get("data")
self.assertTrue(isinstance(roles, list))
roleid1, roleid2, roleid3 = None, None, None
for role in roles:
if role.get("name") == rolename1:
roleid1 = role.get("id")
elif role.get("name") == rolename2:
roleid2 = role.get("id")
elif role.get("name") == rolename3:
roleid3 = role.get("id")
self.assertIsNotNone(roleid1)
self.assertIsNotNone(roleid2)
self.assertIsNotNone(roleid3)
target_url = f"{self.base_url}/user/{user_id}"
resp = req.delete(target_url)
self.assertApiSuccess(resp)
roleids = [roleid1, roleid2, roleid3]
for roleid in roleids:
target_url = f"{self.base_url}/role/{roleid}"
resp = req.delete(target_url)
self.assertApiSuccess(resp)
第三步:测试批量绑定角色和解绑角色
import unittest
import req
class TestUserAddRoles(unittest.TestCase):
def assertApiSuccess(self, resp):
"""断言API接口的响应是成功的"""
self.assertEqual(200, resp.status_code)
data = resp.json()
self.assertEqual(10000, data["code"])
self.assertEqual(True, data["status"])
return data
@classmethod
def setUpClass(cls):
cls.base_url = "http://127.0.0.1:8888/auth"
def test_user_add_roles(self):
"""测试批量绑定角色"""
target_url = f"{self.base_url}/user/register"
username = "zhangdapeng1"
data = {
"username": username,
"password": username,
"re_password": username,
}
resp = req.post(target_url, json=data)
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/role"
rolename1, rolename2, rolename3 = "testrole1", "testrole2", "testrole3"
rolenames = (rolename1, rolename2, rolename3)
for rolename in rolenames:
data = {
"name": rolename,
"nickname": rolename,
}
resp = req.post(target_url, json=data)
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/user"
resp = req.get(target_url)
data = self.assertApiSuccess(resp)
users = data.get("data")
self.assertTrue(isinstance(users, list))
user_id = None
for user in users:
if user.get("username") == username:
user_id = user.get("id")
break
self.assertIsNotNone(user_id)
target_url = f"{self.base_url}/role"
resp = req.get(target_url)
data = self.assertApiSuccess(resp)
roles = data.get("data")
self.assertTrue(isinstance(roles, list))
roleid1, roleid2, roleid3 = None, None, None
for role in roles:
if role.get("name") == rolename1:
roleid1 = role.get("id")
elif role.get("name") == rolename2:
roleid2 = role.get("id")
elif role.get("name") == rolename3:
roleid3 = role.get("id")
self.assertIsNotNone(roleid1)
self.assertIsNotNone(roleid2)
self.assertIsNotNone(roleid3)
target_url = f"{self.base_url}/user/{user_id}/role"
resp = req.post(target_url, json={"ids": [roleid1, roleid2]})
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/user/{user_id}/role"
resp = req.get(target_url)
data = self.assertApiSuccess(resp)
roles = data.get("data")
self.assertTrue(isinstance(roles, list))
self.assertTrue(len(roles) == 2)
target_url = f"{self.base_url}/user/{user_id}"
resp = req.delete(target_url)
self.assertApiSuccess(resp)
roleids = [roleid1, roleid2, roleid3]
for roleid in roleids:
target_url = f"{self.base_url}/role/{roleid}"
resp = req.delete(target_url)
self.assertApiSuccess(resp)
target_url = f"{self.base_url}/user/{user_id}/role"
resp = req.delete(target_url)
self.assertApiSuccess(resp)