user_control.py 7.3 KB
Newer Older
C
cpwu 已提交
1 2 3 4 5 6 7
import taos
import sys

from util.log import *
from util.sql import *
from util.cases import *

C
cpwu 已提交
8

C
cpwu 已提交
9 10 11 12
PRIVILEGES_ALL      = "ALL"
PRIVILEGES_READ     = "READ"
PRIVILEGES_WRITE    = "WRITE"

C
cpwu 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
class TDconnect:
    def __init__(self,
                 host       = None,
                 port       = None,
                 user       = None,
                 password   = None,
                 database   = None,
                 config     = None,
        ) -> None:
        self._conn      = None
        self._host      = host
        self._user      = user
        self._password  = password
        self._database  = database
        self._port      = port
        self._config    = config

    def __enter__(self):
        self._conn = taos.connect(
            host    =self._host,
            port    =self._port,
            user    =self._user,
            password=self._password,
            database=self._database,
            config  =self._config
        )

        self.cursor = self._conn.cursor()
        return self.cursor



    def __exit__(self, types, values, trace):
        if self._conn:
            self.cursor.close()
            self._conn.close()

def taos_connect(
    host    = "127.0.0.1",
    port    = 6030,
    user    = "root",
    passwd  = "taosdata",
    database= None,
    config  = None
):
    return TDconnect(
        host = host,
        port=port,
        user=user,
        password=passwd,
        database=database,
        config=config
    )

C
cpwu 已提交
67 68 69 70 71 72
class TDTestCase:

    def init(self, conn, logSql):
        tdLog.debug(f"start to excute {__file__}")
        tdSql.init(conn.cursor())

C
cpwu 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    @property
    def __user_list(self):
        return  [f"user_test{i}" for i in range(self.users_count) ]

    @property
    def __passwd_list(self):
        return  [f"taosdata{i}" for i in range(self.users_count) ]

    @property
    def __privilege(self):
        return [ PRIVILEGES_ALL, PRIVILEGES_READ, PRIVILEGES_WRITE ]

    def __priv_level(self, dbname=None):
        return f"{dbname}.*" if dbname else "*.*"


C
cpwu 已提交
89
    def create_user_current(self):
C
cpwu 已提交
90 91
        users  = self.__user_list
        passwds = self.__passwd_list
C
cpwu 已提交
92
        for i in range(self.users_count):
C
cpwu 已提交
93
            tdSql.execute(f"create user {users[i]} pass '{passwds[i]}' ")
C
cpwu 已提交
94

C
cpwu 已提交
95 96
        tdSql.query("show users")
        tdSql.checkRows(self.users_count + 1)
C
cpwu 已提交
97

C
cpwu 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    def create_user_err(self):
        sqls = [
            "create users u1 pass 'u1passwd' ",
            "create user '' pass 'u1passwd' ",
            "create user  pass 'u1passwd' ",
            "create user u1 pass u1passwd ",
            "create user u1 password 'u1passwd' ",
            "create user u1 pass u1passwd ",
            "create user u1 pass '' ",
            "create user u1 pass '   ' ",
            "create user u1 pass  ",
            "create user u1 u2 pass 'u1passwd' 'u2passwd' ",
            "create user u1 u2 pass 'u1passwd', 'u2passwd' ",
            "create user u1, u2 pass 'u1passwd', 'u2passwd' ",
            "create user u1, u2 pass 'u1passwd'  'u2passwd' ",
            # length of user_name must <= 23
            "create user u12345678901234567890123 pass 'u1passwd' " ,
            # length of passwd must <= 15
            "create user u1 pass 'u123456789012345' " ,
            # password must have not " ' ~ ` \
            "create user u1 pass 'u1passwd\\' " ,
            "create user u1 pass 'u1passwd~' " ,
            "create user u1 pass 'u1passwd\"' " ,
            "create user u1 pass 'u1passwd\'' " ,
            "create user u1 pass 'u1passwd`' " ,
            # must after create a user named u1
            "create user u1 pass 'u1passwd' " ,
        ]

        tdSql.execute("create user u1 pass 'u1passwd' ")
        for sql in sqls:
            tdSql.error(sql)

C
cpwu 已提交
131 132
    def __alter_pass_sql(self, user, passwd):
        return f'''ALTER USER {user} PASS '{passwd}' '''
C
cpwu 已提交
133 134 135

    def alter_pass_current(self):
        self.__init_pass = True
C
cpwu 已提交
136
        for count, i in enumerate(range(self.users_count)):
C
cpwu 已提交
137 138
            if self.__init_pass:
                tdSql.query(self.__alter_pass_sql(self.__user_list[i], f"new{self.__passwd_list[i]}"))
C
cpwu 已提交
139
                self.__init_pass = count != self.users_count - 1
C
cpwu 已提交
140 141
            else:
                tdSql.query(self.__alter_pass_sql(self.__user_list[i], self.__passwd_list[i] ) )
C
cpwu 已提交
142
                self.__init_pass = count == self.users_count - 1
C
cpwu 已提交
143

C
cpwu 已提交
144
    def alter_pass_err(self):  # sourcery skip: remove-redundant-fstring
C
cpwu 已提交
145
        sqls = [
C
cpwu 已提交
146 147 148 149 150 151
            f"alter users {self.__user_list[0]} pass 'newpass' " ,
            f"alter user {self.__user_list[0]} pass '' " ,
            f"alter user {self.__user_list[0]} pass '  ' " ,
            f"alter user anyuser pass 'newpass' " ,
            f"alter user {self.__user_list[0]} pass  " ,
            f"alter user {self.__user_list[0]} password 'newpass'  " ,
C
cpwu 已提交
152 153 154 155 156
        ]
        for sql in sqls:
            tdSql.error(sql)


C
cpwu 已提交
157 158 159 160 161 162
    def grant_user_privileges(self, privilege,  dbname=None, user_name="root"):
        return f"GRANT {privilege} ON {self.__priv_level(dbname)} TO {user_name} "

    def test_user_create(self):
        self.create_user_current()
        self.create_user_err()
C
cpwu 已提交
163

C
cpwu 已提交
164 165 166 167 168 169
    def test_alter_pass(self):
        self.alter_pass_current()
        self.alter_pass_err()

    def user_login(self, user, passwd):
        login_except = False
C
cpwu 已提交
170
        tdLog.printNoPrefix(login_except)
C
cpwu 已提交
171
        try:
C
cpwu 已提交
172 173
            with taos_connect(user=user, passwd=passwd) as cursor:
                cursor
C
cpwu 已提交
174 175 176 177
        except BaseException:
            login_except = True
            cursor = None

C
cpwu 已提交
178
        tdLog.printNoPrefix(f"end,{login_except}")
C
cpwu 已提交
179

C
cpwu 已提交
180 181 182 183 184
        return login_except, cursor

    def login_currrent(self, user, passwd):
        login_except, _ = self.user_login(user, passwd)
        if login_except:
C
cpwu 已提交
185
            tdLog.exit(f"connect failed, user: {user} and pass: {passwd} do not match!")
C
cpwu 已提交
186 187 188
        else:
            tdLog.info("connect successfully, user and pass matched!")

C
cpwu 已提交
189 190 191 192 193

    def login_err(self, user, passwd):
        login_except, _ = self.user_login(user, passwd)
        if login_except:
            tdLog.info("connect failed, except error occured!")
C
cpwu 已提交
194 195
        else:
            tdLog.exit("connect successfully, except error not occrued!")
C
cpwu 已提交
196 197 198



C
cpwu 已提交
199
    def run(self):
C
cpwu 已提交
200 201 202 203 204 205 206 207 208 209

        # 默认只有 root 用户
        tdLog.printNoPrefix("==========step0: init, user list only has root account")
        tdSql.query("show users")
        tdSql.checkData(0, 0, "root")
        tdSql.checkData(0, 1, "super")

        # root用户权限
        # 创建用户测试
        tdLog.printNoPrefix("==========step1: create user test")
C
cpwu 已提交
210
        self.users_count = 5
C
cpwu 已提交
211 212 213 214 215 216 217 218
        self.test_user_create()

        # 查看用户
        tdLog.printNoPrefix("==========step2: show user test")
        tdSql.query("show users")
        tdSql.checkRows(self.users_count + 2)

        # 修改密码
C
cpwu 已提交
219 220 221
        tdLog.printNoPrefix("==========step3: alter user pass test")
        self.test_alter_pass()

C
cpwu 已提交
222 223

        # 密码登录认证
C
cpwu 已提交
224
        tdLog.printNoPrefix("==========step4: check login test")
C
cpwu 已提交
225
        self.login_err(self.__user_list[0], "passwd1")
C
cpwu 已提交
226
        self.login_currrent(self.__user_list[0], f"new{self.__passwd_list[0]}")
C
cpwu 已提交
227

C
cpwu 已提交
228 229 230 231 232 233 234


        # 删除用户测试
        tdLog.printNoPrefix("==========step2: drop user")



C
cpwu 已提交
235 236 237 238 239 240 241 242 243 244




    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

tdCases.addLinux(__file__, TDTestCase())
tdCases.addWindows(__file__, TDTestCase())