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