@@ -5,25 +5,48 @@ use sqlx::{Connection, Executor, PgConnection};
55
66#[ tokio:: test]
77#[ serial]
8- async fn test_auth ( ) {
8+ async fn test_auth_types ( ) {
99 let admin = admin_sqlx ( ) . await ;
10- let bad_password = "postgres://pgdog:skjfhjk23h4234@127.0.0.1:6432/pgdog" ;
11-
12- admin. execute ( "SET auth_type TO 'trust'" ) . await . unwrap ( ) ;
13- assert_setting_str ( "auth_type" , "trust" ) . await ;
14-
15- let mut any_password = PgConnection :: connect ( bad_password) . await . unwrap ( ) ;
16- any_password. execute ( "SELECT 1" ) . await . unwrap ( ) ;
17-
18- let mut empty_password = PgConnection :: connect ( "postgres://pgdog@127.0.0.1:6432/pgdog" )
19- . await
20- . unwrap ( ) ;
21- empty_password. execute ( "SELECT 1" ) . await . unwrap ( ) ;
22-
23- admin. execute ( "SET auth_type TO 'scram'" ) . await . unwrap ( ) ;
24- assert_setting_str ( "auth_type" , "scram" ) . await ;
10+ let good = "postgres://pgdog:pgdog@127.0.0.1:6432/pgdog" ;
11+ let bad = "postgres://pgdog:wrong@127.0.0.1:6432/pgdog" ;
12+ let none = "postgres://pgdog@127.0.0.1:6432/pgdog" ;
13+
14+ for auth_type in [ "md5" , "scram" , "plain" , "trust" ] {
15+ admin
16+ . execute ( format ! ( "SET auth_type TO '{auth_type}'" ) . as_str ( ) )
17+ . await
18+ . unwrap ( ) ;
19+ assert_setting_str ( "auth_type" , auth_type) . await ;
20+
21+ let mut conn = PgConnection :: connect ( good) . await . unwrap ( ) ;
22+ conn. execute ( "SELECT 1" ) . await . unwrap ( ) ;
23+
24+ if auth_type == "trust" {
25+ let mut conn = PgConnection :: connect ( bad) . await . unwrap ( ) ;
26+ conn. execute ( "SELECT 1" ) . await . unwrap ( ) ;
27+
28+ let mut conn = PgConnection :: connect ( none) . await . unwrap ( ) ;
29+ conn. execute ( "SELECT 1" ) . await . unwrap ( ) ;
30+ } else {
31+ let bad_err = PgConnection :: connect ( bad) . await . err ( ) . unwrap ( ) ;
32+ assert ! (
33+ bad_err
34+ . to_string( )
35+ . contains( "password for user \" pgdog\" and database \" pgdog\" is wrong" ) ,
36+ "{auth_type}: bad password error: {bad_err}"
37+ ) ;
38+ let none_err = PgConnection :: connect ( none) . await . err ( ) . unwrap ( ) ;
39+ assert ! (
40+ none_err
41+ . to_string( )
42+ . contains( "password for user \" pgdog\" and database \" pgdog\" is wrong" ) ,
43+ "{auth_type}: no password error: {none_err}"
44+ ) ;
45+ }
46+ }
2547
26- assert ! ( PgConnection :: connect( bad_password) . await . is_err( ) ) ;
48+ // Reset config.
49+ admin. execute ( "RELOAD" ) . await . unwrap ( ) ;
2750}
2851
2952#[ tokio:: test]
@@ -91,3 +114,55 @@ async fn test_passthrough_auth() {
91114 user. execute ( "SELECT 1" ) . await . unwrap ( ) ;
92115 original. execute ( "SELECT 1" ) . await . unwrap ( ) ;
93116}
117+
118+ #[ tokio:: test]
119+ async fn test_passthrough_password_change ( ) {
120+ let admin = admin_sqlx ( ) . await ;
121+ let mut direct =
122+ PgConnection :: connect ( "postgres://pgdog:pgdog@127.0.0.1:5432/pgdog?sslmode=disable" )
123+ . await
124+ . unwrap ( ) ;
125+
126+ // Ensure clean state.
127+ admin. execute ( "RELOAD" ) . await . unwrap ( ) ;
128+ admin
129+ . execute ( "SET passthrough_auth TO 'enabled_plain_allow_change'" )
130+ . await
131+ . unwrap ( ) ;
132+ assert_setting_str ( "passthrough_auth" , "enabled_plain_allow_change" ) . await ;
133+
134+ // Make sure pgdog1 has the original password.
135+ direct
136+ . execute ( "ALTER USER pgdog1 PASSWORD 'pgdog'" )
137+ . await
138+ . unwrap ( ) ;
139+
140+ // Connect with original password and keep connection alive.
141+ let mut existing = PgConnection :: connect ( "postgres://pgdog1:pgdog@127.0.0.1:6432/pgdog" )
142+ . await
143+ . unwrap ( ) ;
144+ existing. execute ( "SELECT 1" ) . await . unwrap ( ) ;
145+
146+ // Change password in PostgreSQL directly.
147+ direct
148+ . execute ( "ALTER USER pgdog1 PASSWORD 'new_password'" )
149+ . await
150+ . unwrap ( ) ;
151+
152+ // New connection with new password should work.
153+ let mut new_conn = PgConnection :: connect ( "postgres://pgdog1:new_password@127.0.0.1:6432/pgdog" )
154+ . await
155+ . unwrap ( ) ;
156+ new_conn. execute ( "SELECT 1" ) . await . unwrap ( ) ;
157+
158+ // Existing connection should still work.
159+ existing. execute ( "SELECT 1" ) . await . unwrap ( ) ;
160+
161+ // Cleanup: restore original password.
162+ direct
163+ . execute ( "ALTER USER pgdog1 PASSWORD 'pgdog'" )
164+ . await
165+ . unwrap ( ) ;
166+
167+ admin. execute ( "RELOAD" ) . await . unwrap ( ) ;
168+ }
0 commit comments