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
7#![deny(missing_docs)]
8
9use jid::Jid;
10
11generate_element!(
12 /// Gives the identifier a service has stamped on this stanza, often in
13 /// order to identify it inside of [an archive](../mam/index.html).
14 StanzaId, "stanza-id", SID,
15 attributes: [
16 /// The id associated to this stanza by another entity.
17 id: String = "id" => required,
18
19 /// The entity who stamped this stanza-id.
20 by: Jid = "by" => required,
21 ]
22);
23
24generate_element!(
25 /// A hack for MUC before version 1.31 to track a message which may have
26 /// its 'id' attribute changed.
27 OriginId, "origin-id", SID,
28 attributes: [
29 /// The id this client set for this stanza.
30 id: String = "id" => required,
31 ]
32);
33
34#[cfg(test)]
35mod tests {
36 use super::*;
37 use try_from::TryFrom;
38 use minidom::Element;
39 use error::Error;
40 use std::str::FromStr;
41
42 #[test]
43 fn test_simple() {
44 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
45 let stanza_id = StanzaId::try_from(elem).unwrap();
46 assert_eq!(stanza_id.id, String::from("coucou"));
47 assert_eq!(stanza_id.by, Jid::from_str("coucou@coucou").unwrap());
48
49 let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
50 let origin_id = OriginId::try_from(elem).unwrap();
51 assert_eq!(origin_id.id, String::from("coucou"));
52 }
53
54 #[test]
55 fn test_invalid_child() {
56 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'><coucou/></stanza-id>".parse().unwrap();
57 let error = StanzaId::try_from(elem).unwrap_err();
58 let message = match error {
59 Error::ParseError(string) => string,
60 _ => panic!(),
61 };
62 assert_eq!(message, "Unknown child in stanza-id element.");
63 }
64
65 #[test]
66 fn test_invalid_id() {
67 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'/>".parse().unwrap();
68 let error = StanzaId::try_from(elem).unwrap_err();
69 let message = match error {
70 Error::ParseError(string) => string,
71 _ => panic!(),
72 };
73 assert_eq!(message, "Required attribute 'id' missing.");
74 }
75
76 #[test]
77 fn test_invalid_by() {
78 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou'/>".parse().unwrap();
79 let error = StanzaId::try_from(elem).unwrap_err();
80 let message = match error {
81 Error::ParseError(string) => string,
82 _ => panic!(),
83 };
84 assert_eq!(message, "Required attribute 'by' missing.");
85 }
86
87 #[test]
88 fn test_serialise() {
89 let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>".parse().unwrap();
90 let stanza_id = StanzaId { id: String::from("coucou"), by: Jid::from_str("coucou@coucou").unwrap() };
91 let elem2 = stanza_id.into();
92 assert_eq!(elem, elem2);
93 }
94}