aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunoiSayami <[email protected]>2021-05-14 00:06:09 +0800
committerKunoiSayami <[email protected]>2021-05-14 00:06:09 +0800
commitdaa61c3e0c96800c58931f1e933462b462fc7b66 (patch)
tree0d0642a606ced8154f7c5f3db4b6fcca98541920
parent49b8c334a35bbdd7762ceae93445a051084c31f0 (diff)
feat(test): Add test for authenticate cookiev0.3.8
-rw-r--r--Cargo.lock10
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs60
3 files changed, 62 insertions, 10 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c6124fb..ba4a0c8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -377,7 +377,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cgit-simple-authentication"
-version = "0.3.7"
+version = "0.3.8"
dependencies = [
"anyhow",
"argon2",
@@ -1708,9 +1708,9 @@ dependencies = [
[[package]]
name = "serde"
-version = "1.0.125"
+version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171"
+checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
dependencies = [
"serde_derive",
]
@@ -1727,9 +1727,9 @@ dependencies = [
[[package]]
name = "serde_derive"
-version = "1.0.125"
+version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b093b7a2bb58203b5da3056c05b4ec1fed827dcfdb37347a8841695263b3d06d"
+checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43"
dependencies = [
"proc-macro2",
"quote",
diff --git a/Cargo.toml b/Cargo.toml
index a59656e..c5a730a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "cgit-simple-authentication"
-version = "0.3.7"
+version = "0.3.8"
authors = ["KunoiSayami <[email protected]>"]
edition = "2018"
diff --git a/src/main.rs b/src/main.rs
index 1400fd2..932a29f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -50,7 +50,6 @@ impl<R: BufRead, W: Write> IOModule<R, W> {
async fn cmd_authenticate_post(&mut self, matches: &ArgMatches<'_>, cfg: Config) -> Result<()> {
// Read stdin from upstream.
let mut buffer = String::new();
- // TODO: override it that can test function from cargo test
self.reader.read_to_string(&mut buffer)?;
//log::debug!("{}", buffer);
let data = datastructures::FormData::from(buffer);
@@ -590,14 +589,14 @@ fn main() -> Result<()> {
#[cfg(test)]
mod test {
use crate::datastructures::{rand_str, Config};
- use crate::{cmd_add_user, cmd_init};
+ use crate::{cmd_add_user, cmd_init, cmd_authenticate_cookie};
use crate::{get_arg_matches, IOModule};
use argon2::{
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
use redis::AsyncCommands;
- use std::io::Write;
+ use std::io::{Read, Write};
use std::path::Path;
use std::path::PathBuf;
use std::thread::sleep;
@@ -745,7 +744,7 @@ mod test {
#[test]
fn test_3_auth_pass() {
- lock(&PathBuf::from("test/USER_WRITTEN"), 15);
+ lock(&PathBuf::from("test/USER_WRITTEN"), 7);
let s = test_auth_post();
@@ -760,4 +759,57 @@ mod test {
assert!(s.ends_with("\n\n"));
assert!(!Path::new("test/COPIED").exists());
}
+
+ #[test]
+ fn test_4_authenticate_cookie() {
+ 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",
+ "",
+ "",
+ "/",
+ "/?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!()
+ };
+ assert!(result);
+ }
}