report.rs (2253B)
1 #[cfg(not(feature = "std"))] 2 use alloc::string::String; 3 4 use crate::social::{RadrootsReportFileTarget, RadrootsReportType, RadrootsSocialTarget}; 5 6 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 7 #[derive(Clone, Debug)] 8 pub struct RadrootsReport { 9 pub reported_pubkey: String, 10 pub report_type: RadrootsReportType, 11 #[cfg_attr( 12 feature = "serde", 13 serde(default, skip_serializing_if = "Option::is_none") 14 )] 15 pub event: Option<RadrootsSocialTarget>, 16 #[cfg_attr( 17 feature = "serde", 18 serde(default, skip_serializing_if = "Option::is_none") 19 )] 20 pub file: Option<RadrootsReportFileTarget>, 21 #[cfg_attr( 22 feature = "serde", 23 serde(default, skip_serializing_if = "Option::is_none") 24 )] 25 pub content: Option<String>, 26 } 27 28 #[cfg(test)] 29 mod tests { 30 use super::*; 31 32 #[test] 33 fn report_model_requires_reported_pubkey_field() { 34 let report = RadrootsReport { 35 reported_pubkey: "a".repeat(64), 36 report_type: RadrootsReportType::Spam, 37 event: Some(RadrootsSocialTarget::Event { 38 id: "b".repeat(64), 39 author: Some("a".repeat(64)), 40 event_kind: Some(1), 41 relays: None, 42 }), 43 file: None, 44 content: Some("repeated spam".to_string()), 45 }; 46 47 assert_eq!(report.reported_pubkey.len(), 64); 48 assert_eq!(report.report_type, RadrootsReportType::Spam); 49 assert!(matches!( 50 report.event, 51 Some(RadrootsSocialTarget::Event { .. }) 52 )); 53 } 54 55 #[test] 56 fn report_model_supports_file_targets_with_required_pubkey() { 57 let report = RadrootsReport { 58 reported_pubkey: "a".repeat(64), 59 report_type: RadrootsReportType::Malware, 60 event: None, 61 file: Some(RadrootsReportFileTarget { 62 sha256: Some("b".repeat(64)), 63 url: Some("https://example.test/file".to_string()), 64 magnet: None, 65 }), 66 content: None, 67 }; 68 69 assert_eq!(report.reported_pubkey.len(), 64); 70 assert_eq!(report.file.expect("file").sha256.expect("hash").len(), 64); 71 } 72 }