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

Method test_ssl_connection_sslmode

tests/test_connect.py:1883–1941  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1881 await con.close()
1882
1883 async def test_ssl_connection_sslmode(self):
1884 async def verify_works(sslmode, *, host='localhost'):
1885 con = None
1886 try:
1887 con = await self.connect(
1888 dsn='postgresql://foo/postgres?sslmode=' + sslmode,
1889 host=host,
1890 user='ssl_user')
1891 self.assertEqual(await con.fetchval('SELECT 42'), 42)
1892 self.assertTrue(con._protocol.is_ssl)
1893 finally:
1894 if con:
1895 await con.close()
1896
1897 async def verify_fails(sslmode, *, host='localhost', exn_type):
1898 # XXX: uvloop artifact
1899 old_handler = self.loop.get_exception_handler()
1900 con = None
1901 try:
1902 self.loop.set_exception_handler(lambda *args: None)
1903 with self.assertRaises(exn_type):
1904 con = await self.connect(
1905 dsn='postgresql://foo/?sslmode=' + sslmode,
1906 host=host,
1907 user='ssl_user')
1908 await con.fetchval('SELECT 42')
1909 finally:
1910 if con:
1911 await con.close()
1912 self.loop.set_exception_handler(old_handler)
1913
1914 invalid_auth_err = asyncpg.InvalidAuthorizationSpecificationError
1915 await verify_fails('disable', exn_type=invalid_auth_err)
1916 await verify_works('allow')
1917 await verify_works('prefer')
1918 await verify_works('require')
1919 await verify_fails('verify-ca', exn_type=ValueError)
1920 await verify_fails('verify-full', exn_type=ValueError)
1921
1922 with mock_dot_postgresql():
1923 await verify_works('require')
1924 await verify_works('verify-ca')
1925 await verify_works('verify-ca', host='127.0.0.1')
1926 await verify_works('verify-full')
1927 await verify_fails('verify-full', host='127.0.0.1',
1928 exn_type=ssl.CertificateError)
1929
1930 with mock_dot_postgresql(crl=True):
1931 await verify_fails('disable', exn_type=invalid_auth_err)
1932 await verify_works('allow')
1933 await verify_works('prefer')
1934 await verify_fails('require',
1935 exn_type=ssl.SSLError)
1936 await verify_fails('verify-ca',
1937 exn_type=ssl.SSLError)
1938 await verify_fails('verify-ca', host='127.0.0.1',
1939 exn_type=ssl.SSLError)
1940 await verify_fails('verify-full',

Callers

nothing calls this directly

Calls 1

mock_dot_postgresqlFunction · 0.85

Tested by

no test coverage detected