Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing test cases for autodiscovery #265

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wp_api_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async-trait = { workspace = true }
clap = { workspace = true, features = ["derive"] }
futures = { workspace = true }
http = { workspace = true }
reqwest = { workspace = true, features = [ "json" ] }
reqwest = { workspace = true, features = [ "json", "gzip", "brotli", "zstd", "deflate" ] }
serde = { workspace = true, features = [ "derive" ] }
serde_json = { workspace = true }
tokio = { workspace = true, features = [ "full" ] }
Expand Down
14 changes: 14 additions & 0 deletions wp_api_integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,17 @@ impl<T: std::fmt::Debug, E: std::error::Error> AssertResponse for Result<T, E> {
self.unwrap()
}
}

pub trait AssertError {
type Item;
fn assert_error(self) -> Self::Item;
}

impl<T: std::fmt::Debug, E: std::error::Error> AssertError for Result<T, E> {
type Item = E;

fn assert_error(self) -> E {
assert!(self.is_err(), "Request was successful");
self.unwrap_err()
}
}
Comment on lines +169 to +181
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not going to be common enough to have a trait extension. Also, the Request was successful might throw us of in the test logs, because it's not clear that it was expected to fail.

In most cases, we need to validate that the request failed with a certain error for which we already have AssertWpError trait. So, at least for now, I think we should individually implement this assertion and pass in a message that'll point out the issue. If there is enough common usage, then we can implement a specialized one like AssertWpError.

I realize that we discussed me taking over these PRs and implement them as I see fit, but I just wanted to drop my thoughts here, so if I end up changing it, it doesn't come out of nowhere. 😅

24 changes: 23 additions & 1 deletion wp_api_integration_tests/tests/test_login_immut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use rstest::rstest;
use serial_test::serial;
use std::sync::Arc;
use wp_api::login::WpLoginClient;
use wp_api_integration_tests::{AssertResponse, AsyncWpNetworking};
use wp_api_integration_tests::{AssertError, AssertResponse, AsyncWpNetworking};

const LOCALHOST_AUTH_URL: &str = "http://localhost/wp-admin/authorize-application.php";
const AUTOMATTIC_WIDGETS_AUTH_URL: &str =
"https://automatticwidgets.wpcomstaging.com/wp-admin/authorize-application.php";
const VANILLA_WP_SITE_URL: &str = "https://vanilla.wpmt.co/wp-admin/authorize-application.php";

#[rstest]
#[case("http://localhost", LOCALHOST_AUTH_URL)]
Expand Down Expand Up @@ -35,6 +36,12 @@ const AUTOMATTIC_WIDGETS_AUTH_URL: &str =
AUTOMATTIC_WIDGETS_AUTH_URL
)]
#[case("automatticwidgets.wpcomstaging.com/ ", AUTOMATTIC_WIDGETS_AUTH_URL)]
#[case("vanilla.wpmt.co", VANILLA_WP_SITE_URL)]
#[case("http://vanilla.wpmt.co", VANILLA_WP_SITE_URL)]
#[case("https://optional-https.wpmt.co", "https://optional-https.wpmt.co/wp-admin/authorize-application.php")]
#[case("https://わぷー.wpmt.co", "https://xn--39j4bws.wpmt.co/wp-admin/authorize-application.php")]
#[case("https://jetpack.wpmt.co", "https://jetpack.wpmt.co/wp-admin/authorize-application.php")]
#[case("https://aggressive-caching.wpmt.co", "https://jetpack.wpmt.co/wp-admin/authorize-application.php")] // Returns gzip responses, may not always include Link header
#[tokio::test]
#[serial]
async fn test_login_flow(#[case] site_url: &str, #[case] expected_auth_url: &str) {
Expand All @@ -50,3 +57,18 @@ async fn test_login_flow(#[case] site_url: &str, #[case] expected_auth_url: &str
Some(expected_auth_url.to_string())
);
}

#[rstest]
#[case("http://optional-https.wpmt.co")]
#[tokio::test]
#[serial]
async fn test_login_flow_for_empty_authentication_schemes(#[case] site_url: &str) {
let client = WpLoginClient::new(Arc::new(AsyncWpNetworking::default()));
let url_discovery_error = client
.api_discovery(site_url.to_string())
.await
.assert_error();

// TODO: This test should validate that there are no authentication schemes available
assert_eq!(true, false)
}
Loading