2017-05-21 19:22:48 +00:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2024-07-09 15:01:42 +00:00
|
|
|
|
use xso::{AsXml, FromXml};
|
2024-06-26 11:13:02 +00:00
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::date::DateTime;
|
2024-06-26 11:13:02 +00:00
|
|
|
|
use crate::ns;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use crate::presence::PresencePayload;
|
2017-05-21 19:22:48 +00:00
|
|
|
|
|
2024-06-26 11:13:02 +00:00
|
|
|
|
/// Represents the last time the user interacted with their system.
|
2024-07-09 15:01:42 +00:00
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 11:13:02 +00:00
|
|
|
|
#[xml(namespace = ns::IDLE, name = "idle")]
|
|
|
|
|
pub struct Idle {
|
|
|
|
|
/// The time at which the user stopped interacting.
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
pub since: DateTime,
|
|
|
|
|
}
|
2017-05-21 19:22:48 +00:00
|
|
|
|
|
2018-09-20 18:51:48 +00:00
|
|
|
|
impl PresencePayload for Idle {}
|
|
|
|
|
|
2017-05-21 19:22:48 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2024-07-24 18:28:22 +00:00
|
|
|
|
use minidom::Element;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use std::str::FromStr;
|
2024-06-21 14:27:43 +00:00
|
|
|
|
use xso::error::{Error, FromElementError};
|
2017-05-21 19:22:48 +00:00
|
|
|
|
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Idle, 16);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 19:22:48 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-21T20:19:55+01:00'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Idle::try_from(elem).unwrap();
|
2017-05-21 19:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
2024-06-26 11:13:02 +00:00
|
|
|
|
let elem: Element =
|
|
|
|
|
"<idle xmlns='urn:xmpp:idle:1' since='2017-05-21T20:19:55+01:00'><coucou/></idle>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
2017-05-21 19:22:48 +00:00
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2024-06-26 11:13:02 +00:00
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
2017-05-21 19:22:48 +00:00
|
|
|
|
};
|
2024-06-26 11:13:02 +00:00
|
|
|
|
assert_eq!(message, "Unknown child in Idle element.");
|
2017-05-21 19:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_id() {
|
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
2017-05-21 19:22:48 +00:00
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-05-21 19:22:48 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2024-06-26 11:13:02 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
message,
|
|
|
|
|
"Required attribute field 'since' on Idle element missing."
|
|
|
|
|
);
|
2017-05-21 19:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-27 11:20:19 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_date() {
|
|
|
|
|
// There is no thirteenth month.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-13-01T12:23:34Z'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-27 11:20:19 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string))
|
|
|
|
|
if string.is::<chrono::ParseError>() =>
|
|
|
|
|
{
|
|
|
|
|
string
|
|
|
|
|
}
|
|
|
|
|
other => panic!("unexpected result: {:?}", other),
|
2017-05-27 11:20:19 +00:00
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.to_string(), "input is out of range");
|
2017-05-27 11:20:19 +00:00
|
|
|
|
|
|
|
|
|
// Timezone ≥24:00 aren’t allowed.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11:02+25:00'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-27 11:20:19 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string))
|
|
|
|
|
if string.is::<chrono::ParseError>() =>
|
|
|
|
|
{
|
|
|
|
|
string
|
|
|
|
|
}
|
2017-05-27 11:20:19 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.to_string(), "input is out of range");
|
2017-05-27 11:20:19 +00:00
|
|
|
|
|
|
|
|
|
// Timezone without the : separator aren’t allowed.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11:02+0100'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-27 11:20:19 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string))
|
|
|
|
|
if string.is::<chrono::ParseError>() =>
|
|
|
|
|
{
|
|
|
|
|
string
|
|
|
|
|
}
|
2017-05-27 11:20:19 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.to_string(), "input contains invalid characters");
|
2017-05-27 11:20:19 +00:00
|
|
|
|
|
|
|
|
|
// No seconds, error message could be improved.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11+01:00'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-27 11:20:19 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string))
|
|
|
|
|
if string.is::<chrono::ParseError>() =>
|
|
|
|
|
{
|
|
|
|
|
string
|
|
|
|
|
}
|
2017-05-27 11:20:19 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.to_string(), "input contains invalid characters");
|
2017-05-27 11:20:19 +00:00
|
|
|
|
|
|
|
|
|
// TODO: maybe we’ll want to support this one, as per XEP-0082 §4.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='20170527T12:11:02+01:00'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-27 11:20:19 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string))
|
|
|
|
|
if string.is::<chrono::ParseError>() =>
|
|
|
|
|
{
|
|
|
|
|
string
|
|
|
|
|
}
|
2017-05-27 11:20:19 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.to_string(), "input contains invalid characters");
|
2017-05-27 11:20:19 +00:00
|
|
|
|
|
|
|
|
|
// No timezone.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-27T12:11:02'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-27 11:20:19 +00:00
|
|
|
|
let error = Idle::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string))
|
|
|
|
|
if string.is::<chrono::ParseError>() =>
|
|
|
|
|
{
|
|
|
|
|
string
|
|
|
|
|
}
|
2017-05-27 11:20:19 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2020-01-21 22:46:00 +00:00
|
|
|
|
assert_eq!(message.to_string(), "premature end of input");
|
2017-05-27 11:20:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 19:22:48 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialise() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<idle xmlns='urn:xmpp:idle:1' since='2017-05-21T20:19:55+01:00'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let idle = Idle {
|
|
|
|
|
since: DateTime::from_str("2017-05-21T20:19:55+01:00").unwrap(),
|
|
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = idle.into();
|
2017-05-21 19:22:48 +00:00
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
|
}
|
|
|
|
|
}
|