parsers/xmpp: MUC bookmarks have nickname typed as ResourcePart

This commit is contained in:
xmppftw xmppftw 2024-12-17 17:21:08 +01:00 committed by xmpp ftw
commit c16456f381
10 changed files with 65 additions and 28 deletions

View file

@ -19,6 +19,7 @@ use xso::{AsXml, FromXml};
use jid::BareJid;
pub use crate::bookmarks2;
use crate::jid::ResourcePart;
use crate::ns;
/// A conference bookmark.
@ -38,8 +39,8 @@ pub struct Conference {
pub name: Option<String>,
/// The nick the user will use to join this conference.
#[xml(extract(default, fields(text(type_ = String))))]
pub nick: Option<String>,
#[xml(extract(default, fields(text(type_ = ResourcePart))))]
pub nick: Option<ResourcePart>,
/// The password required to join this conference.
#[xml(extract(default, fields(text(type_ = String))))]
@ -129,6 +130,18 @@ mod tests {
assert_eq!(elem1, elem2);
}
#[test]
fn wrong_resource() {
// This emoji is not valid according to Resource prep
let elem: Element = "<storage xmlns='storage:bookmarks'><url name='Example' url='https://example.com/'/><conference autojoin='true' jid='foo@muc.localhost' name='TEST'><nick>Whatever\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}</nick></conference></storage>".parse().unwrap();
let res = Storage::try_from(elem);
assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string().as_str(),
"text parse error: resource doesnt pass resourceprep validation"
);
}
#[test]
fn complete() {
let elem: Element = "<storage xmlns='storage:bookmarks'><url name='Example' url='https://example.org/'/><conference autojoin='true' jid='test-muc@muc.localhost' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></storage>".parse().unwrap();
@ -143,7 +156,10 @@ mod tests {
BareJid::new("test-muc@muc.localhost").unwrap()
);
assert_eq!(storage.conferences[0].clone().name.unwrap(), "Test MUC");
assert_eq!(storage.conferences[0].clone().nick.unwrap(), "Coucou");
assert_eq!(
storage.conferences[0].clone().nick.unwrap().as_str(),
"Coucou"
);
assert_eq!(storage.conferences[0].clone().password.unwrap(), "secret");
}
}

View file

@ -14,6 +14,7 @@
use xso::{AsXml, FromXml};
use crate::jid::ResourcePart;
use crate::ns;
use minidom::Element;
@ -39,8 +40,8 @@ pub struct Conference {
pub name: Option<String>,
/// The nick the user will use to join this conference.
#[xml(extract(default, fields(text(type_ = String))))]
pub nick: Option<String>,
#[xml(extract(default, fields(text(type_ = ResourcePart))))]
pub nick: Option<ResourcePart>,
/// The password required to join this conference.
#[xml(extract(default, fields(text(type_ = String))))]
@ -91,13 +92,25 @@ mod tests {
assert_eq!(elem1, elem2);
}
#[test]
fn wrong_resource() {
// This emoji is not valid according to Resource prep
let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:1' autojoin='true'><nick>Whatever\u{1F469}\u{1F3FE}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F469}\u{1F3FC}</nick></conference>".parse().unwrap();
let res = Conference::try_from(elem);
assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string().as_str(),
"text parse error: resource doesnt pass resourceprep validation"
);
}
#[test]
fn complete() {
let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:1' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password><extensions><test xmlns='urn:xmpp:unknown' /></extensions></conference>".parse().unwrap();
let conference = Conference::try_from(elem).unwrap();
assert_eq!(conference.autojoin, true);
assert_eq!(conference.name, Some(String::from("Test MUC")));
assert_eq!(conference.clone().nick.unwrap(), "Coucou");
assert_eq!(conference.clone().nick.unwrap().as_str(), "Coucou");
assert_eq!(conference.clone().password.unwrap(), "secret");
let payloads = conference.clone().extensions.unwrap().payloads;
assert_eq!(payloads.len(), 1);
@ -115,7 +128,7 @@ mod tests {
println!("FOO: conference: {:?}", conference);
assert_eq!(conference.autojoin, true);
assert_eq!(conference.name, Some(String::from("Test MUC")));
assert_eq!(conference.clone().nick.unwrap(), "Coucou");
assert_eq!(conference.clone().nick.unwrap().as_str(), "Coucou");
assert_eq!(conference.clone().password.unwrap(), "secret");
let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='urn:xmpp:bookmarks:1'><item xmlns='http://jabber.org/protocol/pubsub#event' id='test-muc@muc.localhost'><conference xmlns='urn:xmpp:bookmarks:1' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></item></items></event>".parse().unwrap();
@ -132,7 +145,7 @@ mod tests {
let conference = Conference::try_from(payload).unwrap();
assert_eq!(conference.autojoin, true);
assert_eq!(conference.name, Some(String::from("Test MUC")));
assert_eq!(conference.clone().nick.unwrap(), "Coucou");
assert_eq!(conference.clone().nick.unwrap().as_str(), "Coucou");
assert_eq!(conference.clone().password.unwrap(), "secret");
}
}