Skip to content

Commit 0c1364e

Browse files
committed
add more tests for builder
1 parent b4d69e6 commit 0c1364e

File tree

3 files changed

+214
-2
lines changed

3 files changed

+214
-2
lines changed

src/builder.rs

+212
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,215 @@ impl<H: Hasher> SdJwtBuilder<H> {
171171
Ok(SdJwt::new(jwt, disclosures, None))
172172
}
173173
}
174+
175+
#[cfg(test)]
176+
mod test {
177+
use serde_json::json;
178+
179+
use super::*;
180+
181+
mod marking_properties_as_concealable {
182+
use super::*;
183+
184+
mod that_exist {
185+
use super::*;
186+
187+
mod on_top_level {
188+
use super::*;
189+
190+
#[test]
191+
fn can_be_done_for_object_values() {
192+
let result = SdJwtBuilder::new(json!({ "address": {} }))
193+
.unwrap()
194+
.make_concealable("/address");
195+
196+
assert!(result.is_ok());
197+
}
198+
199+
#[test]
200+
fn can_be_done_for_array_elements() {
201+
let result = SdJwtBuilder::new(json!({ "nationalities": ["US", "DE"] }))
202+
.unwrap()
203+
.make_concealable("/nationalities");
204+
205+
assert!(result.is_ok());
206+
}
207+
}
208+
209+
mod as_subproperties {
210+
use super::*;
211+
212+
#[test]
213+
fn can_be_done_for_object_values() {
214+
let result = SdJwtBuilder::new(json!({ "address": { "country": "US" } }))
215+
.unwrap()
216+
.make_concealable("/address/country");
217+
218+
assert!(result.is_ok());
219+
}
220+
221+
#[test]
222+
fn can_be_done_for_array_elements() {
223+
let result = SdJwtBuilder::new(json!({
224+
"address": { "contact_person": [ "Jane Dow", "John Doe" ] }
225+
}))
226+
.unwrap()
227+
.make_concealable("/address/contact_person/0");
228+
229+
assert!(result.is_ok());
230+
}
231+
}
232+
}
233+
234+
mod that_do_not_exist {
235+
use super::*;
236+
mod on_top_level {
237+
use super::*;
238+
239+
#[test]
240+
fn returns_an_error_for_nonexistant_object_paths() {
241+
let result = SdJwtBuilder::new(json!({})).unwrap().make_concealable("/email");
242+
243+
assert_eq!(
244+
result.unwrap_err(),
245+
Error::InvalidPath("email does not exist".to_string()),
246+
);
247+
}
248+
249+
#[test]
250+
fn returns_an_error_for_nonexistant_array_paths() {
251+
let result = SdJwtBuilder::new(json!({}))
252+
.unwrap()
253+
.make_concealable("/nationalities/0");
254+
255+
assert_eq!(
256+
result.unwrap_err(),
257+
Error::InvalidPath("nationalities/0 does not exist".to_string()),
258+
);
259+
}
260+
261+
#[test]
262+
fn returns_an_error_for_nonexistant_array_entries() {
263+
let result = SdJwtBuilder::new(json!({
264+
"nationalities": ["US", "DE"]
265+
}))
266+
.unwrap()
267+
.make_concealable("/nationalities/2");
268+
269+
assert_eq!(
270+
result.unwrap_err(),
271+
Error::InvalidPath("nationalities/2 does not exist".to_string()),
272+
);
273+
}
274+
}
275+
276+
mod as_subproperties {
277+
use super::*;
278+
279+
#[test]
280+
fn returns_an_error_for_nonexistant_object_paths() {
281+
let result = SdJwtBuilder::new(json!({
282+
"address": {}
283+
}))
284+
.unwrap()
285+
.make_concealable("/address/region");
286+
287+
assert_eq!(
288+
result.unwrap_err(),
289+
Error::InvalidPath("address/region does not exist".to_string()),
290+
);
291+
}
292+
293+
#[test]
294+
fn returns_an_error_for_nonexistant_array_paths() {
295+
let result = SdJwtBuilder::new(json!({
296+
"address": {}
297+
}))
298+
.unwrap()
299+
.make_concealable("/address/contact_person/2");
300+
301+
assert_eq!(
302+
result.unwrap_err(),
303+
Error::InvalidPath("address/contact_person/2 does not exist".to_string()),
304+
);
305+
}
306+
307+
#[test]
308+
fn returns_an_error_for_nonexistant_array_entries() {
309+
let result = SdJwtBuilder::new(json!({
310+
"address": { "contact_person": [ "Jane Dow", "John Doe" ] }
311+
}))
312+
.unwrap()
313+
.make_concealable("/address/contact_person/2");
314+
315+
assert_eq!(
316+
result.unwrap_err(),
317+
Error::InvalidPath("address/contact_person/2 does not exist".to_string()),
318+
);
319+
}
320+
}
321+
}
322+
}
323+
324+
mod adding_decoys {
325+
use super::*;
326+
327+
mod on_top_level {
328+
use super::*;
329+
330+
#[test]
331+
fn can_add_zero_object_value_decoys_for_a_path() {
332+
let result = SdJwtBuilder::new(json!({})).unwrap().add_decoys("", 0);
333+
334+
assert!(result.is_ok());
335+
}
336+
337+
#[test]
338+
fn can_add_object_value_decoys_for_a_path() {
339+
let result = SdJwtBuilder::new(json!({})).unwrap().add_decoys("", 2);
340+
341+
assert!(result.is_ok());
342+
}
343+
}
344+
345+
mod for_subproperties {
346+
use super::*;
347+
348+
#[test]
349+
fn can_add_zero_object_value_decoys_for_a_path() {
350+
let result = SdJwtBuilder::new(json!({ "address": {} }))
351+
.unwrap()
352+
.add_decoys("/address", 0);
353+
354+
assert!(result.is_ok());
355+
}
356+
357+
#[test]
358+
fn can_add_object_value_decoys_for_a_path() {
359+
let result = SdJwtBuilder::new(json!({ "address": {} }))
360+
.unwrap()
361+
.add_decoys("/address", 2);
362+
363+
assert!(result.is_ok());
364+
}
365+
366+
#[test]
367+
fn can_add_zero_array_element_decoys_for_a_path() {
368+
let result = SdJwtBuilder::new(json!({ "nationalities": ["US", "DE"] }))
369+
.unwrap()
370+
.add_decoys("/nationalities", 0);
371+
372+
assert!(result.is_ok());
373+
}
374+
375+
#[test]
376+
fn can_add_array_element_decoys_for_a_path() {
377+
let result = SdJwtBuilder::new(json!({ "nationalities": ["US", "DE"] }))
378+
.unwrap()
379+
.add_decoys("/nationalities", 2);
380+
381+
assert!(result.is_ok());
382+
}
383+
}
384+
}
385+
}

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/// Alias for a `Result` with the error type [`Error`].
55
pub type Result<T> = ::core::result::Result<T, Error>;
66

7-
#[derive(Debug, thiserror::Error, strum::IntoStaticStr)]
7+
#[derive(Debug, thiserror::Error, strum::IntoStaticStr, PartialEq)]
88
#[non_exhaustive]
99
pub enum Error {
1010
#[error("invalid input: {0}")]

src/hasher.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait Hasher: Sync + Send {
3737
}
3838

3939
/// An implementation of [`Hasher`] that uses the `sha-256` hash function.
40-
#[derive(Default, Clone, Copy)]
40+
#[derive(Default, Clone, Copy, Debug)]
4141
#[cfg(feature = "sha")]
4242
pub struct Sha256Hasher;
4343

0 commit comments

Comments
 (0)