Skip to main content

alibi_core/wire/
mod.rs

1//! Concrete auth types for API responses and framework callbacks.
2//!
3//! These types decouple JSON response shapes from app-owned `SeaORM` entities.
4//! Each view implements its corresponding `Auth*` entity trait, allowing it
5//! to be used in trait-generic framework code (hooks, helpers).
6mod 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// LCOV_EXCL_START
19#[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            extension_fields: std::collections::BTreeMap::default(),
87        };
88
89        let json =
90            serde_json::to_value(SessionView::from(&session)).expect("serialize session view");
91        assert!((*(json).get("expiresAt").unwrap_or(&serde_json::Value::Null)).is_string());
92        assert_eq!(
93            (*(json).get("ipAddress").unwrap_or(&serde_json::Value::Null)),
94            "127.0.0.1"
95        );
96        assert_eq!(
97            (*(json)
98                .get("activeOrganizationId")
99                .unwrap_or(&serde_json::Value::Null)),
100            "org-1"
101        );
102    }
103
104    #[test]
105    fn account_view_omits_password_on_serialize() {
106        let account = AccountView {
107            id: "acc-1".to_owned(),
108            account_id: "account-id".to_owned(),
109            provider_id: "credential".to_owned(),
110            user_id: "user-1".to_owned(),
111            access_token: None,
112            refresh_token: None,
113            id_token: None,
114            access_token_expires_at: None,
115            refresh_token_expires_at: None,
116            scope: None,
117            password: Some("$2a$hash".to_owned()),
118            created_at: Utc::now(),
119            updated_at: Utc::now(),
120        };
121
122        let json = serde_json::to_value(&account).expect("serialize account view");
123        assert!(
124            json.get("password").is_none(),
125            "password field must not appear in serialized output"
126        );
127    }
128}
129// LCOV_EXCL_STOP