Line data Source code
1 : // SPDX-License-Identifier: EUPL-1.2
2 : //! Who and what wrote a request's history: the `provenance` member a
3 : //! history row carries. This broker reads no provenance header; every write
4 : //! through the API is recorded as `channel: api`.
5 :
6 : use axum::http::HeaderMap;
7 : use serde_json::{json, Value};
8 :
9 : /// The provenance member that marks a row kept because its write was
10 : /// audited.
11 : pub const AUDIT: &str = "audit";
12 :
13 : /// Whether `provenance` is an audited write's.
14 93152 : pub fn audited(provenance: &Value) -> bool {
15 93152 : provenance.get(AUDIT) == Some(&Value::Bool(true))
16 93152 : }
17 :
18 : /// No header is read, every write is `channel: api`.
19 55042 : pub fn of_request(_headers: &HeaderMap, _gateway: Option<&str>) -> Value {
20 55042 : json!({"channel": "api"})
21 55042 : }
22 :
23 : /// `provenance` with its `operation`.
24 132008 : pub fn with_operation(provenance: &Value, operation: &str) -> Value {
25 132008 : let mut out = provenance.clone();
26 132008 : if let Some(o) = out.as_object_mut() {
27 132008 : o.insert("operation".into(), json!(operation));
28 132008 : }
29 132008 : out
30 132008 : }
31 :
32 : #[cfg(test)]
33 : mod tests {
34 : use super::*;
35 :
36 : #[test]
37 4 : fn every_request_is_the_api_channel() {
38 4 : let mut h = HeaderMap::new();
39 4 : h.insert("x-request-id", "4f1c-9a07".parse().expect("header"));
40 4 : assert_eq!(of_request(&h, None), json!({"channel": "api"}));
41 4 : }
42 :
43 : #[test]
44 4 : fn the_operation_is_added() {
45 4 : assert_eq!(
46 4 : with_operation(&json!({"channel": "ui"}), "delete"),
47 4 : json!({"channel": "ui", "operation": "delete"})
48 : );
49 4 : }
50 : }
|