instant-epp/src/login.rs

97 lines
2.8 KiB
Rust
Raw Normal View History

2021-11-29 22:25:35 +00:00
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use crate::{
common::{NoExtension, Options, ServiceExtension, Services, StringValue},
contact, domain, host,
request::{Command, Transaction, EPP_LANG, EPP_VERSION},
2021-11-29 22:25:35 +00:00
};
impl Transaction<NoExtension> for Login {}
2021-11-26 22:21:38 +00:00
#[derive(Serialize, Deserialize, Debug, PartialEq)]
/// Type corresponding to the &lt;login&gt; tag in an EPP XML login request
pub struct Login {
/// The username to use for the login
#[serde(rename(serialize = "clID", deserialize = "clID"))]
username: StringValue,
/// The password to use for the login
#[serde(rename = "pw", default)]
password: StringValue,
/// Data under the <options> tag
options: Options,
/// Data under the <svcs> tag
#[serde(rename = "svcs")]
services: Services,
2021-11-26 22:21:38 +00:00
}
2021-11-29 22:25:35 +00:00
impl Login {
pub fn new(username: &str, password: &str, ext_uris: Option<Vec<String>>) -> Self {
let ext_uris = ext_uris.map(|uris| uris.iter().map(|u| u.as_str().into()).collect());
2021-11-29 22:25:35 +00:00
Self {
username: username.into(),
password: password.into(),
options: Options {
version: EPP_VERSION.into(),
lang: EPP_LANG.into(),
},
services: Services {
obj_uris: vec![
host::XMLNS.into(),
contact::XMLNS.into(),
domain::XMLNS.into(),
],
svc_ext: Some(ServiceExtension { ext_uris }),
2021-11-29 22:25:35 +00:00
},
2021-11-26 22:21:38 +00:00
}
2021-11-29 22:25:35 +00:00
}
/// Sets the <options> tag data
pub fn options(&mut self, options: Options) {
self.options = options;
2021-11-29 22:25:35 +00:00
}
/// Sets the <svcs> tag data
pub fn services(&mut self, services: Services) {
self.services = services;
2021-11-29 22:25:35 +00:00
}
}
impl Command for Login {
type Response = ();
const COMMAND: &'static str = "login";
2021-11-29 22:25:35 +00:00
}
#[cfg(test)]
mod tests {
use super::Login;
use crate::request::Transaction;
use crate::tests::{get_xml, CLTRID, SUCCESS_MSG, SVTRID};
#[test]
fn command() {
let ext_uris = Some(vec![
"http://schema.ispapi.net/epp/xml/keyvalue-1.0".to_string()
]);
let xml = get_xml("request/login.xml").unwrap();
let object = Login::new("username", "password", ext_uris);
let serialized = object.serialize_request(None, CLTRID).unwrap();
assert_eq!(xml, serialized);
}
#[test]
fn response() {
let xml = get_xml("response/login.xml").unwrap();
let object = Login::deserialize_response(xml.as_str()).unwrap();
assert_eq!(object.result.code, 1000);
assert_eq!(object.result.message, SUCCESS_MSG.into());
assert_eq!(object.tr_ids.client_tr_id.unwrap(), CLTRID.into());
assert_eq!(object.tr_ids.server_tr_id, SVTRID.into());
}
}