bookmarks2.rs

  1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7generate_attribute!(
  8    /// Whether a conference bookmark should be joined automatically.
  9    Autojoin,
 10    "autojoin",
 11    bool
 12);
 13
 14generate_element!(
 15    /// A conference bookmark.
 16    Conference, "conference", BOOKMARKS2,
 17    attributes: [
 18        /// Whether a conference bookmark should be joined automatically.
 19        autojoin: Default<Autojoin> = "autojoin",
 20
 21        /// A user-defined name for this conference.
 22        name: Option<String> = "name",
 23    ],
 24    children: [
 25        /// The nick the user will use to join this conference.
 26        nick: Option<String> = ("nick", BOOKMARKS2) => String,
 27
 28        /// The password required to join this conference.
 29        password: Option<String> = ("password", BOOKMARKS2) => String
 30    ]
 31);
 32
 33impl Conference {
 34    /// Create a new conference.
 35    pub fn new() -> Conference {
 36        Conference {
 37            autojoin: Autojoin::False,
 38            name: None,
 39            nick: None,
 40            password: None,
 41        }
 42    }
 43}
 44
 45#[cfg(test)]
 46mod tests {
 47    use super::*;
 48    use crate::util::compare_elements::NamespaceAwareCompare;
 49    use crate::Element;
 50    use std::convert::TryFrom;
 51    use crate::pubsub::pubsub::Item as PubSubItem;
 52    use crate::pubsub::event::PubSubEvent;
 53    use crate::ns;
 54
 55    #[cfg(target_pointer_width = "32")]
 56    #[test]
 57    fn test_size() {
 58        assert_size!(Conference, 40);
 59    }
 60
 61    #[cfg(target_pointer_width = "64")]
 62    #[test]
 63    fn test_size() {
 64        assert_size!(Conference, 80);
 65    }
 66
 67    #[test]
 68    fn simple() {
 69        let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:0'/>".parse().unwrap();
 70        let elem1 = elem.clone();
 71        let conference = Conference::try_from(elem).unwrap();
 72        assert_eq!(conference.autojoin, Autojoin::False);
 73        assert_eq!(conference.name, None);
 74        assert_eq!(conference.nick, None);
 75        assert_eq!(conference.password, None);
 76
 77        let elem2 = Element::from(Conference::new());
 78        assert!(elem1.compare_to(&elem2));
 79    }
 80
 81    #[test]
 82    fn complete() {
 83        let elem: Element = "<conference xmlns='urn:xmpp:bookmarks:0' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference>".parse().unwrap();
 84        let conference = Conference::try_from(elem).unwrap();
 85        assert_eq!(conference.autojoin, Autojoin::True);
 86        assert_eq!(conference.name, Some(String::from("Test MUC")));
 87        assert_eq!(conference.clone().nick.unwrap(), "Coucou");
 88        assert_eq!(conference.clone().password.unwrap(), "secret");
 89    }
 90
 91    #[test]
 92    fn wrapped() {
 93        let elem: Element = "<item xmlns='http://jabber.org/protocol/pubsub' id='test-muc@muc.localhost'><conference xmlns='urn:xmpp:bookmarks:0' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></item>".parse().unwrap();
 94        let item = PubSubItem::try_from(elem).unwrap();
 95        let payload = item.payload.clone().unwrap();
 96        let conference = Conference::try_from(payload).unwrap();
 97        assert_eq!(conference.autojoin, Autojoin::True);
 98        assert_eq!(conference.name, Some(String::from("Test MUC")));
 99        assert_eq!(conference.clone().nick.unwrap(), "Coucou");
100        assert_eq!(conference.clone().password.unwrap(), "secret");
101
102        let elem: Element = "<event xmlns='http://jabber.org/protocol/pubsub#event'><items node='urn:xmpp:bookmarks:0'><item xmlns='http://jabber.org/protocol/pubsub#event' id='test-muc@muc.localhost'><conference xmlns='urn:xmpp:bookmarks:0' autojoin='true' name='Test MUC'><nick>Coucou</nick><password>secret</password></conference></item></items></event>".parse().unwrap();
103        let mut items = match PubSubEvent::try_from(elem) {
104            Ok(PubSubEvent::PublishedItems { node, items }) => {
105                assert_eq!(&node.0, ns::BOOKMARKS2);
106                items
107            },
108            _ => panic!(),
109        };
110        assert_eq!(items.len(), 1);
111        let item = items.pop().unwrap();
112        let payload = item.payload.clone().unwrap();
113        let conference = Conference::try_from(payload).unwrap();
114        assert_eq!(conference.autojoin, Autojoin::True);
115        assert_eq!(conference.name, Some(String::from("Test MUC")));
116        assert_eq!(conference.clone().nick.unwrap(), "Coucou");
117        assert_eq!(conference.clone().password.unwrap(), "secret");
118    }
119}