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
use std::fmt::Debug;

use instant_xml::ToXml;

use crate::{
    common::{NoExtension, Options, ServiceExtension, Services, EPP_XMLNS},
    contact, domain, host,
    request::{Command, Transaction, EPP_LANG, EPP_VERSION},
};

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

/// Type corresponding to the `<login>` tag in an EPP XML login request
#[derive(Debug, Eq, PartialEq, ToXml)]
#[xml(rename = "login", ns(EPP_XMLNS))]
pub struct Login<'a> {
    /// The username to use for the login
    #[xml(rename = "clID")]
    username: &'a str,
    /// The password to use for the login
    #[xml(rename = "pw")]
    password: &'a str,
    /// A new password which should be set
    #[xml(rename = "newPW")]
    new_password: Option<&'a str>,
    /// Data under the `<options>` tag
    options: Options<'a>,
    /// Data under the `<svcs>` tag
    #[xml(rename = "svcs")]
    services: Services<'a>,
}

impl<'a> Login<'a> {
    pub fn new(
        username: &'a str,
        password: &'a str,
        new_password: Option<&'a str>,
        ext_uris: Option<&'_ [&'a str]>,
    ) -> Self {
        Self {
            username,
            password,
            new_password,
            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: ext_uris.and_then(|uris| {
                    (!uris.is_empty()).then(|| ServiceExtension {
                        ext_uris: uris.iter().map(|&u| u.into()).collect(),
                    })
                }),
            },
        }
    }

    /// Sets the `<options>` tag data
    pub fn options(&mut self, options: Options<'a>) {
        self.options = options;
    }

    /// Sets the `<svcs>` tag data
    pub fn services(&mut self, services: Services<'a>) {
        self.services = services;
    }
}

impl<'a> Command for Login<'a> {
    type Response = ();
    const COMMAND: &'static str = "login";
}

#[cfg(test)]
mod tests {
    use super::Login;
    use crate::response::ResultCode;
    use crate::tests::{assert_serialized, response_from_file, CLTRID, SUCCESS_MSG, SVTRID};

    #[test]
    fn command() {
        let ext_uris = Some(&["http://schema.ispapi.net/epp/xml/keyvalue-1.0"][..]);
        let object = Login::new("username", "password", Some("new-password"), ext_uris);
        assert_serialized("request/login.xml", &object);
    }

    #[test]
    fn command_no_extension() {
        let object = Login::new("username", "password", None, None);
        assert_serialized("request/login_no_extension.xml", &object);
        let object = Login::new("username", "password", None, Some(&[]));
        assert_serialized("request/login_no_extension.xml", &object);
    }

    #[test]
    fn response() {
        let object = response_from_file::<Login>("response/login.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);
    }
}