• zdppy_amauth如何测试给用户批量绑定角色接口


    第一步:先实现新增用户的单元测试

    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):
            """测试批量绑定角色"""
            # 1、添加用户
            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)
    
            # 2、添加3个角色
            # 3、获取用户id
            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)
    
            # 4、获取角色id
            # 5、给用户绑定两个角色
            # 6、获取用户已经绑定的所有角色
            # 7、删除用户
            target_url = f"{self.base_url}/user/{user_id}"
            resp = req.delete(target_url)
            self.assertApiSuccess(resp)
    
            # 8、删除角色
            # 9、批量解绑用户关联的所有角色
    
    

    第二步:实现新增角色的单元测试

    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):
            """测试批量绑定角色"""
            # 1、添加用户
            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)
    
            # 2、添加3个角色
            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)
    
            # 3、获取用户id
            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)
    
            # 4、获取角色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)
    
            # 5、给用户绑定两个角色
            # 6、获取用户已经绑定的所有角色
            # 7、删除用户
            target_url = f"{self.base_url}/user/{user_id}"
            resp = req.delete(target_url)
            self.assertApiSuccess(resp)
    
            # 8、删除角色
            roleids = [roleid1, roleid2, roleid3]
            for roleid in roleids:
                target_url = f"{self.base_url}/role/{roleid}"
                resp = req.delete(target_url)
                self.assertApiSuccess(resp)
    
            # 9、批量解绑用户关联的所有角色
    
    

    第三步:测试批量绑定角色和解绑角色

    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):
            """测试批量绑定角色"""
            # 1、添加用户
            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)
    
            # 2、添加3个角色
            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)
    
            # 3、获取用户id
            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)
    
            # 4、获取角色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)
    
            # 5、给用户绑定两个角色
            target_url = f"{self.base_url}/user/{user_id}/role"
            resp = req.post(target_url, json={"ids": [roleid1, roleid2]})
            self.assertApiSuccess(resp)
    
            # 6、获取用户已经绑定的所有角色
            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)
    
            # 7、删除用户
            target_url = f"{self.base_url}/user/{user_id}"
            resp = req.delete(target_url)
            self.assertApiSuccess(resp)
    
            # 8、删除角色
            roleids = [roleid1, roleid2, roleid3]
            for roleid in roleids:
                target_url = f"{self.base_url}/role/{roleid}"
                resp = req.delete(target_url)
                self.assertApiSuccess(resp)
    
            # 9、批量解绑用户关联的所有角色
            target_url = f"{self.base_url}/user/{user_id}/role"
            resp = req.delete(target_url)
            self.assertApiSuccess(resp)
    
    
  • 相关阅读:
    深度学习——卷积神经网络
    flutter实现上拉加载下拉刷新
    Jemeter实现多用户登录的两种方式
    【Vue基础-数字大屏】自定义主题
    3d可视化设备产品场景展示更加直观形象
    Ant Design Vue Tree 选中子节点同时半选中父级节点
    CSS读书笔记
    【前端】Vue基础+本地应用
    天眼查数据采集、分析、深度挖掘
    修改el-form-item中label样式
  • 原文地址:https://blog.csdn.net/qq_37703224/article/details/139843076