use gpui_kit::{AnyWindowHandle, App, AppContext, TestAppContext, Window}; use gpui_kit::test::TestWindowExt; use tempfile::TempDir; use uuid::Uuid; use crate::app::launch; use crate::config::AppConfig; use crate::credentials::AwsCredential; use crate::db::Db; use crate::error::CreddyError; use crate::screens::Screen; use crate::session::AppSession; pub const TEST_PASSPHRASE: &'static str = "correct horse battery staple"; pub struct TestEnv { // we have to keep this around or the tempdir will be deleted as soon as it's dropped _dir: TempDir, pub config: AppConfig, } impl TestEnv { pub fn new() -> Result { let dir = tempfile::tempdir()?; let path = dir.path().to_path_buf(); let config = AppConfig::load_from(path.clone(), path)?; Ok(TestEnv { _dir: dir, config }) } pub fn db(&self) -> Result { Db::connect(&self.config, TEST_PASSPHRASE) } } /// Start the app against the test environment, as `main` would. fn open_app(env: &TestEnv, cx: &mut TestAppContext) -> AnyWindowHandle { let window = cx.update(|cx| { launch( cx, env.config.clone(), AppSession::load(&env.config), ) }); // first frame, so that elements exist to be found cx.update_window(window.into(), |_, window, cx| window.render_frame(cx)).unwrap(); window.into() } /// Do something in the window, then let any resulting tasks (e.g. the KDF) finish. fn act(window: AnyWindowHandle, cx: &mut TestAppContext, f: impl FnOnce(&mut Window, &mut App)) { cx.update_window(window, |_, window, cx| f(window, cx)).unwrap(); cx.run_until_parked(); cx.update_window(window, |_, window, cx| window.render_frame(cx)).unwrap(); } /// Click into a field and type. fn fill(window: AnyWindowHandle, id: &'static str, text: &str, cx: &mut TestAppContext) { act(window, cx, |window, cx| { window.click(id, cx); window.input(text, cx); }); } fn click(window: AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) { act(window, cx, |window, cx| window.click(id, cx)); } /// The message in the error alert, if one is showing. fn error_message(window: AnyWindowHandle, cx: &mut TestAppContext) -> Option { cx.update_window(window, |_, window, _| { window.try_find("error").and_then(|e| e.label().map(String::from)) }).unwrap() } fn is_unlocked(cx: &TestAppContext) -> bool { cx.read(|cx| matches!(cx.global::(), AppSession::Unlocked(_))) } #[gpui_kit::test] fn test_welcome_success(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); fill(window, "confirm", TEST_PASSPHRASE, cx); click(window, "submit", cx); assert_eq!(error_message(window, cx), None); assert!(is_unlocked(cx)); assert!(env.config.db_path().exists()); } #[gpui_kit::test] fn test_welcome_mismatch(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); fill(window, "confirm", "something else", cx); click(window, "submit", cx); assert_eq!(error_message(window, cx).as_deref(), Some("Passphrases do not match.")); assert!(!is_unlocked(cx)); assert!(!env.config.db_path().exists()); } #[gpui_kit::test] fn test_welcome_empty(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let window = open_app(&env, cx); click(window, "submit", cx); assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty.")); assert!(!is_unlocked(cx)); assert!(!env.config.db_path().exists()); } #[gpui_kit::test] fn test_unlock_success(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); env.db().unwrap(); // create the db, then close it again so the app starts out locked let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); assert_eq!(error_message(window, cx), None); assert!(is_unlocked(cx)); } #[gpui_kit::test] fn test_unlock_wrong_passphrase(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); env.db().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", "not the passphrase", cx); click(window, "submit", cx); assert_eq!(error_message(window, cx).as_deref(), Some("Incorrect passphrase.")); assert!(!is_unlocked(cx)); } #[gpui_kit::test] fn test_unlock_restores_private_config(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let mut saved = env.config.clone(); saved.rehide_ms = 5000; env.db().unwrap().save_private_config(&saved.private_config()).unwrap(); let window = open_app(&env, cx); // before unlocking, only the defaults are available assert_eq!(cx.read(|cx| cx.global::().rehide_ms), env.config.rehide_ms); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); assert!(is_unlocked(cx)); assert_eq!(cx.read(|cx| cx.global::().rehide_ms), 5000); } #[gpui_kit::test] fn test_lock(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); env.db().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); assert!(is_unlocked(cx)); click(window, "lock", cx); assert!(cx.read(|cx| matches!(cx.global::(), AppSession::Locked))); // The unlock screen should be a new one, not the old one with the passphrase still in it. // The masked input doesn't expose its value, so check by submitting it as-is. click(window, "submit", cx); assert_eq!(error_message(window, cx).as_deref(), Some("Passphrase is empty.")); assert!(!is_unlocked(cx)); } fn is_visible(window: AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) -> bool { cx.update_window(window, |_, window, _| window.try_find(id).is_some()).unwrap() } #[gpui_kit::test] fn test_credentials_navigation(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); env.db().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); assert_eq!(cx.read(|cx| *cx.global::()), Screen::Credentials); assert!(is_visible(window, "back", cx)); click(window, "back", cx); assert_eq!(cx.read(|cx| *cx.global::()), Screen::Home); assert!(is_visible(window, "credentials", cx)); } #[gpui_kit::test] fn test_credentials_tabs(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let cred = AwsCredential { id: Uuid::new_v4(), name: "work".into(), access_key_id: "AKIAEXAMPLE".into(), secret_access_key: "EXAMPLESECRET".into(), is_default: false, }; env.db().unwrap().save_aws_credential(&cred).unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); assert!(is_visible(window, "work", cx)); act(window, cx, |window, cx| window.within("credential-tabs").click(1usize, cx)); assert!(!is_visible(window, "work", cx)); act(window, cx, |window, cx| window.within("credential-tabs").click(0usize, cx)); assert!(is_visible(window, "work", cx)); } #[gpui_kit::test] fn test_add_aws_credential(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); env.db().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); click(window, "aws-add", cx); fill(window, "aws-name", "work", cx); fill(window, "aws-key-id", "AKIAEXAMPLE", cx); fill(window, "aws-secret", "EXAMPLESECRET", cx); click(window, "aws-default", cx); click(window, "aws-save", cx); assert_eq!(error_message(window, cx), None); assert!(is_visible(window, "work", cx)); assert!(!is_visible(window, "aws-save", cx)); let saved = cx.read(|cx| { let AppSession::Unlocked(db) = cx.global::() else { panic!("locked") }; db.load_aws_credential("work").unwrap() }); assert_eq!(saved.access_key_id, "AKIAEXAMPLE"); assert_eq!(saved.secret_access_key, "EXAMPLESECRET"); assert!(saved.is_default); } #[gpui_kit::test] fn test_add_aws_credential_missing_fields(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); env.db().unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); click(window, "aws-add", cx); fill(window, "aws-name", "work", cx); click(window, "aws-save", cx); assert_eq!(error_message(window, cx).as_deref(), Some("All fields are required.")); assert!(!is_visible(window, "work", cx)); } #[gpui_kit::test] fn test_add_aws_credential_duplicate_name(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let cred = AwsCredential { id: Uuid::new_v4(), name: "work".into(), access_key_id: "AKIAEXAMPLE".into(), secret_access_key: "EXAMPLESECRET".into(), is_default: false, }; env.db().unwrap().save_aws_credential(&cred).unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); click(window, "aws-add", cx); fill(window, "aws-name", "work", cx); fill(window, "aws-key-id", "AKIAOTHER", cx); fill(window, "aws-secret", "OTHERSECRET", cx); click(window, "aws-save", cx); assert_eq!( error_message(window, cx).as_deref(), Some("An AWS credential named \"work\" already exists."), ); let saved = cx.read(|cx| { let AppSession::Unlocked(db) = cx.global::() else { panic!("locked") }; db.load_aws_credential("work").unwrap() }); assert_eq!(saved.access_key_id, "AKIAEXAMPLE"); } #[gpui_kit::test] fn test_edit_aws_credential(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let cred = AwsCredential { id: Uuid::new_v4(), name: "work".into(), access_key_id: "AKIAEXAMPLE".into(), secret_access_key: "EXAMPLESECRET".into(), is_default: false, }; env.db().unwrap().save_aws_credential(&cred).unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); click(window, "edit-work", cx); act(window, cx, |window, cx| { window.click("aws-name", cx); window.press("ctrl-a", cx); window.input("personal", cx); }); click(window, "aws-default", cx); click(window, "aws-save", cx); assert_eq!(error_message(window, cx), None); assert!(is_visible(window, "personal", cx)); assert!(!is_visible(window, "work", cx)); let creds = cx.read(|cx| { let AppSession::Unlocked(db) = cx.global::() else { panic!("locked") }; db.list_aws_credentials().unwrap() }); assert_eq!(creds.len(), 1); // fields that weren't touched keep the values the form was opened with assert_eq!(creds[0].id, cred.id); assert_eq!(creds[0].name, "personal"); assert_eq!(creds[0].access_key_id, "AKIAEXAMPLE"); assert_eq!(creds[0].secret_access_key, "EXAMPLESECRET"); assert!(creds[0].is_default); } #[gpui_kit::test] fn test_delete_aws_credential(cx: &mut TestAppContext) { let env = TestEnv::new().unwrap(); let cred = AwsCredential { id: Uuid::new_v4(), name: "work".into(), access_key_id: "AKIAEXAMPLE".into(), secret_access_key: "EXAMPLESECRET".into(), is_default: false, }; env.db().unwrap().save_aws_credential(&cred).unwrap(); let window = open_app(&env, cx); fill(window, "passphrase", TEST_PASSPHRASE, cx); click(window, "submit", cx); click(window, "credentials", cx); click(window, "delete-work", cx); click(window, "aws-delete-cancel", cx); assert!(is_visible(window, "work", cx)); click(window, "delete-work", cx); click(window, "aws-delete-confirm", cx); assert_eq!(error_message(window, cx), None); assert!(!is_visible(window, "work", cx)); let creds = cx.read(|cx| { let AppSession::Unlocked(db) = cx.global::() else { panic!("locked") }; db.list_aws_credentials().unwrap() }); assert!(creds.is_empty()); }