matroskin 0.1.0

whatsminer api client lib
Documentation
//! Implement `set.system.factory_reset` command
//!
//! Resets the device to its factory default settings.
//! This operation will revert network configuration, system passwords,
//! user permissions, API switch, web pool settings, saved pools,
//! power mode, and power limit. The device will reboot upon completion.
//!
//! - Command: [SetSystemFactoryReset]
//! - ApiDoc: <https://apidoc.whatsminer.com/#api-System-system_factory_reset>
use core::str;

use crate::{
    command::{Command, CommandLevel},
    error::Result,
    permission::PermissionLevel,
};

/// This command represents the `set.system.factory_reset` operation.
///
/// Resets the device to its factory default settings.
/// Only executable by an account with super privileges.
///
/// - ApiDoc: <https://apidoc.whatsminer.com/#api-System-system_factory_reset>
///
/// # Example
/// ```rust,no_run
/// use matroskin::actor::Actor;
/// use matroskin::account::Account;
/// use matroskin::password::Password;
/// use matroskin::command::set_system_factory_reset::SetSystemFactoryReset;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let actor = Actor::new("1.1.1.1:4433", Account::Super, Password::Super);
///     actor.connect()?;
///
///     let cmd = SetSystemFactoryReset;
///     let res = actor.send(cmd).await?;
///     println!("res: {:#?}", res);
///     Ok(())
/// }
/// ```
#[derive(Debug, Default)]
pub struct SetSystemFactoryReset;

impl Command for SetSystemFactoryReset {
    type Response = String;
    const CMD_NAME: &'static str = "set.system.factory_reset";
    const LEVEL: CommandLevel = CommandLevel::Secured;
    const MINIMAL_PERMISSIONS: PermissionLevel = PermissionLevel::All;
    fn params(&self) -> Result<Option<impl serde::Serialize>> {
        Ok(None::<()>)
    }
}

#[cfg(test)]
mod tests {
    use crate::account::Account;
    use crate::auth_data::AuthData;

    use super::*;

    const RESPONSE_JSON: &str =
        r#"{"code":0,"when":1692685512,"msg":"ok","desc":"set.system.factory_reset"}"#;

    #[test]
    fn request_is_secured_without_params() {
        let cmd = SetSystemFactoryReset;
        assert!(cmd.is_secured());
        assert!(!cmd.is_encrypted());

        let auth = AuthData::new::<SetSystemFactoryReset>(Account::Super, "pass", "salt").unwrap();
        let encoded = cmd.encode(Some(auth)).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();

        assert_eq!(json["cmd"], "set.system.factory_reset");
        assert_eq!(json["account"], "super");
        assert!(json["ts"].as_u64().unwrap() > 0);
        assert_eq!(json["token"].as_str().unwrap().len(), 8);
        assert!(json.get("param").is_none());
    }

    #[test]
    fn request_without_auth_returns_no_token() {
        let cmd = SetSystemFactoryReset;
        let encoded = cmd.encode(None).unwrap();
        let json: serde_json::Value = serde_json::from_slice(&encoded.0).unwrap();
        assert!(json.get("token").is_none());
        assert!(json.get("param").is_none());
    }

    #[test]
    fn deserialize_response_fixture() {
        let response = SetSystemFactoryReset::parse_response(RESPONSE_JSON.as_bytes()).unwrap();
        assert!(response.code.is_success());
        assert_eq!(response.desc, "set.system.factory_reset");
        assert_eq!(response.when, 1692685512);
        assert_eq!(response.msg, "ok");
    }
}