From d785d5549583127078bdbb42cbab536aa38f0cc7 Mon Sep 17 00:00:00 2001 From: BakerNet Date: Thu, 7 Mar 2024 11:57:22 -0800 Subject: [PATCH] more unit tests --- crates/uv-client/src/registry_client.rs | 2 +- crates/uv-keyring/src/lib.rs | 48 ++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/crates/uv-client/src/registry_client.rs b/crates/uv-client/src/registry_client.rs index 619cc3694bc7..8e25f6494d1f 100644 --- a/crates/uv-client/src/registry_client.rs +++ b/crates/uv-client/src/registry_client.rs @@ -147,7 +147,7 @@ impl RegistryClientBuilder { }; // Initialize keyring middleware. - // Note - this will overwrite netrc middleware if both are used and a + // Note - this will overwrite netrc middleware's basic auth header if both are used and a // keyring password exists let client = if self.use_keyring { client.with_init(KeyringMiddleware) diff --git a/crates/uv-keyring/src/lib.rs b/crates/uv-keyring/src/lib.rs index e05ccfe760e8..0dcc42fb0deb 100644 --- a/crates/uv-keyring/src/lib.rs +++ b/crates/uv-keyring/src/lib.rs @@ -72,7 +72,18 @@ pub fn get_keyring_auth(url: &Url) -> Result { mod test { use url::Url; - use crate::get_keyring_auth; + use crate::{get_keyring_auth, BasicAuthData, PASSWORDS}; + + #[test] + fn hostless_url_should_err() { + let url = Url::parse("file:/etc/bin/").unwrap(); + let res = get_keyring_auth(&url); + assert!(res.is_err()); + assert_eq!( + res.unwrap_err().to_string(), + "Should only use keyring for urls with host" + ); + } #[test] fn passworded_url_should_err() { @@ -84,4 +95,39 @@ mod test { "Url already contains password - keyring not required" ); } + + #[test] + fn memo_return_works() { + let found_first_url = Url::parse("https://example1.com/simple/first/").unwrap(); + let not_found_first_url = Url::parse("https://example2.com/simple/first/").unwrap(); + + { + // simulate output memoization from keyring CLI result + let mut passwords = PASSWORDS.lock().unwrap(); + passwords.insert( + found_first_url.host_str().unwrap().to_string(), + Some(BasicAuthData { + username: "u".to_string(), + password: "p".to_string(), + }), + ); + passwords.insert(not_found_first_url.host_str().unwrap().to_string(), None); + } + + let found_second_url = Url::parse("https://example1.com/simple/second/").unwrap(); + let not_found_second_url = Url::parse("https://example2.com/simple/second/").unwrap(); + + let found_res = get_keyring_auth(&found_second_url); + assert!(found_res.is_ok()); + let found_res = found_res.unwrap(); + assert_eq!(found_res.username, "u"); + assert_eq!(found_res.password, "p"); + + let not_fount_res = get_keyring_auth(¬_found_second_url); + assert!(not_fount_res.is_err()); + assert_eq!( + not_fount_res.unwrap_err().to_string(), + "Previously failed to find keyring password" + ) + } }