Skip to content

Commit de1f0fc

Browse files
authored
chore(rust/signed-doc): Refactor catalyst-signed-doc tests, move most of the rules unit tests into the separate tests.rs files (#580)
* wip * wip * wip * fix * wip * wip
1 parent 1cbeb58 commit de1f0fc

File tree

27 files changed

+3628
-3345
lines changed

27 files changed

+3628
-3345
lines changed

rust/signed_doc/src/validator/rules/collaborators.rs

Lines changed: 0 additions & 156 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//! `collaborators` rule type impl.
2+
3+
#[cfg(test)]
4+
mod tests;
5+
6+
use crate::CatalystSignedDocument;
7+
8+
/// `collaborators` field validation rule
9+
#[derive(Debug)]
10+
pub(crate) enum CollaboratorsRule {
11+
/// Is 'collaborators' specified
12+
#[allow(dead_code)]
13+
Specified {
14+
/// optional flag for the `collaborators` field
15+
optional: bool,
16+
},
17+
/// 'collaborators' is not specified
18+
NotSpecified,
19+
}
20+
21+
impl CollaboratorsRule {
22+
/// Field validation rule
23+
#[allow(clippy::unused_async)]
24+
pub(crate) async fn check(
25+
&self,
26+
doc: &CatalystSignedDocument,
27+
) -> anyhow::Result<bool> {
28+
if let Self::Specified { optional } = self {
29+
if doc.doc_meta().collaborators().is_empty() && !optional {
30+
doc.report().missing_field(
31+
"collaborators",
32+
"Document must have at least one entry in 'collaborators' field",
33+
);
34+
return Ok(false);
35+
}
36+
}
37+
if let Self::NotSpecified = self {
38+
if !doc.doc_meta().collaborators().is_empty() {
39+
doc.report().unknown_field(
40+
"collaborators",
41+
&format!(
42+
"{:#?}",
43+
doc.doc_meta()
44+
.collaborators()
45+
.iter()
46+
.map(ToString::to_string)
47+
.reduce(|a, b| format!("{a}, {b}"))
48+
),
49+
"Document does not expect to have a 'collaborators' field",
50+
);
51+
return Ok(false);
52+
}
53+
}
54+
55+
Ok(true)
56+
}
57+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use catalyst_types::catalyst_id::role_index::RoleId;
2+
use test_case::test_case;
3+
4+
use super::*;
5+
use crate::{
6+
builder::tests::Builder, metadata::SupportedField,
7+
validator::rules::utils::create_dummy_key_pair,
8+
};
9+
10+
#[test_case(
11+
|| {
12+
Builder::new()
13+
.with_metadata_field(SupportedField::Collaborators(
14+
vec![create_dummy_key_pair(RoleId::Role0).2].into()
15+
))
16+
.build()
17+
}
18+
=> true
19+
;
20+
"valid 'collaborators' field present"
21+
)]
22+
#[test_case(
23+
|| {
24+
Builder::new().build()
25+
}
26+
=> true
27+
;
28+
"missing 'collaborators' field"
29+
)]
30+
#[tokio::test]
31+
async fn section_rule_specified_optional_test(
32+
doc_gen: impl FnOnce() -> CatalystSignedDocument
33+
) -> bool {
34+
let rule = CollaboratorsRule::Specified { optional: true };
35+
36+
let doc = doc_gen();
37+
rule.check(&doc).await.unwrap()
38+
}
39+
40+
#[test_case(
41+
|| {
42+
Builder::new()
43+
.with_metadata_field(SupportedField::Collaborators(
44+
vec![create_dummy_key_pair(RoleId::Role0).2].into()
45+
))
46+
.build()
47+
}
48+
=> true
49+
;
50+
"valid 'collaborators' field present"
51+
)]
52+
#[test_case(
53+
|| {
54+
Builder::new().build()
55+
}
56+
=> false
57+
;
58+
"missing 'collaborators' field"
59+
)]
60+
#[tokio::test]
61+
async fn section_rule_specified_not_optional_test(
62+
doc_gen: impl FnOnce() -> CatalystSignedDocument
63+
) -> bool {
64+
let rule = CollaboratorsRule::Specified { optional: false };
65+
66+
let doc = doc_gen();
67+
rule.check(&doc).await.unwrap()
68+
}
69+
70+
#[test_case(
71+
|| {
72+
Builder::new().build()
73+
}
74+
=> true
75+
;
76+
"missing 'collaborators' field"
77+
)]
78+
#[test_case(
79+
|| {
80+
Builder::new()
81+
.with_metadata_field(SupportedField::Collaborators(
82+
vec![create_dummy_key_pair(RoleId::Role0).2].into()
83+
))
84+
.build()
85+
}
86+
=> false
87+
;
88+
"valid 'collaborators' field present"
89+
)]
90+
#[tokio::test]
91+
async fn section_rule_not_specified_test(doc_gen: impl FnOnce() -> CatalystSignedDocument) -> bool {
92+
let rule = CollaboratorsRule::NotSpecified;
93+
94+
let doc = doc_gen();
95+
rule.check(&doc).await.unwrap()
96+
}

0 commit comments

Comments
 (0)