|
10 | 10 | from asyncpg import _testbase as tb
|
11 | 11 |
|
12 | 12 |
|
13 |
| -class TestPool(tb.ClusterTestCase): |
| 13 | +class TestPool(tb.ConnectedTestCase): |
14 | 14 |
|
15 | 15 | async def test_pool_01(self):
|
16 | 16 | for n in {1, 3, 5, 10, 20, 100}:
|
@@ -94,3 +94,47 @@ async def setup(con):
|
94 | 94 | con = await pool.acquire()
|
95 | 95 |
|
96 | 96 | self.assertIs(con, await fut)
|
| 97 | + |
| 98 | + async def test_pool_auth(self): |
| 99 | + if not self.cluster.is_managed(): |
| 100 | + self.skipTest('unmanaged cluster') |
| 101 | + |
| 102 | + self.cluster.reset_hba() |
| 103 | + |
| 104 | + self.cluster.add_hba_entry( |
| 105 | + type='local', |
| 106 | + database='postgres', user='pooluser', |
| 107 | + auth_method='md5') |
| 108 | + |
| 109 | + self.cluster.add_hba_entry( |
| 110 | + type='host', address='127.0.0.0/32', |
| 111 | + database='postgres', user='pooluser', |
| 112 | + auth_method='md5') |
| 113 | + |
| 114 | + self.cluster.reload() |
| 115 | + |
| 116 | + try: |
| 117 | + await self.con.execute(''' |
| 118 | + CREATE ROLE pooluser WITH LOGIN PASSWORD 'poolpassword' |
| 119 | + ''') |
| 120 | + |
| 121 | + pool = await self.create_pool(database='postgres', |
| 122 | + user='pooluser', |
| 123 | + password='poolpassword', |
| 124 | + min_size=5, max_size=10) |
| 125 | + |
| 126 | + async def worker(): |
| 127 | + con = await pool.acquire() |
| 128 | + self.assertEqual(await con.fetchval('SELECT 1'), 1) |
| 129 | + await pool.release(con) |
| 130 | + |
| 131 | + tasks = [worker() for _ in range(5)] |
| 132 | + await asyncio.gather(*tasks, loop=self.loop) |
| 133 | + await pool.close() |
| 134 | + |
| 135 | + finally: |
| 136 | + await self.con.execute('DROP ROLE pooluser') |
| 137 | + |
| 138 | + # Reset cluster's pg_hba.conf since we've meddled with it |
| 139 | + self.cluster.trust_local_connections() |
| 140 | + self.cluster.reload() |
0 commit comments