Skip to content

Commit

Permalink
Fix tests failing due to oauth or config file location
Browse files Browse the repository at this point in the history
  • Loading branch information
nick42d committed Dec 5, 2024
1 parent 01c321b commit 0c56dc0
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 101 deletions.
6 changes: 0 additions & 6 deletions youtui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,11 @@ fn destruct_terminal() -> Result<()> {
/// # Panics
/// If tracing fails to initialise, function will panic
fn init_tracing(debug: bool) -> Result<()> {
// NOTE: It seems that tui-logger only displays events at info or higher,
// possibly a limitation with the implementation.
// https://github.com/gin66/tui-logger/issues/66
// TODO: PR upstream
let tui_logger_layer = tui_logger::tracing_subscriber_layer();
if debug {
let log_file_name = get_data_dir()?.join(LOG_FILE_NAME);
let log_file = std::fs::File::create(&log_file_name)?;
let log_file_layer = tracing_subscriber::fmt::layer().with_writer(Arc::new(log_file));
// TODO: Confirm if this filter is correct.
let context_layer =
tracing_subscriber::filter::Targets::new().with_target("youtui", tracing::Level::DEBUG);
tracing_subscriber::registry()
Expand All @@ -237,7 +232,6 @@ fn init_tracing(debug: bool) -> Result<()> {
.expect("Expected logger to initialise succesfully");
info!("Started in debug mode, logging to {:?}.", log_file_name);
} else {
// TODO: Confirm if this filter is correct.
let context_layer =
tracing_subscriber::filter::Targets::new().with_target("youtui", tracing::Level::INFO);
tracing_subscriber::registry()
Expand Down
29 changes: 10 additions & 19 deletions youtui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,38 +95,29 @@ impl Config {

#[cfg(test)]
mod tests {
use crate::{
config::{keymap::YoutuiKeymap, Config, ConfigIR, CONFIG_FILE_NAME},
get_config_dir,
};
use crate::config::{keymap::YoutuiKeymap, Config, ConfigIR};
use pretty_assertions::assert_eq;

async fn example_config_file() -> String {
tokio::fs::read_to_string("./config/config.toml")
.await
.unwrap()
}

#[tokio::test]
async fn test_deserialize_default_config_to_ir() {
let config_dir = get_config_dir().unwrap();
let config_file_location = config_dir.join(CONFIG_FILE_NAME);
let config_file = tokio::fs::read_to_string(&config_file_location)
.await
.unwrap();
let config_file = example_config_file().await;
toml::from_str::<ConfigIR>(&config_file).unwrap();
}
#[tokio::test]
async fn test_convert_ir_to_config() {
let config_dir = get_config_dir().unwrap();
let config_file_location = config_dir.join(CONFIG_FILE_NAME);
let config_file = tokio::fs::read_to_string(&config_file_location)
.await
.unwrap();
let config_file = example_config_file().await;
let ir: ConfigIR = toml::from_str(&config_file).unwrap();
Config::try_from(ir).unwrap();
}
#[tokio::test]
async fn test_default_config_equals_deserialized_config() {
let config_dir = get_config_dir().unwrap();
let config_file_location = config_dir.join(CONFIG_FILE_NAME);
let config_file = tokio::fs::read_to_string(&config_file_location)
.await
.unwrap();
let config_file = example_config_file().await;
let ir: ConfigIR = toml::from_str(&config_file).unwrap();
let Config {
auth_type,
Expand Down
41 changes: 7 additions & 34 deletions ytmapi-rs/tests/live_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async fn test_get_oauth_code() {

// NOTE: Internal only - due to use of error.is_oauth_expired()
#[tokio::test]
#[ignore = "Oauth is broken https://github.com/nick42d/youtui/issues/179"]
async fn test_expired_oauth() {
// XXX: Assuming this error only occurs for expired headers.
// This assumption may be incorrect.
Expand Down Expand Up @@ -257,17 +258,7 @@ async fn test_get_mood_playlists() {
.next()
.unwrap();
let query = GetMoodPlaylistsQuery::new(first_mood_playlist.params);
let oauth_fut = async {
let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to
//date, just refresh it each time.
api.refresh_token().await.unwrap();
api.query(query.clone()).await.unwrap();
};
let browser_fut = async {
browser_api.query(query.clone()).await.unwrap();
};
tokio::join!(oauth_fut, browser_fut);
browser_api.query(query.clone()).await.unwrap();
}

#[ignore = "Ignored by default due to quota"]
Expand All @@ -282,17 +273,7 @@ async fn test_get_library_upload_artist() {
.next()
.expect("To run this test, you will need to upload songs from at least one artist");
let query = GetLibraryUploadArtistQuery::new(first_artist.artist_id);
let oauth_fut = async {
let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
// refresh it each time.
api.refresh_token().await.unwrap();
let _ = api.query(query.clone()).await.unwrap();
};
let browser_fut = async {
browser_api.query(query.clone()).await.unwrap();
};
tokio::join!(oauth_fut, browser_fut);
browser_api.query(query.clone()).await.unwrap();
}

#[ignore = "Ignored by default due to quota"]
Expand All @@ -307,17 +288,7 @@ async fn test_get_library_upload_album() {
.next()
.expect("To run this test, you will need to upload songs from at least one album");
let query = GetLibraryUploadAlbumQuery::new(first_album.album_id);
let oauth_fut = async {
let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
// refresh it each time.
api.refresh_token().await.unwrap();
let _ = api.query(query.clone()).await.unwrap();
};
let browser_fut = async {
browser_api.query(query.clone()).await.unwrap();
};
tokio::join!(oauth_fut, browser_fut);
browser_api.query(query.clone()).await.unwrap();
}

#[tokio::test]
Expand Down Expand Up @@ -447,7 +418,7 @@ async fn test_add_remove_history_items() {
}

#[tokio::test]
#[ignore = "Ignored by default due to quota"]
#[ignore = "Ignored by default due to quota, also oauth is broken"]
async fn test_delete_create_playlist_oauth() {
let mut api = new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
Expand Down Expand Up @@ -642,6 +613,7 @@ async fn test_edit_playlist() {
// # BASIC TESTS WITH ADDITIONAL ASSERTIONS

#[tokio::test]
#[ignore = "Oauth is broken https://github.com/nick42d/youtui/issues/179"]
async fn test_get_library_playlists_oauth() {
let mut api = new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
Expand All @@ -657,6 +629,7 @@ async fn test_get_library_playlists() {
assert!(!res.playlists.is_empty());
}
#[tokio::test]
#[ignore = "Oauth is broken https://github.com/nick42d/youtui/issues/179"]
async fn test_get_library_artists_oauth() {
let mut api = new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
Expand Down
106 changes: 64 additions & 42 deletions ytmapi-rs/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,30 @@ macro_rules! generate_query_test {
$fname:ident,$query:expr) => {
#[tokio::test]
async fn $fname() {
let oauth_future = async {
let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
// refresh it each time.
api.refresh_token().await.unwrap();
api.query($query)
.await
.expect("Expected query to run succesfully under oauth");
};
let browser_auth_future = async {
let api = crate::utils::new_standard_api().await.unwrap();
api.query($query)
.await
.expect("Expected query to run succesfully under browser auth");
};
tokio::join!(oauth_future, browser_auth_future);
// NOTE: Code to handle Oath and Browser tests commented out due to oauth
// issues.
//
// https://github.com/nick42d/youtui/issues/179
// let oauth_future = async {
// let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// // Don't stuff around trying the keep the local OAuth secret up to date,
// just // refresh it each time.
// api.refresh_token().await.unwrap();
// api.query($query)
// .await
// .expect("Expected query to run succesfully under oauth");
// };
// let browser_auth_future = async {
// let api = crate::utils::new_standard_api().await.unwrap();
// api.query($query)
// .await
// .expect("Expected query to run succesfully under browser auth");
// };
// tokio::join!(oauth_future, browser_auth_future);
let api = crate::utils::new_standard_api().await.unwrap();
api.query($query)
.await
.expect("Expected query to run succesfully under browser auth");
}
};
}
Expand All @@ -85,33 +93,47 @@ macro_rules! generate_stream_test {
$fname:ident,$query:expr) => {
#[tokio::test]
async fn $fname() {
// NOTE: Code to handle Oath and Browser tests commented out due to oauth
// issues.
//
// https://github.com/nick42d/youtui/issues/179
use futures::stream::{StreamExt, TryStreamExt};
let oauth_future = async {
let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// Don't stuff around trying the keep the local OAuth secret up to date, just
// refresh it each time.
api.refresh_token().await.unwrap();
let query = $query;
let stream = api.stream(&query);
tokio::pin!(stream);
stream
.try_collect::<Vec<_>>()
.await
.expect("Expected all results from oauth stream to suceed");
};
let browser_auth_future = async {
let api = crate::utils::new_standard_api().await.unwrap();
let query = $query;
let stream = api.stream(&query);
tokio::pin!(stream);
stream
// limit test to 5 results to avoid overload
.take(5)
.try_collect::<Vec<_>>()
.await
.expect("Expected all results from browser stream to suceed");
};
tokio::join!(oauth_future, browser_auth_future);
// let oauth_future = async {
// let mut api = crate::utils::new_standard_oauth_api().await.unwrap();
// // Don't stuff around trying the keep the local OAuth secret up to date,
// just // refresh it each time.
// api.refresh_token().await.unwrap();
// let query = $query;
// let stream = api.stream(&query);
// tokio::pin!(stream);
// stream
// .try_collect::<Vec<_>>()
// .await
// .expect("Expected all results from oauth stream to suceed");
// };
// let browser_auth_future = async {
// let api = crate::utils::new_standard_api().await.unwrap();
// let query = $query;
// let stream = api.stream(&query);
// tokio::pin!(stream);
// stream
// // limit test to 5 results to avoid overload
// .take(5)
// .try_collect::<Vec<_>>()
// .await
// .expect("Expected all results from browser stream to suceed");
// };
// tokio::join!(oauth_future, browser_auth_future);
let api = crate::utils::new_standard_api().await.unwrap();
let query = $query;
let stream = api.stream(&query);
tokio::pin!(stream);
stream
// limit test to 5 results to avoid overload
.take(5)
.try_collect::<Vec<_>>()
.await
.expect("Expected all results from browser stream to suceed");
}
};
}

0 comments on commit 0c56dc0

Please sign in to comment.