From 6423baebd92e45c9be85157c443dff42e65d8d14 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 1 Sep 2023 00:00:00 +0000 Subject: refactor: rebuild app skeleton Create skeleton for a complete rebuild of the prototype as a production ready product. Includes design patterns for: - dependency injection - unit testing with dependency mocking - integration testing - error handling - config storage BREAKING-CHANGE: ground-up redesign with incompatible protocol standards --- tests/login.rs | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 tests/login.rs (limited to 'tests/login.rs') diff --git a/tests/login.rs b/tests/login.rs new file mode 100644 index 0000000..a7e1889 --- /dev/null +++ b/tests/login.rs @@ -0,0 +1,145 @@ +use anyhow::Result; +use serial_test::serial; +use test_utils::*; + +static EXPECTED_NSEC_PROMPT: &str = "login with nsec (or hex private key)"; + +fn standard_login() -> Result { + let mut p = CliTester::new(["login"]); + + p.expect_input_eventually(EXPECTED_NSEC_PROMPT)? + .succeeds_with(TEST_KEY_1_NSEC)?; + + p.expect_end_eventually()?; + Ok(p) +} + +mod when_first_time_login { + use super::*; + + #[test] + #[serial] + fn prompts_for_nsec() -> Result<()> { + with_fresh_config(|| { + standard_login()?; + Ok(()) + }) + } + + #[test] + #[serial] + fn succeeds_with_text_logged_in_as_npub() -> Result<()> { + with_fresh_config(|| { + let mut p = CliTester::new(["login"]); + + p.expect_input(EXPECTED_NSEC_PROMPT)? + .succeeds_with(TEST_KEY_1_NSEC)?; + + p.expect_end_with(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str()) + }) + } + + #[test] + #[serial] + fn next_time_returns_logged_in_as_npub() -> Result<()> { + with_fresh_config(|| { + standard_login()?.exit()?; + + CliTester::new(["login"]) + .expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())? + .exit() + }) + } +} + +mod when_called_with_nsec_parameter { + use super::*; + + #[test] + #[serial] + fn valid_nsec_param_succeeds_without_prompts() -> Result<()> { + with_fresh_config(|| { + CliTester::new(["--nsec", TEST_KEY_2_NSEC, "login"]) + .expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?; + + CliTester::new(["login"]) + .expect(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())? + .exit() + }) + } + + mod when_logging_in_as_different_nsec { + use super::*; + + #[test] + #[serial] + fn valid_nsec_param_succeeds_without_prompts_and_logs_in() -> Result<()> { + with_fresh_config(|| { + standard_login()?.exit()?; + + CliTester::new(["--nsec", TEST_KEY_2_NSEC, "login"]) + .expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())? + .expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?; + + CliTester::new(["login"]) + .expect(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())? + .exit() + }) + } + } +} + +mod when_logged_in { + use super::*; + + #[test] + #[serial] + fn returns_logged_in_as_npub() -> Result<()> { + with_fresh_config(|| { + standard_login()?.exit()?; + + CliTester::new(["login"]) + .expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())? + .exit() + }) + } + + #[test] + #[serial] + fn prompts_to_log_in_with_different_nsec() -> Result<()> { + with_fresh_config(|| { + standard_login()?.exit()?; + + let mut p = CliTester::new(["login"]); + p.expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?; + + p.expect_input(EXPECTED_NSEC_PROMPT)? + .succeeds_with(TEST_KEY_2_NSEC)?; + + p.expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str()) + }) + } + mod when_logging_in_as_different_nsec { + use super::*; + + #[test] + #[serial] + fn confirmed_as_logged_in_as_additional_user() -> Result<()> { + with_fresh_config(|| { + standard_login()?.exit()?; + + let mut p = CliTester::new(["login"]); + p.expect(format!("logged in as {}\r\n", TEST_KEY_1_NSEC).as_str())?; + + p.expect_input(EXPECTED_NSEC_PROMPT)? + .succeeds_with(TEST_KEY_2_NSEC)?; + + p.expect_end_with(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())?; + + CliTester::new(["login"]) + .expect(format!("logged in as {}\r\n", TEST_KEY_2_NSEC).as_str())? + .exit() + }) + } + } +} -- cgit v1.2.3