MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / BaseTestAuthentication

Class BaseTestAuthentication

tests/test_connect.py:147–220  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

145
146
147class BaseTestAuthentication(tb.ConnectedTestCase):
148 USERS = []
149
150 def setUp(self):
151 super().setUp()
152
153 if not self.cluster.is_managed():
154 self.skipTest('unmanaged cluster')
155
156 self.cluster.reset_hba()
157
158 create_script = []
159 for username, method, password in self.USERS:
160 if method == 'scram-sha-256' and self.server_version.major < 10:
161 continue
162
163 # if this is a SCRAM password, we need to set the encryption method
164 # to "scram-sha-256" in order to properly hash the password
165 if method == 'scram-sha-256':
166 create_script.append(
167 "SET password_encryption = 'scram-sha-256';"
168 )
169
170 create_script.append(
171 'CREATE ROLE "{}" WITH LOGIN{};'.format(
172 username,
173 f' PASSWORD E{(password or "")!r}'
174 )
175 )
176
177 # to be courteous to the MD5 test, revert back to MD5 after the
178 # scram-sha-256 password is set
179 if method == 'scram-sha-256':
180 create_script.append(
181 "SET password_encryption = 'md5';"
182 )
183
184 if _system != 'Windows' and method != 'gss':
185 self.cluster.add_hba_entry(
186 type='local',
187 database='postgres', user=username,
188 auth_method=method)
189
190 self.cluster.add_hba_entry(
191 type='host', address=ipaddress.ip_network('127.0.0.0/24'),
192 database='postgres', user=username,
193 auth_method=method)
194
195 self.cluster.add_hba_entry(
196 type='host', address=ipaddress.ip_network('::1/128'),
197 database='postgres', user=username,
198 auth_method=method)
199
200 # Put hba changes into effect
201 self.cluster.reload()
202
203 create_script = '\n'.join(create_script)
204 self.loop.run_until_complete(self.con.execute(create_script))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected