1mod account;
7mod plugins;
8mod session;
9mod user;
10mod verification;
11
12pub use account::AccountView;
13pub use plugins::{ApiKeyView, InvitationView, OrganizationView, PasskeyView};
14pub use session::SessionView;
15pub use user::UserView;
16pub use verification::VerificationView;
17
18#[cfg(test)]
20mod tests {
21 use super::*;
22 use chrono::Utc;
23
24 #[test]
25 fn user_view_serializes_camel_case() {
26 let user = UserView {
27 omitted_fields: std::collections::BTreeSet::default(),
28 id: "user-1".to_owned(),
29 name: Some("Ada".to_owned()),
30 email: Some("ada@example.com".to_owned()),
31 email_verified: true,
32 image: None,
33 created_at: Utc::now(),
34 updated_at: Utc::now(),
35 username: Some("ada".to_owned()),
36 display_username: Some("Ada".to_owned()),
37 two_factor_enabled: Some(true),
38 role: Some("admin".to_owned()),
39 banned: Some(false),
40 ban_reason: None,
41 ban_expires: None,
42 is_anonymous: None,
43 phone_number: None,
44 phone_number_verified: None,
45 last_login_method: None,
46 extension_fields: std::collections::BTreeMap::default(),
47 metadata: serde_json::json!({}),
48 };
49
50 let json = serde_json::to_value(UserView::from(&user)).expect("serialize user view");
51 assert_eq!(
52 (*(json)
53 .get("emailVerified")
54 .unwrap_or(&serde_json::Value::Null)),
55 true
56 );
57 assert_eq!(
58 (*(json)
59 .get("displayUsername")
60 .unwrap_or(&serde_json::Value::Null)),
61 "Ada"
62 );
63 assert_eq!(
64 (*(json)
65 .get("twoFactorEnabled")
66 .unwrap_or(&serde_json::Value::Null)),
67 true
68 );
69 }
70
71 #[test]
72 fn session_view_serializes_camel_case() {
73 let session = SessionView {
74 omitted_fields: std::collections::BTreeSet::default(),
75 id: "session-1".to_owned(),
76 expires_at: Utc::now(),
77 token: "token".to_owned(),
78 created_at: Utc::now(),
79 updated_at: Utc::now(),
80 ip_address: Some("127.0.0.1".to_owned()),
81 user_agent: Some("agent".to_owned()),
82 user_id: "user-1".to_owned(),
83 impersonated_by: Some("admin-1".to_owned()),
84 active_organization_id: Some("org-1".to_owned()),
85 active_team_id: None,
86 active: true,
87 extension_fields: std::collections::BTreeMap::default(),
88 };
89
90 let json =
91 serde_json::to_value(SessionView::from(&session)).expect("serialize session view");
92 assert!((*(json).get("expiresAt").unwrap_or(&serde_json::Value::Null)).is_string());
93 assert_eq!(
94 (*(json).get("ipAddress").unwrap_or(&serde_json::Value::Null)),
95 "127.0.0.1"
96 );
97 assert_eq!(
98 (*(json)
99 .get("activeOrganizationId")
100 .unwrap_or(&serde_json::Value::Null)),
101 "org-1"
102 );
103 }
104
105 #[test]
106 fn account_view_omits_password_on_serialize() {
107 let account = AccountView {
108 id: "acc-1".to_owned(),
109 account_id: "account-id".to_owned(),
110 provider_id: "credential".to_owned(),
111 user_id: "user-1".to_owned(),
112 access_token: None,
113 refresh_token: None,
114 id_token: None,
115 access_token_expires_at: None,
116 refresh_token_expires_at: None,
117 scope: None,
118 password: Some("$2a$hash".to_owned()),
119 created_at: Utc::now(),
120 updated_at: Utc::now(),
121 };
122
123 let json = serde_json::to_value(&account).expect("serialize account view");
124 assert!(
125 json.get("password").is_none(),
126 "password field must not appear in serialized output"
127 );
128 }
129}
130