MCPcopy Create free account
hub / github.com/PyMySQL/PyMySQL / TempUser

Class TempUser

pymysql/tests/test_connection.py:13–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11
12
13class TempUser:
14 def __init__(self, c, user, db, auth=None, authdata=None, password=None):
15 self._c = c
16 self._user = user
17 self._db = db
18 create = "CREATE USER " + user
19 if password is not None:
20 create += " IDENTIFIED BY '%s'" % password
21 elif auth is not None:
22 create += " IDENTIFIED WITH %s" % auth
23 if authdata is not None:
24 create += " AS '%s'" % authdata
25 try:
26 c.execute(create)
27 self._created = True
28 except pymysql.err.InternalError:
29 # already exists - TODO need to check the same plugin applies
30 self._created = False
31 try:
32 c.execute(f"GRANT SELECT ON {db}.* TO {user}")
33 self._grant = True
34 except pymysql.err.InternalError:
35 self._grant = False
36
37 def __enter__(self):
38 return self
39
40 def __exit__(self, exc_type, exc_value, traceback):
41 if self._grant:
42 self._c.execute(f"REVOKE SELECT ON {self._db}.* FROM {self._user}")
43 if self._created:
44 self._c.execute("DROP USER %s" % self._user)
45
46
47class TestAuthentication(base.PyMySQLTestCase):

Callers 6

realtestSocketAuthMethod · 0.85
realTestPamAuthMethod · 0.85
testAuthSHA256Method · 0.85
testAuthEd25519Method · 0.85

Calls

no outgoing calls

Tested by 6

realtestSocketAuthMethod · 0.68
realTestPamAuthMethod · 0.68
testAuthSHA256Method · 0.68
testAuthEd25519Method · 0.68