1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//! Types for EPP domain info request

use chrono::{DateTime, Utc};
use instant_xml::{FromXml, ToXml};

use super::{DomainAuthInfo, DomainContact, HostAttr, NameServers, Status, XMLNS};
use crate::common::{NoExtension, EPP_XMLNS};
use crate::request::{Command, Transaction};

impl<'a> Transaction<NoExtension> for DomainInfo<'a> {}

impl<'a> Command for DomainInfo<'a> {
    type Response = InfoData;
    const COMMAND: &'static str = "info";
}

impl<'a> DomainInfo<'a> {
    pub fn new(name: &'a str, auth_password: Option<&'a str>) -> Self {
        Self {
            info: DomainInfoRequestData {
                name: Domain { hosts: "all", name },
                auth_info: auth_password.map(|password| DomainAuthInfo {
                    password: password.into(),
                }),
            },
        }
    }
}

// Request

/// Type for data under the `<name>` element tag for the domain `<info>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "name", ns(XMLNS))]
pub struct Domain<'a> {
    /// The hosts attribute. Default value is "all"
    #[xml(attribute)]
    hosts: &'a str,
    /// The name of the domain
    #[xml(direct)]
    name: &'a str,
}

/// Type for `<name>` element under the domain `<info>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "info", ns(XMLNS))]
pub struct DomainInfoRequestData<'a> {
    /// The data for the domain to be queried
    name: Domain<'a>,
    /// The auth info for the domain
    auth_info: Option<DomainAuthInfo<'a>>,
}

#[derive(Debug, ToXml)]
/// Type for EPP XML `<info>` command for domains
#[xml(rename = "info", ns(EPP_XMLNS))]
pub struct DomainInfo<'a> {
    /// The data under the `<info>` tag for domain info
    info: DomainInfoRequestData<'a>,
}

// Response

/// The two types of ns lists, hostObj and hostAttr, that may be returned in the
/// domain info response
#[derive(Debug, FromXml)]
pub struct DomainNsList {
    /// List of `<hostObj>` ns elements
    #[xml(rename = "hostObj")]
    pub host_obj: Option<Vec<String>>,
    /// List of `<hostAttr>` ns elements
    pub host_attr: Option<Vec<HostAttr<'static>>>,
}

/// Type that represents the `<infData>` tag for domain info response
#[derive(Debug, FromXml)]
#[xml(rename = "infData", ns(XMLNS))]
pub struct InfoData {
    /// The domain name
    pub name: String,
    /// The domain ROID
    pub roid: String,
    /// The list of domain statuses
    #[xml(rename = "status")]
    pub statuses: Option<Vec<Status>>,
    /// The domain registrant
    pub registrant: Option<String>,
    /// The list of domain contacts
    #[xml(rename = "contact")]
    pub contacts: Option<Vec<DomainContact<'static>>>,
    /// The list of domain nameservers
    pub ns: Option<NameServers<'static>>,
    /// The list of domain hosts
    #[xml(rename = "host")]
    pub hosts: Option<Vec<String>>,
    /// The epp user who owns the domain
    #[xml(rename = "clID")]
    pub client_id: String,
    /// The epp user who created the domain
    #[xml(rename = "crID")]
    pub creator_id: Option<String>,
    /// The domain creation date
    #[xml(rename = "crDate")]
    pub created_at: Option<DateTime<Utc>>,
    /// The domain expiry date
    #[xml(rename = "exDate")]
    pub expiring_at: Option<DateTime<Utc>>,
    /// The epp user who last updated the domain
    #[xml(rename = "upID")]
    pub updater_id: Option<String>,
    /// The domain last updated date
    #[xml(rename = "upDate")]
    pub updated_at: Option<DateTime<Utc>>,
    /// The domain transfer date
    #[xml(rename = "trDate")]
    pub transferred_at: Option<DateTime<Utc>>,
    /// The domain auth info
    #[xml(rename = "authInfo")]
    pub auth_info: Option<DomainAuthInfo<'static>>,
}

#[cfg(test)]
mod tests {
    use super::DomainInfo;
    use crate::domain::{HostInfo, HostObj, Status};
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
    use chrono::{TimeZone, Utc};

    #[test]
    fn command() {
        let object = DomainInfo::new("eppdev.com", Some("2fooBAR"));
        assert_serialized("request/domain/info.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<DomainInfo>("response/domain/info.xml");
        dbg!(&object);

        let result = object.res_data().unwrap();
        let auth_info = result.auth_info.as_ref().unwrap();
        let ns = result.ns.as_ref().unwrap();
        let hosts = result.hosts.as_ref().unwrap();
        let statuses = result.statuses.as_ref().unwrap();
        let registrant = result.registrant.as_ref().unwrap();
        let contacts = result.contacts.as_ref().unwrap();

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(result.name, "eppdev-1.com");
        assert_eq!(result.roid, "125899511_DOMAIN_COM-VRSN");
        assert_eq!(statuses[0], Status::Ok);
        assert_eq!(statuses[1], Status::ClientTransferProhibited);
        assert_eq!(*registrant, "eppdev-contact-2");
        assert_eq!(contacts[0].id, "eppdev-contact-2".to_string());
        assert_eq!(contacts[0].contact_type, "admin".to_string());
        assert_eq!(contacts[1].id, "eppdev-contact-2".to_string());
        assert_eq!(contacts[1].contact_type, "tech".to_string());
        assert_eq!(contacts[2].id, "eppdev-contact-2".to_string());
        assert_eq!(contacts[2].contact_type, "billing".to_string());
        assert_eq!(
            ns.ns[0],
            HostInfo::Obj(HostObj {
                name: "ns1.eppdev-1.com".into()
            })
        );
        assert_eq!(
            ns.ns[1],
            HostInfo::Obj(HostObj {
                name: "ns2.eppdev-1.com".into()
            })
        );
        assert_eq!((*hosts)[0], "ns1.eppdev-1.com");
        assert_eq!((*hosts)[1], "ns2.eppdev-1.com");
        assert_eq!(result.client_id, "eppdev");
        assert_eq!(*result.creator_id.as_ref().unwrap(), "SYSTEM");
        assert_eq!(
            *result.created_at.as_ref().unwrap(),
            Utc.with_ymd_and_hms(2021, 7, 23, 15, 31, 20).unwrap()
        );
        assert_eq!(*result.updater_id.as_ref().unwrap(), "SYSTEM");
        assert_eq!(
            *result.updated_at.as_ref().unwrap(),
            Utc.with_ymd_and_hms(2021, 7, 23, 15, 31, 21).unwrap()
        );
        assert_eq!(
            *result.expiring_at.as_ref().unwrap(),
            Utc.with_ymd_and_hms(2023, 7, 23, 15, 31, 20).unwrap()
        );
        assert_eq!(auth_info.password, "epP4uthd#v");
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

    #[test]
    fn response_alt() {
        response_from_file::<DomainInfo>("response/domain/info_alt.xml");
    }
}