Skip to content

Commit

Permalink
moar tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Nov 26, 2024
1 parent b77837d commit c9934dd
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 48 deletions.
224 changes: 224 additions & 0 deletions crates/rules/src/matrix_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,230 @@ mod tests {
}
}

mod validation_for_addition_of_factor_source_for_each {
use super::*;

mod primary {

use super::*;

#[test]
fn empty() {
let sut = make();
let xs = sut
.validation_for_addition_of_factor_source_to_primary_threshold_for_each(
&IndexSet::new(),
);
assert_eq!(xs, IndexSet::new());
}

#[test]
fn device_threshold_3x_first_ok_second_not() {
let mut sut = make();
let xs = sut
.validation_for_addition_of_factor_source_to_primary_threshold_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device()
),
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
)
]
);

sut.add_factor_source_to_primary_threshold(FactorSourceID::sample_device())
.unwrap();

let xs = sut
.validation_for_addition_of_factor_source_to_primary_threshold_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);

pretty_assertions::assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device(),
ForeverInvalidReason::FactorSourceAlreadyPresent
),
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
ForeverInvalidReason::PrimaryCannotHaveMultipleDevices
),
]
);
}

#[test]
fn device_2x_threshold_override_first_ok_second_not() {
let mut sut = make();
let xs = sut
.validation_for_addition_of_factor_source_to_primary_threshold_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device()
),
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
)
]
);

sut.add_factor_source_to_primary_threshold(FactorSourceID::sample_device())
.unwrap();

let xs = sut.validation_for_addition_of_factor_source_to_primary_override_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);

pretty_assertions::assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device(),
ForeverInvalidReason::FactorSourceAlreadyPresent
),
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
ForeverInvalidReason::PrimaryCannotHaveMultipleDevices
),
]
);
}

#[test]
fn device_threshold_override_2x_first_ok_second_not() {
let mut sut = make();
let xs = sut
.validation_for_addition_of_factor_source_to_primary_threshold_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device()
),
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
)
]
);

sut.add_factor_source_to_primary_override(FactorSourceID::sample_device())
.unwrap();

let xs = sut.validation_for_addition_of_factor_source_to_primary_override_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);

pretty_assertions::assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device(),
ForeverInvalidReason::FactorSourceAlreadyPresent
),
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
ForeverInvalidReason::PrimaryCannotHaveMultipleDevices
),
]
);
}

#[test]
fn device_2x_override_threshold_first_ok_second_not() {
let mut sut = make();
let xs = sut.validation_for_addition_of_factor_source_to_primary_override_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device()
),
FactorSourceInRoleBuilderValidationStatus::ok(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
)
]
);

sut.add_factor_source_to_primary_override(FactorSourceID::sample_device())
.unwrap();

let xs = sut
.validation_for_addition_of_factor_source_to_primary_threshold_for_each(
&IndexSet::from_iter([
FactorSourceID::sample_device(),
FactorSourceID::sample_device_other(),
]),
);

pretty_assertions::assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device(),
ForeverInvalidReason::FactorSourceAlreadyPresent
),
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
sargon::RoleKind::Primary,
FactorSourceID::sample_device_other(),
ForeverInvalidReason::PrimaryCannotHaveMultipleDevices
),
]
);
}
}
}

mod validation_of_addition_of_kind {
use super::*;

Expand Down
122 changes: 74 additions & 48 deletions crates/rules/src/roles_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,51 @@ pub struct FactorSourceInRoleBuilderValidationStatus {
pub validation: RoleBuilderMutateResult,
}

impl FactorSourceInRoleBuilderValidationStatus {
pub(super) fn new(
role: RoleKind,
factor_source_id: FactorSourceID,
validation: RoleBuilderMutateResult,
) -> Self {
Self {
role,
factor_source_id,
validation,
}
}
}

#[cfg(test)]
impl FactorSourceInRoleBuilderValidationStatus {
pub(super) fn ok(role: RoleKind, factor_source_id: FactorSourceID) -> Self {
Self::new(role, factor_source_id, Ok(()))
}

pub(super) fn forever_invalid(
role: RoleKind,
factor_source_id: FactorSourceID,
reason: ForeverInvalidReason,
) -> Self {
Self::new(
role,
factor_source_id,
RoleBuilderMutateResult::forever_invalid(reason),
)
}

pub(super) fn not_yet_valid(
role: RoleKind,
factor_source_id: FactorSourceID,
reason: NotYetValidReason,
) -> Self {
Self::new(
role,
factor_source_id,
RoleBuilderMutateResult::not_yet_valid(reason),
)
}
}

pub type RoleBuilderMutateResult = Result<(), RoleBuilderValidation>;
pub type RoleBuilderBuildResult = Result<RoleWithFactorSourceIds, RoleBuilderValidation>;

Expand Down Expand Up @@ -398,11 +443,11 @@ pub trait BuilderOfFactorsInRole: Sized + private::UncheckedBuilderOfFactorsInRo
factor_source_id,
factor_list_kind,
);
FactorSourceInRoleBuilderValidationStatus {
role: self.role(),
factor_source_id: *factor_source_id,
validation: validation_status,
}
FactorSourceInRoleBuilderValidationStatus::new(
self.role(),
*factor_source_id,
validation_status,
)
})
.collect()
}
Expand Down Expand Up @@ -1070,16 +1115,11 @@ mod tests {
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Recovery,
factor_source_id: sample(),
validation: Ok(())
},
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Recovery,
factor_source_id: sample_other(),
validation: Ok(())
}
FactorSourceInRoleBuilderValidationStatus::ok(RoleKind::Recovery, sample()),
FactorSourceInRoleBuilderValidationStatus::ok(
RoleKind::Recovery,
sample_other(),
)
]
);
}
Expand Down Expand Up @@ -2111,23 +2151,19 @@ mod tests {
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Primary,
factor_source_id: fs0,
validation: Ok(())
},
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Primary,
factor_source_id: fs1,
validation: MutRes::not_yet_valid(
FactorSourceInRoleBuilderValidationStatus::ok(
RoleKind::Primary,
fs0,
),
FactorSourceInRoleBuilderValidationStatus::not_yet_valid(
RoleKind::Primary,
fs1,
NotYetValidReason::PrimaryRoleWithPasswordInThresholdListMustHaveAnotherFactor
)
},
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Primary,
factor_source_id: fs2,
validation: Ok(())
},
),
FactorSourceInRoleBuilderValidationStatus::ok(
RoleKind::Primary,
fs2,
),
]
);
_ = sut.add_factor_source_to_list(fs0, list());
Expand All @@ -2140,23 +2176,13 @@ mod tests {
assert_eq!(
xs.into_iter().collect::<Vec<_>>(),
vec![
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Primary,
factor_source_id: fs0,
validation: MutRes::forever_invalid(
ForeverInvalidReason::FactorSourceAlreadyPresent
)
},
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Primary,
factor_source_id: fs1,
validation: Ok(())
},
FactorSourceInRoleBuilderValidationStatus {
role: RoleKind::Primary,
factor_source_id: fs2,
validation: Ok(())
},
FactorSourceInRoleBuilderValidationStatus::forever_invalid(
RoleKind::Primary,
fs0,
ForeverInvalidReason::FactorSourceAlreadyPresent
),
FactorSourceInRoleBuilderValidationStatus::ok(RoleKind::Primary, fs1,),
FactorSourceInRoleBuilderValidationStatus::ok(RoleKind::Primary, fs2,),
]
);
}
Expand Down

0 comments on commit c9934dd

Please sign in to comment.