diff --git a/crates/dav/src/resources/mod.rs b/crates/dav/src/resources/mod.rs index 985820b..0d17512 100644 --- a/crates/dav/src/resources/mod.rs +++ b/crates/dav/src/resources/mod.rs @@ -1,3 +1,72 @@ pub mod root; pub use root::{RootResource, RootResourceService}; + +#[cfg(test)] +pub mod test { + use crate::{ + Error, Principal, + extensions::{CommonPropertiesExtension, CommonPropertiesProp}, + namespace::NS_DAV, + privileges::UserPrivilegeSet, + resource::{PrincipalUri, Resource}, + xml::{Resourcetype, ResourcetypeInner}, + }; + + #[derive(Debug, Clone)] + pub struct TestPrincipal(pub String); + + impl Principal for TestPrincipal { + fn get_id(&self) -> &str { + &self.0 + } + } + + impl Resource for TestPrincipal { + type Prop = CommonPropertiesProp; + type Error = Error; + type Principal = Self; + + fn is_collection(&self) -> bool { + true + } + + fn get_resourcetype(&self) -> crate::xml::Resourcetype { + Resourcetype(&[ResourcetypeInner(Some(NS_DAV), "collection")]) + } + + fn get_prop( + &self, + principal_uri: &impl crate::resource::PrincipalUri, + principal: &Self::Principal, + prop: &::Names, + ) -> Result { + ::get_prop(self, principal_uri, principal, prop) + } + + fn get_displayname(&self) -> Option<&str> { + Some(&self.0) + } + + fn get_user_privileges( + &self, + principal: &Self::Principal, + ) -> Result { + Ok(UserPrivilegeSet::owner_only( + principal.get_id() == self.get_id(), + )) + } + } + + #[derive(Debug, Clone)] + pub struct TestPrincipalUri; + + impl PrincipalUri for TestPrincipalUri { + fn principal_collection(&self) -> String { + "/".to_owned() + } + fn principal_uri(&self, principal: &str) -> String { + format!("/{principal}/") + } + } +} diff --git a/crates/dav/src/resources/root.rs b/crates/dav/src/resources/root.rs index 0976ec3..9d59fd8 100644 --- a/crates/dav/src/resources/root.rs +++ b/crates/dav/src/resources/root.rs @@ -105,3 +105,33 @@ impl + Clone, P: Principal, PURI: PrincipalU for RootResourceService { } + +#[cfg(test)] +mod test { + use crate::{ + resource::Resource, + resources::{ + RootResource, + test::{TestPrincipal, TestPrincipalUri}, + }, + }; + + #[test] + fn test_root_resource() { + let resource = RootResource::::default(); + let propfind = RootResource::::parse_propfind( + r#""#, + ) + .unwrap(); + + let _response = resource + .propfind( + "/", + &propfind.prop, + propfind.include.as_ref(), + &TestPrincipalUri, + &TestPrincipal("user".to_owned()), + ) + .unwrap(); + } +}