Skip to content

Commit

Permalink
feat(tests): simplify httpd tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser authored and vincenzopalazzo committed Oct 7, 2023
1 parent 2034f99 commit 2711bd6
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 194 deletions.
38 changes: 0 additions & 38 deletions coffee_lib/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,6 @@ pub mod request {
use paperclip::actix::Apiv2Schema;
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "open-api"))]
#[derive(Debug, Deserialize, Serialize)]
pub struct Install {
pub plugin: String,
pub try_dynamic: bool,
}

#[cfg(not(feature = "open-api"))]
#[derive(Debug, Deserialize, Serialize)]
pub struct Remove {
pub plugin: String,
}

#[cfg(not(feature = "open-api"))]
#[derive(Debug, Deserialize, Serialize)]
pub struct RemoteAdd {
pub repository_name: String,
pub repository_url: String,
}

#[cfg(not(feature = "open-api"))]
#[derive(Debug, Deserialize, Serialize)]
pub struct RemoteRm {
pub repository_name: String,
}

#[cfg(not(feature = "open-api"))]
#[derive(Debug, Deserialize, Serialize)]
pub struct Show {
pub plugin: String,
}

#[cfg(not(feature = "open-api"))]
#[derive(Debug, Deserialize, Serialize)]
pub struct Search {
pub plugin: String,
}

#[cfg(feature = "open-api")]
#[derive(Debug, Deserialize, Apiv2Schema, Serialize)]
pub struct Install {
Expand Down
291 changes: 135 additions & 156 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,103 +103,6 @@ pub async fn httpd_add_remove_plugins() {
let body = response.text().await.unwrap();
log::info!("/remote/add response: {}", body);

// Define the request body to be sent to the /remote/list_plugins endpoint
let remote_plugins_list_request = RemotePluginsList {
repository_name: "lightningd".to_string(),
};

let response = client
.get(format!("{}/remote/list_plugins", url))
.json(&remote_plugins_list_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

let body = response.json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/remote/list_plugins response: {}", body);

// Assert the response plugin list is not empty
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();

// Assert that the "helpme" plugin exists in the response
assert!(
plugins.iter().any(|plugin| plugin["name"] == "helpme"),
"helpme plugin not found in the response"
);

// Define the request body to be sent to the /show endpoint
let show_request = Show {
plugin: "helpme".to_string(),
};

let response = client
.get(format!("{}/show", url))
.json(&show_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/show response: {}", body);

// Parse the response body
let response_json = serde_json::from_str(&body);
assert!(response_json.is_ok(), "{:?}", response_json);
let response_json: serde_json::Value = response_json.unwrap();

// Extract the `readme` field from the response JSON
let readme = response_json["readme"].as_str();
assert!(readme.is_some(), "{:?}", readme);
let readme = readme.unwrap();

// Assert that the `readme` starts with the expected content
assert!(readme.starts_with("# Helpme plugin"), "{:?}", readme);

// Define the request body to be sent to the /search endpoint
let search_request = Search {
plugin: "summary".to_string(),
};

let response = client
.get(format!("{}/search", url))
.json(&search_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/search response: {}", body);

// Parse the response body
let response_json = serde_json::from_str(&body);
assert!(response_json.is_ok(), "{:?}", response_json);
let response_json: serde_json::Value = response_json.unwrap();

// Extract the `repository_url` field from the response JSON
let repository_url = response_json["repository_url"].as_str();
assert!(repository_url.is_some(), "{:?}", repository_url);
let repository_url = repository_url.unwrap();

// Assert that repository_url is the expected value
assert_eq!(
repository_url, "https://github.com/lightningd/plugins",
"{:?}",
repository_url
);

// Define the request body to be sent to the /install endpoint
let install_request = Install {
plugin: "summary".to_string(),
Expand All @@ -218,24 +121,6 @@ pub async fn httpd_add_remove_plugins() {
let body = response.text().await.unwrap();
log::info!("/install response: {}", body);

// Define the request body to be sent to the /install endpoint
let install_request = Install {
plugin: "helpme".to_string(),
try_dynamic: false,
};

let response = client
.post(format!("{}/install", url))
.json(&install_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
let body = response.text().await.unwrap();
log::info!("/install response: {}", body);

let body = reqwest::get(format!("{}/remote/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
Expand Down Expand Up @@ -265,14 +150,10 @@ pub async fn httpd_add_remove_plugins() {
// Log the response body
log::info!("/list response: {}", body);

// Assert that the "helpme" and "summary" plugin exist in the response
// Assert that the "summary" plugin exist in the response
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();
assert!(
plugins.iter().any(|plugin| plugin["name"] == "helpme"),
"helpme plugin not found in the response"
);
assert!(
plugins.iter().any(|plugin| plugin["name"] == "summary"),
"summary plugin not found in the response"
Expand All @@ -296,24 +177,6 @@ pub async fn httpd_add_remove_plugins() {
let body = response.text().await.unwrap();
log::info!("Response body: {}", body);

let body = reqwest::get(format!("{}/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/list response: {}", body);

// Assert that the "summary" plugin was removed
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();
assert!(
!(plugins.iter().any(|plugin| plugin["name"] == "summary")),
"summary plugin is found in the list response while it should have been removed"
);

// Define the request body to be sent
let remote_rm_request = RemoteRm {
repository_name: "lightningd".to_string(),
Expand All @@ -333,24 +196,6 @@ pub async fn httpd_add_remove_plugins() {
let body = response.text().await.unwrap();
log::info!("/remote/rm response: {}", body);

let body = reqwest::get(format!("{}/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/list response: {}", body);

// Assert that the "helpme" plugin was removed
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();
assert!(
!(plugins.iter().any(|plugin| plugin["name"] == "helpme")),
"helpme plugin is found in the list response while it should have been removed"
);

let body = reqwest::get(format!("{}/remote/list", url)).await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap().json::<serde_json::Value>().await;
Expand All @@ -373,3 +218,137 @@ pub async fn httpd_add_remove_plugins() {

cln.stop().await.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(560000)]
pub async fn httpd_search_list_plugins() {
init();

let mut cln = Node::tmp("regtest").await.unwrap();
let lightning_dir = cln.rpc().getinfo().unwrap().ligthning_dir;
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
let manager = CoffeeHTTPDTesting::tmp(lightning_dir.to_string()).await;
assert!(manager.is_ok(), "{:?}", manager);
let manager = manager.unwrap();
log::info!("lightning path: {lightning_dir}");
let url = manager.url();
log::info!("base url: {url}");
let client = reqwest::Client::new();

// Define the request body to be sent to the /remote/add endpoint
let remote_add_request = RemoteAdd {
repository_name: "lightningd".to_string(),
repository_url: "https://github.com/lightningd/plugins.git".to_string(),
};

let response = client
.post(format!("{}/remote/add", url))
.json(&remote_add_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/remote/add response: {}", body);

// Define the request body to be sent to the /remote/list_plugins endpoint
let remote_plugins_list_request = RemotePluginsList {
repository_name: "lightningd".to_string(),
};

let response = client
.get(format!("{}/remote/list_plugins", url))
.json(&remote_plugins_list_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

let body = response.json::<serde_json::Value>().await;
assert!(body.is_ok(), "{:?}", body);
let body = body.unwrap();

// Log the response body
log::info!("/remote/list_plugins response: {}", body);

// Assert the response plugin list is not empty
let plugins = body["plugins"].as_array();
assert!(plugins.is_some(), "{:?}", plugins);
let plugins = plugins.unwrap();

// Assert that the "helpme" plugin exists in the response
assert!(
plugins.iter().any(|plugin| plugin["name"] == "helpme"),
"helpme plugin not found in the response"
);

// Define the request body to be sent to the /show endpoint
let show_request = Show {
plugin: "helpme".to_string(),
};

let response = client
.get(format!("{}/show", url))
.json(&show_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/show response: {}", body);

// Parse the response body
let response_json = serde_json::from_str(&body);
assert!(response_json.is_ok(), "{:?}", response_json);
let response_json: serde_json::Value = response_json.unwrap();

// Extract the `readme` field from the response JSON
let readme = response_json["readme"].as_str();
assert!(readme.is_some(), "{:?}", readme);
let readme = readme.unwrap();

// Assert that the `readme` starts with the expected content
assert!(readme.starts_with("# Helpme plugin"), "{:?}", readme);

// Define the request body to be sent to the /search endpoint
let search_request = Search {
plugin: "summary".to_string(),
};

let response = client
.get(format!("{}/search", url))
.json(&search_request)
.send()
.await;
assert!(response.is_ok(), "{:?}", response);
let response = response.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/search response: {}", body);

// Parse the response body
let response_json = serde_json::from_str(&body);
assert!(response_json.is_ok(), "{:?}", response_json);
let response_json: serde_json::Value = response_json.unwrap();

// Extract the `repository_url` field from the response JSON
let repository_url = response_json["repository_url"].as_str();
assert!(repository_url.is_some(), "{:?}", repository_url);
let repository_url = repository_url.unwrap();

// Assert that repository_url is the expected value
assert_eq!(
repository_url, "https://github.com/lightningd/plugins",
"{:?}",
repository_url
);
cln.stop().await.unwrap();
}

0 comments on commit 2711bd6

Please sign in to comment.