自学内容网 自学内容网

zdppy_amauth如何测试批量获取用户绑定的所有权限

简单的思路

1、添加用户
2、添加角色
3、绑定用户和角色的关系
4、添加3个权限
5、绑定角色和权限关系,给角色绑定2个权限
6、查询用户绑定的所有权限
7、删除用户
8、删除角色
9、删除权限
10、解绑用户的所有角色
11、解绑角色的所有权限

第一步:改造之前测试批量绑定角色的代码

之前的代码包含了如下步骤:
1、添加用户
2、添加角色
3、绑定用户和角色的关系
7、删除用户
8、删除角色
10、解绑用户的所有角色

还缺少如下步骤:
4、添加3个权限
5、绑定角色和权限关系,给角色绑定2个权限
6、查询用户绑定的所有权限
9、删除权限
11、解绑角色的所有权限

import unittest
import req


class TestUserGetAuths(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_get_auths(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"
        rolename = "testrole"
        data = {
            "name": rolename,
            "nickname": rolename,
        }
        resp = req.post(target_url, json=data)
        self.assertApiSuccess(resp)

        # 获取用户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)

        # 获取角色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))
        roleid = None
        for role in roles:
            if role.get("name") == rolename:
                roleid = role.get("id")
                break
        self.assertIsNotNone(roleid)

        # 给用户绑定角色
        target_url = f"{self.base_url}/user/{user_id}/role"
        resp = req.post(target_url, json={"ids": [roleid]})
        self.assertApiSuccess(resp)

        # 删除用户
        target_url = f"{self.base_url}/user/{user_id}"
        resp = req.delete(target_url)
        self.assertApiSuccess(resp)

        # 删除角色
        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)

第二步:补充剩余的测试步骤

import unittest
import req


class TestUserGetAuths(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_get_auths(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"
        rolename = "testrole"
        data = {
            "name": rolename,
            "nickname": rolename,
        }
        resp = req.post(target_url, json=data)
        self.assertApiSuccess(resp)

        # 获取用户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)

        # 获取角色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))
        roleid = None
        for role in roles:
            if role.get("name") == rolename:
                roleid = role.get("id")
                break
        self.assertIsNotNone(roleid)

        # 给用户绑定角色
        target_url = f"{self.base_url}/user/{user_id}/role"
        resp = req.post(target_url, json={"ids": [roleid]})
        self.assertApiSuccess(resp)

        # 添加3个权限
        target_url = f"{self.base_url}/auth"
        authname1, authname2, authname3 = "testauth1", "testauth2", "testauth3"
        authnames = [authname1, authname2, authname3]
        for authname in authnames:
            data = {
                "name": authname,
                "nickname": authname,
            }
            resp = req.post(target_url, json=data)
            self.assertApiSuccess(resp)

        # 查询权限ID
        target_url = f"{self.base_url}/auth"
        resp = req.get(target_url)
        data = self.assertApiSuccess(resp)
        auths = data.get("data")
        self.assertTrue(isinstance(auths, list))
        authid1, authid2, authid3 = None, None, None
        for auth in auths:
            if auth.get("name") == authname1:
                authid1 = auth.get("id")
            elif auth.get("name") == authname2:
                authid2 = auth.get("id")
            elif auth.get("name") == authname3:
                authid3 = auth.get("id")
        self.assertIsNotNone(authid1)
        self.assertIsNotNone(authid2)
        self.assertIsNotNone(authid3)

        # 绑定角色和权限关系,给角色绑定2个权限
        target_url = f"{self.base_url}/role/{roleid}/auth"
        resp = req.post(target_url, json={"ids": [authid1, authid2]})
        self.assertApiSuccess(resp)

        # 询用户绑定的所有权限
        target_url = f"{self.base_url}/user/{user_id}/auth"
        resp = req.get(target_url)
        data = self.assertApiSuccess(resp)
        auths = data.get("data")
        self.assertTrue(isinstance(auths, list))
        self.assertTrue(len(auths) == 2)

        # 删除用户
        target_url = f"{self.base_url}/user/{user_id}"
        resp = req.delete(target_url)
        self.assertApiSuccess(resp)

        # 删除角色
        target_url = f"{self.base_url}/role/{roleid}"
        resp = req.delete(target_url)
        self.assertApiSuccess(resp)

        # 删除权限
        for authid in [authid1, authid2, authid3]:
            target_url = f"{self.base_url}/auth/{authid}"
            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)

        # 解绑角色的所有权限
        target_url = f"{self.base_url}/role/{roleid}/auth"
        resp = req.delete(target_url)
        self.assertApiSuccess(resp)


原文地址:https://blog.csdn.net/qq_37703224/article/details/139869986

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!