Skip to content

Commit 210a21e

Browse files
committed
move unit tests for IdRule and VerRule
1 parent 94dfdf3 commit 210a21e

File tree

4 files changed

+183
-106
lines changed

4 files changed

+183
-106
lines changed

rust/signed_doc/src/validator/mod.rs

Lines changed: 7 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn proposal_rule() -> Rules {
4040
CATEGORY_PARAMETERS.clone(),
4141
];
4242
Rules {
43-
id: Some(IdRule),
44-
ver: Some(VerRule),
43+
id: IdRule,
44+
ver: VerRule,
4545
content_type: ContentTypeRule {
4646
exp: ContentType::Json,
4747
},
@@ -76,8 +76,8 @@ fn proposal_comment_rule() -> Rules {
7676
CATEGORY_PARAMETERS.clone(),
7777
];
7878
Rules {
79-
id: Some(IdRule),
80-
ver: Some(VerRule),
79+
id: IdRule,
80+
ver: VerRule,
8181
content_type: ContentTypeRule {
8282
exp: ContentType::Json,
8383
},
@@ -129,8 +129,8 @@ fn proposal_submission_action_rule() -> Rules {
129129
.expect("Must be a valid json scheme file");
130130

131131
Rules {
132-
id: Some(IdRule),
133-
ver: Some(VerRule),
132+
id: IdRule,
133+
ver: VerRule,
134134
content_type: ContentTypeRule {
135135
exp: ContentType::Json,
136136
},
@@ -285,92 +285,7 @@ where
285285

286286
#[cfg(test)]
287287
mod tests {
288-
use std::time::SystemTime;
289-
290-
use uuid::{Timestamp, Uuid};
291-
292-
use crate::{
293-
builder::tests::Builder,
294-
metadata::SupportedField,
295-
providers::{tests::TestCatalystSignedDocumentProvider, CatalystSignedDocumentProvider},
296-
validator::{
297-
document_rules_init,
298-
rules::{IdRule, VerRule},
299-
},
300-
UuidV7,
301-
};
302-
303-
#[tokio::test]
304-
async fn document_id_and_ver_test() {
305-
let provider = TestCatalystSignedDocumentProvider::default();
306-
let now = SystemTime::now()
307-
.duration_since(SystemTime::UNIX_EPOCH)
308-
.unwrap()
309-
.as_secs();
310-
311-
let uuid_v7 = UuidV7::new();
312-
let doc = Builder::new()
313-
.with_metadata_field(SupportedField::Id(uuid_v7))
314-
.with_metadata_field(SupportedField::Ver(uuid_v7))
315-
.build();
316-
317-
let is_id_valid = IdRule.check(&doc, &provider).await.unwrap();
318-
let is_ver_valid = VerRule.check(&doc).await.unwrap();
319-
assert!(is_id_valid && is_ver_valid);
320-
321-
let ver = Uuid::new_v7(Timestamp::from_unix_time(now - 1, 0, 0, 0))
322-
.try_into()
323-
.unwrap();
324-
let id = Uuid::new_v7(Timestamp::from_unix_time(now + 1, 0, 0, 0))
325-
.try_into()
326-
.unwrap();
327-
assert!(ver < id);
328-
let doc = Builder::new()
329-
.with_metadata_field(SupportedField::Id(id))
330-
.with_metadata_field(SupportedField::Ver(ver))
331-
.build();
332-
333-
let is_id_valid = IdRule.check(&doc, &provider).await.unwrap();
334-
let is_ver_valid = VerRule.check(&doc).await.unwrap();
335-
assert!(is_id_valid);
336-
assert!(!is_ver_valid);
337-
338-
let to_far_in_past = Uuid::new_v7(Timestamp::from_unix_time(
339-
now - provider.past_threshold().unwrap().as_secs() - 1,
340-
0,
341-
0,
342-
0,
343-
))
344-
.try_into()
345-
.unwrap();
346-
let doc = Builder::new()
347-
.with_metadata_field(SupportedField::Id(to_far_in_past))
348-
.with_metadata_field(SupportedField::Ver(to_far_in_past))
349-
.build();
350-
351-
let is_id_valid = IdRule.check(&doc, &provider).await.unwrap();
352-
let is_ver_valid = VerRule.check(&doc).await.unwrap();
353-
assert!(!is_id_valid);
354-
assert!(is_ver_valid);
355-
356-
let to_far_in_future = Uuid::new_v7(Timestamp::from_unix_time(
357-
now + provider.future_threshold().unwrap().as_secs() + 1,
358-
0,
359-
0,
360-
0,
361-
))
362-
.try_into()
363-
.unwrap();
364-
let doc = Builder::new()
365-
.with_metadata_field(SupportedField::Id(to_far_in_future))
366-
.with_metadata_field(SupportedField::Ver(to_far_in_future))
367-
.build();
368-
369-
let is_id_valid = IdRule.check(&doc, &provider).await.unwrap();
370-
let is_ver_valid = VerRule.check(&doc).await.unwrap();
371-
assert!(!is_id_valid);
372-
assert!(is_ver_valid);
373-
}
288+
use crate::validator::document_rules_init;
374289

375290
#[test]
376291
fn document_rules_init_test() {

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,88 @@ impl IdRule {
9191
Ok(is_valid)
9292
}
9393
}
94+
95+
#[cfg(test)]
96+
mod tests {
97+
use std::time::SystemTime;
98+
99+
use test_case::test_case;
100+
use uuid::{Timestamp, Uuid};
101+
102+
use super::*;
103+
use crate::{
104+
builder::tests::Builder, metadata::SupportedField,
105+
providers::tests::TestCatalystSignedDocumentProvider, UuidV7,
106+
};
107+
108+
#[test_case(
109+
|_| {
110+
let uuid_v7 = UuidV7::new();
111+
Builder::new()
112+
.with_metadata_field(SupportedField::Id(uuid_v7))
113+
.build()
114+
}
115+
=> true;
116+
"valid id"
117+
)]
118+
#[test_case(
119+
|provider| {
120+
let now = SystemTime::now()
121+
.duration_since(SystemTime::UNIX_EPOCH)
122+
.unwrap()
123+
.as_secs();
124+
let to_far_in_past = Uuid::new_v7(Timestamp::from_unix_time(
125+
now - provider.past_threshold().unwrap().as_secs() - 1,
126+
0,
127+
0,
128+
0,
129+
))
130+
.try_into()
131+
.unwrap();
132+
Builder::new()
133+
.with_metadata_field(SupportedField::Id(to_far_in_past))
134+
.build()
135+
}
136+
=> false;
137+
"`id` to far in past"
138+
)]
139+
#[test_case(
140+
|provider| {
141+
let now = SystemTime::now()
142+
.duration_since(SystemTime::UNIX_EPOCH)
143+
.unwrap()
144+
.as_secs();
145+
let to_far_in_future = Uuid::new_v7(Timestamp::from_unix_time(
146+
now + provider.future_threshold().unwrap().as_secs() + 1,
147+
0,
148+
0,
149+
0,
150+
))
151+
.try_into()
152+
.unwrap();
153+
Builder::new()
154+
.with_metadata_field(SupportedField::Id(to_far_in_future))
155+
.build()
156+
}
157+
=> false;
158+
"`id` to far in future"
159+
)]
160+
#[test_case(
161+
|_| {
162+
Builder::new()
163+
.with_metadata_field(SupportedField::Ver(UuidV7::new()))
164+
.build()
165+
}
166+
=> false;
167+
"missing `id` field"
168+
)]
169+
#[tokio::test]
170+
async fn id_test(
171+
doc_gen: impl FnOnce(&TestCatalystSignedDocumentProvider) -> CatalystSignedDocument
172+
) -> bool {
173+
let provider = TestCatalystSignedDocumentProvider::default();
174+
let doc = doc_gen(&provider);
175+
176+
IdRule.check(&doc, &provider).await.unwrap()
177+
}
178+
}

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ pub(crate) use ver::VerRule;
3030
/// Struct represented a full collection of rules for all fields
3131
pub(crate) struct Rules {
3232
/// 'id' field validation rule
33-
pub(crate) id: Option<IdRule>,
33+
pub(crate) id: IdRule,
3434
/// 'ver' field validation rule
35-
pub(crate) ver: Option<VerRule>,
35+
pub(crate) ver: VerRule,
3636
/// 'content-type' field validation rule
3737
pub(crate) content_type: ContentTypeRule,
3838
/// 'content-encoding' field validation rule
@@ -62,12 +62,8 @@ impl Rules {
6262
Provider: CatalystSignedDocumentProvider,
6363
{
6464
let rules = [
65-
self.id
66-
.as_ref()
67-
.map_or_else(|| pass().boxed(), |rule| rule.check(doc, provider).boxed()),
68-
self.ver
69-
.as_ref()
70-
.map_or_else(|| pass().boxed(), |rule| rule.check(doc).boxed()),
65+
self.id.check(doc, provider).boxed(),
66+
self.ver.check(doc).boxed(),
7167
self.content_type.check(doc).boxed(),
7268
self.content_encoding.check(doc).boxed(),
7369
self.content.check(doc, provider).boxed(),
@@ -87,9 +83,3 @@ impl Rules {
8783
Ok(res)
8884
}
8985
}
90-
91-
/// An async no-op function to pass a rule validation.
92-
#[allow(clippy::unused_async)]
93-
pub async fn pass() -> anyhow::Result<bool> {
94-
Ok(true)
95-
}

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,90 @@ impl VerRule {
4141
Ok(true)
4242
}
4343
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use std::time::SystemTime;
48+
49+
use test_case::test_case;
50+
use uuid::{Timestamp, Uuid};
51+
52+
use super::*;
53+
use crate::{builder::tests::Builder, metadata::SupportedField, UuidV7};
54+
55+
#[test_case(
56+
|| {
57+
let uuid_v7 = UuidV7::new();
58+
Builder::new()
59+
.with_metadata_field(SupportedField::Id(uuid_v7))
60+
.with_metadata_field(SupportedField::Ver(uuid_v7))
61+
.build()
62+
}
63+
=> true;
64+
"`ver` and `id` are equal"
65+
)]
66+
#[test_case(
67+
|| {
68+
let now = SystemTime::now()
69+
.duration_since(SystemTime::UNIX_EPOCH)
70+
.unwrap()
71+
.as_secs();
72+
let ver = Uuid::new_v7(Timestamp::from_unix_time(now + 1, 0, 0, 0))
73+
.try_into()
74+
.unwrap();
75+
let id = Uuid::new_v7(Timestamp::from_unix_time(now - 1, 0, 0, 0))
76+
.try_into()
77+
.unwrap();
78+
Builder::new()
79+
.with_metadata_field(SupportedField::Id(id))
80+
.with_metadata_field(SupportedField::Ver(ver))
81+
.build()
82+
}
83+
=> true;
84+
"`ver` greater than `id` are equal"
85+
)]
86+
#[test_case(
87+
|| {
88+
let now = SystemTime::now()
89+
.duration_since(SystemTime::UNIX_EPOCH)
90+
.unwrap()
91+
.as_secs();
92+
let ver = Uuid::new_v7(Timestamp::from_unix_time(now - 1, 0, 0, 0))
93+
.try_into()
94+
.unwrap();
95+
let id = Uuid::new_v7(Timestamp::from_unix_time(now + 1, 0, 0, 0))
96+
.try_into()
97+
.unwrap();
98+
Builder::new()
99+
.with_metadata_field(SupportedField::Id(id))
100+
.with_metadata_field(SupportedField::Ver(ver))
101+
.build()
102+
}
103+
=> false;
104+
"`ver` less than `id` are equal"
105+
)]
106+
#[test_case(
107+
|| {
108+
Builder::new()
109+
.with_metadata_field(SupportedField::Id(UuidV7::new()))
110+
.build()
111+
}
112+
=> false;
113+
"missing `ver` field"
114+
)]
115+
#[test_case(
116+
|| {
117+
Builder::new()
118+
.with_metadata_field(SupportedField::Ver(UuidV7::new()))
119+
.build()
120+
}
121+
=> false;
122+
"missing `id` field"
123+
)]
124+
#[tokio::test]
125+
async fn ver_test(doc_gen: impl FnOnce() -> CatalystSignedDocument) -> bool {
126+
let doc = doc_gen();
127+
128+
VerRule.check(&doc).await.unwrap()
129+
}
130+
}

0 commit comments

Comments
 (0)