From 7fbfa4859068d70eddb644465054896b7212a0c2 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 30 Oct 2023 11:58:44 +0100 Subject: [PATCH] fix tests --- crates/api_common/src/context.rs | 20 ++++++++++++++++++-- crates/api_common/src/request.rs | 9 +++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/crates/api_common/src/context.rs b/crates/api_common/src/context.rs index b8eeda3e9..8d8dc5013 100644 --- a/crates/api_common/src/context.rs +++ b/crates/api_common/src/context.rs @@ -55,16 +55,32 @@ impl LemmyContext { &self.rate_limit_cell } - /// Initialize a context for use in tests, doesn't allow network requests. + /// Initialize a context for use in tests, optionally blocks network requests. /// /// Do not use this in production code. pub async fn init_test_context() -> Data { + Self::build_test_context(true).await + } + + /// Initialize a context for use in tests, with network requests allowed. + /// TODO: get rid of this if possible. + /// + /// Do not use this in production code. + pub async fn init_test_context_with_networking() -> Data { + Self::build_test_context(false).await + } + + async fn build_test_context(block_networking: bool) -> Data { // call this to run migrations let pool = build_db_pool_for_tests().await; let client = client_builder(&SETTINGS).build().expect("build client"); - let client = ClientBuilder::new(client).with(BlockedMiddleware).build(); + let mut client = ClientBuilder::new(client); + if block_networking { + client = client.with(BlockedMiddleware); + } + let client = client.build(); let secret = Secret { id: 0, jwt_secret: String::new(), diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index 8b355c866..954a6cd88 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -288,12 +288,14 @@ mod tests { context::LemmyContext, request::{extract_opengraph_data, fetch_link_metadata}, }; + use serial_test::serial; use url::Url; // These helped with testing #[tokio::test] + #[serial] async fn test_link_metadata() { - let context = LemmyContext::init_test_context().await; + let context = LemmyContext::init_test_context_with_networking().await; let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ").unwrap(); let sample_res = fetch_link_metadata(&sample_url, false, &context) .await @@ -315,7 +317,10 @@ mod tests { sample_res.image ); assert_eq!(None, sample_res.embed_video_url); - assert_eq!(None, sample_res.content_type); + assert_eq!( + Some(mime::TEXT_HTML_UTF_8.to_string()), + sample_res.content_type + ); assert_eq!(None, sample_res.thumbnail); }