Skip to content

Commit 92a937c

Browse files
authored
feat(rust/signed-doc): Signed Documents integration tests (#562)
* refactor existing proposal tests using test case crate * add more test cases * update comment tests * wip * wip * wip * wip * wip * wip * update SignatureKidRule initialisation, include admin roles * fix * wip * wip * wip * wip * wip * fix clippy * wip * fix
1 parent 2df110d commit 92a937c

18 files changed

+980
-966
lines changed

rust/signed_doc/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ impl ContentBuilder {
9292
json: &serde_json::Value,
9393
) -> anyhow::Result<SignaturesBuilder> {
9494
anyhow::ensure!(
95-
self.metadata.content_type() == Some(ContentType::Json),
95+
self.metadata.content_type() == Some(ContentType::Json)
96+
|| self.metadata.content_type() == Some(ContentType::SchemaJson),
9697
"Already set metadata field `content-type` is not JSON value"
9798
);
9899

rust/signed_doc/src/validator/mod.rs

Lines changed: 3 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -5,182 +5,24 @@ pub(crate) mod rules;
55

66
use std::{collections::HashMap, sync::LazyLock};
77

8-
use catalyst_types::catalyst_id::role_index::RoleId;
9-
use rules::{
10-
ContentEncodingRule, ContentRule, ContentSchema, ContentTypeRule, IdRule, OriginalAuthorRule,
11-
ParametersRule, RefRule, ReplyRule, Rules, SectionRule, SignatureKidRule, VerRule,
12-
};
8+
use rules::Rules;
139

1410
use crate::{
15-
doc_types::{
16-
BRAND_PARAMETERS, CAMPAIGN_PARAMETERS, CATEGORY_PARAMETERS, PROPOSAL, PROPOSAL_COMMENT,
17-
PROPOSAL_COMMENT_FORM_TEMPLATE, PROPOSAL_FORM_TEMPLATE, PROPOSAL_SUBMISSION_ACTION,
18-
},
1911
metadata::DocType,
2012
providers::{CatalystIdProvider, CatalystSignedDocumentProvider},
21-
validator::rules::{CollaboratorsRule, SignatureRule, TemplateRule},
22-
CatalystSignedDocument, ContentEncoding, ContentType,
13+
CatalystSignedDocument,
2314
};
2415

2516
/// A table representing a full set or validation rules per document id.
2617
static DOCUMENT_RULES: LazyLock<HashMap<DocType, Rules>> = LazyLock::new(document_rules_init);
2718

28-
/// Proposal
29-
/// Require field: type, id, ver, template, parameters
30-
/// <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/docs/proposal/>
31-
fn proposal_rule() -> Rules {
32-
// Parameter can be either brand, campaign or category
33-
let parameters = vec![
34-
BRAND_PARAMETERS.clone(),
35-
CAMPAIGN_PARAMETERS.clone(),
36-
CATEGORY_PARAMETERS.clone(),
37-
];
38-
Rules {
39-
id: IdRule,
40-
ver: VerRule,
41-
content_type: ContentTypeRule::Specified {
42-
exp: ContentType::Json,
43-
},
44-
content_encoding: ContentEncodingRule::Specified {
45-
exp: vec![ContentEncoding::Brotli],
46-
optional: false,
47-
},
48-
template: TemplateRule::Specified {
49-
allowed_type: PROPOSAL_FORM_TEMPLATE.clone(),
50-
},
51-
parameters: ParametersRule::Specified {
52-
allowed_type: parameters.clone(),
53-
optional: false,
54-
},
55-
doc_ref: RefRule::NotSpecified,
56-
reply: ReplyRule::NotSpecified,
57-
section: SectionRule::NotSpecified,
58-
collaborators: CollaboratorsRule::NotSpecified,
59-
content: ContentRule::NotNil,
60-
kid: SignatureKidRule {
61-
allowed_roles: [RoleId::Proposer].into_iter().collect(),
62-
},
63-
signature: SignatureRule { mutlisig: false },
64-
original_author: OriginalAuthorRule,
65-
}
66-
}
67-
68-
/// Proposal Comment
69-
/// Require field: type, id, ver, ref, template, parameters
70-
/// <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/docs/proposal_comment_template/>
71-
fn proposal_comment_rule() -> Rules {
72-
// Parameter can be either brand, campaign or category
73-
let parameters = vec![
74-
BRAND_PARAMETERS.clone(),
75-
CAMPAIGN_PARAMETERS.clone(),
76-
CATEGORY_PARAMETERS.clone(),
77-
];
78-
Rules {
79-
id: IdRule,
80-
ver: VerRule,
81-
content_type: ContentTypeRule::Specified {
82-
exp: ContentType::Json,
83-
},
84-
content_encoding: ContentEncodingRule::Specified {
85-
exp: vec![ContentEncoding::Brotli],
86-
optional: false,
87-
},
88-
template: TemplateRule::Specified {
89-
allowed_type: PROPOSAL_COMMENT_FORM_TEMPLATE.clone(),
90-
},
91-
doc_ref: RefRule::Specified {
92-
allowed_type: vec![PROPOSAL.clone()],
93-
multiple: false,
94-
optional: false,
95-
},
96-
reply: ReplyRule::Specified {
97-
allowed_type: PROPOSAL_COMMENT.clone(),
98-
optional: true,
99-
},
100-
section: SectionRule::NotSpecified,
101-
parameters: ParametersRule::Specified {
102-
allowed_type: parameters.clone(),
103-
optional: false,
104-
},
105-
collaborators: CollaboratorsRule::NotSpecified,
106-
content: ContentRule::NotNil,
107-
kid: SignatureKidRule {
108-
allowed_roles: [RoleId::Role0].into_iter().collect(),
109-
},
110-
signature: SignatureRule { mutlisig: false },
111-
original_author: OriginalAuthorRule,
112-
}
113-
}
114-
115-
/// Proposal Submission Action
116-
/// Require fields: type, id, ver, ref, parameters
117-
/// <https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/signed_doc/docs/proposal_submission_action/>
118-
#[allow(clippy::expect_used)]
119-
fn proposal_submission_action_rule() -> Rules {
120-
// Parameter can be either brand, campaign or category
121-
let parameters = vec![
122-
BRAND_PARAMETERS.clone(),
123-
CAMPAIGN_PARAMETERS.clone(),
124-
CATEGORY_PARAMETERS.clone(),
125-
];
126-
127-
let proposal_action_json_schema_content = &serde_json::from_str(include_str!(
128-
"./../../../../specs/definitions/signed_docs/docs/payload_schemas/proposal_submission_action.schema.json"
129-
))
130-
.expect("Must be a valid json file");
131-
132-
let proposal_action_json_schema =
133-
json_schema::JsonSchema::try_from(proposal_action_json_schema_content)
134-
.expect("Must be a valid json scheme file");
135-
136-
Rules {
137-
id: IdRule,
138-
ver: VerRule,
139-
content_type: ContentTypeRule::Specified {
140-
exp: ContentType::Json,
141-
},
142-
content_encoding: ContentEncodingRule::Specified {
143-
exp: vec![ContentEncoding::Brotli],
144-
optional: false,
145-
},
146-
template: TemplateRule::NotSpecified,
147-
parameters: ParametersRule::Specified {
148-
allowed_type: parameters,
149-
optional: false,
150-
},
151-
doc_ref: RefRule::Specified {
152-
allowed_type: vec![PROPOSAL.clone()],
153-
multiple: false,
154-
optional: false,
155-
},
156-
reply: ReplyRule::NotSpecified,
157-
section: SectionRule::NotSpecified,
158-
collaborators: CollaboratorsRule::NotSpecified,
159-
content: ContentRule::StaticSchema(ContentSchema::Json(proposal_action_json_schema)),
160-
kid: SignatureKidRule {
161-
allowed_roles: [RoleId::Proposer].into_iter().collect(),
162-
},
163-
signature: SignatureRule { mutlisig: false },
164-
original_author: OriginalAuthorRule,
165-
}
166-
}
167-
16819
/// `DOCUMENT_RULES` initialization function
16920
#[allow(clippy::expect_used)]
17021
fn document_rules_init() -> HashMap<DocType, Rules> {
171-
let mut document_rules_map: HashMap<DocType, Rules> = Rules::documents_rules()
22+
let document_rules_map: HashMap<DocType, Rules> = Rules::documents_rules()
17223
.expect("cannot fail to initialize validation rules")
17324
.collect();
17425

175-
// TODO: remove this redefinitions of the validation rules after
176-
// `catalyst_signed_documents_rules!` macro would be fully finished
177-
document_rules_map.insert(PROPOSAL.clone(), proposal_rule());
178-
document_rules_map.insert(PROPOSAL_COMMENT.clone(), proposal_comment_rule());
179-
document_rules_map.insert(
180-
PROPOSAL_SUBMISSION_ACTION.clone(),
181-
proposal_submission_action_rule(),
182-
);
183-
18426
document_rules_map
18527
}
18628

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod utils;
2525
mod ver;
2626

2727
pub(crate) use collaborators::CollaboratorsRule;
28-
pub(crate) use content::{ContentRule, ContentSchema};
28+
pub(crate) use content::ContentRule;
2929
pub(crate) use content_encoding::ContentEncodingRule;
3030
pub(crate) use content_type::ContentTypeRule;
3131
pub(crate) use doc_ref::RefRule;

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ impl TemplateRule {
8484
return false;
8585
};
8686
match template_content_type {
87-
ContentType::Json | ContentType::SchemaJson => {
88-
templated_json_schema_check(doc, template_doc)
89-
},
90-
ContentType::Cddl
87+
ContentType::SchemaJson => templated_json_schema_check(doc, template_doc),
88+
ContentType::Json
89+
| ContentType::Cddl
9190
| ContentType::Cbor
9291
| ContentType::Css
9392
| ContentType::CssHandlebars
@@ -98,7 +97,7 @@ impl TemplateRule {
9897
| ContentType::Plain
9998
| ContentType::PlainHandlebars => {
10099
// TODO: not implemented yet
101-
true
100+
false
102101
},
103102
}
104103
};
@@ -183,7 +182,7 @@ mod tests {
183182
.with_metadata_field(SupportedField::Id(*template_ref.id()))
184183
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
185184
.with_metadata_field(SupportedField::Type(allowed_type))
186-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
185+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
187186
.with_content(json_schema)
188187
.build();
189188
provider.add_document(None, &doc).unwrap();
@@ -212,7 +211,7 @@ mod tests {
212211
.with_metadata_field(SupportedField::Id(*template_ref.id()))
213212
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
214213
.with_metadata_field(SupportedField::Type(allowed_type))
215-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
214+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
216215
.with_content(json_schema)
217216
.build();
218217
provider.add_document(None, &doc).unwrap();
@@ -238,7 +237,7 @@ mod tests {
238237
.with_metadata_field(SupportedField::Id(*template_ref.id()))
239238
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
240239
.with_metadata_field(SupportedField::Type(allowed_type))
241-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
240+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
242241
.with_content(json_schema)
243242
.build();
244243
provider.add_document(None, &doc).unwrap();
@@ -265,7 +264,7 @@ mod tests {
265264
.with_metadata_field(SupportedField::Id(*template_ref.id()))
266265
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
267266
.with_metadata_field(SupportedField::Type(allowed_type))
268-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
267+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
269268
.with_content(json_schema)
270269
.build();
271270
provider.add_document(None, &doc).unwrap();
@@ -293,7 +292,7 @@ mod tests {
293292
.with_metadata_field(SupportedField::Id(*template_ref.id()))
294293
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
295294
.with_metadata_field(SupportedField::Type(UuidV4::new().into()))
296-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
295+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
297296
.with_content(json_schema)
298297
.build();
299298
provider.add_document(None, &doc).unwrap();
@@ -321,7 +320,7 @@ mod tests {
321320
let doc = Builder::new()
322321
.with_metadata_field(SupportedField::Id(*template_ref.id()))
323322
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
324-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
323+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
325324
.with_content(json_schema)
326325
.build();
327326
provider.add_document(None, &doc).unwrap();
@@ -377,7 +376,7 @@ mod tests {
377376
.with_metadata_field(SupportedField::Id(*template_ref.id()))
378377
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
379378
.with_metadata_field(SupportedField::Type(allowed_type))
380-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
379+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
381380
.build();
382381
provider.add_document(None, &doc).unwrap();
383382
@@ -404,7 +403,7 @@ mod tests {
404403
.with_metadata_field(SupportedField::Id(*template_ref.id()))
405404
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
406405
.with_metadata_field(SupportedField::Type(allowed_type))
407-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
406+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
408407
.with_content(vec![1,2 ,3])
409408
.build();
410409
provider.add_document(None, &doc).unwrap();
@@ -478,7 +477,7 @@ mod tests {
478477
.with_metadata_field(SupportedField::Id(*template_ref.id()))
479478
.with_metadata_field(SupportedField::Ver(*template_ref.ver()))
480479
.with_metadata_field(SupportedField::Type(allowed_type))
481-
.with_metadata_field(SupportedField::ContentType(ContentType::Json))
480+
.with_metadata_field(SupportedField::ContentType(ContentType::SchemaJson))
482481
.with_content(json_schema)
483482
.build();
484483
provider.add_document(None, &doc).unwrap();

0 commit comments

Comments
 (0)