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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Types for EPP domain create request

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

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

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

impl<'a> Command for DomainCreate<'a> {
    type Response = CreateData;
    const COMMAND: &'static str = "create";
}

// Request

/// Type for elements under the domain `<create>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "create", ns(XMLNS))]
pub struct DomainCreateRequestData<'a> {
    /// The domain name
    pub name: &'a str,
    /// The period of registration
    pub period: Period,
    /// The list of nameserver hosts
    /// either of type `HostObjList` or `HostAttrList`
    pub ns: Option<NameServers<'a>>,
    /// The domain registrant
    pub registrant: Option<&'a str>,
    /// The list of contacts for the domain
    pub contacts: Option<&'a [DomainContact<'a>]>,
    /// The auth info for the domain
    pub auth_info: DomainAuthInfo<'a>,
}

#[derive(Debug, ToXml)]
/// Type for EPP XML `<create>` command for domains
#[xml(rename = "create", ns(EPP_XMLNS))]
pub struct DomainCreate<'a> {
    /// The data for the domain to be created with
    /// T being the type of nameserver list (`HostObjList` or `HostAttrList`)
    /// to be supplied
    pub domain: DomainCreateRequestData<'a>,
}

impl<'a> DomainCreate<'a> {
    pub fn new(
        name: &'a str,
        period: Period,
        ns: Option<&'a [HostInfo<'a>]>,
        registrant: Option<&'a str>,
        auth_password: &'a str,
        contacts: Option<&'a [DomainContact<'a>]>,
    ) -> Self {
        Self {
            domain: DomainCreateRequestData {
                name,
                period,
                ns: ns.map(|ns| NameServers { ns: ns.into() }),
                registrant,
                auth_info: DomainAuthInfo::new(auth_password),
                contacts,
            },
        }
    }
}

// Response

/// Type that represents the `<chkData>` tag for domain create response
#[derive(Debug, FromXml)]
#[xml(rename = "creData", ns(XMLNS))]
pub struct CreateData {
    /// The domain name
    pub name: String,
    /// The creation date
    #[xml(rename = "crDate")]
    pub created_at: DateTime<Utc>,
    /// The expiry date
    #[xml(rename = "exDate")]
    pub expiring_at: Option<DateTime<Utc>>,
}

#[cfg(test)]
mod tests {
    use std::net::IpAddr;

    use chrono::{TimeZone, Utc};

    use super::{DomainContact, DomainCreate, Period};
    use crate::domain::{HostAttr, HostInfo, HostObj};
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn command() {
        let contacts = &[
            DomainContact {
                contact_type: "admin".into(),
                id: "eppdev-contact-3".into(),
            },
            DomainContact {
                contact_type: "tech".into(),
                id: "eppdev-contact-3".into(),
            },
            DomainContact {
                contact_type: "billing".into(),
                id: "eppdev-contact-3".into(),
            },
        ];

        let object = DomainCreate::new(
            "eppdev-1.com",
            Period::years(1).unwrap(),
            None,
            Some("eppdev-contact-3"),
            "epP4uthd#v",
            Some(contacts),
        );

        assert_serialized("request/domain/create.xml", &object);
    }

    #[test]
    fn command_with_host_obj() {
        let contacts = &[
            DomainContact {
                contact_type: "admin".into(),
                id: "eppdev-contact-3".into(),
            },
            DomainContact {
                contact_type: "tech".into(),
                id: "eppdev-contact-3".into(),
            },
            DomainContact {
                contact_type: "billing".into(),
                id: "eppdev-contact-3".into(),
            },
        ];

        let hosts = &[
            HostInfo::Obj(HostObj {
                name: "ns1.test.com".into(),
            }),
            HostInfo::Obj(HostObj {
                name: "ns2.test.com".into(),
            }),
        ];
        let object = DomainCreate::new(
            "eppdev-1.com",
            Period::years(1).unwrap(),
            Some(hosts),
            Some("eppdev-contact-3"),
            "epP4uthd#v",
            Some(contacts),
        );

        assert_serialized("request/domain/create_with_host_obj.xml", &object);
    }

    #[test]
    fn command_with_host_attr() {
        let contacts = &[
            DomainContact {
                contact_type: "admin".into(),
                id: "eppdev-contact-3".into(),
            },
            DomainContact {
                contact_type: "tech".into(),
                id: "eppdev-contact-3".into(),
            },
            DomainContact {
                contact_type: "billing".into(),
                id: "eppdev-contact-3".into(),
            },
        ];

        let hosts = &[
            HostInfo::Attr(HostAttr {
                name: "ns1.eppdev-1.com".into(),
                addresses: None,
            }),
            HostInfo::Attr(HostAttr {
                name: "ns2.eppdev-1.com".into(),
                addresses: Some(vec![
                    IpAddr::from([177, 232, 12, 58]),
                    IpAddr::from([0x2404, 0x6800, 0x4001, 0x801, 0, 0, 0, 0x200e]),
                ]),
            }),
        ];

        let object = DomainCreate::new(
            "eppdev-2.com",
            Period::years(1).unwrap(),
            Some(hosts),
            Some("eppdev-contact-3"),
            "epP4uthd#v",
            Some(contacts),
        );

        assert_serialized("request/domain/create_with_host_attr.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<DomainCreate>("response/domain/create.xml");

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

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(result.name, "eppdev-2.com");
        assert_eq!(
            result.created_at,
            Utc.with_ymd_and_hms(2021, 7, 25, 18, 11, 35).unwrap()
        );
        assert_eq!(
            *result.expiring_at.as_ref().unwrap(),
            Utc.with_ymd_and_hms(2022, 7, 25, 18, 11, 34).unwrap()
        );
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }
}