Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Mar 16, 2023
1 parent 63b9722 commit 3a3a0a7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl VerifierSM {
pub fn get_verification_status(&self) -> PresentationVerificationStatus {
match self.state {
VerifierFullState::Finished(ref state) => state.verification_status.clone(),
_ => PresentationVerificationStatus::Unavailable(),
_ => PresentationVerificationStatus::Unavailable,
}
}

Expand Down Expand Up @@ -769,7 +769,7 @@ pub mod unit_tests {

assert_match!(VerifierState::Failed, verifier_sm.get_state());
assert_match!(
PresentationVerificationStatus::Unavailable(),
PresentationVerificationStatus::Unavailable,
verifier_sm.get_verification_status()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ pub struct FinishedState {
pub presentation: Option<Presentation>,
pub status: Status,
#[serde(
default = "PresentationVerificationStatus::Unavailable",
deserialize_with = "null_to_unavailable"
default = "verification_status_unavailable", // todo: to be removed in 0.54.0, this supports legacy serialization when the field was 'null'
deserialize_with = "null_to_unavailable" // todo: to be removed in 0.54.0, this supports legacy serialization when the field was 'undefined'
)]
#[serde(alias = "revocation_status")]
// todo: to be removed in 0.54.0, this supports legacy serialization when the field was named 'revocation_status'
pub verification_status: PresentationVerificationStatus,
}

fn verification_status_unavailable() -> PresentationVerificationStatus {
PresentationVerificationStatus::Unavailable
}

// For backwards compatibility, if "revocation_status / verification_status" is null, we deserialize as Unavailable
fn null_to_unavailable<'de, D>(deserializer: D) -> Result<PresentationVerificationStatus, D::Error>
where
D: Deserializer<'de>,
{
let opt = Option::deserialize(deserializer)?;
Ok(opt.unwrap_or_else(PresentationVerificationStatus::Unavailable))
Ok(opt.unwrap_or(PresentationVerificationStatus::Unavailable))
}

impl FinishedState {
Expand All @@ -36,7 +41,7 @@ impl FinishedState {
presentation_request: None,
presentation: None,
status: Status::Declined(problem_report),
verification_status: PresentationVerificationStatus::Unavailable(),
verification_status: PresentationVerificationStatus::Unavailable,
}
}
}
Expand All @@ -51,15 +56,14 @@ pub mod unit_tests {
use messages::protocols::proof_presentation::presentation_request::test_utils::_presentation_request;
use messages::protocols::proof_presentation::test_utils::{_ack, _problem_report};

use crate::common::proofs::proof_request::test_utils::_presentation_request_data;
use crate::common::test_utils::mock_profile;
use crate::test::source_id;
use crate::utils::devsetup::{SetupEmpty, SetupMocks};

use super::*;

#[test]
fn test_verifier_state_finished_ser() {
fn test_verifier_state_finished_ser_deser_valid() {
let state = FinishedState {
presentation_request: None,
presentation: None,
Expand All @@ -70,6 +74,23 @@ pub mod unit_tests {
let expected =
r#"{"presentation_request":null,"presentation":null,"status":"Success","verification_status":"Valid"}"#;
assert_eq!(serialized, expected);
let deserialized: FinishedState = serde_json::from_str(&serialized).unwrap();
assert_eq!(state, deserialized)
}

#[test]
fn test_verifier_state_finished_ser_deser_unavailable() {
let state = FinishedState {
presentation_request: None,
presentation: None,
status: Status::Success,
verification_status: PresentationVerificationStatus::Unavailable,
};
let serialized = serde_json::to_string(&state).unwrap();
let expected = r#"{"presentation_request":null,"presentation":null,"status":"Success","verification_status":"Unavailable"}"#;
// assert_eq!(serialized, expected);
let deserialized: FinishedState = serde_json::from_str(&serialized).unwrap();
assert_eq!(state, deserialized)
}

#[test]
Expand All @@ -92,13 +113,13 @@ pub mod unit_tests {
}

#[test]
fn test_verifier_state_finished_deser_legacy() {
fn test_verifier_state_finished_deser_legacy_values_verification_status() {
{
let serialized = r#"{"presentation":null,"presentation_request":null,"status":"Success"}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(
deserialized.verification_status,
PresentationVerificationStatus::Unavailable()
PresentationVerificationStatus::Unavailable
)
}
{
Expand All @@ -107,7 +128,7 @@ pub mod unit_tests {
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(
deserialized.verification_status,
PresentationVerificationStatus::Unavailable()
PresentationVerificationStatus::Unavailable
)
}
{
Expand All @@ -126,14 +147,14 @@ pub mod unit_tests {
}

#[test]
fn test_verifier_state_finished_deser_legacy_2() {
fn test_verifier_state_finished_deser_legacy_values_revocation_status() {
{
let serialized =
r#"{"presentation":null,"presentation_request":null,"status":"Success","revocation_status":null}"#;
let deserialized: FinishedState = serde_json::from_str(serialized).unwrap();
assert_eq!(
deserialized.verification_status,
PresentationVerificationStatus::Unavailable()
PresentationVerificationStatus::Unavailable
)
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl From<(PresentationRequestSentState, ProblemReport)> for FinishedState {
presentation_request: Some(state.presentation_request),
presentation: None,
status: Status::Failed(problem_report),
verification_status: PresentationVerificationStatus::Unavailable(),
verification_status: PresentationVerificationStatus::Unavailable,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use serde::{Deserialize, Deserializer};
pub enum PresentationVerificationStatus {
Valid,
Invalid,
Unavailable(),
Unavailable,
}

// todo: to be removed in 0.54.0, this supports legacy serialization when the enum had values "Revoked" and "NotRevoked"
impl<'de> Deserialize<'de> for PresentationVerificationStatus {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand All @@ -16,8 +17,11 @@ impl<'de> Deserialize<'de> for PresentationVerificationStatus {
match s.as_str() {
"Valid" | "NonRevoked" => Ok(PresentationVerificationStatus::Valid),
"Invalid" | "Revoked" => Ok(PresentationVerificationStatus::Invalid),
"Unavailable" => Ok(PresentationVerificationStatus::Unavailable()),
_ => Err(serde::de::Error::custom(format!("unexpected value for Color: {}", s))),
"Unavailable" => Ok(PresentationVerificationStatus::Unavailable),
_ => Err(serde::de::Error::custom(format!(
"Unexpected value of PresentationVerificationStatus: {}",
s
))),
}
}
}
Expand Down Expand Up @@ -57,7 +61,7 @@ pub mod unit_tests {
serde_json::from_str("\"Revoked\"").unwrap()
);
assert_eq!(
PresentationVerificationStatus::Unavailable(),
PresentationVerificationStatus::Unavailable,
serde_json::from_str("\"Unavailable\"").unwrap()
);
}
Expand Down
2 changes: 1 addition & 1 deletion libvcx_core/src/api_vcx/api_handle/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl From<PresentationVerificationStatus> for VcxPresentationVerificationStatus
match verification_status {
PresentationVerificationStatus::Valid => VcxPresentationVerificationStatus::Valid,
PresentationVerificationStatus::Invalid => VcxPresentationVerificationStatus::Invalid,
PresentationVerificationStatus::Unavailable() => VcxPresentationVerificationStatus::Unavailable,
PresentationVerificationStatus::Unavailable => VcxPresentationVerificationStatus::Unavailable,
}
}
}
Expand Down

0 comments on commit 3a3a0a7

Please sign in to comment.