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

refactor(tests): Hide bitcoind stopper #261

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
69 changes: 34 additions & 35 deletions teos/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ mod test_helpers {

use crate::api::internal::InternalAPI;
use crate::protos::public_tower_services_server::PublicTowerServicesServer;
use crate::test_utils::{create_api_with_config, ApiConfig, BitcoindStopper};
use crate::test_utils::{create_api_with_config, ApiConfig};

pub(crate) enum RequestBody<'a> {
Jsonify(&'a str),
Expand All @@ -346,8 +346,8 @@ mod test_helpers {

pub(crate) async fn run_tower_in_background_with_config(
api_config: ApiConfig,
) -> (SocketAddr, Arc<InternalAPI>, BitcoindStopper) {
let (internal_rpc_api, bitcoind_stopper) = create_api_with_config(api_config).await;
) -> (SocketAddr, Arc<InternalAPI>) {
let internal_rpc_api = create_api_with_config(api_config).await;
let cloned = internal_rpc_api.clone();

let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
Expand All @@ -361,14 +361,13 @@ mod test_helpers {
.unwrap();
});

(addr, cloned, bitcoind_stopper)
(addr, cloned)
}

pub(crate) async fn run_tower_in_background() -> (SocketAddr, BitcoindStopper) {
let (sock_addr, _, bitcoind_stopper) =
run_tower_in_background_with_config(ApiConfig::default()).await;
pub(crate) async fn run_tower_in_background() -> SocketAddr {
let (sock_addr, _) = run_tower_in_background_with_config(ApiConfig::default()).await;

(sock_addr, bitcoind_stopper)
sock_addr
}

pub(crate) async fn check_api_error(
Expand Down Expand Up @@ -447,7 +446,7 @@ mod tests_failures {

#[tokio::test]
async fn test_no_json_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) =
check_api_error(Endpoint::Register, RequestBody::Body(""), server_addr).await;
assert!(api_error.error.contains("EOF while parsing"));
Expand All @@ -457,7 +456,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_json_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::DoNotJsonify(""),
Expand All @@ -471,7 +470,7 @@ mod tests_failures {

#[tokio::test]
async fn test_empty_json_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{}"#),
Expand All @@ -485,7 +484,7 @@ mod tests_failures {

#[tokio::test]
async fn test_empty_field() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": ""}"#),
Expand All @@ -499,7 +498,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_field_hex_encoding_odd() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": "a"}"#),
Expand All @@ -513,7 +512,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_hex_encoding_character() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) =
check_api_error(Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": "022fa2900ed7fc07b4e8ca3ea081e846245b0497944644aa78ea0b994ac22074dZ"}"#),
Expand All @@ -527,7 +526,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_field_size() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::Jsonify(r#"{"user_id": "aa"}"#),
Expand All @@ -542,7 +541,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_field_type() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::Register,
RequestBody::DoNotJsonify(r#"{"user_id": 1}"#),
Expand All @@ -557,7 +556,7 @@ mod tests_failures {
#[tokio::test]
async fn test_request_missing_field() {
// We'll use a different endpoint here since we need a json object with more than one field
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (api_error, status) = check_api_error(
Endpoint::AddAppointment,
RequestBody::Jsonify(r#"{"signature": "aa"}"#),
Expand All @@ -573,7 +572,7 @@ mod tests_failures {

#[tokio::test]
async fn test_empty_request_body() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand All @@ -593,7 +592,7 @@ mod tests_failures {

#[tokio::test]
async fn test_payload_too_large() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand All @@ -614,7 +613,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_endpoint() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand All @@ -634,7 +633,7 @@ mod tests_failures {

#[tokio::test]
async fn test_wrong_method() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let grpc_conn = PublicTowerServicesClient::connect(format!(
"http://{}:{}",
server_addr.ip(),
Expand Down Expand Up @@ -671,7 +670,7 @@ mod tests_methods {

#[tokio::test]
async fn test_register() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let response =
request_to_api::<common_msgs::RegisterRequest, common_msgs::RegisterResponse>(
Endpoint::Register,
Expand All @@ -686,7 +685,7 @@ mod tests_methods {

#[tokio::test]
async fn test_register_max_slots() {
let (server_addr, _, _s) =
let (server_addr, _) =
run_tower_in_background_with_config(ApiConfig::new(u32::MAX, DURATION)).await;
let user_id = get_random_user_id();

Expand Down Expand Up @@ -723,7 +722,7 @@ mod tests_methods {

#[tokio::test]
async fn test_register_service_unavailable() {
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down Expand Up @@ -751,7 +750,7 @@ mod tests_methods {

#[tokio::test]
async fn test_add_appointment() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -790,7 +789,7 @@ mod tests_methods {

#[tokio::test]
async fn test_add_appointment_non_registered() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;
let (user_sk, _s) = cryptography::get_random_keypair();
let appointment = generate_dummy_appointment(None).inner;
let signature = cryptography::sign(&appointment.to_vec(), &user_sk).unwrap();
Expand Down Expand Up @@ -818,7 +817,7 @@ mod tests_methods {
#[tokio::test]
async fn test_add_appointment_already_triggered() {
// Get the InternalAPI so we can mess with the inner state
let (server_addr, internal_api, _s) =
let (server_addr, internal_api) =
run_tower_in_background_with_config(ApiConfig::new(u32::MAX, DURATION)).await;

// Register
Expand Down Expand Up @@ -869,7 +868,7 @@ mod tests_methods {

#[tokio::test]
async fn test_add_appointment_service_unavailable() {
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down Expand Up @@ -899,7 +898,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -954,7 +953,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment_non_registered() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// User is not registered
let (user_sk, _) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -987,7 +986,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment_not_found() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -1030,7 +1029,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_appointment_service_unavailable() {
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down Expand Up @@ -1065,7 +1064,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_subscription_info() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// Register first
let (user_sk, user_pk) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -1101,7 +1100,7 @@ mod tests_methods {

#[tokio::test]
async fn test_get_subscription_info_non_registered() {
let (server_addr, _s) = run_tower_in_background().await;
let server_addr = run_tower_in_background().await;

// User is not registered
let (user_sk, _) = cryptography::get_random_keypair();
Expand Down Expand Up @@ -1129,7 +1128,7 @@ mod tests_methods {
#[tokio::test]
async fn test_get_subscription_info_service_unavailable() {
let (user_sk, _) = cryptography::get_random_keypair();
let (server_addr, _, _s) = run_tower_in_background_with_config(
let (server_addr, _) = run_tower_in_background_with_config(
ApiConfig::new(SLOTS, DURATION).bitcoind_unreachable(),
)
.await;
Expand Down
Loading
Loading