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
//! Types for EPP RGP restore request

use instant_xml::{FromXml, ToXml};

use crate::{
    domain::{info::DomainInfo, update::DomainUpdate},
    request::{Extension, Transaction},
};

use super::XMLNS;

impl<'a> Transaction<Update<RgpRestoreRequest<'a>>> for DomainUpdate<'a> {}

impl<'a> Transaction<Update<RgpRestoreRequest<'a>>> for DomainInfo<'a> {}

impl<'a> Extension for Update<RgpRestoreRequest<'a>> {
    type Response = RgpRequestResponse;
}

// Request

#[derive(Debug, FromXml, ToXml)]
#[xml(rename = "update", ns(XMLNS))]
pub struct Update<T> {
    pub data: T,
}

/// Type corresponding to the `<restore>` tag for an rgp restore request
#[derive(Debug, ToXml)]
#[xml(rename = "restore", ns(XMLNS))]
pub struct RgpRestoreRequest<'a> {
    /// The value of the op attribute in the `<restore>` tag
    #[xml(attribute)]
    pub op: &'a str,
}

impl Default for RgpRestoreRequest<'static> {
    fn default() -> Self {
        Self { op: "request" }
    }
}

// Response

/// Type that represents the `<rgpStatus>` tag for domain rgp restore request response
#[derive(Debug, FromXml)]
#[xml(rename = "rgpStatus", ns(XMLNS))]
pub struct RgpStatus {
    /// The domain RGP status
    #[xml(rename = "s", attribute)]
    pub status: String,
}

#[derive(Debug, FromXml)]
#[xml(rename = "upData", ns(XMLNS))]
/// Type that represents the `<resData>` tag for domain transfer response
pub struct RgpRequestUpdateResponse {
    /// Data under the `<rgpStatus>` tag
    pub rgp_status: Vec<RgpStatus>,
}

#[derive(Debug, FromXml)]
#[xml(rename = "infData", ns(XMLNS))]
/// Type that represents the `<resData>` tag for domain transfer response
pub struct RgpRequestInfoResponse {
    /// Data under the `<rgpStatus>` tag
    pub rgp_status: Vec<RgpStatus>,
}

/// Type that represents the `<resData>` tag for domain transfer response
#[derive(Debug, FromXml)]
#[xml(forward)]
pub enum RgpRequestResponse {
    Update(RgpRequestUpdateResponse),
    Info(RgpRequestInfoResponse),
}

#[cfg(test)]
mod tests {
    use super::{RgpRestoreRequest, Update};
    use crate::domain::info::DomainInfo;
    use crate::domain::update::{DomainChangeInfo, DomainUpdate};
    use crate::extensions::rgp::request::RgpRequestResponse;
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file_with_ext, SUCCESS_MSG, SVTRID};

    #[test]
    fn request_command() {
        let domain_restore_request = Update {
            data: RgpRestoreRequest::default(),
        };

        let mut object = DomainUpdate::new("eppdev.com");

        let change_info = DomainChangeInfo {
            registrant: None,
            auth_info: None,
        };

        object.info(change_info);

        assert_serialized(
            "request/extensions/rgp_restore_request.xml",
            (&object, &domain_restore_request),
        );
    }

    #[test]
    fn request_response() {
        let object = response_from_file_with_ext::<DomainUpdate, Update<RgpRestoreRequest>>(
            "response/extensions/rgp_restore.xml",
        );
        let ext = object.extension.unwrap();

        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
        assert_eq!(object.result.message, SUCCESS_MSG);

        let data = match ext.data {
            RgpRequestResponse::Update(data) => data,
            _ => panic!("Unexpected response type"),
        };

        assert_eq!(data.rgp_status[0].status, "pendingRestore".to_string());
        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
    }

    #[test]
    fn domain_info_request_response() {
        let object = response_from_file_with_ext::<DomainInfo, Update<RgpRestoreRequest>>(
            "response/extensions/domain_info_rgp.xml",
        );
        let ext = object.extension.unwrap();

        let data = match ext.data {
            RgpRequestResponse::Info(data) => data,
            _ => panic!("Unexpected response type"),
        };

        assert_eq!(data.rgp_status[0].status, "addPeriod");
        assert_eq!(data.rgp_status[1].status, "renewPeriod");
    }
}