) { lock(&PathBuf::from("test/RESPONSE"), 15); let mut buffer = String::new(); let mut file = std::fs::File::open("test/RESPONSE").unwrap(); file.read_to_string(&mut buffer).unwrap(); let buffer = buffer; let mut cookie = ""; for line in buffer.lines().map(|x| x.trim()) { if !line.starts_with("Set-Cookie") { continue; } let (_, value) = line.split_once(":").unwrap(); let (value, _) = value.split_once(";").unwrap(); cookie = value.trim(); break; } let matches = get_arg_matches(Some(vec![ "a", "authenticate-cookie", cookie, "GET", "", "https://git.example.com/", "/", "git.example.com", "on", repo, "", "/", "/?p=login", ])); let result = match matches.subcommand() { Some(("authenticate-cookie", matches)) => tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap() .block_on(cmd_authenticate_cookie( matches, Config::generate_test_config(), )) .unwrap(), _ => unreachable!(), }; if let Some(path) = path { std::fs::File::create(path).unwrap(); } assert!(result); } fn write_to_specify_file(path: &PathBuf, data: &[u8]) -> Result<(), std::io::Error> { let mut file = std::fs::OpenOptions::new() .create(true) .write(true) .truncate(true) .open(path)?; file.write_all(data)?; file.sync_all()?; Ok(()) } #[test] fn test_protected_repo_parser() { let tmpdir = tempfile::TempDir::new().unwrap(); let another_file_path = format!( "include={}/REPO_SETTING # TEST\ncgit-simple-auth-protect=part", tmpdir.path().to_str().unwrap() ); write_to_specify_file(&tmpdir.path().join("CFG"), another_file_path.as_bytes()).unwrap(); write_to_specify_file( &tmpdir.path().join("REPO_SETTING"), b"repo.url=test\nrepo.protect=true", ) .unwrap(); let cfg = Config::load_from_path(tmpdir.path().join("CFG")); assert!(cfg.check_repo_protect("test"), "struct: {:#?}", cfg); assert!(!cfg.get_white_list_mode_status()); assert!(!cfg.query_is_all_protected()); write_to_specify_file( &tmpdir.path().join("REPO_SETTING"), b"repo.protect=true\nrepo.url=test", ) .unwrap(); let cfg = Config::load_from_path(tmpdir.path().join("CFG")); assert!(!cfg.check_repo_protect("test")); assert!(!cfg.get_white_list_mode_status()); assert!(!cfg.query_is_all_protected()); tmpdir.close().unwrap(); } async fn clear_redis_setting() -> anyhow::Result<()> { let mut conn = crate::datastructures::get_redis_connection(crate::datastructures::DEFAULT_REDIS_URL) .await?; for key in &[ "cgit_repo_test", "cgit_repo_repo", "cgit_auth_failed_hunter2", ] { conn.del::<_, i32>(*key).await?; } Ok(()) } #[test] fn test_99_clear() { lock(&PathBuf::from("test/COOKIE_TEST_1"), 9); lock(&PathBuf::from("test/COOKIE_TEST_2"), 6); tokio::runtime::Builder::new_current_thread() .enable_all() .build() .unwrap() .block_on(clear_redis_setting()) .unwrap(); } }