From 65a7670a1b66e361535c96b13454e3fe71b526bc Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 21 Feb 2021 23:30:09 +0100 Subject: [PATCH] Moved some tests --- src/data/client.rs | 31 +------------- src/data/mod.rs | 2 +- tests/caldav_client.rs | 97 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 30 deletions(-) create mode 100644 tests/caldav_client.rs diff --git a/src/data/client.rs b/src/data/client.rs index 25e447d..756b35c 100644 --- a/src/data/client.rs +++ b/src/data/client.rs @@ -62,6 +62,7 @@ static TASKS_BODY: &str = r#" "#; + pub struct Client { url: Url, username: String, @@ -215,7 +216,7 @@ impl Client { Ok(calendars) } - async fn get_tasks(&mut self, calendar: &Url) -> Result<(), Box> { + pub async fn get_tasks(&mut self, calendar: &Url) -> Result<(), Box> { let method = Method::from_bytes(b"REPORT") .expect("cannot create REPORT method."); @@ -240,31 +241,3 @@ impl Client { } } - - - - - -#[cfg(test)] -mod test { - use super::*; - - use crate::settings::URL; - use crate::settings::USERNAME; - use crate::settings::PASSWORD; - - #[tokio::test] - async fn test_client() { - let _ = env_logger::builder().is_test(true).try_init(); - - let mut client = Client::new(URL, USERNAME, PASSWORD).unwrap(); - let calendars = client.get_calendars().await.unwrap(); - - println!("Calendars:"); - calendars.iter() - .map(|cal| println!(" {}", cal.name())) - .collect::<()>(); - - client.get_tasks(&calendars[3].url()).await; - } -} diff --git a/src/data/mod.rs b/src/data/mod.rs index 3d0c265..3c8e08a 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -7,7 +7,7 @@ use std::error::Error; pub mod calendar; mod task; -mod client; +pub mod client; pub use calendar::Calendar; pub use task::Task; diff --git a/tests/caldav_client.rs b/tests/caldav_client.rs new file mode 100644 index 0000000..2337666 --- /dev/null +++ b/tests/caldav_client.rs @@ -0,0 +1,97 @@ +//! Some tests of a CalDAV client. +//! Most of them are not really integration tests, but just development tests that should be cleaned up one day. + +use std::error::Error; +use std::convert::TryFrom; + +use reqwest::Method; +use reqwest::header::CONTENT_TYPE; +use minidom::Element; +use url::Url; + +use my_tasks::data::client::Client; +use my_tasks::settings::URL; +use my_tasks::settings::USERNAME; +use my_tasks::settings::PASSWORD; +use my_tasks::settings::EXAMPLE_TASK_URL; +use my_tasks::settings::EXAMPLE_CALENDAR_URL; + +static EXAMPLE_TASKS_BODY_LAST_MODIFIED: &str = r#" + + + + + + + + + + + + + + + +"#; + +#[tokio::test] +async fn test_client() { + let _ = env_logger::builder().is_test(true).try_init(); + + let mut client = Client::new(URL, USERNAME, PASSWORD).unwrap(); + let calendars = client.get_calendars().await.unwrap(); + + println!("Calendars:"); + calendars.iter() + .map(|cal| println!(" {}\t{}", cal.name(), cal.url().as_str())) + .collect::<()>(); + + client.get_tasks(&calendars[3].url()).await; +} + +#[tokio::test] +async fn profind() { + let _ = env_logger::builder().is_test(true).try_init(); + + let url: Url = EXAMPLE_TASK_URL.parse().unwrap(); + + let method = Method::from_bytes(b"PROPFIND") + .expect("cannot create PROPFIND method."); + + let res = reqwest::Client::new() + .request(method, url.as_str()) + .header("Depth", 0) + .header(CONTENT_TYPE, "application/xml") + .basic_auth(USERNAME, Some(PASSWORD)) + //.body(body) + .send() + .await + .unwrap(); + + println!("{:?}", res.text().await); +} + +#[tokio::test] +async fn last_modified() { + let _ = env_logger::builder().is_test(true).try_init(); + + let url: Url = EXAMPLE_CALENDAR_URL.parse().unwrap(); + + let method = Method::from_bytes(b"REPORT") + .expect("cannot create REPORT method."); + + let res = reqwest::Client::new() + .request(method, url.as_str()) + .header("Depth", 1) + .header(CONTENT_TYPE, "application/xml") + .basic_auth(USERNAME, Some(PASSWORD)) + .body(EXAMPLE_TASKS_BODY_LAST_MODIFIED) + .send() + .await + .unwrap(); + + let el: Element = res.text().await.unwrap().parse().unwrap(); + my_tasks::utils::print_xml(&el); +}