Skip to main content

vcon_server/
reveal.rs

1//! A reveal: a read of a vCon that says which part of it the caller is
2//! about to show, so the audit log tells it from any other read. The
3//! marker changes the record's action and nothing else: the route, the
4//! scope it needs and the answer are those of the read without it.
5
6use crate::query::{self, BadQuery};
7
8/// The query parameter that marks a read as a reveal.
9pub const MARKER: &str = "reveal";
10
11/// A part of a vCon that a reveal shows.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum Part {
14    /// The parties: names, numbers and addresses.
15    Parties,
16    /// The subject.
17    Subject,
18    /// The dialog: what was said.
19    Dialog,
20    /// The analysis, transcripts among it.
21    Analysis,
22    /// Where the vCon links to.
23    Links,
24    /// The whole vCon.
25    All,
26    /// One dialog's recording, to play it.
27    Audio,
28}
29
30impl Part {
31    /// Every part, in the order the console offers them.
32    pub const ALL: [Part; 7] = [
33        Part::Parties,
34        Part::Subject,
35        Part::Dialog,
36        Part::Analysis,
37        Part::Links,
38        Part::All,
39        Part::Audio,
40    ];
41
42    /// The part's name, as the marker gives it and the log records it.
43    #[must_use]
44    pub fn name(self) -> &'static str {
45        match self {
46            Part::Parties => "parties",
47            Part::Subject => "subject",
48            Part::Dialog => "dialog",
49            Part::Analysis => "analysis",
50            Part::Links => "links",
51            Part::All => "all",
52            Part::Audio => "audio",
53        }
54    }
55
56    /// The part a read's query marks it as a reveal of, or None for a read
57    /// that carries no marker.
58    ///
59    /// # Errors
60    ///
61    /// [`BadQuery`] if the query names the marker and the marker is not
62    /// exactly one known part.
63    pub fn from_query(query: Option<&str>) -> Result<Option<Part>, BadQuery> {
64        if !query::names(query, MARKER) {
65            return Ok(None);
66        }
67        let read = query::pairs(query, &[MARKER])?;
68        read.get(MARKER)
69            .and_then(|value| Part::named(value))
70            .map(Some)
71            .ok_or(BadQuery)
72    }
73
74    /// The part with this name, if there is one.
75    fn named(name: &str) -> Option<Part> {
76        Part::ALL.into_iter().find(|part| part.name() == name)
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use crate::audit::Action;
84
85    #[test]
86    fn each_part_has_the_name_the_marker_and_the_log_use() {
87        let named: Vec<&str> = Part::ALL.into_iter().map(Part::name).collect();
88        assert_eq!(
89            named,
90            [
91                "parties", "subject", "dialog", "analysis", "links", "all", "audio"
92            ],
93            "the names"
94        );
95    }
96
97    #[test]
98    fn a_read_with_no_query_is_not_a_reveal() {
99        assert_eq!(Part::from_query(None), Ok(None), "no query");
100    }
101
102    #[test]
103    fn a_query_that_names_a_part_is_a_reveal_of_it() {
104        for part in Part::ALL {
105            let query = format!("reveal={}", part.name());
106            assert_eq!(Part::from_query(Some(&query)), Ok(Some(part)), "{query}");
107        }
108    }
109
110    #[test]
111    fn a_marker_that_names_no_known_part_is_refused() {
112        for unknown in ["everything", "party", "audio2", "*", "video"] {
113            let query = format!("reveal={unknown}");
114            assert_eq!(Part::from_query(Some(&query)), Err(BadQuery), "{query}");
115        }
116    }
117
118    /// A read has never looked at its query, so one that does not name
119    /// the marker is the read it always was.
120    #[test]
121    fn a_query_that_does_not_name_the_marker_is_not_a_reveal() {
122        for query in ["limit=3", "x=1&y=2", "revealed=all", "part=all"] {
123            assert_eq!(Part::from_query(Some(query)), Ok(None), "{query}");
124        }
125    }
126
127    /// A marked read takes the marker and nothing else, so a marker that
128    /// is not first is not passed over as an unmarked read.
129    #[test]
130    fn a_marker_beside_another_parameter_is_refused() {
131        for query in [
132            "x=1&reveal=links",
133            "reveal=links&x=1",
134            "limit=3&reveal=all&y=2",
135        ] {
136            assert_eq!(Part::from_query(Some(query)), Err(BadQuery), "{query}");
137        }
138    }
139
140    #[test]
141    fn a_marker_given_twice_is_refused() {
142        for query in ["reveal=links&reveal=links", "reveal=links&reveal=all"] {
143            assert_eq!(Part::from_query(Some(query)), Err(BadQuery), "{query}");
144        }
145    }
146
147    /// As in every query this server reads, an empty piece names nothing.
148    #[test]
149    fn an_empty_piece_beside_the_marker_names_nothing() {
150        for query in ["reveal=links&", "&reveal=links", "&&reveal=links&&"] {
151            assert_eq!(
152                Part::from_query(Some(query)),
153                Ok(Some(Part::Links)),
154                "{query}"
155            );
156        }
157    }
158
159    /// The marker's name with no value is still the marker: it is refused,
160    /// not read as an unmarked read.
161    #[test]
162    fn a_marker_with_no_value_is_refused() {
163        for query in ["reveal", "reveal=", "reveal&x=1"] {
164            assert_eq!(Part::from_query(Some(query)), Err(BadQuery), "{query}");
165        }
166    }
167
168    /// The name and the value are percent-decoded as every query's are, so
169    /// an encoded marker is the marker and cannot pass as an unmarked read.
170    #[test]
171    fn a_percent_encoded_marker_is_the_marker() {
172        for query in ["%72eveal=links", "reveal=%6Cinks", "r%65veal=li%6Eks"] {
173            assert_eq!(
174                Part::from_query(Some(query)),
175                Ok(Some(Part::Links)),
176                "{query}"
177            );
178        }
179    }
180
181    #[test]
182    fn a_reveal_is_recorded_under_an_action_that_names_its_part() {
183        let named: Vec<&str> = Part::ALL
184            .into_iter()
185            .map(|part| Action::Reveal(part).as_str())
186            .collect();
187        assert_eq!(
188            named,
189            [
190                "reveal_parties",
191                "reveal_subject",
192                "reveal_dialog",
193                "reveal_analysis",
194                "reveal_links",
195                "reveal_all",
196                "reveal_audio"
197            ],
198            "the names"
199        );
200    }
201
202    #[test]
203    fn a_legal_reveal_is_recorded_under_a_legal_action_that_names_its_part() {
204        let named: Vec<&str> = Part::ALL
205            .into_iter()
206            .map(|part| Action::LegalReveal(part).as_str())
207            .collect();
208        assert_eq!(
209            named,
210            [
211                "legal_reveal_parties",
212                "legal_reveal_subject",
213                "legal_reveal_dialog",
214                "legal_reveal_analysis",
215                "legal_reveal_links",
216                "legal_reveal_all",
217                "legal_reveal_audio"
218            ],
219            "the names"
220        );
221    }
222
223    /// The name of the part a query marks, or the refusal.
224    fn marked(query: &str) -> Result<Option<&'static str>, BadQuery> {
225        Part::from_query(Some(query)).map(|part| part.map(Part::name))
226    }
227
228    #[test]
229    fn a_marker_of_audio_is_a_reveal_of_a_recording() {
230        assert_eq!(marked("reveal=audio"), Ok(Some("audio")), "the part");
231    }
232
233    #[test]
234    fn a_reveal_of_a_recording_has_actions_of_its_own() {
235        let audio = Part::ALL
236            .into_iter()
237            .find(|part| part.name() == "audio")
238            .expect("a part named audio");
239        assert_eq!(
240            (
241                Action::Reveal(audio).as_str(),
242                Action::LegalReveal(audio).as_str()
243            ),
244            ("reveal_audio", "legal_reveal_audio"),
245            "the names"
246        );
247    }
248}