Skip to main content

instant_epp/
poll.rs

1use instant_xml::ser::Context;
2use instant_xml::{FromXml, ToXml};
3
4use crate::common::{NoExtension, EPP_XMLNS};
5use crate::domain;
6use crate::domain::transfer::TransferData;
7use crate::extensions::low_balance::LowBalance;
8use crate::extensions::rgp::poll::RgpPollData;
9use crate::host;
10use crate::request::{Command, Transaction};
11
12impl Transaction<NoExtension> for Poll {}
13
14impl Command for Poll {
15    type Response = PollData;
16    const COMMAND: &'static str = "poll";
17}
18
19impl Transaction<NoExtension> for Ack<'_> {}
20
21impl Command for Ack<'_> {
22    type Response = String;
23    const COMMAND: &'static str = "poll";
24}
25
26// Request
27
28/// Type for EPP XML `<poll>` command with `op="req"`
29#[derive(Debug)]
30pub struct Poll;
31
32impl ToXml for Poll {
33    fn serialize<W: std::fmt::Write + ?Sized>(
34        &self,
35        _: Option<instant_xml::Id<'_>>,
36        serializer: &mut instant_xml::Serializer<W>,
37    ) -> Result<(), instant_xml::Error> {
38        serializer.write_start("poll", EPP_XMLNS, None::<Context<0>>)?;
39        serializer.write_attr("op", EPP_XMLNS, &"req")?;
40        serializer.end_empty()
41    }
42}
43
44/// Type for EPP XML `<poll>` command with `op="ack"`
45#[derive(Debug)]
46pub struct Ack<'a> {
47    /// The ID of the message to be acknowledged
48    pub message_id: &'a str,
49}
50
51impl ToXml for Ack<'_> {
52    fn serialize<W: std::fmt::Write + ?Sized>(
53        &self,
54        _: Option<instant_xml::Id<'_>>,
55        serializer: &mut instant_xml::Serializer<W>,
56    ) -> Result<(), instant_xml::Error> {
57        serializer.write_start("poll", EPP_XMLNS, None::<Context<0>>)?;
58        serializer.write_attr("op", EPP_XMLNS, &"ack")?;
59        serializer.write_attr("msgID", EPP_XMLNS, &self.message_id)?;
60        serializer.end_empty()
61    }
62}
63
64// Response
65
66/// Type that represents the `<resData>` tag for message poll response
67#[derive(Debug, FromXml)]
68#[xml(forward)]
69pub enum PollData {
70    /// Data under the `<domain:trnData>` tag
71    DomainTransfer(TransferData),
72    /// Data under the `<domain:infData>` tag
73    DomainInfo(domain::InfoData),
74    /// Data under the `<host:infData>` tag
75    HostInfo(host::InfoData),
76    /// Data under the `<lowbalance>` tag
77    LowBalance(LowBalance),
78    /// Data under the `<rgp-poll:pollData>` tag
79    RgpPoll(RgpPollData),
80}
81
82#[cfg(test)]
83mod tests {
84    use super::{Ack, Poll, PollData};
85    use crate::host::Status;
86    use crate::response::ResultCode;
87    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
88
89    use chrono::{TimeZone, Utc};
90    use std::net::IpAddr;
91
92    #[test]
93    fn ack_command() {
94        let object = Ack {
95            message_id: "12345",
96        };
97        assert_serialized("request/poll/ack.xml", &object);
98    }
99
100    #[test]
101    fn response() {
102        let object = response_from_file::<Ack>("response/poll/ack.xml");
103        let msg = object.message_queue().unwrap();
104
105        assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
106        assert_eq!(object.result.message, SUCCESS_MSG);
107        assert_eq!(msg.count, 4);
108        assert_eq!(msg.id, "12345".to_string());
109        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
110    }
111
112    #[test]
113    fn poll_command() {
114        let object = Poll;
115        assert_serialized("request/poll/poll.xml", &object);
116    }
117
118    #[test]
119    fn domain_transfer_response() {
120        let object = response_from_file::<Poll>("response/poll/poll_domain_transfer.xml");
121        let result = object.res_data().unwrap();
122        let msg = object.message_queue().unwrap();
123
124        assert_eq!(
125            object.result.code,
126            ResultCode::CommandCompletedSuccessfullyAckToDequeue
127        );
128        assert_eq!(
129            object.result.message,
130            "Command completed successfully; ack to dequeue"
131        );
132        assert_eq!(msg.count, 5);
133        assert_eq!(msg.id, "12345".to_string());
134        assert_eq!(
135            msg.date,
136            Utc.with_ymd_and_hms(2021, 7, 23, 19, 12, 43).single()
137        );
138        assert_eq!(msg.message.as_ref().unwrap().text, "Transfer requested.");
139
140        if let PollData::DomainTransfer(tr) = &result {
141            assert_eq!(tr.name, "eppdev-transfer.com");
142            assert_eq!(tr.transfer_status, "pending");
143            assert_eq!(tr.requester_id, "eppdev");
144            assert_eq!(
145                tr.requested_at,
146                Utc.with_ymd_and_hms(2021, 7, 23, 15, 31, 21).unwrap()
147            );
148            assert_eq!(tr.ack_id, "ClientY");
149            assert_eq!(
150                tr.ack_by,
151                Utc.with_ymd_and_hms(2021, 7, 28, 15, 31, 21).unwrap()
152            );
153            assert_eq!(
154                tr.expiring_at,
155                Utc.with_ymd_and_hms(2022, 7, 2, 14, 53, 19).single()
156            );
157        } else {
158            panic!("Wrong type");
159        }
160
161        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
162        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
163    }
164
165    #[test]
166    fn host_info_response() {
167        let object = response_from_file::<Poll>("response/poll/poll_host_info.xml");
168        let result = object.res_data().unwrap();
169        let msg = object.message_queue().unwrap();
170
171        assert_eq!(
172            object.result.code,
173            ResultCode::CommandCompletedSuccessfullyAckToDequeue
174        );
175        assert_eq!(
176            object.result.message,
177            "Command completed successfully; ack to dequeue"
178        );
179        assert_eq!(msg.count, 4);
180        assert_eq!(msg.id, "12345".to_string());
181        assert_eq!(
182            msg.date,
183            Utc.with_ymd_and_hms(2022, 1, 2, 11, 30, 45).single()
184        );
185        assert_eq!(msg.message.as_ref().unwrap().text, "Unused objects policy");
186
187        if let PollData::HostInfo(host) = &result {
188            assert_eq!(host.name, "ns.test.com");
189
190            assert_eq!(host.roid, "1234");
191            assert!(host.statuses.contains(&Status::Ok));
192            assert!(host
193                .addresses
194                .iter()
195                .any(|a| a == &IpAddr::from([1, 1, 1, 1])));
196            assert_eq!(host.client_id, "1234");
197            assert_eq!(host.creator_id, "user");
198            assert_eq!(
199                host.created_at,
200                Utc.with_ymd_and_hms(2021, 12, 1, 22, 40, 48).unwrap()
201            );
202            assert_eq!(host.updater_id, Some("user".into()));
203            assert_eq!(
204                host.updated_at,
205                Utc.with_ymd_and_hms(2021, 12, 1, 22, 40, 48).single()
206            );
207        } else {
208            panic!("Wrong type");
209        }
210
211        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
212        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
213    }
214
215    #[test]
216    fn message_only_response() {
217        let object = response_from_file::<Poll>("response/poll/poll_message_only.xml");
218        let msg = object.message_queue().unwrap();
219        dbg!(&msg);
220
221        assert_eq!(
222            object.result.code,
223            ResultCode::CommandCompletedSuccessfullyAckToDequeue
224        );
225        assert_eq!(
226            object.result.message,
227            "Command completed successfully; ack to dequeue"
228        );
229
230        assert_eq!(msg.count, 4);
231        assert_eq!(msg.id, "12346".to_string());
232        assert_eq!(
233            msg.date,
234            Utc.with_ymd_and_hms(2000, 6, 8, 22, 10, 0).single()
235        );
236        assert_eq!(msg.message.as_ref().unwrap().text, "Credit balance low.");
237
238        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
239        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
240    }
241
242    #[test]
243    fn empty_queue_response() {
244        let object = response_from_file::<Poll>("response/poll/poll_empty_queue.xml");
245
246        assert_eq!(
247            object.result.code,
248            ResultCode::CommandCompletedSuccessfullyNoMessages
249        );
250        assert_eq!(
251            object.result.message,
252            "Command completed successfully; no messages"
253        );
254
255        assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
256        assert_eq!(object.tr_ids.server_tr_id, SVTRID);
257    }
258}