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
//! Types for EPP contact info request

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

use super::{ContactAuthInfo, Fax, PostalInfo, Status, Voice, XMLNS};
use crate::common::{NoExtension, EPP_XMLNS};
use crate::request::{Command, Transaction};

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

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

// Request

/// Type for elements under the contact `<info>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "info", ns(XMLNS))]
pub struct ContactInfoRequest<'a> {
    /// The contact id for the info command
    id: &'a str,
    /// The `<authInfo>` data
    auth_info: ContactAuthInfo<'a>,
}

/// Type for EPP XML `<info>` command for contacts
#[derive(Debug, ToXml)]
#[xml(rename = "info", ns(EPP_XMLNS))]
pub struct ContactInfo<'a> {
    /// Data for `<info>` command for contact
    info: ContactInfoRequest<'a>,
}

impl<'a> ContactInfo<'a> {
    pub fn new(id: &'a str, auth_password: &'a str) -> ContactInfo<'a> {
        Self {
            info: ContactInfoRequest {
                id,
                auth_info: ContactAuthInfo::new(auth_password),
            },
        }
    }
}

// Response

/// Type that represents the `<infData>` tag for contact check response
#[derive(Debug, FromXml)]
#[xml(rename = "infData", ns(XMLNS))]
pub struct InfoData {
    /// The contact id
    pub id: String,
    /// The contact ROID
    pub roid: String,
    /// The list of contact statuses
    pub statuses: Vec<Status>,
    /// The postal info for the contact
    pub postal_info: PostalInfo<'static>,
    /// The voice data for the contact
    pub voice: Option<Voice<'static>>,
    /// The fax data for the contact
    pub fax: Option<Fax<'static>>,
    /// The email for the contact
    pub email: String,
    /// The epp user to whom the contact belongs
    #[xml(rename = "clID")]
    pub client_id: String,
    /// The epp user who created the contact
    #[xml(rename = "crID")]
    pub creator_id: String,
    /// The creation date
    #[xml(rename = "crDate")]
    pub created_at: DateTime<Utc>,
    /// The epp user who last updated the contact
    #[xml(rename = "upID")]
    pub updater_id: Option<String>,
    /// The last update date
    #[xml(rename = "upDate")]
    pub updated_at: Option<DateTime<Utc>>,
    /// The contact transfer date
    #[xml(rename = "trDate")]
    pub transferred_at: Option<DateTime<Utc>>,
    /// The contact auth info
    #[xml(rename = "authInfo")]
    pub auth_info: Option<ContactAuthInfo<'static>>,
}

#[cfg(test)]
mod tests {
    use chrono::{TimeZone, Utc};

    use super::ContactInfo;
    use crate::contact::Status;
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn command() {
        let object = ContactInfo::new("eppdev-contact-3", "eppdev-387323");
        assert_serialized("request/contact/info.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<ContactInfo>("response/contact/info.xml");

        let result = object.res_data().unwrap();
        let fax = result.fax.as_ref().unwrap();
        let voice_ext = result.voice.as_ref().unwrap().extension.as_ref().unwrap();
        let fax_ext = fax.extension.as_ref().unwrap();
        let auth_info = result.auth_info.as_ref().unwrap();

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(result.id, "eppdev-contact-3");
        assert_eq!(result.roid, "UNDEF-ROID");
        assert_eq!(result.statuses[0], Status::Ok);
        assert_eq!(result.postal_info.info_type, "loc");
        assert_eq!(result.postal_info.name, "John Doe");
        assert_eq!(result.postal_info.organization, Some("Acme Widgets".into()));
        assert_eq!(result.postal_info.address.street[0], "58");
        assert_eq!(result.postal_info.address.street[1], "Orchid Road");
        assert_eq!(result.postal_info.address.city, "Paris");
        assert_eq!(result.postal_info.address.province, Some("Paris".into()));
        assert_eq!(
            result.postal_info.address.postal_code,
            Some("392374".into())
        );
        assert_eq!(result.postal_info.address.country.alpha2, "FR");
        assert_eq!(
            result.voice.as_ref().unwrap().number,
            "+33.47237942".to_string()
        );
        assert_eq!(*voice_ext, "123".to_string());
        assert_eq!(fax.number, "+33.86698799".to_string());
        assert_eq!(*fax_ext, "243".to_string());
        assert_eq!(result.email, "contact@eppdev.net");
        assert_eq!(result.client_id, "eppdev");
        assert_eq!(result.creator_id, "SYSTEM");
        assert_eq!(
            result.created_at,
            Utc.with_ymd_and_hms(2021, 7, 23, 13, 9, 9).unwrap(),
        );
        assert_eq!(*(result.updater_id.as_ref().unwrap()), "SYSTEM");
        assert_eq!(
            result.updated_at,
            Utc.with_ymd_and_hms(2021, 7, 23, 13, 9, 9).single()
        );
        assert_eq!(auth_info.password, "eppdev-387323");
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

    #[test]
    fn response_minimal() {
        let object = response_from_file::<ContactInfo>("response/contact/info_minimal.xml");

        let result = object.res_data().unwrap();

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(result.id, "eppdev-contact-3");
        assert_eq!(result.roid, "UNDEF-ROID");
        assert_eq!(result.statuses[0], Status::Ok);
        assert_eq!(result.postal_info.info_type, "loc");
        assert_eq!(result.postal_info.name, "John Doe");
        assert_eq!(result.postal_info.organization, None);
        assert_eq!(result.postal_info.address.street[0], "58");
        assert_eq!(result.postal_info.address.street[1], "Orchid Road");
        assert_eq!(result.postal_info.address.city, "Paris");
        assert_eq!(result.postal_info.address.province, None);
        assert_eq!(result.postal_info.address.postal_code, None);
        assert_eq!(result.postal_info.address.country.alpha2, "FR");
        assert_eq!(result.voice, None);
        assert_eq!(result.fax, None);
        assert_eq!(result.email, "contact@eppdev.net");
        assert_eq!(result.client_id, "eppdev");
        assert_eq!(result.creator_id, "SYSTEM");
        assert_eq!(
            result.created_at,
            Utc.with_ymd_and_hms(2021, 7, 23, 13, 9, 9).unwrap(),
        );
        assert_eq!(result.updater_id, None);
        assert_eq!(result.updated_at, None);
        assert_eq!(result.auth_info, None);
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }
}