@@ -30,7 +30,11 @@ import (
3030 "github.com/jackc/pgx/v5/pgtype"
3131)
3232
33- const pgbouncerConnStringEnvVar = "PGX_TEST_PGBOUNCER_CONN_STRING"
33+ const (
34+ pgbouncerConnStringEnvVar = "PGX_TEST_PGBOUNCER_CONN_STRING"
35+ // runOAuthTestEnvVar has to be set to "true" to run OAuth tests
36+ runOAuthTestEnvVar = "PGX_TEST_OAUTH"
37+ )
3438
3539func TestConnect (t * testing.T ) {
3640 tests := []struct {
@@ -118,6 +122,54 @@ func TestConnectTLS(t *testing.T) {
118122 closeConn (t , conn )
119123}
120124
125+ // TestConnectOAuth is separate from other connect tests because it specifically
126+ // needs a configured OAuthTokenProvider. Further it's only available in Postgres
127+ // 18+ and requires the dummy OAuth validator module installed.
128+ func TestConnectOAuth (t * testing.T ) {
129+ if os .Getenv (runOAuthTestEnvVar ) != "true" {
130+ t .Skipf ("Skipping as '%s=true' is not set" , runOAuthTestEnvVar )
131+ }
132+
133+ config , err := pgconn .ParseConfig ("host=127.0.0.1 user=pgx_oauth dbname=pgx_test" )
134+ require .NoError (t , err )
135+
136+ // Configure OAuthTokenProvider for dummy validator.
137+ // The dummy validator accepts any token and maps it to the user equal to the
138+ // token string.
139+ config .OAuthTokenProvider = func (ctx context.Context ) (string , error ) {
140+ return "pgx_oauth" , nil
141+ }
142+
143+ conn , err := pgconn .ConnectConfig (context .Background (), config )
144+ require .NoError (t , err )
145+ defer closeConn (t , conn )
146+
147+ result := conn .ExecParams (context .Background (), "SELECT CURRENT_USER" , nil , nil , nil , nil ).Read ()
148+ require .NoError (t , result .Err )
149+ require .Len (t , result .Rows , 1 )
150+ require .Len (t , result .Rows [0 ], 1 )
151+ require .Equalf (t , "pgx_oauth" , string (result .Rows [0 ][0 ]), "not logged in as expected user." )
152+ }
153+
154+ func TestConnectOAuthError (t * testing.T ) {
155+ if os .Getenv (runOAuthTestEnvVar ) != "true" {
156+ t .Skipf ("Skipping as '%s=true' is not set" , runOAuthTestEnvVar )
157+ }
158+
159+ config , err := pgconn .ParseConfig ("host=127.0.0.1 user=pgx_oauth dbname=pgx_test" )
160+ require .NoError (t , err )
161+
162+ // Configure OAuthTokenProvider for dummy validator.
163+ // The dummy validator accepts any token and maps it to the user equal to the
164+ // token string. In this case that token will be accepted but as there is no
165+ // user 'INVALID_TOKEN' the connection should fail.
166+ config .OAuthTokenProvider = func (ctx context.Context ) (string , error ) {
167+ return "INVALID_TOKEN" , nil
168+ }
169+
170+ _ , err = pgconn .ConnectConfig (context .Background (), config )
171+ require .Error (t , err , "connect should return error for invalid token" )
172+ }
121173func TestConnectTLSPasswordProtectedClientCertWithSSLPassword (t * testing.T ) {
122174 t .Parallel ()
123175
0 commit comments