Skip to main content

api_bones_test/builders/
principal.rs

1use api_bones::audit::{Principal, PrincipalId, PrincipalKind};
2use api_bones::org_id::OrgId;
3use uuid::Uuid;
4
5/// Builder for a fake [`Principal`].
6///
7/// `Principal` carries no `scopes` field; the `.scopes()` method is provided
8/// for ergonomic parity with higher-level test fixtures but has no effect on
9/// the built `Principal` value.
10///
11/// # Quick start
12///
13/// ```rust
14/// use api_bones::audit::PrincipalKind;
15/// use api_bones_test::builders::FakePrincipal;
16///
17/// let p = FakePrincipal::user(uuid::Uuid::new_v4()).build();
18/// assert!(matches!(p.kind, PrincipalKind::User));
19/// ```
20pub struct FakePrincipal {
21    id: PrincipalId,
22    kind: PrincipalKind,
23    org_path: Vec<OrgId>,
24}
25
26impl FakePrincipal {
27    #[must_use]
28    pub fn user(id: Uuid) -> Self {
29        Self {
30            id: PrincipalId::from_uuid(id),
31            kind: PrincipalKind::User,
32            org_path: Vec::new(),
33        }
34    }
35
36    #[must_use]
37    pub fn agent(id: Uuid) -> Self {
38        Self {
39            id: PrincipalId::from_uuid(id),
40            kind: PrincipalKind::Agent,
41            org_path: Vec::new(),
42        }
43    }
44
45    #[must_use]
46    pub fn org_path(mut self, path: Vec<OrgId>) -> Self {
47        self.org_path = path;
48        self
49    }
50
51    /// Stored for ergonomic parity; `Principal` has no `scopes` field so these
52    /// are not propagated to the built value.
53    #[must_use]
54    pub fn scopes(self, _scopes: &[&str]) -> Self {
55        self
56    }
57
58    #[must_use]
59    pub fn build(self) -> Principal {
60        Principal {
61            id: self.id,
62            kind: self.kind,
63            org_path: self.org_path,
64        }
65    }
66}