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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Types for EPP domain transfer request

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

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

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

impl<'a> Command for DomainTransfer<'a> {
    type Response = TransferData;
    const COMMAND: &'static str = "transfer";
}

impl<'a> DomainTransfer<'a> {
    pub fn new(name: &'a str, period: Option<Period>, auth_password: &'a str) -> Self {
        Self::build(
            "request",
            name,
            period,
            Some(DomainAuthInfo::new(auth_password)),
        )
    }

    pub fn query(name: &'a str, auth_password: &'a str) -> Self {
        Self::build(
            "query",
            name,
            None,
            Some(DomainAuthInfo::new(auth_password)),
        )
    }

    pub fn approve(name: &'a str) -> Self {
        Self::build("approve", name, None, None)
    }

    pub fn reject(name: &'a str) -> Self {
        Self::build("reject", name, None, None)
    }

    pub fn cancel(name: &'a str) -> Self {
        Self::build("cancel", name, None, None)
    }

    fn build(
        operation: &'a str,
        name: &'a str,
        period: Option<Period>,
        auth_info: Option<DomainAuthInfo<'a>>,
    ) -> Self {
        Self {
            operation,
            domain: DomainTransferReqData {
                name,
                period,
                auth_info,
            },
        }
    }
}

// Request

/// Type for elements under the domain `<transfer>` tag
#[derive(Debug, ToXml)]
#[xml(rename = "transfer", ns(XMLNS))]
pub struct DomainTransferReqData<'a> {
    /// The name of the domain under transfer
    name: &'a str,
    /// The period of renewal upon a successful transfer
    /// Only applicable in case of a transfer request
    period: Option<Period>,
    /// The authInfo for the domain under transfer
    /// Only applicable to domain transfer and domain transfer query requests
    #[xml(rename = "authInfo")]
    auth_info: Option<DomainAuthInfo<'a>>,
}

#[derive(Debug, ToXml)]
#[xml(rename = "transfer", ns(EPP_XMLNS))]
/// Type for EPP XML `<transfer>` command for domains
pub struct DomainTransfer<'a> {
    /// The transfer operation to perform indicated by the 'op' attr
    /// The values are one of transfer or query
    #[xml(rename = "op", attribute)]
    operation: &'a str,
    /// The data under the `<transfer>` tag in the transfer request
    domain: DomainTransferReqData<'a>,
}

// Response

/// Type that represents the `<trnData>` tag for domain transfer response
#[derive(Debug, FromXml)]
#[xml(rename = "trnData", ns(XMLNS))]
pub struct TransferData {
    /// The domain name
    pub name: String,
    /// The domain transfer status
    #[xml(rename = "trStatus")]
    pub transfer_status: String,
    /// The epp user who requested the transfer
    #[xml(rename = "reID")]
    pub requester_id: String,
    /// The transfer rquest date
    #[xml(rename = "reDate")]
    pub requested_at: DateTime<Utc>,
    /// The epp user who should acknowledge the transfer request
    #[xml(rename = "acID")]
    pub ack_id: String,
    /// THe date by which the acknowledgment should be made
    #[xml(rename = "acDate")]
    pub ack_by: DateTime<Utc>,
    /// The domain expiry date
    #[xml(rename = "exDate")]
    pub expiring_at: Option<DateTime<Utc>>,
}

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

    use super::{DomainTransfer, Period};
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn request_command() {
        let object =
            DomainTransfer::new("testing.com", Some(Period::years(1).unwrap()), "epP4uthd#v");
        assert_serialized("request/domain/transfer_request.xml", &object);
    }

    #[test]
    fn approve_command() {
        let object = DomainTransfer::approve("testing.com");
        assert_serialized("request/domain/transfer_approve.xml", &object);
    }

    #[test]
    fn reject_command() {
        let object = DomainTransfer::reject("testing.com");
        assert_serialized("request/domain/transfer_reject.xml", &object);
    }

    #[test]
    fn cancel_command() {
        let object = DomainTransfer::cancel("testing.com");
        assert_serialized("request/domain/transfer_cancel.xml", &object);
    }

    #[test]
    fn query_command() {
        let object = DomainTransfer::query("testing.com", "epP4uthd#v");
        assert_serialized("request/domain/transfer_query.xml", &object);
    }

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

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

        assert_eq!(
            object.result.code,
            ResultCode::CommandCompletedSuccessfullyActionPending
        );
        assert_eq!(
            object.result.message,
            "Command completed successfully; action pending"
        );
        assert_eq!(result.name, "eppdev-transfer.com");
        assert_eq!(result.transfer_status, "pending");
        assert_eq!(result.requester_id, "eppdev");
        assert_eq!(
            result.requested_at,
            Utc.with_ymd_and_hms(2021, 7, 23, 15, 31, 21).unwrap(),
        );
        assert_eq!(result.ack_id, "ClientY");
        assert_eq!(
            result.ack_by,
            Utc.with_ymd_and_hms(2021, 7, 28, 15, 31, 21).unwrap()
        );
        assert_eq!(
            result.expiring_at,
            Utc.with_ymd_and_hms(2022, 7, 2, 14, 53, 19).single(),
        );
        assert_eq!(*object.tr_ids.client_tr_id.as_ref().unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

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

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

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

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

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

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

    #[test]
    fn query_response() {
        let object = response_from_file::<DomainTransfer>("response/domain/transfer_query.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-transfer.com");
        assert_eq!(result.transfer_status, "pending");
        assert_eq!(result.requester_id, "eppdev");
        assert_eq!(
            result.requested_at,
            Utc.with_ymd_and_hms(2021, 7, 23, 15, 31, 21).unwrap()
        );
        assert_eq!(result.ack_id, "ClientY");
        assert_eq!(
            result.ack_by,
            Utc.with_ymd_and_hms(2021, 7, 28, 15, 31, 21).unwrap()
        );
        assert_eq!(
            result.expiring_at,
            Utc.with_ymd_and_hms(2022, 7, 2, 14, 53, 19).single()
        );
        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }
}