Skip to main content

bitwarden_policies/policies/
reset_password.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{PolicyDefinition, PolicyType, policy_type::PolicyDataType};
4
5/// Account Recovery Administration policy.
6pub struct ResetPasswordPolicy;
7
8impl PolicyDefinition for ResetPasswordPolicy {
9    type Data = ResetPasswordPolicyData;
10
11    fn policy_type(&self) -> PolicyType {
12        PolicyType::ResetPassword
13    }
14
15    fn to_erased(&self, data: Self::Data) -> PolicyDataType {
16        PolicyDataType::ResetPassword(data)
17    }
18}
19
20/// Configuration data for the Account Recovery Administration policy.
21#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
22#[serde(rename_all = "camelCase")]
23#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
24#[bitwarden_ffi::wasm_record]
25pub struct ResetPasswordPolicyData {
26    /// Whether members are automatically enrolled in account recovery when
27    /// they join the organization.
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub auto_enroll_enabled: Option<bool>,
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn round_trips() {
38        let data = ResetPasswordPolicyData {
39            auto_enroll_enabled: Some(true),
40        };
41        let json = serde_json::to_string(&data).unwrap();
42        assert_eq!(json, r#"{"autoEnrollEnabled":true}"#);
43        assert_eq!(
44            serde_json::from_str::<ResetPasswordPolicyData>(&json).unwrap(),
45            data
46        );
47    }
48
49    #[test]
50    fn empty_serializes_to_empty_object() {
51        assert_eq!(
52            serde_json::to_string(&ResetPasswordPolicyData::default()).unwrap(),
53            "{}"
54        );
55    }
56
57    #[test]
58    fn ignores_unknown_fields() {
59        let data: ResetPasswordPolicyData =
60            serde_json::from_str(r#"{"autoEnrollEnabled":false,"unknown":1}"#).unwrap();
61        assert_eq!(data.auto_enroll_enabled, Some(false));
62    }
63}