Skip to main content

bitwarden_policies/policies/
reset_password.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{Policy, PolicyType, policy_type::PolicyDataType};
4
5/// Account Recovery Administration policy.
6pub struct ResetPasswordPolicy;
7
8impl Policy 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#[cfg_attr(
25    feature = "wasm",
26    derive(tsify::Tsify),
27    tsify(into_wasm_abi, from_wasm_abi)
28)]
29pub struct ResetPasswordPolicyData {
30    /// Whether members are automatically enrolled in account recovery when
31    /// they join the organization.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub auto_enroll_enabled: Option<bool>,
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn round_trips() {
42        let data = ResetPasswordPolicyData {
43            auto_enroll_enabled: Some(true),
44        };
45        let json = serde_json::to_string(&data).unwrap();
46        assert_eq!(json, r#"{"autoEnrollEnabled":true}"#);
47        assert_eq!(
48            serde_json::from_str::<ResetPasswordPolicyData>(&json).unwrap(),
49            data
50        );
51    }
52
53    #[test]
54    fn empty_serializes_to_empty_object() {
55        assert_eq!(
56            serde_json::to_string(&ResetPasswordPolicyData::default()).unwrap(),
57            "{}"
58        );
59    }
60
61    #[test]
62    fn ignores_unknown_fields() {
63        let data: ResetPasswordPolicyData =
64            serde_json::from_str(r#"{"autoEnrollEnabled":false,"unknown":1}"#).unwrap();
65        assert_eq!(data.auto_enroll_enabled, Some(false));
66    }
67}