1// Copyright (c) 2017 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
7use try_from::TryFrom;
8
9use minidom::Element;
10use jid::Jid;
11
12use error::Error;
13
14use ns;
15
16generate_element_with_only_attributes!(StanzaId, "stanza-id", ns::SID, [
17 id: String = "id" => required,
18 by: Jid = "by" => required,
19]);
20
21generate_element_with_only_attributes!(OriginId, "origin-id", ns::SID, [
22 id: String = "id" => required,
23]);
24
25#[cfg(test)]
26mod tests {
27 use super::*;
28 use std::str::FromStr;
29
30 #[test]
31 fn test_simple() {
32 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
33 let stanza_id = StanzaId::try_from(elem).unwrap();
34 assert_eq!(stanza_id.id, String::from("coucou"));
35 assert_eq!(stanza_id.by, Jid::from_str("coucou@coucou").unwrap());
36
37 let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
38 let origin_id = OriginId::try_from(elem).unwrap();
39 assert_eq!(origin_id.id, String::from("coucou"));
40 }
41
42 #[test]
43 fn test_invalid_child() {
44 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'><coucou/></stanza-id>".parse().unwrap();
45 let error = StanzaId::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 stanza-id element.");
51 }
52
53 #[test]
54 fn test_invalid_id() {
55 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'/>".parse().unwrap();
56 let error = StanzaId::try_from(elem).unwrap_err();
57 let message = match error {
58 Error::ParseError(string) => string,
59 _ => panic!(),
60 };
61 assert_eq!(message, "Required attribute 'id' missing.");
62 }
63
64 #[test]
65 fn test_invalid_by() {
66 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
67 let error = StanzaId::try_from(elem).unwrap_err();
68 let message = match error {
69 Error::ParseError(string) => string,
70 _ => panic!(),
71 };
72 assert_eq!(message, "Required attribute 'by' missing.");
73 }
74
75 #[test]
76 fn test_serialise() {
77 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
78 let stanza_id = StanzaId { id: String::from("coucou"), by: Jid::from_str("coucou@coucou").unwrap() };
79 let elem2 = stanza_id.into();
80 assert_eq!(elem, elem2);
81 }
82}