mod common;
use serde_json::json;
use wiremock::matchers::{body_string, method, path};
use wiremock::{Mock, ResponseTemplate};
use common::{MockEero, TEST_TOKEN, fixture, session_cookie};
use rusteero::auth::Session;
use rusteero::client::Client;
async fn client(mock: &MockEero) -> Client {
Client::builder()
.base_url(mock.uri())
.session(Some(Session::from_token(TEST_TOKEN)))
.build()
.await
.expect("a MockServer's own URI is always a valid base URL")
}
#[tokio::test]
async fn get_thread_forwards_the_cached_network_as_parent() -> anyhow::Result<()> {
let mock = MockEero::start().await;
Mock::given(method("GET"))
.and(path("/2.2/networks/network-0001"))
.and(session_cookie())
.respond_with(
ResponseTemplate::new(200)
.set_body_string(fixture("dns_network_with_settings_link.json")),
)
.expect(1)
.mount(&mock.server)
.await;
Mock::given(method("GET"))
.and(path("/2.4/networks/network-0001/thread"))
.and(session_cookie())
.respond_with(ResponseTemplate::new(200).set_body_string(fixture("thread_status.json")))
.expect(1)
.mount(&mock.server)
.await;
let client = client(&mock).await;
client.get_network(Some("network-0001"), false).await?;
client.get_thread(Some("network-0001")).await?;
Ok(())
}
#[tokio::test]
async fn set_thread_enabled_ignores_the_cached_network_and_targets_the_literal_v22_path()
-> anyhow::Result<()> {
let mock = MockEero::start().await;
Mock::given(method("GET"))
.and(path("/2.2/networks/network-0001"))
.and(session_cookie())
.respond_with(ResponseTemplate::new(200).set_body_string(fixture("network.json")))
.expect(2)
.mount(&mock.server)
.await;
Mock::given(method("PUT"))
.and(path("/2.2/networks/network-0001/thread"))
.and(session_cookie())
.and(body_string(json!({ "enabled": true }).to_string()))
.respond_with(
ResponseTemplate::new(200)
.set_body_string(json!({"meta": {"code": 200}, "data": {}}).to_string()),
)
.expect(1)
.mount(&mock.server)
.await;
let client = client(&mock).await;
client.get_network(Some("network-0001"), false).await?;
client
.set_thread_enabled(true, Some("network-0001"))
.await?;
client.get_network(Some("network-0001"), false).await?;
Ok(())
}
#[tokio::test]
async fn update_thread_invalidates_the_network_bucket() -> anyhow::Result<()> {
let mock = MockEero::start().await;
Mock::given(method("GET"))
.and(path("/2.2/networks/network-0001"))
.and(session_cookie())
.respond_with(ResponseTemplate::new(200).set_body_string(fixture("network.json")))
.expect(2)
.mount(&mock.server)
.await;
Mock::given(method("PUT"))
.and(path("/2.2/networks/network-0001/thread"))
.and(session_cookie())
.respond_with(
ResponseTemplate::new(200)
.set_body_string(json!({"meta": {"code": 200}, "data": {}}).to_string()),
)
.expect(1)
.mount(&mock.server)
.await;
let client = client(&mock).await;
client.get_network(Some("network-0001"), false).await?;
client
.update_thread(Some(true), None, Some("network-0001"))
.await?;
client.get_network(Some("network-0001"), false).await?;
Ok(())
}
#[tokio::test]
async fn regenerate_thread_credentials_invalidates_the_network_bucket() -> anyhow::Result<()> {
let mock = MockEero::start().await;
Mock::given(method("GET"))
.and(path("/2.2/networks/network-0001"))
.and(session_cookie())
.respond_with(ResponseTemplate::new(200).set_body_string(fixture("network.json")))
.expect(2)
.mount(&mock.server)
.await;
Mock::given(method("POST"))
.and(path("/2.2/networks/network-0001/thread"))
.and(session_cookie())
.and(body_string("\"\""))
.respond_with(
ResponseTemplate::new(200).set_body_string(
json!({"meta": {"code": 200}, "data": {"network": {}}}).to_string(),
),
)
.expect(1)
.mount(&mock.server)
.await;
let client = client(&mock).await;
client.get_network(Some("network-0001"), false).await?;
client
.regenerate_thread_credentials(Some("network-0001"))
.await?;
client.get_network(Some("network-0001"), false).await?;
Ok(())
}