From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 175BC1FF0B7 for ; Fri, 13 Nov 2026 16:12:48 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id AE26A21996; Fri, 13 Nov 2026 16:09:56 +0100 (CET) From: Lukas Wagner To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox 32/55] notify: add test suite for Notification type Date: Fri, 13 Nov 2026 16:08:05 +0100 Message-ID: <20261113150828.756836-33-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261113150828.756836-1-l.wagner@proxmox.com> References: <20261113150828.756836-1-l.wagner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794582527078 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.208 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: H7FNAO634JTSS63YWWV26DM4PNJWGOTU X-Message-ID-Hash: H7FNAO634JTSS63YWWV26DM4PNJWGOTU X-MailFrom: l.wagner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Lukas Wagner --- proxmox-notify/src/notification.rs | 110 +++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/proxmox-notify/src/notification.rs b/proxmox-notify/src/notification.rs index 4d938b846..45ab08b87 100644 --- a/proxmox-notify/src/notification.rs +++ b/proxmox-notify/src/notification.rs @@ -154,3 +154,113 @@ impl Notification { &mut self.metadata.additional_fields } } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::*; + + const TEST_MAIL: &[u8] = include_bytes!("../testdata/test1.msg"); + + const ALL_SEVERITIES: [Severity; 5] = [ + Severity::Info, + Severity::Notice, + Severity::Warning, + Severity::Error, + Severity::Unknown, + ]; + + #[test] + fn severity_string_roundtrip() { + for severity in ALL_SEVERITIES { + let parsed: Severity = severity.to_string().parse().unwrap(); + assert_eq!(parsed, severity); + } + } + + #[test] + fn severity_display_matches_serde() { + for severity in ALL_SEVERITIES { + let serialized = serde_json::to_value(severity).unwrap(); + assert_eq!(serialized, Value::String(severity.to_string())); + } + } + + #[test] + fn severity_from_invalid_str() { + assert!("".parse::().is_err()); + assert!("Info".parse::().is_err()); + assert!("critical".parse::().is_err()); + } + + #[test] + fn severity_ordering() { + assert!(Severity::Info < Severity::Notice); + assert!(Severity::Notice < Severity::Warning); + assert!(Severity::Warning < Severity::Error); + } + + #[test] + fn from_template() { + let fields = HashMap::from([("hostname".to_string(), "pve".to_string())]); + let data = json!({"key": "value"}); + + let notification = + Notification::from_template(Severity::Warning, "test", data.clone(), fields.clone()); + + assert_eq!(notification.severity(), Severity::Warning); + assert_eq!(notification.fields(), &fields); + assert_eq!( + notification.content, + Content::Template { + template_name: "test".into(), + data, + } + ); + } + + #[test] + fn new_forwarded_mail() { + let raw = TEST_MAIL.to_vec(); + + let notification = + Notification::new_forwarded_mail(Severity::Unknown, raw.clone(), HashMap::new()) + .unwrap(); + + assert_eq!(notification.severity(), Severity::Unknown); + assert!(notification.fields().is_empty()); + assert_eq!(notification.content, Content::ForwardedMail { raw }); + } + + #[test] + fn fields_mut() { + let mut notification = + Notification::from_template(Severity::Info, "test", Value::Null, HashMap::new()); + + notification + .fields_mut() + .insert("type".into(), "vzdump".into()); + + assert_eq!( + notification.fields().get("type").map(String::as_str), + Some("vzdump") + ); + } + + #[test] + fn serde_roundtrip() { + let fields = HashMap::from([("hostname".to_string(), "pve".to_string())]); + let notifications = [ + Notification::from_template(Severity::Error, "test", json!({"a": [1, 2]}), fields), + Notification::new_forwarded_mail(Severity::Info, TEST_MAIL.to_vec(), HashMap::new()) + .unwrap(), + ]; + + for notification in notifications { + let serialized = serde_json::to_string(¬ification).unwrap(); + let deserialized: Notification = serde_json::from_str(&serialized).unwrap(); + assert_eq!(deserialized, notification); + } + } +} -- 2.47.3