From 4589d06023b21a49a4d82fa24228a3879300cd7f Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sat, 27 Jan 2024 22:12:45 +0000 Subject: fix(login): stop bailing when no name in metadata now falls back to displayName, display_name or npub test pass when loggined printed to the cli in e0f543e8adb144f6deff6ff7ea0c412c9fcac5b4 are commented out --- tests/login.rs | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) (limited to 'tests') diff --git a/tests/login.rs b/tests/login.rs index 4141a59..ee7450d 100644 --- a/tests/login.rs +++ b/tests/login.rs @@ -63,6 +63,9 @@ mod with_relays { p.expect("searching for your details...\r\n")?; p.expect("\r")?; + // p.expect_end_with( + // format!("logged in as {}\r\n", TEST_KEY_1_NPUB).as_str(), + // )?; p.expect_end_with("logged in as fred\r\n")?; for p in [51, 52] { shutdown_relay(8000 + p)?; @@ -78,6 +81,46 @@ mod with_relays { Ok(()) } + async fn run_test_displays_fallback_to_npub( + relay_listener1: Option>, + relay_listener2: Option>, + ) -> Result<()> { + let (mut r51, mut r52) = ( + Relay::new(8051, None, relay_listener1), + Relay::new(8052, None, relay_listener2), + ); + + let cli_tester_handle = std::thread::spawn(move || -> Result<()> { + with_fresh_config(|| { + let mut p = CliTester::new(["login"]); + + p.expect_input(EXPECTED_NSEC_PROMPT)? + .succeeds_with(TEST_KEY_1_NSEC)?; + + p.expect_password(EXPECTED_SET_PASSWORD_PROMPT)? + .with_confirmation(EXPECTED_SET_PASSWORD_CONFIRM_PROMPT)? + .succeeds_with(TEST_PASSWORD)?; + + p.expect("searching for your details...\r\n")?; + p.expect("\r")?; + + p.expect_end_with( + format!("logged in as {}\r\n", TEST_KEY_1_NPUB).as_str(), + )?; + for p in [51, 52] { + shutdown_relay(8000 + p)?; + } + Ok(()) + }) + }); + + // launch relay + let _ = join!(r51.listen_until_close(), r52.listen_until_close(),); + + cli_tester_handle.join().unwrap()?; + Ok(()) + } + #[test] #[serial] fn when_latest_metadata_and_relay_list_on_all_relays() -> Result<()> { @@ -107,6 +150,107 @@ mod with_relays { )) } + mod poorly_quality_metadata_event { + use super::*; + + #[test] + #[serial] + fn when_metadata_contains_only_display_name() -> Result<()> { + futures::executor::block_on(run_test_displays_correct_name( + Some(&|relay, client_id, subscription_id, _| -> Result<()> { + relay.respond_events( + client_id, + &subscription_id, + &vec![ + nostr::event::EventBuilder::metadata( + &nostr::Metadata::new().display_name("fred"), + ) + .to_event(&TEST_KEY_1_KEYS) + .unwrap(), + generate_test_key_1_relay_list_event_same_as_fallback(), + ], + )?; + Ok(()) + }), + None, + )) + } + + #[test] + #[serial] + fn when_metadata_contains_only_displayname() -> Result<()> { + println!( + "displayName: {}", + nostr::Metadata::new() + .custom_field("displayName", "fred") + .custom + .get("displayName") + .unwrap() + ); + println!( + "name: {}", + nostr::Metadata::new().name("fred").name.unwrap() + ); + + futures::executor::block_on(run_test_displays_correct_name( + Some(&|relay, client_id, subscription_id, _| -> Result<()> { + relay.respond_events( + client_id, + &subscription_id, + &vec![ + nostr::event::EventBuilder::metadata( + &nostr::Metadata::new() + .custom_field("displayName", "fred"), + ) + .to_event(&TEST_KEY_1_KEYS) + .unwrap(), + generate_test_key_1_relay_list_event_same_as_fallback(), + ], + )?; + Ok(()) + }), + None, + )) + } + + #[test] + #[serial] + fn displays_npub_when_metadata_contains_no_name_displayname_or_display_name() + -> Result<()> { + println!( + "displayName: {}", + nostr::Metadata::new() + .custom_field("displayName", "fred") + .custom + .get("displayName") + .unwrap() + ); + println!( + "name: {}", + nostr::Metadata::new().name("fred").name.unwrap() + ); + + futures::executor::block_on(run_test_displays_fallback_to_npub( + Some(&|relay, client_id, subscription_id, _| -> Result<()> { + relay.respond_events( + client_id, + &subscription_id, + &vec![ + nostr::event::EventBuilder::metadata( + &nostr::Metadata::new(), + ) + .to_event(&TEST_KEY_1_KEYS) + .unwrap(), + generate_test_key_1_relay_list_event_same_as_fallback(), + ], + )?; + Ok(()) + }), + None, + )) + } + } + #[test] #[serial] fn when_latest_metadata_and_relay_list_on_some_relays_but_others_have_none() -- cgit v1.2.3