diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f51bad5..59764ea 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,7 @@ -on: [push] +on: + pull_request: + branches: + - main name: build jobs: build: diff --git a/.github/workflows/security-audit.yml b/.github/workflows/security-audit.yml index fa95789..6c1c529 100644 --- a/.github/workflows/security-audit.yml +++ b/.github/workflows/security-audit.yml @@ -1,7 +1,5 @@ name: Security audit on: - schedule: - - cron: "0 8 * * *" push: paths: - "**/Cargo.*" diff --git a/Cargo.toml b/Cargo.toml index fe84069..97ea287 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,6 @@ path = "src/se_ms_api.rs" chrono = "0.4" reqwest = { version = "0.11", features = ["json", "blocking", "cookies"] } serde = { version = "1", features = ["derive"] } + +[dev-dependencies] +lazy_static = "1.4" diff --git a/src/se_ms_api.rs b/src/se_ms_api.rs index 8235fea..88c2387 100644 --- a/src/se_ms_api.rs +++ b/src/se_ms_api.rs @@ -19,11 +19,11 @@ //! let site_id = "my_site_id"; //! let api_key = "my_api_key"; //! -//! let solar_edge = SolaredgeCredentials::new(&site_id, &api_key); // (1) -//! let req = SiteDetailsReq::new(); // (2) -//! let resp = req.send(&solar_edge); // (3) +//! let cred = SolaredgeCredentials::create(&site_id, &api_key); // (1) +//! let req = SiteDetailsReq::new(); // (2) +//! let resp = req.send(&cred); // (3) //! -//! match resp { // (4) +//! match resp { // (4) //! Ok(r) => { //! println!("My site's status is {}.", r.details.status); //! } @@ -37,7 +37,7 @@ //! * [SiteEnergyDetailedReq] / [SiteEnergyDetailedResp] //! -#![deny(unused_crate_dependencies)] +//#![warn(unused_crate_dependencies)] #![deny(unused_extern_crates)] #![warn(missing_docs)] @@ -73,7 +73,7 @@ impl SolaredgeCredentials { const MONITORING_API_URL: &'static str = "https://monitoringapi.solaredge.com/"; /// Create a Solaredge destination for the requests from the given site id and api_key. - pub fn new(site_id: &str, api_key: &str) -> Self { + pub fn create(site_id: &str, api_key: &str) -> Self { let url_start = SolaredgeCredentials::MONITORING_API_URL.to_string(); let site_id = site_id.to_string(); let url_end = format!("api_key={}", api_key); @@ -84,6 +84,11 @@ impl SolaredgeCredentials { url_end, } } + + /// See the site ID bing used in the credentials. + pub fn site_id(&self) -> &str { + &self.site_id + } } #[cfg(test)] @@ -91,10 +96,11 @@ mod tests { use super::*; #[test] - fn solaredge_new_unit_test() { - let se = SolaredgeCredentials::new("id", "key"); + fn solaredge_credentials_unit_test() { + let se = SolaredgeCredentials::create("id", "key"); assert_eq!(se.url_start, SolaredgeCredentials::MONITORING_API_URL); assert_eq!(se.site_id, "id"); + assert_eq!(se.site_id(), "id"); assert_eq!(se.url_end, "api_key=key"); } } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index d06ff85..220f809 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,34 +1,32 @@ +use se_ms_api::SolaredgeCredentials; use std::env; use std::fs; -use std::sync::Once; -pub const TIME_FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; +pub const TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S"; -const TEST_CREDENTIALS_FILE: &'static str = "test_credentials.txt"; +const TEST_CREDENTIALS_FILE: &str = "test_credentials.txt"; -static mut SITE_ID: String = String::new(); -static mut API_KEY: String = String::new(); -static INIT: Once = Once::new(); +lazy_static! { + pub static ref TEST_CREDENTIALS: SolaredgeCredentials = { + let mut site_id = String::new(); + let mut api_key = String::new(); -pub fn get_site_id_and_api_key() -> (&'static str, &'static str) { - unsafe { - INIT.call_once(|| { - let path = env::current_dir().unwrap(); - let path = path.join("tests").join(TEST_CREDENTIALS_FILE); + let path = env::current_dir().unwrap(); + let path = path.join("tests").join(TEST_CREDENTIALS_FILE); - let contents = fs::read_to_string(path) - .expect(&format!("Unable to read {}.", TEST_CREDENTIALS_FILE)); - let mut lines = contents.lines(); - if let Some(s) = lines.next() { - SITE_ID = s.to_string(); - } - if let Some(s) = lines.next() { - API_KEY = s.to_string(); - } - if SITE_ID.len() == 0 || API_KEY.len() == 0 { - panic!("Ill formed credentials file."); - } - }); - (&SITE_ID, &API_KEY) - } + let contents = fs::read_to_string(path) + .unwrap_or_else(|_| panic!("Unable to read {}.", TEST_CREDENTIALS_FILE)); + let mut lines = contents.lines(); + if let Some(s) = lines.next() { + site_id = s.to_string(); + } + if let Some(s) = lines.next() { + api_key = s.to_string(); + } + if site_id.is_empty() || api_key.is_empty() { + panic!("Ill formed credentials file."); + } + + SolaredgeCredentials::create(&site_id, &api_key) + }; } diff --git a/tests/integration_reqs_test.rs b/tests/integration_reqs_test.rs index 9d8028d..fe95335 100644 --- a/tests/integration_reqs_test.rs +++ b/tests/integration_reqs_test.rs @@ -1,18 +1,16 @@ use chrono::NaiveDateTime; +#[macro_use] +extern crate lazy_static; + mod common; use se_ms_api::{ - CurrentVersionReq, MeterType, SiteDetailsReq, SiteEnergyDetailedReq, SolaredgeCredentials, - SupportedVersionsReq, + CurrentVersionReq, MeterType, SiteDetailsReq, SiteEnergyDetailedReq, SupportedVersionsReq, }; #[test] fn site_energy_detailed_integration_test() { - let (site_id, api_key) = common::get_site_id_and_api_key(); - - let solar_edge = SolaredgeCredentials::new(&site_id, &api_key); - let start_ndt = match NaiveDateTime::parse_from_str("2022-01-01 00:00:00", common::TIME_FORMAT) { Ok(dt) => dt, @@ -31,7 +29,7 @@ fn site_energy_detailed_integration_test() { Some(vec![MeterType::SelfConsumption]), ); - let resp = req.send(&solar_edge); + let resp = req.send(&common::TEST_CREDENTIALS); match resp { Ok(r) => { @@ -56,12 +54,8 @@ fn site_energy_detailed_integration_test() { #[test] fn current_version_integration_test() { - let (site_id, api_key) = common::get_site_id_and_api_key(); - - let solar_edge = SolaredgeCredentials::new(&site_id, &api_key); - let req = CurrentVersionReq::new(); - let resp = req.send(&solar_edge); + let resp = req.send(&common::TEST_CREDENTIALS); match resp { Ok(r) => { @@ -75,12 +69,8 @@ fn current_version_integration_test() { #[test] fn supported_versions_integration_test() { - let (site_id, api_key) = common::get_site_id_and_api_key(); - - let solar_edge = SolaredgeCredentials::new(&site_id, &api_key); - let req = SupportedVersionsReq::new(); - let resp = req.send(&solar_edge); + let resp = req.send(&common::TEST_CREDENTIALS); match resp { Ok(r) => { @@ -94,16 +84,12 @@ fn supported_versions_integration_test() { #[test] fn site_details_integration_test() { - let (site_id, api_key) = common::get_site_id_and_api_key(); - - let solar_edge = SolaredgeCredentials::new(&site_id, &api_key); - let req = SiteDetailsReq::new(); - let resp = req.send(&solar_edge); + let resp = req.send(&common::TEST_CREDENTIALS); match resp { Ok(r) => { - assert_eq!(r.details.id.to_string(), site_id); + assert_eq!(r.details.id.to_string(), common::TEST_CREDENTIALS.site_id()); assert_eq!(r.details.status, "Active"); assert_eq!(r.details.location.countryCode, "US"); assert_eq!(r.details.primaryModule.manufacturerName, "LG");