1use kaniop_k8s_util::types::{compare_mail, get_first_cloned, parse_time};
2use kaniop_operator::controller::kanidm::KanidmResource;
3use kaniop_operator::crd::{is_default, KanidmAccountPosixAttributes, KanidmRef};
4use kaniop_operator::kanidm::crd::Kanidm;
5
6use k8s_openapi::apimachinery::pkg::apis::meta::v1::{Condition, LabelSelector, Time};
7use kanidm_proto::constants::{
8 ATTR_ACCOUNT_EXPIRE, ATTR_ACCOUNT_VALID_FROM, ATTR_DISPLAYNAME, ATTR_LEGALNAME, ATTR_MAIL,
9};
10use kanidm_proto::v1::Entry;
11use kube::CustomResource;
12#[cfg(feature = "schemars")]
13use schemars::JsonSchema;
14use serde::{Deserialize, Serialize};
15
16#[derive(CustomResource, Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
23#[cfg_attr(feature = "schemars", derive(JsonSchema))]
24#[kube(
25 category = "kaniop",
26 group = "kaniop.rs",
27 version = "v1beta1",
28 kind = "KanidmPersonAccount",
29 plural = "kanidmpersonaccounts",
30 singular = "kanidmpersonaccount",
31 shortname = "person",
32 namespaced,
33 status = "KanidmPersonAccountStatus",
34 doc = r#"The Kanidm person account custom resource definition (CRD) defines a person account in Kanidm."#,
35 printcolumn = r#"{"name":"Kanidm","type":"string","jsonPath":".status.kanidmRef"}"#,
36 printcolumn = r#"{"name":"GID","type":"integer","jsonPath":".status.gid"}"#,
37 printcolumn = r#"{"name":"Valid","type":"string","jsonPath":".status.conditions[?(@.type == 'Valid')].status"}"#,
38 printcolumn = r#"{"name":"Ready","type":"boolean","jsonPath":".status.ready"}"#,
39 printcolumn = r#"{"name":"Age","type":"date","jsonPath":".metadata.creationTimestamp"}"#,
40 derive = "Default"
41)]
42#[serde(rename_all = "camelCase")]
43pub struct KanidmPersonAccountSpec {
44 pub kanidm_ref: KanidmRef,
45
46 #[schemars(extend("x-kubernetes-validations" = [{"message": "kanidmName cannot be changed.", "rule": "self == oldSelf"}]))]
51 #[serde(skip_serializing_if = "Option::is_none")]
52 pub kanidm_name: Option<String>,
53
54 pub person_attributes: KanidmPersonAttributes,
55 #[serde(skip_serializing_if = "Option::is_none")]
58 pub posix_attributes: Option<KanidmAccountPosixAttributes>,
59
60 #[serde(
64 default = "default_credentials_token_ttl",
65 skip_serializing_if = "is_default"
66 )]
67 pub credentials_token_ttl: u32,
68}
69
70impl KanidmResource for KanidmPersonAccount {
71 #[inline]
72 fn kanidm_ref_spec(&self) -> &KanidmRef {
73 &self.spec.kanidm_ref
74 }
75
76 #[inline]
77 fn get_namespace_selector(kanidm: &Kanidm) -> &Option<LabelSelector> {
78 &kanidm.spec.person_namespace_selector
79 }
80
81 #[inline]
82 fn kanidm_name_override(&self) -> Option<&str> {
83 self.spec.kanidm_name.as_deref()
84 }
85}
86
87#[derive(Serialize, Deserialize, Clone, Debug, Default)]
93#[cfg_attr(feature = "schemars", derive(JsonSchema))]
94#[serde(rename_all = "camelCase")]
95pub struct KanidmPersonAttributes {
96 pub displayname: String,
98
99 #[serde(skip_serializing_if = "Option::is_none")]
102 pub mail: Option<Vec<String>>,
103
104 #[serde(skip_serializing_if = "Option::is_none")]
106 pub legalname: Option<String>,
107
108 #[serde(skip_serializing_if = "Option::is_none")]
112 pub account_valid_from: Option<Time>,
113
114 #[serde(skip_serializing_if = "Option::is_none")]
118 pub account_expire: Option<Time>,
119}
120
121impl PartialEq for KanidmPersonAttributes {
122 fn eq(&self, other: &Self) -> bool {
123 self.displayname == other.displayname
124 && (self.mail.is_none()
125 || self.mail.as_ref().is_some_and(|m| {
126 other.mail.as_ref().is_some_and(|o| compare_mail(m, o))
127 }))
128 && (self.legalname.is_none() || self.legalname == other.legalname)
129 && (self.account_valid_from.is_none()
130 || self.account_valid_from == other.account_valid_from)
131 && (self.account_expire.is_none() || self.account_expire == other.account_expire)
132 }
133}
134
135impl From<Entry> for KanidmPersonAttributes {
136 fn from(entry: Entry) -> Self {
137 KanidmPersonAttributes {
138 displayname: get_first_cloned(&entry, ATTR_DISPLAYNAME).unwrap_or_default(),
139 mail: entry.attrs.get(ATTR_MAIL).cloned(),
140 legalname: get_first_cloned(&entry, ATTR_LEGALNAME),
141 account_valid_from: parse_time(&entry, ATTR_ACCOUNT_VALID_FROM),
142 account_expire: parse_time(&entry, ATTR_ACCOUNT_EXPIRE),
143 }
144 }
145}
146
147fn default_credentials_token_ttl() -> u32 {
148 3600
149}
150
151#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
154#[cfg_attr(feature = "schemars", derive(JsonSchema))]
155#[serde(rename_all = "camelCase")]
156pub enum CredentialState {
157 #[default]
158 Unknown,
159 Present,
160 Absent,
161}
162
163#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)]
165#[cfg_attr(feature = "schemars", derive(JsonSchema))]
166#[serde(rename_all = "camelCase")]
167pub enum CredentialBootstrapState {
168 #[default]
169 Pending,
170 TokenIssued,
171 Complete,
172}
173
174#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
177#[cfg_attr(feature = "schemars", derive(JsonSchema))]
178#[serde(rename_all = "camelCase")]
179pub struct CredentialBootstrapStatus {
180 #[serde(default)]
181 pub state: CredentialBootstrapState,
182
183 #[serde(default, skip_serializing_if = "Option::is_none")]
184 pub token_expires_at: Option<Time>,
185}
186
187#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
191#[cfg_attr(feature = "schemars", derive(JsonSchema))]
192#[serde(rename_all = "camelCase")]
193pub struct KanidmPersonAccountStatus {
194 #[serde(default, skip_serializing_if = "Option::is_none")]
195 #[cfg_attr(
196 feature = "schemars",
197 schemars(extend("x-kubernetes-list-type" = "map", "x-kubernetes-list-map-keys" = ["type"]))
198 )]
199 pub conditions: Option<Vec<Condition>>,
200
201 pub ready: bool,
202
203 #[serde(default, skip_serializing_if = "Option::is_none")]
204 pub gid: Option<u32>,
205
206 pub kanidm_ref: String,
207
208 #[serde(default)]
209 pub credential_state: CredentialState,
210
211 #[serde(default)]
212 pub credential_bootstrap: CredentialBootstrapStatus,
213}
214
215#[cfg(test)]
216mod tests {
217 use super::*;
218 use kanidm_proto::v1::Entry;
219 use std::collections::BTreeMap;
220 use k8s_openapi::apimachinery::pkg::apis::meta::v1::Time;
221 use k8s_openapi::jiff::Timestamp;
222
223 fn create_entry(attrs: BTreeMap<String, Vec<String>>) -> Entry {
224 Entry { attrs }
225 }
226
227 #[test]
228 fn exact_match() {
229 let timestamp1 = Timestamp::from_second(1672531200).unwrap(); let timestamp2 = Timestamp::from_second(1704067200).unwrap(); let spec_attrs = KanidmPersonAttributes {
232 displayname: "Alice".to_string(),
233 mail: Some(vec!["alice@example.com".to_string()]),
234 legalname: Some("Alice Cooper".to_string()),
235 account_valid_from: Some(Time(timestamp1)),
236 account_expire: Some(Time(timestamp2)),
237 };
238
239 let mut attrs_map = BTreeMap::new();
240 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
241 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
242 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
243 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
244 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
245
246 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
247
248 assert_eq!(spec_attrs, kanidm_attrs);
249 }
250
251 #[test]
252 fn displayname_missing_in_kanidm() {
253 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
254 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
255 let spec_attrs = KanidmPersonAttributes {
256 displayname: "Alice".to_string(),
257 mail: Some(vec!["alice@example.com".to_string()]),
258 legalname: Some("Alice Cooper".to_string()),
259 account_valid_from: Some(Time(timestamp1)),
260 account_expire: Some(Time(timestamp2)),
261 };
262
263 let mut attrs_map = BTreeMap::new();
264 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
266 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
267 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
268 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
269
270 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
271 assert_ne!(spec_attrs, kanidm_attrs);
274 }
275
276 #[test]
277 fn displayname_different() {
278 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
279 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
280 let spec_attrs = KanidmPersonAttributes {
281 displayname: "Alice".to_string(),
282 mail: Some(vec!["alice@example.com".to_string()]),
283 legalname: Some("Alice Cooper".to_string()),
284 account_valid_from: Some(Time(timestamp1)),
285 account_expire: Some(Time(timestamp2)),
286 };
287
288 let mut attrs_map = BTreeMap::new();
289 attrs_map.insert("displayname".to_string(), vec!["Bob".to_string()]); attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
291 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
292 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
293 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
294
295 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
296
297 assert_ne!(spec_attrs, kanidm_attrs);
298 }
299
300 #[test]
301 fn mail_missing_in_kanidm() {
302 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
303 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
304 let spec_attrs = KanidmPersonAttributes {
305 displayname: "Alice".to_string(),
306 mail: Some(vec!["alice@example.com".to_string()]), legalname: Some("Alice Cooper".to_string()),
308 account_valid_from: Some(Time(timestamp1)),
309 account_expire: Some(Time(timestamp2)),
310 };
311
312 let mut attrs_map = BTreeMap::new();
313 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
314 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
316 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
317 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
318
319 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
320 assert_ne!(spec_attrs, kanidm_attrs);
323 }
324
325 #[test]
326 fn mail_different_count() {
327 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
328 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
329 let spec_attrs = KanidmPersonAttributes {
330 displayname: "Alice".to_string(),
331 mail: Some(vec!["alice@example.com".to_string(), "a@test.com".to_string()]), legalname: Some("Alice Cooper".to_string()),
333 account_valid_from: Some(Time(timestamp1)),
334 account_expire: Some(Time(timestamp2)),
335 };
336
337 let mut attrs_map = BTreeMap::new();
338 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
339 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]); attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
341 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
342 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
343
344 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
345
346 assert_ne!(spec_attrs, kanidm_attrs);
347 }
348
349 #[test]
350 fn mail_reordered() {
351 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
352 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
353 let spec_attrs = KanidmPersonAttributes {
354 displayname: "Alice".to_string(),
355 mail: Some(vec!["primary@example.com".to_string(), "secondary@example.com".to_string(), "third@example.com".to_string()]),
356 legalname: Some("Alice Cooper".to_string()),
357 account_valid_from: Some(Time(timestamp1)),
358 account_expire: Some(Time(timestamp2)),
359 };
360
361 let mut attrs_map = BTreeMap::new();
362 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
363 attrs_map.insert("mail".to_string(), vec!["primary@example.com".to_string(), "third@example.com".to_string(), "secondary@example.com".to_string()]);
364 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
365 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
366 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
367
368 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
369 assert_eq!(spec_attrs, kanidm_attrs);
370 }
371
372 #[test]
373 fn mail_order_independent() {
374 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
375 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
376 let spec_attrs = KanidmPersonAttributes {
377 displayname: "Alice".to_string(),
378 mail: Some(vec!["primary@example.com".to_string(), "secondary@example.com".to_string()]),
379 legalname: Some("Alice Cooper".to_string()),
380 account_valid_from: Some(Time(timestamp1)),
381 account_expire: Some(Time(timestamp2)),
382 };
383
384 let mut attrs_map = BTreeMap::new();
385 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
386 attrs_map.insert("mail".to_string(), vec!["secondary@example.com".to_string(), "primary@example.com".to_string()]);
387 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
388 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
389 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
390
391 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
392 assert_eq!(spec_attrs, kanidm_attrs);
393 }
394
395 #[test]
396 fn mail_spec_none_kanidm_has_mail() {
397 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
398 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
399 let spec_attrs = KanidmPersonAttributes {
400 displayname: "Alice".to_string(),
401 mail: None, legalname: Some("Alice Cooper".to_string()),
403 account_valid_from: Some(Time(timestamp1)),
404 account_expire: Some(Time(timestamp2)),
405 };
406
407 let mut attrs_map = BTreeMap::new();
408 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
409 attrs_map.insert("mail".to_string(), vec!["x@example.com".to_string()]); attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
411 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
412 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
413
414 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
415 assert_eq!(spec_attrs, kanidm_attrs);
417 }
418
419 #[test]
420 fn legalname_spec_some_kanidm_missing() {
421 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
422 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
423 let spec_attrs = KanidmPersonAttributes {
424 displayname: "Alice".to_string(),
425 mail: Some(vec!["alice@example.com".to_string()]),
426 legalname: Some("Alice Cooper".to_string()), account_valid_from: Some(Time(timestamp1)),
428 account_expire: Some(Time(timestamp2)),
429 };
430
431 let mut attrs_map = BTreeMap::new();
432 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
433 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
434 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
436 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
437
438 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
439 assert_ne!(spec_attrs, kanidm_attrs);
442 }
443
444 #[test]
445 fn legalname_spec_none_kanidm_has_value() {
446 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
447 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
448 let spec_attrs = KanidmPersonAttributes {
449 displayname: "Alice".to_string(),
450 mail: Some(vec!["alice@example.com".to_string()]),
451 legalname: None, account_valid_from: Some(Time(timestamp1)),
453 account_expire: Some(Time(timestamp2)),
454 };
455
456 let mut attrs_map = BTreeMap::new();
457 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
458 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
459 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]); attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
461 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
462
463 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
464 assert_eq!(spec_attrs, kanidm_attrs);
466 }
467
468 #[test]
469 fn account_valid_from_matching() {
470 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
471 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
472 let spec_attrs = KanidmPersonAttributes {
473 displayname: "Alice".to_string(),
474 mail: Some(vec!["alice@example.com".to_string()]),
475 legalname: Some("Alice Cooper".to_string()),
476 account_valid_from: Some(Time(timestamp1)),
477 account_expire: Some(Time(timestamp2)),
478 };
479
480 let mut attrs_map = BTreeMap::new();
481 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
482 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
483 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
484 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
485 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
486
487 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
488
489 assert_eq!(spec_attrs, kanidm_attrs);
490 }
491
492 #[test]
493 fn account_valid_from_spec_set_kanidm_missing() {
494 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
495 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
496 let spec_attrs = KanidmPersonAttributes {
497 displayname: "Alice".to_string(),
498 mail: Some(vec!["alice@example.com".to_string()]),
499 legalname: Some("Alice Cooper".to_string()),
500 account_valid_from: Some(Time(timestamp1)), account_expire: Some(Time(timestamp2)),
502 };
503
504 let mut attrs_map = BTreeMap::new();
505 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
506 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
507 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
508 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
510
511 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
512 assert_ne!(spec_attrs, kanidm_attrs);
515 }
516
517 #[test]
518 fn account_expire_matching() {
519 let timestamp1 = Timestamp::from_second(1672531200).unwrap();
520 let timestamp2 = Timestamp::from_second(1704067200).unwrap();
521 let spec_attrs = KanidmPersonAttributes {
522 displayname: "Alice".to_string(),
523 mail: Some(vec!["alice@example.com".to_string()]),
524 legalname: Some("Alice Cooper".to_string()),
525 account_valid_from: Some(Time(timestamp1)),
526 account_expire: Some(Time(timestamp2)),
527 };
528
529 let mut attrs_map = BTreeMap::new();
530 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
531 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
532 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
533 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
534 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
535
536 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
537
538 assert_eq!(spec_attrs, kanidm_attrs);
539 }
540
541 #[test]
542 fn minimal_spec() {
543 let spec_attrs = KanidmPersonAttributes {
544 displayname: "Alice".to_string(),
545 mail: None,
546 legalname: None,
547 account_valid_from: None,
548 account_expire: None,
549 };
550
551 let mut attrs_map = BTreeMap::new();
552 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
553 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
556
557 assert_eq!(spec_attrs, kanidm_attrs);
558 }
559
560 #[test]
561 fn timestamp_format_roundtrip() {
562 let timestamp1 = Timestamp::from_second(1672531200).unwrap(); let timestamp2 = Timestamp::from_second(1704067200).unwrap(); let spec_attrs = KanidmPersonAttributes {
565 displayname: "Alice".to_string(),
566 mail: Some(vec!["alice@example.com".to_string()]),
567 legalname: Some("Alice Cooper".to_string()),
568 account_valid_from: Some(Time(timestamp1)),
569 account_expire: Some(Time(timestamp2)),
570 };
571
572 let mut attrs_map = BTreeMap::new();
573 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
574 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
575 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
576 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00Z".to_string()]);
577 attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
578
579 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
580
581 assert_eq!(spec_attrs, kanidm_attrs);
582 }
583
584 #[test]
585 fn timestamp_different_format() {
586 let timestamp1 = Timestamp::from_second(1672531200).unwrap(); let timestamp2 = Timestamp::from_second(1704067200).unwrap(); let spec_attrs = KanidmPersonAttributes {
589 displayname: "Alice".to_string(),
590 mail: Some(vec!["alice@example.com".to_string()]),
591 legalname: Some("Alice Cooper".to_string()),
592 account_valid_from: Some(Time(timestamp1)),
593 account_expire: Some(Time(timestamp2)),
594 };
595
596 let mut attrs_map = BTreeMap::new();
597 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
598 attrs_map.insert("mail".to_string(), vec!["alice@example.com".to_string()]);
599 attrs_map.insert("legalname".to_string(), vec!["Alice Cooper".to_string()]);
600 attrs_map.insert("account_valid_from".to_string(), vec!["2023-01-01T00:00:00+00:00".to_string()]); attrs_map.insert("account_expire".to_string(), vec!["2024-01-01T00:00:00Z".to_string()]);
602
603 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
604
605 assert_eq!(spec_attrs, kanidm_attrs);
606 }
607
608 #[test]
609 fn displayname_spec_empty_vs_kanidm_missing() {
610 let spec_attrs = KanidmPersonAttributes {
612 displayname: "".to_string(), mail: None,
614 legalname: None,
615 account_valid_from: None,
616 account_expire: None,
617 };
618
619 let attrs_map = BTreeMap::new();
620 let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
622
623 assert_eq!(spec_attrs, kanidm_attrs);
626 }
627
628 #[test]
629 fn displayname_spec_vs_kanidm_empty() {
630 let spec_attrs = KanidmPersonAttributes {
631 displayname: "Alice".to_string(),
632 mail: None,
633 legalname: None,
634 account_valid_from: None,
635 account_expire: None,
636 };
637
638 let mut attrs_map = BTreeMap::new();
639 attrs_map.insert("displayname".to_string(), vec!["".to_string()]); let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
641
642 assert_ne!(spec_attrs, kanidm_attrs);
644 }
645
646 #[test]
647 fn mail_spec_none_vs_kanidm_empty_vec() {
648 let spec_attrs = KanidmPersonAttributes {
650 displayname: "Alice".to_string(),
651 mail: None, legalname: None,
653 account_valid_from: None,
654 account_expire: None,
655 };
656
657 let mut attrs_map = BTreeMap::new();
658 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
659 attrs_map.insert("mail".to_string(), vec![]); let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
661
662 assert_eq!(spec_attrs, kanidm_attrs);
664 }
665
666 #[test]
667 fn mail_spec_some_vs_kanidm_empty_vec() {
668 let spec_attrs = KanidmPersonAttributes {
670 displayname: "Alice".to_string(),
671 mail: Some(vec!["test@example.com".to_string()]), legalname: None,
673 account_valid_from: None,
674 account_expire: None,
675 };
676
677 let mut attrs_map = BTreeMap::new();
678 attrs_map.insert("displayname".to_string(), vec!["Alice".to_string()]);
679 attrs_map.insert("mail".to_string(), vec![]); let kanidm_attrs = KanidmPersonAttributes::from(create_entry(attrs_map));
681
682 assert_ne!(spec_attrs, kanidm_attrs);
684 }
685
686 #[test]
687 fn partial_eq_asymmetry_is_intentional() {
688 let spec = KanidmPersonAttributes {
695 displayname: "Alice".to_string(),
696 mail: Some(vec!["alice@example.com".to_string()]),
697 legalname: None,
698 account_valid_from: None,
699 account_expire: None,
700 };
701
702 let state = KanidmPersonAttributes {
703 displayname: "Alice".to_string(),
704 mail: Some(vec!["alice@example.com".to_string()]),
705 legalname: Some("Alice Smith".to_string()),
706 account_valid_from: None,
707 account_expire: None,
708 };
709
710 assert_eq!(spec, state);
712
713 assert_ne!(state, spec);
715 }
716}