Skip to content

Commit

Permalink
add gateway param & update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapacan authored and drkaine committed Apr 8, 2024
1 parent 733d398 commit 857f67b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "livebox-exporter-rs"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
39 changes: 26 additions & 13 deletions src/livebox_client_rs/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use hyper::{
use log::{debug, trace};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::net::Ipv4Addr;

#[derive(Debug, Clone)]
pub struct Client {
Expand All @@ -27,11 +26,12 @@ pub struct Client {
}

impl Client {
pub fn new(password: &str) -> Self {
pub fn new(password: &str, ip: &str) -> Self {
trace!("Creating a new client.");
assert!(!password.is_empty(), "Password is empty!");
assert!(!ip.is_empty(), "Gateway livebox ip address is empty!");
Self {
ip: Ipv4Addr::new(192, 168, 1, 1).to_string(),
ip: ip.to_string(),
username: "admin".to_string(),
password: password.to_string(),
cookies: Vec::new(),
Expand Down Expand Up @@ -317,16 +317,29 @@ mod tests {
}

#[tokio::test]
async fn test_client_instantiation() {
async fn test_client_instantiation_default_gateway() {
let password = "test_password";
let client = Client::new(password);
let gateway = "192.168.1.1";
let client = Client::new(password, gateway);
assert_eq!(client.ip, "192.168.1.1");
assert_eq!(client.username, "admin");
assert_eq!(client.password, password);
assert!(client.cookies.is_empty());
assert!(client.context_id.is_none());
}

#[tokio::test]
async fn test_client_instantiation_custom_gateway() {
let password = "test_password";
let gateway = "192.168.1.10";
let client = Client::new(password, gateway);
assert_eq!(client.ip, "192.168.1.10");
assert_eq!(client.username, "admin");
assert_eq!(client.password, password);
assert!(client.cookies.is_empty());
assert!(client.context_id.is_none());
}

#[tokio::test]
async fn test_login_success() {
let server = MockServer::start();
Expand All @@ -339,7 +352,7 @@ mod tests {
.header("set-cookie", "session=mocked_session_id")
.body(json!({"status": 0, "data": {"contextID": "test-context-id"}}).to_string());
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.login().await;
assert_eq!(client.cookies.len(), 1);
Expand All @@ -356,7 +369,7 @@ mod tests {
.header("x-context", "test-context-id");
then.status(200).body(mock_status);
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.cookies.push("session=mocked_session_id".to_string());
client.context_id = Some("test-context-id".to_string());
Expand All @@ -374,7 +387,7 @@ mod tests {
.header("x-context", "test-context-id");
then.status(200).body(mock_wan_config);
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.cookies.push("session=mocked_session_id".to_string());
client.context_id = Some("test-context-id".to_string());
Expand All @@ -392,7 +405,7 @@ mod tests {
.header("x-context", "test-context-id");
then.status(200).body(mock_devices);
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.cookies.push("session=mocked_session_id".to_string());
client.context_id = Some("test-context-id".to_string());
Expand All @@ -410,7 +423,7 @@ mod tests {
.header("x-context", "test-context-id");
then.status(200).body(mock_metrics);
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.cookies.push("session=mocked_session_id".to_string());
client.context_id = Some("test-context-id".to_string());
Expand All @@ -428,7 +441,7 @@ mod tests {
.header("cookie", "session=mocked_session_id");
then.status(200);
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.cookies.push("session=mocked_session_id".to_string());
client.context_id = Some("test-context-id".to_string());
Expand All @@ -448,7 +461,7 @@ mod tests {
.header("authorization", "X-Sah-Login");
then.status(401);
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.login().await;
}
Expand All @@ -463,7 +476,7 @@ mod tests {
.header("x-context", "test-context-id");
then.status(500).body("Internal Server Error");
});
let mut client = Client::new("password");
let mut client = Client::new("password", "192.168.1.1");
client.ip = server.address().to_string();
client.cookies.push("session=mocked_session_id".to_string());
client.context_id = Some("test-context-id".to_string());
Expand Down
46 changes: 41 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ use std::{
#[derive(Debug, Clone, Default)]
struct MyOptions {}

static LIVEBOX_EXPORTER_NAME: &str = env!("CARGO_PKG_NAME");
static LIVEBOX_EXPORTER_VERSION: &str = env!("CARGO_PKG_VERSION");

#[tokio::main]
async fn main() {
let matches = Command::new("livebox-exporter-rs")
.version("0.1.0")
let matches = Command::new(LIVEBOX_EXPORTER_NAME)
.version(LIVEBOX_EXPORTER_VERSION)
.author("tchapacan")
.arg(
Arg::new("port")
Expand Down Expand Up @@ -57,6 +60,14 @@ async fn main() {
.value_parser(value_parser!(String))
.required(true),
)
.arg(
Arg::new("gateway")
.short('G')
.long("gateway")
.help("Livebox gateway ip address")
.value_parser(value_parser!(String))
.default_value("192.168.1.1"),
)
.get_matches();

let verbosity = matches.get_count("verbose");
Expand Down Expand Up @@ -115,7 +126,14 @@ async fn render_livebox_metrics(
std::process::exit(1);
}
};
let mut client = Client::new(&livebox_password);
let ip = match matches.get_one::<String>("gateway") {
Some(gateway) => gateway.clone(),
None => {
eprintln!("Please provide a livebox gateway ip address with the -G or --gateway flag.");
std::process::exit(1);
}
};
let mut client = Client::new(&livebox_password, &ip);
client.login().await;
let status = client.get_status().await;
let wan = client.get_wan_config().await;
Expand Down Expand Up @@ -285,8 +303,8 @@ mod tests {
use maplit::hashmap;

fn parse_args(args: Vec<&str>) -> clap::ArgMatches {
Command::new("livebox-exporter-rs")
.version("0.1.0")
Command::new(LIVEBOX_EXPORTER_NAME)
.version(LIVEBOX_EXPORTER_VERSION)
.author("tchapacan")
.arg(
Arg::new("port")
Expand Down Expand Up @@ -319,6 +337,14 @@ mod tests {
.value_name("PASSWORD")
.required(true),
)
.arg(
Arg::new("gateway")
.short('G')
.long("gateway")
.help("Livebox gateway ip address")
.value_parser(value_parser!(String))
.default_value("192.168.1.1"),
)
.get_matches_from(args)
}

Expand All @@ -339,6 +365,10 @@ mod tests {
matches.get_one::<String>("password"),
Some(&String::from("mypassword"))
);
assert_eq!(
matches.get_one::<String>("gateway"),
Some(&String::from("192.168.1.1"))
);
}

#[test]
Expand All @@ -352,6 +382,8 @@ mod tests {
"-vvv",
"-P",
"mypassword",
"-G",
"192.168.1.10"
];
let matches = parse_args(args);
assert_eq!(
Expand All @@ -367,6 +399,10 @@ mod tests {
matches.get_one::<String>("password"),
Some(&String::from("mypassword"))
);
assert_eq!(
matches.get_one::<String>("gateway"),
Some(&String::from("192.168.1.10"))
);
}

#[test]
Expand Down

0 comments on commit 857f67b

Please sign in to comment.