1// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
2// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8use date::DateTime;
9
10generate_element!(
11 History, "history", MUC,
12 attributes: [
13 maxchars: Option<u32> = "maxchars" => optional,
14 maxstanzas: Option<u32> = "maxstanzas" => optional,
15 seconds: Option<u32> = "seconds" => optional,
16 since: Option<DateTime> = "since" => optional,
17 ]
18);
19
20generate_element!(
21 Muc, "x", MUC, children: [
22 password: Option<String> = ("password", MUC) => String,
23 history: Option<History> = ("history", MUC) => History
24 ]
25);
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30 use try_from::TryFrom;
31 use minidom::Element;
32 use error::Error;
33 use std::str::FromStr;
34 use compare_elements::NamespaceAwareCompare;
35
36 #[test]
37 fn test_muc_simple() {
38 let elem: Element = "<x xmlns='http://jabber.org/protocol/muc'/>".parse().unwrap();
39 Muc::try_from(elem).unwrap();
40 }
41
42 #[test]
43 fn test_muc_invalid_child() {
44 let elem: Element = "<x xmlns='http://jabber.org/protocol/muc'><coucou/></x>".parse().unwrap();
45 let error = Muc::try_from(elem).unwrap_err();
46 let message = match error {
47 Error::ParseError(string) => string,
48 _ => panic!(),
49 };
50 assert_eq!(message, "Unknown child in x element.");
51 }
52
53 #[test]
54 fn test_muc_serialise() {
55 let elem: Element = "<x xmlns='http://jabber.org/protocol/muc'/>".parse().unwrap();
56 let muc = Muc {
57 password: None,
58 history: None,
59 };
60 let elem2 = muc.into();
61 assert_eq!(elem, elem2);
62 }
63
64 #[test]
65 fn test_muc_invalid_attribute() {
66 let elem: Element = "<x xmlns='http://jabber.org/protocol/muc' coucou=''/>".parse().unwrap();
67 let error = Muc::try_from(elem).unwrap_err();
68 let message = match error {
69 Error::ParseError(string) => string,
70 _ => panic!(),
71 };
72 assert_eq!(message, "Unknown attribute in x element.");
73 }
74
75 #[test]
76 fn test_muc_simple_password() {
77 let elem: Element = "
78 <x xmlns='http://jabber.org/protocol/muc'>
79 <password>coucou</password>
80 </x>"
81 .parse().unwrap();
82 let elem1 = elem.clone();
83 let muc = Muc::try_from(elem).unwrap();
84 assert_eq!(muc.password, Some("coucou".to_owned()));
85
86 let elem2 = Element::from(muc);
87 assert!(elem1.compare_to(&elem2));
88 }
89
90 #[test]
91 fn history() {
92 let elem: Element = "
93 <x xmlns='http://jabber.org/protocol/muc'>
94 <history maxstanzas='0'/>
95 </x>"
96 .parse().unwrap();
97 let muc = Muc::try_from(elem).unwrap();
98 let history = muc.history.unwrap();
99 assert_eq!(history.maxstanzas, Some(0));
100 assert_eq!(history.maxchars, None);
101 assert_eq!(history.seconds, None);
102 assert_eq!(history.since, None);
103
104 let elem: Element = "
105 <x xmlns='http://jabber.org/protocol/muc'>
106 <history since='1970-01-01T00:00:00Z'/>
107 </x>"
108 .parse().unwrap();
109 let muc = Muc::try_from(elem).unwrap();
110 assert_eq!(muc.history.unwrap().since.unwrap(), DateTime::from_str("1970-01-01T00:00:00+00:00").unwrap());
111 }
112}