use roxmltree::Document;
pub(crate) fn extract_xml_tag(tag_name: &str, body: &str) -> Option<String> {
let doc = Document::parse(body).ok()?;
let node = doc.descendants().find(|n| n.has_tag_name(tag_name))?;
Some(node.text()?.to_string())
}
pub(crate) fn create_login_envelope(username: &str, password: &str) -> String {
[
"<se:Envelope xmlns:se='http://schemas.xmlsoap.org/soap/envelope/'>",
"<se:Header/>",
"<se:Body>",
"<login xmlns='urn:partner.soap.sforce.com'>",
&format!("<username>{}</username>", username),
&format!("<password>{}</password>", password),
"</login>",
"</se:Body>",
"</se:Envelope>",
]
.join("")
}
#[cfg(test)]
mod tests {
use super::*;
use html_escape::decode_html_entities;
#[test]
fn test_create_envelope() {
let envelope = create_login_envelope("u", "p");
assert_eq!(envelope, "<se:Envelope xmlns:se='http://schemas.xmlsoap.org/soap/envelope/'><se:Header/><se:Body><login xmlns='urn:partner.soap.sforce.com'><username>u</username><password>p</password></login></se:Body></se:Envelope>")
}
#[test]
fn test_create_envelope_with_special_chars() {
let envelope = create_login_envelope("user@test.com", "p@ss&word");
assert!(envelope.contains("<username>user@test.com</username>"));
assert!(envelope.contains("<password>p@ss&word</password>"));
}
#[test]
fn test_extract_existing_tag() {
let xml = r#"<?xml version="1.0"?><root><sessionId>abc123</sessionId></root>"#;
let result = extract_xml_tag("sessionId", xml);
assert_eq!(result, Some("abc123".to_string()));
}
#[test]
fn test_extract_nonexistent_tag() {
let xml = r#"<?xml version="1.0"?><root><other>value</other></root>"#;
let result = extract_xml_tag("sessionId", xml);
assert_eq!(result, None);
}
#[test]
fn test_extract_invalid_xml() {
let result = extract_xml_tag("tag", "not xml at all");
assert_eq!(result, None);
}
#[test]
fn test_extract_empty_tag() {
let xml = r#"<?xml version="1.0"?><root><sessionId></sessionId></root>"#;
let result = extract_xml_tag("sessionId", xml);
assert_eq!(result, None); // empty text node returns None
}
#[test]
fn test_extract_nested_tag() {
let xml = r#"<?xml version="1.0"?><root><body><result><serverUrl>https://na1.salesforce.com</serverUrl></result></body></root>"#;
let result = extract_xml_tag("serverUrl", xml);
assert_eq!(
result,
Some("https://na1.salesforce.com".to_string())
);
}
#[test]
fn test_extract_with_namespaces() {
let xml = r#"<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>sf:INVALID_LOGIN</faultcode>
<faultstring>Invalid username</faultstring>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>"#;
let faultcode = extract_xml_tag("faultcode", xml);
assert_eq!(faultcode, Some("sf:INVALID_LOGIN".to_string()));
let faultstring = extract_xml_tag("faultstring", xml);
assert_eq!(faultstring, Some("Invalid username".to_string()));
}
#[test]
fn test_extra_error_from_body() {
let response = "<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><loginResponse><result><metadataServerUrl>https://test.salesforce.com/services/Soap/m/21.0/00DDG00000NAWhM</metadataServerUrl><passwordExpired>false</passwordExpired><sandbox>true</sandbox><serverUrl>https://test.salesforce.com/services/Soap/u/21.0/00DDG00000NAWhM</serverUrl><sessionId>KamehamehaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</sessionId><userId>005a000000CNVshAAH</userId><userInfo><accessibilityMode>false</accessibilityMode><currencySymbol>$</currencySymbol><orgAttachmentFileSizeLimit>5242880</orgAttachmentFileSizeLimit><orgDefaultCurrencyIsoCode>USD</orgDefaultCurrencyIsoCode><orgDisallowHtmlAttachments>false</orgDisallowHtmlAttachments><orgHasPersonAccounts>false</orgHasPersonAccounts><organizationId>00DDG00000NAWhM2AX</organizationId><organizationMultiCurrency>false</organizationMultiCurrency><organizationName>AWESOME ORG LLC</organizationName><profileId>00ea0000001du5ZAAQ</profileId><roleId>00E0c000002TiOnEAK</roleId><sessionSecondsValid>28800</sessionSecondsValid><userDefaultCurrencyIsoCode xsi:nil="true"/><userEmail>goku@carrot.com.invalid</userEmail><userFullName>Goku Carrot Cake</userFullName><userId>005a000000CNVshAAH</userId><userLanguage>en_US</userLanguage><userLocale>en_US</userLocale><userName>goku@carrot.com.fullsb</userName><userTimeZone>America/Los_Angeles</userTimeZone><userType>Standard</userType><userUiSkin>Theme3</userUiSkin></userInfo></result></loginResponse></soapenv:Body></soapenv:Envelope>";
let decoded_response = decode_html_entities(response).to_string();
let access_token =
extract_xml_tag("sessionId", &decoded_response).unwrap_or_else(|| "".to_string());
assert_eq!(
access_token,
"KamehamehaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
);
}
}