MCPcopy
hub / github.com/jackc/pgx / TestConnectOAuth

Function TestConnectOAuth

pgconn/pgconn_test.go:257–281  ·  view source on GitHub ↗

TestConnectOAuth is separate from other connect tests because it specifically needs a configured OAuthTokenProvider. Further it's only available in Postgres 18+ and requires the dummy OAuth validator module installed.

(t *testing.T)

Source from the content-addressed store, hash-verified

255// needs a configured OAuthTokenProvider. Further it's only available in Postgres
256// 18+ and requires the dummy OAuth validator module installed.
257func TestConnectOAuth(t *testing.T) {
258 if os.Getenv(runOAuthTestEnvVar) != "true" {
259 t.Skipf("Skipping as '%s=true' is not set", runOAuthTestEnvVar)
260 }
261
262 config, err := pgconn.ParseConfig("host=127.0.0.1 user=pgx_oauth dbname=pgx_test")
263 require.NoError(t, err)
264
265 // Configure OAuthTokenProvider for dummy validator.
266 // The dummy validator accepts any token and maps it to the user equal to the
267 // token string.
268 config.OAuthTokenProvider = func(ctx context.Context) (string, error) {
269 return "pgx_oauth", nil
270 }
271
272 conn, err := pgconn.ConnectConfig(context.Background(), config)
273 require.NoError(t, err)
274 defer closeConn(t, conn)
275
276 result := conn.ExecParams(context.Background(), "SELECT CURRENT_USER", nil, nil, nil, nil).Read()
277 require.NoError(t, result.Err)
278 require.Len(t, result.Rows, 1)
279 require.Len(t, result.Rows[0], 1)
280 require.Equalf(t, "pgx_oauth", string(result.Rows[0][0]), "not logged in as expected user.")
281}
282
283func TestConnectOAuthError(t *testing.T) {
284 if os.Getenv(runOAuthTestEnvVar) != "true" {

Callers

nothing calls this directly

Calls 6

ParseConfigFunction · 0.92
ConnectConfigFunction · 0.92
closeConnFunction · 0.70
LenMethod · 0.65
ReadMethod · 0.45
ExecParamsMethod · 0.45

Tested by

no test coverage detected