instant_epp/contact/
create.rs1use chrono::{DateTime, Utc};
4use instant_xml::{FromXml, ToXml};
5
6use super::{ContactAuthInfo, Fax, PostalInfo, Voice, XMLNS};
7use crate::common::{NoExtension, EPP_XMLNS};
8use crate::request::{Command, Transaction};
9
10impl<'a> Transaction<NoExtension> for ContactCreate<'a> {}
11
12impl<'a> Command for ContactCreate<'a> {
13 type Response = CreateData;
14 const COMMAND: &'static str = "create";
15}
16
17#[derive(Debug, ToXml)]
21#[xml(rename = "create", ns(XMLNS))]
22pub struct ContactCreateRequest<'a> {
23 id: &'a str,
25 postal_info: PostalInfo<'a>,
27 voice: Option<Voice<'a>>,
29 fax: Option<Fax<'a>>,
31 email: &'a str,
33 auth_info: ContactAuthInfo<'a>,
35}
36
37#[derive(Debug, ToXml)]
39#[xml(rename = "create", ns(EPP_XMLNS))]
40pub struct ContactCreate<'a> {
41 pub contact: ContactCreateRequest<'a>,
43}
44
45impl<'a> ContactCreate<'a> {
46 pub fn new(
47 id: &'a str,
48 email: &'a str,
49 postal_info: PostalInfo<'a>,
50 voice: Option<Voice<'a>>,
51 auth_password: &'a str,
52 ) -> Self {
53 Self {
54 contact: ContactCreateRequest {
55 id,
56 postal_info,
57 voice,
58 fax: None,
59 email,
60 auth_info: ContactAuthInfo::new(auth_password),
61 },
62 }
63 }
64
65 pub fn set_fax(&mut self, fax: Fax<'a>) {
67 self.contact.fax = Some(fax);
68 }
69}
70
71#[derive(Debug, FromXml)]
75#[xml(rename = "creData", ns(XMLNS))]
76pub struct CreateData {
77 pub id: String,
79 #[xml(rename = "crDate")]
80 pub created_at: DateTime<Utc>,
82}
83
84#[cfg(test)]
85mod tests {
86 use chrono::{TimeZone, Utc};
87
88 use super::{ContactCreate, Fax, PostalInfo, Voice};
89 use crate::contact::Address;
90 use crate::response::ResultCode;
91 use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};
92
93 #[test]
94 fn command() {
95 let street = &["58", "Orchid Road"];
96 let address = Address::new(
97 street,
98 "Paris",
99 Some("Paris"),
100 Some("392374"),
101 "FR".parse().unwrap(),
102 );
103 let postal_info = PostalInfo::new("int", "John Doe", Some("Acme Widgets"), address);
104 let mut voice = Voice::new("+33.47237942");
105 voice.set_extension("123");
106 let mut fax = Fax::new("+33.86698799");
107 fax.set_extension("677");
108
109 let mut object = ContactCreate::new(
110 "eppdev-contact-3",
111 "contact@eppdev.net",
112 postal_info,
113 Some(voice),
114 "eppdev-387323",
115 );
116 object.set_fax(fax);
117
118 assert_serialized("request/contact/create.xml", &object);
119 }
120
121 #[test]
122 fn command_minimal() {
123 let address = Address::new(&[], "Paris", None, None, "FR".parse().unwrap());
124 let postal_info = PostalInfo::new("int", "John Doe", None, address);
125 let object = ContactCreate::new(
126 "eppdev-contact-3",
127 "contact@eppdev.net",
128 postal_info,
129 None,
130 "eppdev-387323",
131 );
132
133 assert_serialized("request/contact/create_minimal.xml", &object);
134 }
135
136 #[test]
137 fn response() {
138 let object = response_from_file::<ContactCreate>("response/contact/create.xml");
139 let results = object.res_data().unwrap();
140
141 assert_eq!(object.result.code, ResultCode::CommandCompletedSuccessfully);
142 assert_eq!(object.result.message, SUCCESS_MSG);
143 assert_eq!(results.id, "eppdev-contact-4");
144 assert_eq!(
145 results.created_at,
146 Utc.with_ymd_and_hms(2021, 7, 25, 16, 5, 32).unwrap(),
147 );
148 assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID);
149 assert_eq!(object.tr_ids.server_tr_id, SVTRID);
150 }
151}