Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
docker_container_repo_dir=/app

# Common docker options
rust_docker_container := public.ecr.aws/docker/library/rust:1.76
rust_docker_container := public.ecr.aws/docker/library/rust:1.79

docker_opts_shared := --rm -v "$(PWD)":$(docker_container_repo_dir) -w $(docker_container_repo_dir)
rust_docker_run := docker run -v $(PWD):/$(docker_container_repo_dir) -w $(docker_container_repo_dir) -it -e CARGO_HOME=/app/.cargo $(rust_docker_container)
Expand Down
73 changes: 73 additions & 0 deletions wp_api/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::{
env,
error::Error,
fs::{read_to_string, File},
io::{BufWriter, Write},
path::Path,
};

fn main() -> Result<(), Box<dyn Error>> {
generate_test_credentials_file()
}

fn generate_test_credentials_file() -> Result<(), Box<dyn Error>> {
// Tell Cargo to rerun if the test credentials file changes
println!("cargo::rerun-if-changed=../test_credentials");

let out_dir = env::var("OUT_DIR")?;
let dest_path = Path::new(&out_dir).join("generated_test_credentials.rs");
let mut buf_writer = BufWriter::new(File::create(dest_path)?);
let generated_content = TestCredentials::from_raw_test_credentials_file("../test_credentials")
.unwrap_or_default()
.generated_test_credentials_file_content();
write!(buf_writer, "{}", generated_content)?;

Ok(())
}

#[derive(Debug, Default)]
struct TestCredentials {
site_url: String,
admin_username: String,
admin_password: String,
subscriber_username: String,
subscriber_password: String,
}

impl TestCredentials {
fn from_raw_test_credentials_file(file_path: &str) -> Option<Self> {
if let Ok(file_contents) = read_to_string(file_path) {
let lines: Vec<&str> = file_contents.lines().collect();

if !lines.is_empty() {
return Some(Self {
site_url: lines[0].to_string(),
admin_username: lines[1].to_string(),
admin_password: lines[2].to_string(),
subscriber_username: lines[3].to_string(),
subscriber_password: lines[4].to_string(),
});
}
}
None
}

fn generated_test_credentials_file_content(&self) -> String {
format!(
r#"
pub const TEST_CREDENTIALS_SITE_URL: &str = "{}";
pub const TEST_CREDENTIALS_ADMIN_USERNAME: &str = "{}";
pub const TEST_CREDENTIALS_ADMIN_PASSWORD: &str = "{}";
pub const TEST_CREDENTIALS_SUBSCRIBER_USERNAME: &str = "{}";
pub const TEST_CREDENTIALS_SUBSCRIBER_PASSWORD: &str = "{}";
"#,
self.site_url,
self.admin_username,
self.admin_password,
self.subscriber_username,
self.subscriber_password
)
.trim()
.to_string()
}
}
39 changes: 9 additions & 30 deletions wp_api/tests/integration_test_common.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use async_trait::async_trait;
use futures::Future;
use http::HeaderMap;
use std::{fs::read_to_string, process::Command, sync::Arc};
use std::{process::Command, sync::Arc};
use wp_api::{
request::{RequestExecutor, RequestMethod, WpNetworkRequest, WpNetworkResponse},
users::UserId,
RequestExecutionError, WpApiError, WpAuthentication, WpRequestBuilder, WpRestError,
WpRestErrorCode, WpRestErrorWrapper,
};

include!(concat!(env!("OUT_DIR"), "/generated_test_credentials.rs"));

// The first user is also the current user
pub const FIRST_USER_ID: UserId = UserId(1);
pub const SECOND_USER_ID: UserId = UserId(2);
Expand All @@ -19,27 +21,25 @@ pub const CLASSIC_EDITOR_PLUGIN_SLUG: &str = "classic-editor/classic-editor";
pub const WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS: &str = "classic-widgets";

pub fn request_builder() -> WpRequestBuilder {
let credentials = read_test_credentials_from_file();
let authentication = WpAuthentication::from_username_and_password(
credentials.admin_username,
credentials.admin_password,
TEST_CREDENTIALS_ADMIN_USERNAME.to_string(),
TEST_CREDENTIALS_ADMIN_PASSWORD.to_string(),
);
WpRequestBuilder::new(
credentials.site_url,
TEST_CREDENTIALS_SITE_URL.to_string(),
authentication,
Arc::new(AsyncWpNetworking::default()),
)
.expect("Site url is generated by our tooling")
}

pub fn request_builder_as_subscriber() -> WpRequestBuilder {
let credentials = read_test_credentials_from_file();
let authentication = WpAuthentication::from_username_and_password(
credentials.subscriber_username,
credentials.subscriber_password,
TEST_CREDENTIALS_SUBSCRIBER_USERNAME.to_string(),
TEST_CREDENTIALS_SUBSCRIBER_PASSWORD.to_string(),
);
WpRequestBuilder::new(
credentials.site_url,
TEST_CREDENTIALS_SITE_URL.to_string(),
authentication,
Arc::new(AsyncWpNetworking::default()),
)
Expand Down Expand Up @@ -91,27 +91,6 @@ impl<T: std::fmt::Debug> AssertWpError<T> for Result<T, WpApiError> {
}
}

#[derive(Debug)]
pub struct TestCredentials {
pub site_url: String,
pub admin_username: String,
pub admin_password: String,
pub subscriber_username: String,
pub subscriber_password: String,
}

pub fn read_test_credentials_from_file() -> TestCredentials {
let file_contents = read_to_string("../test_credentials").unwrap();
let lines: Vec<&str> = file_contents.lines().collect();
TestCredentials {
site_url: lines[0].to_string(),
admin_username: lines[1].to_string(),
admin_password: lines[2].to_string(),
subscriber_username: lines[3].to_string(),
subscriber_password: lines[4].to_string(),
}
}

fn expected_status_code_for_wp_rest_error_code(error_code: &WpRestErrorCode) -> u16 {
match error_code {
WpRestErrorCode::CannotActivatePlugin => 403,
Expand Down
10 changes: 5 additions & 5 deletions wp_api/tests/test_manual_request_builder_immut.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use integration_test_common::{
read_test_credentials_from_file, AsyncWpNetworking, FIRST_USER_ID, SECOND_USER_ID,
AsyncWpNetworking, FIRST_USER_ID, SECOND_USER_ID, TEST_CREDENTIALS_ADMIN_PASSWORD,
TEST_CREDENTIALS_ADMIN_USERNAME, TEST_CREDENTIALS_SITE_URL,
};
use reusable_test_cases::list_users_cases;
use rstest::*;
Expand All @@ -21,15 +22,14 @@ pub mod reusable_test_cases;
#[apply(list_users_cases)]
#[tokio::test]
async fn list_users_with_edit_context(#[case] params: UserListParams) {
let credentials = read_test_credentials_from_file();
let authentication = WpAuthentication::from_username_and_password(
credentials.admin_username,
credentials.admin_password,
TEST_CREDENTIALS_ADMIN_USERNAME.to_string(),
TEST_CREDENTIALS_ADMIN_PASSWORD.to_string(),
);
let async_wp_networking = Arc::new(AsyncWpNetworking::default());

let request_builder = WpApiRequestBuilder::new(
credentials.site_url,
TEST_CREDENTIALS_SITE_URL.to_string(),
authentication,
// TODO: A request executor shouldn't be necessary, but we don't have a standalone request
// builder yet
Expand Down
4 changes: 2 additions & 2 deletions wp_api/tests/test_users_err.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use integration_test_common::{AsyncWpNetworking, SECOND_USER_EMAIL};
use integration_test_common::{AsyncWpNetworking, SECOND_USER_EMAIL, TEST_CREDENTIALS_SITE_URL};
use wp_api::{
users::{
UserCreateParams, UserDeleteParams, UserId, UserListParams, UserUpdateParams,
Expand Down Expand Up @@ -164,7 +164,7 @@ async fn retrieve_user_err_user_invalid_id() {
#[tokio::test]
async fn retrieve_user_err_unauthorized() {
wp_api::WpRequestBuilder::new(
integration_test_common::read_test_credentials_from_file().site_url,
TEST_CREDENTIALS_SITE_URL.to_string(),
WpAuthentication::None,
Arc::new(AsyncWpNetworking::default()),
)
Expand Down