) { + 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() { + ("authenticate-cookie", Some(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_02_protected_repo_parser() { + let tmpdir = tempdir::TempDir::new("test").unwrap(); + + let another_file_path = format!( + "include={}/REPO_SETTING # TEST\ncgit-simple-auth-full-protect=false", + 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")); + 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.query_is_all_protected()); + + tmpdir.close().unwrap(); + } + + async fn clear_redis_setting() -> anyhow::Result<()> { + let client = redis::Client::open("redis://127.0.0.1")?; + let mut conn = client.get_async_connection().await?; + + for key in &["cgit_repo_test", "cgit_repo_repo"] { + 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(); + } +} -- cgit v1.3.1