From bceaccfb07b9f26dfe396653fa887e13881ca57a Mon Sep 17 00:00:00 2001 From: Apisit Ritruengroj Date: Tue, 9 Dec 2025 20:55:00 +0700 Subject: [PATCH 1/2] fix: initial --- .../src/validator/rules/ownership/mod.rs | 48 +++++++++++-------- .../validator/rules/ownership/tests/mod.rs | 38 +++++++++++++++ 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/rust/signed_doc/src/validator/rules/ownership/mod.rs b/rust/signed_doc/src/validator/rules/ownership/mod.rs index 65f238b737..d2a6f0f46b 100644 --- a/rust/signed_doc/src/validator/rules/ownership/mod.rs +++ b/rust/signed_doc/src/validator/rules/ownership/mod.rs @@ -91,27 +91,35 @@ impl DocumentOwnershipRule { Self::OriginalAuthor => { // only run check for the non first version of the document if doc_id != doc.doc_ver()? { - let first_doc = provider - .try_get_first_doc(doc_id) - .await? - .ok_or(anyhow::anyhow!("cannot get a first version document"))?; + let Some(first_doc) = provider.try_get_first_doc(doc_id).await? else { + doc.report().other( + "Cannot find a first version of the referenced document", + REPORT_CONTEXT, + ); + return Ok(false); + }; allowed_authors.extend(first_doc.authors()); } }, Self::CollaboratorsFieldBased => { // only run check for the non first version of the document if doc_id != doc.doc_ver()? { - let first_doc = provider - .try_get_first_doc(doc_id) - .await? - .ok_or(anyhow::anyhow!("cannot get a first version document"))?; + let Some(first_doc) = provider.try_get_first_doc(doc_id).await? else { + doc.report().other( + "Cannot find a first version of the referenced document", + REPORT_CONTEXT, + ); + return Ok(false); + }; allowed_authors.extend(first_doc.authors()); - let last_doc = provider.try_get_last_doc(doc_id).await?.ok_or( - anyhow::anyhow!( - "A latest version of the document must exist if a first version exists" - ), - )?; + let Some(last_doc) = provider.try_get_last_doc(doc_id).await? else { + doc.report().other( + "A latest version of the document must exist if a first version exists", + REPORT_CONTEXT, + ); + return Ok(false); + }; allowed_authors.extend(last_doc.doc_meta().collaborators().iter().cloned()); } @@ -135,13 +143,13 @@ impl DocumentOwnershipRule { }; allowed_authors.extend(first_ref_doc.authors()); - let last_doc = - provider - .try_get_last_doc(*doc_ref.id()) - .await? - .ok_or(anyhow::anyhow!( - "A latest version of the document must exist if a first version exists" - ))?; + let Some(last_doc) = provider.try_get_last_doc(*doc_ref.id()).await? else { + doc.report().other( + "A latest version of the document must exist if a first version exists", + REPORT_CONTEXT, + ); + return Ok(false); + }; allowed_authors.extend(last_doc.doc_meta().collaborators().iter().cloned()); }, diff --git a/rust/signed_doc/src/validator/rules/ownership/tests/mod.rs b/rust/signed_doc/src/validator/rules/ownership/tests/mod.rs index 156ee8ad29..ada28b665e 100644 --- a/rust/signed_doc/src/validator/rules/ownership/tests/mod.rs +++ b/rust/signed_doc/src/validator/rules/ownership/tests/mod.rs @@ -1,5 +1,43 @@ //! Ownership Validation Rule testing +use catalyst_types::{catalyst_id::role_index::RoleId, uuid::UuidV7}; +use ed25519_dalek::ed25519::signature::Signer; + +use crate::{ + builder::tests::Builder, + metadata::SupportedField, + providers::tests::TestCatalystProvider, + validator::rules::{DocumentOwnershipRule, utils::create_dummy_key_pair}, +}; + mod collaborators_field_based; mod ref_field_based; mod without_collaborators; + +#[tokio::test] +async fn empty_provider_test() { + let provider = TestCatalystProvider::default(); + + let (a_sk, a_kid) = create_dummy_key_pair(RoleId::Role0); + let id = UuidV7::new(); + let ver = UuidV7::new(); + let doc = Builder::new() + .with_metadata_field(SupportedField::Id(id)) + .with_metadata_field(SupportedField::Ver(ver)) + .add_signature(|m| a_sk.sign(&m).to_vec(), a_kid.clone()) + .unwrap() + .build(); + + let result = DocumentOwnershipRule::OriginalAuthor + .check_inner(&doc, &provider) + .await; + assert!(matches!(result, Ok(false))); + let result = DocumentOwnershipRule::RefFieldBased + .check_inner(&doc, &provider) + .await; + assert!(matches!(result, Ok(false))); + let result = DocumentOwnershipRule::CollaboratorsFieldBased + .check_inner(&doc, &provider) + .await; + assert!(matches!(result, Ok(false))); +} From cb9214c7d18aa9da9507eeefaa6f7c15db0238d4 Mon Sep 17 00:00:00 2001 From: Apisit Ritruengroj Date: Wed, 10 Dec 2025 14:22:51 +0700 Subject: [PATCH 2/2] fix: last doc --- .../src/validator/rules/ownership/mod.rs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/rust/signed_doc/src/validator/rules/ownership/mod.rs b/rust/signed_doc/src/validator/rules/ownership/mod.rs index d2a6f0f46b..c7565a653c 100644 --- a/rust/signed_doc/src/validator/rules/ownership/mod.rs +++ b/rust/signed_doc/src/validator/rules/ownership/mod.rs @@ -113,13 +113,11 @@ impl DocumentOwnershipRule { }; allowed_authors.extend(first_doc.authors()); - let Some(last_doc) = provider.try_get_last_doc(doc_id).await? else { - doc.report().other( - "A latest version of the document must exist if a first version exists", - REPORT_CONTEXT, - ); - return Ok(false); - }; + let last_doc = provider.try_get_last_doc(doc_id).await?.ok_or( + anyhow::anyhow!( + "A latest version of the document must exist if a first version exists" + ), + )?; allowed_authors.extend(last_doc.doc_meta().collaborators().iter().cloned()); } @@ -143,13 +141,13 @@ impl DocumentOwnershipRule { }; allowed_authors.extend(first_ref_doc.authors()); - let Some(last_doc) = provider.try_get_last_doc(*doc_ref.id()).await? else { - doc.report().other( - "A latest version of the document must exist if a first version exists", - REPORT_CONTEXT, - ); - return Ok(false); - }; + let last_doc = + provider + .try_get_last_doc(*doc_ref.id()) + .await? + .ok_or(anyhow::anyhow!( + "A latest version of the document must exist if a first version exists" + ))?; allowed_authors.extend(last_doc.doc_meta().collaborators().iter().cloned()); },