instant_epp/domain/
renew.rs

1//! Types for EPP domain renew request
2
3use chrono::{DateTime, NaiveDate, Utc};
4use instant_xml::{FromXml, ToXml};
5
6use super::{Period, XMLNS};
7use crate::common::{NoExtension, EPP_XMLNS};
8use crate::request::{Command, Transaction};
9
10impl<'a> Transaction<NoExtension> for DomainRenew<'a> {}
11
12impl<'a> Command for DomainRenew<'a> {
13    type Response = RenewData;
14    const COMMAND: &'static str = "renew";
15}
16
17impl<'a> DomainRenew<'a> {
18    pub fn new(name: &'a str, current_expiry_date: NaiveDate, period: Period) -> Self {
19        Self {
20            domain: DomainRenewRequestData {
21                name,
22                current_expiry_date,
23                period,
24            },
25        }
26    }
27}
28
29// Request
30
31/// Type for data under the domain `<renew>` tag
32#[derive(Debug, ToXml)]
33#[xml(rename = "renew", ns(XMLNS))]
34pub struct DomainRenewRequestData<'a> {
35    /// The name of the domain to be renewed
36    name: &'a str,
37    /// The current expiry date of the domain in 'Y-m-d' format
38    #[xml(rename = "curExpDate")]
39    current_expiry_date: NaiveDate,
40    /// The period of renewal
41    period: Period,
42}
43
44#[derive(Debug, ToXml)]
45/// Type for EPP XML `<renew>` command for domains
46#[xml(rename = "renew", ns(EPP_XMLNS))]
47pub struct DomainRenew<'a> {
48    /// The data under the `<renew>` tag for the domain renewal
49    #[xml(rename = "renew")]
50    domain: DomainRenewRequestData<'a>,
51}
52
53// Response
54
55/// Type that represents the `<renData>` tag for domain renew response
56#[derive(Debug, FromXml)]
57#[xml(rename = "renData", ns(XMLNS))]
58pub struct RenewData {
59    /// The name of the domain
60    pub name: String,
61    /// The new expiry date after renewal
62    #[xml(rename = "exDate")]
63    pub expiring_at: Option<DateTime<Utc>>,
64}
65
66#[cfg(test)]
67mod tests {
68    use super::{DomainRenew, Period};
69    use crate::response::ResultCode;
70    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
71
72    use chrono::{NaiveDate, TimeZone, Utc};
73
74    #[test]
75    fn command() {
76        let exp_date = NaiveDate::from_ymd_opt(2022, 7, 23).unwrap();
77        let object = DomainRenew::new("eppdev.com", exp_date, Period::years(1).unwrap());
78        assert_serialized("request/domain/renew.xml", &object);
79    }
80
81    #[test]
82    fn response() {
83        let object = response_from_file::<DomainRenew>("response/domain/renew.xml");
84
85        let result = object.res_data().unwrap();
86
87        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
88        assert_eq!(object.result.message, SUCCESS_MSG);
89        assert_eq!(result.name, "eppdev-1.com");
90        assert_eq!(
91            *result.expiring_at.as_ref().unwrap(),
92            Utc.with_ymd_and_hms(2024, 7, 23, 15, 31, 20).unwrap()
93        );
94        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
95        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
96    }
97}