44//! Run with: `cargo test --features auth,const --test auth_integration`
55
66use anyhow:: Result ;
7- use supabase_testcontainers_modules:: { Auth , AUTH_PORT , DOCKER_INTERNAL_HOST , LOCAL_HOST } ;
7+ use supabase_testcontainers_modules:: { Auth , AUTH_PORT , LOCAL_HOST } ;
88use testcontainers:: runners:: AsyncRunner ;
99use testcontainers:: { ContainerAsync , ImageExt } ;
1010use testcontainers_modules:: postgres:: Postgres ;
1111use tokio_postgres:: NoTls ;
1212
1313/// PostgreSQL port constant
1414const POSTGRES_PORT : u16 = 5432 ;
15+ /// Network name for container-to-container communication
16+ const TEST_NETWORK : & str = "supabase-test-network" ;
17+ /// PostgreSQL container alias on the shared network
18+ const POSTGRES_ALIAS : & str = "postgres" ;
1519
1620/// Helper struct containing both containers and connection info
1721pub struct AuthTestContext {
@@ -42,14 +46,27 @@ pub struct AuthTestContext {
4246/// // Containers are automatically stopped when ctx goes out of scope
4347/// ```
4448pub async fn setup_auth_with_postgres ( ) -> Result < AuthTestContext > {
45- // Start PostgreSQL 15 container (Auth migrations require PostgreSQL 12+ for generated columns)
46- let postgres = Postgres :: default ( ) . with_tag ( "15-alpine" ) . start ( ) . await ?;
49+ // Generate unique network and container names for test isolation
50+ let test_id = std:: time:: SystemTime :: now ( )
51+ . duration_since ( std:: time:: UNIX_EPOCH )
52+ . unwrap ( )
53+ . as_nanos ( ) ;
54+ let network_name = format ! ( "{}-{}" , TEST_NETWORK , test_id) ;
55+ let postgres_name = format ! ( "{}-{}" , POSTGRES_ALIAS , test_id) ;
56+
57+ // Start PostgreSQL 15 on shared network with a known container name
58+ let postgres = Postgres :: default ( )
59+ . with_tag ( "15-alpine" )
60+ . with_network ( & network_name)
61+ . with_container_name ( & postgres_name)
62+ . start ( )
63+ . await ?;
4764 let postgres_port = postgres. get_host_port_ipv4 ( POSTGRES_PORT ) . await ?;
4865
49- // Connection string for Auth container (uses docker internal host )
66+ // Connection string for Auth container (uses container name on shared network )
5067 let auth_db_url = format ! (
5168 "postgres://supabase_auth_admin:testpassword@{}:{}/postgres" ,
52- DOCKER_INTERNAL_HOST , postgres_port
69+ postgres_name , POSTGRES_PORT
5370 ) ;
5471
5572 // Connection string for schema init (uses localhost from host machine)
@@ -58,14 +75,15 @@ pub async fn setup_auth_with_postgres() -> Result<AuthTestContext> {
5875 LOCAL_HOST , postgres_port
5976 ) ;
6077
61- // Initialize Auth with database schema and start container
78+ // Initialize Auth with database schema and start container on same network
6279 let auth = Auth :: default ( )
6380 . with_db_url ( & auth_db_url)
6481 . with_mailer_autoconfirm ( true )
6582 . with_sms_autoconfirm ( true )
6683 . with_anonymous_users ( true )
6784 . init_db_schema ( & local_db_url, "testpassword" )
6885 . await ?
86+ . with_network ( & network_name)
6987 . start ( )
7088 . await ?;
7189
@@ -180,25 +198,39 @@ mod tests {
180198 /// Test that signup is rejected when disabled
181199 #[ tokio:: test]
182200 async fn test_signup_rejected_when_disabled ( ) -> Result < ( ) > {
183- // Start PostgreSQL 15 container (Auth migrations require PostgreSQL 12+)
184- let postgres = Postgres :: default ( ) . with_tag ( "15-alpine" ) . start ( ) . await ?;
201+ // Generate unique network and container names for test isolation
202+ let test_id = std:: time:: SystemTime :: now ( )
203+ . duration_since ( std:: time:: UNIX_EPOCH )
204+ . unwrap ( )
205+ . as_nanos ( ) ;
206+ let network_name = format ! ( "{}-{}" , TEST_NETWORK , test_id) ;
207+ let postgres_name = format ! ( "{}-{}" , POSTGRES_ALIAS , test_id) ;
208+
209+ // Start PostgreSQL 15 on shared network
210+ let postgres = Postgres :: default ( )
211+ . with_tag ( "15-alpine" )
212+ . with_network ( & network_name)
213+ . with_container_name ( & postgres_name)
214+ . start ( )
215+ . await ?;
185216 let postgres_port = postgres. get_host_port_ipv4 ( POSTGRES_PORT ) . await ?;
186217
187218 let auth_db_url = format ! (
188219 "postgres://supabase_auth_admin:testpassword@{}:{}/postgres" ,
189- DOCKER_INTERNAL_HOST , postgres_port
220+ postgres_name , POSTGRES_PORT
190221 ) ;
191222 let local_db_url = format ! (
192223 "postgres://postgres:postgres@{}:{}/postgres" ,
193224 LOCAL_HOST , postgres_port
194225 ) ;
195226
196- // Create Auth with signup disabled
227+ // Create Auth with signup disabled on same network
197228 let auth = Auth :: default ( )
198229 . with_db_url ( & auth_db_url)
199230 . with_signup_disabled ( true )
200231 . init_db_schema ( & local_db_url, "testpassword" )
201232 . await ?
233+ . with_network ( & network_name)
202234 . start ( )
203235 . await ?;
204236
0 commit comments