From 690e0d2e10f942d1243ea2bae1ca902ffc322cb6 Mon Sep 17 00:00:00 2001 From: johannesd3 Date: Fri, 2 Apr 2021 14:44:42 +0200 Subject: [PATCH] Add simple tests to librespot-core The first test checks whether apresolve works. A second test tries to create a Spotify sessions with fake credentials and asserts that an error is returned. --- core/src/apresolve.rs | 23 +++++++++++++++++++++ core/tests/connect.rs | 48 +++++++++++++++---------------------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/core/src/apresolve.rs b/core/src/apresolve.rs index 85baba69..b11e275f 100644 --- a/core/src/apresolve.rs +++ b/core/src/apresolve.rs @@ -66,3 +66,26 @@ pub async fn apresolve(proxy: Option<&Url>, ap_port: Option) -> String { AP_FALLBACK.into() }) } + +#[cfg(test)] +mod test { + use std::net::ToSocketAddrs; + + use super::try_apresolve; + + #[tokio::test] + async fn test_apresolve() { + let ap = try_apresolve(None, None).await.unwrap(); + + // Assert that the result contains a valid host and port + ap.to_socket_addrs().unwrap().next().unwrap(); + } + + #[tokio::test] + async fn test_apresolve_port_443() { + let ap = try_apresolve(None, Some(443)).await.unwrap(); + + let port = ap.to_socket_addrs().unwrap().next().unwrap().port(); + assert_eq!(port, 443); + } +} diff --git a/core/tests/connect.rs b/core/tests/connect.rs index b7bc29f8..4f1dbe6b 100644 --- a/core/tests/connect.rs +++ b/core/tests/connect.rs @@ -1,34 +1,18 @@ -use librespot_core::*; +use librespot_core::authentication::Credentials; +use librespot_core::config::SessionConfig; +use librespot_core::session::Session; -// TODO: test is broken -// #[cfg(test)] -// mod tests { -// use super::*; -// // Test AP Resolve -// use apresolve::apresolve_or_fallback; -// #[tokio::test] -// async fn test_ap_resolve() { -// env_logger::init(); -// let ap = apresolve_or_fallback(&None, &None).await; -// println!("AP: {:?}", ap); -// } +#[tokio::test] +async fn test_connection() { + let result = Session::connect( + SessionConfig::default(), + Credentials::with_password("test", "test"), + None, + ) + .await; -// // Test connect -// use authentication::Credentials; -// use config::SessionConfig; -// #[tokio::test] -// async fn test_connection() -> Result<(), Box> { -// println!("Running connection test"); -// let ap = apresolve_or_fallback(&None, &None).await; -// let credentials = Credentials::with_password(String::from("test"), String::from("test")); -// let session_config = SessionConfig::default(); -// let proxy = None; - -// println!("Connecting to AP \"{}\"", ap); -// let mut connection = connection::connect(ap, &proxy).await?; -// let rc = connection::authenticate(&mut connection, credentials, &session_config.device_id) -// .await?; -// println!("Authenticated as \"{}\"", rc.username); -// Ok(()) -// } -// } + match result { + Ok(_) => panic!("Authentication succeeded despite of bad credentials."), + Err(e) => assert_eq!(e.to_string(), "Login failed with reason: Bad credentials"), + }; +}