client_cli.rs (6635B)
1 //! Integration tests for client-mgmt CLI 2 3 use oauth2_gateway::db; 4 use sqlx::PgPool; 5 6 async fn setup_pool() -> PgPool { 7 let database_url = std::env::var("TEST_DATABASE_URL") 8 .unwrap_or_else(|_| "postgresql://oauth2gw:password@localhost:5432/oauth2gw".to_string()); 9 db::create_pool(&database_url).await.unwrap() 10 } 11 12 async fn cleanup_clients(pool: &PgPool) { 13 sqlx::query("DELETE FROM oauth2gw.clients") 14 .execute(pool) 15 .await 16 .unwrap(); 17 } 18 19 #[tokio::test] 20 #[serial_test::serial] 21 async fn test_cli_client_create_and_list() { 22 let pool = setup_pool().await; 23 cleanup_clients(&pool).await; 24 25 // Create a client 26 let client = db::clients::register_client( 27 &pool, 28 "test-cli-client", 29 "secret123", 30 "https://example.com/webhook", 31 "https://verifier.example.com", 32 None, 33 ) 34 .await 35 .unwrap(); 36 37 assert_eq!(client.client_id, "test-cli-client"); 38 assert_eq!(client.webhook_url, "https://example.com/webhook"); 39 40 // List clients 41 let clients = db::clients::list_clients(&pool).await.unwrap(); 42 assert_eq!(clients.len(), 1); 43 assert_eq!(clients[0].client_id, "test-cli-client"); 44 45 cleanup_clients(&pool).await; 46 } 47 48 #[tokio::test] 49 #[serial_test::serial] 50 async fn test_cli_client_show() { 51 let pool = setup_pool().await; 52 cleanup_clients(&pool).await; 53 54 // Create a client 55 let created = db::clients::register_client( 56 &pool, 57 "show-test-client", 58 "secret456", 59 "https://example.com/hook", 60 "https://verifier.example.com", 61 Some("/custom/api/path"), 62 ) 63 .await 64 .unwrap(); 65 66 // Show client by client_id 67 let found = db::clients::get_client_by_id(&pool, "show-test-client") 68 .await 69 .unwrap() 70 .unwrap(); 71 72 assert_eq!(found.id, created.id); 73 assert_eq!(found.client_id, "show-test-client"); 74 assert_eq!(found.verifier_management_api_path, "/custom/api/path"); 75 76 cleanup_clients(&pool).await; 77 } 78 79 #[tokio::test] 80 #[serial_test::serial] 81 async fn test_cli_client_update() { 82 let pool = setup_pool().await; 83 cleanup_clients(&pool).await; 84 85 // Create a client 86 let created = db::clients::register_client( 87 &pool, 88 "update-test-client", 89 "secret789", 90 "https://old-webhook.com", 91 "https://old-verifier.com", 92 None, 93 ) 94 .await 95 .unwrap(); 96 97 // Update the client 98 let updated = db::clients::update_client( 99 &pool, 100 created.id, 101 Some("https://new-webhook.com"), 102 Some("https://new-verifier.com"), 103 Some("/new/api/path"), 104 ) 105 .await 106 .unwrap(); 107 108 assert_eq!(updated.webhook_url, "https://new-webhook.com"); 109 assert_eq!(updated.verifier_url, "https://new-verifier.com"); 110 assert_eq!(updated.verifier_management_api_path, "/new/api/path"); 111 112 cleanup_clients(&pool).await; 113 } 114 115 #[tokio::test] 116 #[serial_test::serial] 117 async fn test_cli_client_update_partial() { 118 let pool = setup_pool().await; 119 cleanup_clients(&pool).await; 120 121 // Create a client 122 let created = db::clients::register_client( 123 &pool, 124 "partial-update-client", 125 "secret", 126 "https://webhook.com", 127 "https://verifier.com", 128 None, 129 ) 130 .await 131 .unwrap(); 132 133 // Update only webhook_url 134 let updated = db::clients::update_client( 135 &pool, 136 created.id, 137 Some("https://updated-webhook.com"), 138 None, 139 None, 140 ) 141 .await 142 .unwrap(); 143 144 assert_eq!(updated.webhook_url, "https://updated-webhook.com"); 145 assert_eq!(updated.verifier_url, "https://verifier.com"); // unchanged 146 assert_eq!(updated.verifier_management_api_path, "/management/api/verifications"); // default 147 148 cleanup_clients(&pool).await; 149 } 150 151 #[tokio::test] 152 #[serial_test::serial] 153 async fn test_cli_client_delete() { 154 let pool = setup_pool().await; 155 cleanup_clients(&pool).await; 156 157 // Create a client 158 let created = db::clients::register_client( 159 &pool, 160 "delete-test-client", 161 "secret", 162 "https://webhook.com", 163 "https://verifier.com", 164 None, 165 ) 166 .await 167 .unwrap(); 168 169 // Verify it exists 170 let found = db::clients::get_client_by_id(&pool, "delete-test-client") 171 .await 172 .unwrap(); 173 assert!(found.is_some()); 174 175 // Delete the client 176 let deleted = db::clients::delete_client(&pool, created.id).await.unwrap(); 177 assert!(deleted); 178 179 // Verify it's gone 180 let not_found = db::clients::get_client_by_id(&pool, "delete-test-client") 181 .await 182 .unwrap(); 183 assert!(not_found.is_none()); 184 185 cleanup_clients(&pool).await; 186 } 187 188 #[tokio::test] 189 #[serial_test::serial] 190 async fn test_cli_client_not_found() { 191 let pool = setup_pool().await; 192 cleanup_clients(&pool).await; 193 194 // Try to find non-existent client 195 let not_found = db::clients::get_client_by_id(&pool, "nonexistent-client") 196 .await 197 .unwrap(); 198 assert!(not_found.is_none()); 199 200 cleanup_clients(&pool).await; 201 } 202 203 #[tokio::test] 204 #[serial_test::serial] 205 async fn test_cli_client_duplicate_id() { 206 let pool = setup_pool().await; 207 cleanup_clients(&pool).await; 208 209 // Create first client 210 db::clients::register_client( 211 &pool, 212 "duplicate-client", 213 "secret1", 214 "https://webhook1.com", 215 "https://verifier1.com", 216 None, 217 ) 218 .await 219 .unwrap(); 220 221 // Try to create duplicate 222 let result = db::clients::register_client( 223 &pool, 224 "duplicate-client", 225 "secret2", 226 "https://webhook2.com", 227 "https://verifier2.com", 228 None, 229 ) 230 .await; 231 232 assert!(result.is_err()); 233 234 cleanup_clients(&pool).await; 235 } 236 237 #[tokio::test] 238 #[serial_test::serial] 239 async fn test_cli_list_empty() { 240 let pool = setup_pool().await; 241 cleanup_clients(&pool).await; 242 243 let clients = db::clients::list_clients(&pool).await.unwrap(); 244 assert!(clients.is_empty()); 245 } 246 247 #[tokio::test] 248 #[serial_test::serial] 249 async fn test_cli_list_multiple_clients() { 250 let pool = setup_pool().await; 251 cleanup_clients(&pool).await; 252 253 // Create multiple clients 254 for i in 1..=3 { 255 db::clients::register_client( 256 &pool, 257 &format!("client-{}", i), 258 &format!("secret-{}", i), 259 &format!("https://webhook{}.com", i), 260 &format!("https://verifier{}.com", i), 261 None, 262 ) 263 .await 264 .unwrap(); 265 } 266 267 let clients = db::clients::list_clients(&pool).await.unwrap(); 268 assert_eq!(clients.len(), 3); 269 270 cleanup_clients(&pool).await; 271 }