Skip to content

Commit

Permalink
Fix EndpointDidSov de/serialization, add tests
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 Dec 24, 2022
1 parent 79bddf6 commit 36509b3
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions aries_vcx/src/common/ledger/service_didsov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ pub const SERVICE_TYPE: &str = "IndyAgent";
// https://sovrin-foundation.github.io/sovrin/spec/did-method-spec-template.html
#[derive(Debug, Deserialize, Serialize, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(rename_all = "camelCase")]
pub struct EndpointDidSov {
pub endpoint: String,
#[serde(default)]
#[serde(rename = "camelCase")]
pub routing_keys: Option<Vec<String>>,
#[serde(default)]
pub types: Option<Vec<DidSovServiceType>>
pub types: Option<Vec<DidSovServiceType>>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
Expand All @@ -24,7 +24,7 @@ pub enum DidSovServiceType {
#[serde(rename = "DIDComm")] // DIDComm V2
DIDComm,
#[serde(other)]
Unknown
Unknown,
}

impl EndpointDidSov {
Expand All @@ -41,6 +41,11 @@ impl EndpointDidSov {
self.routing_keys = routing_keys;
self
}

pub fn set_types(mut self, types: Option<Vec<DidSovServiceType>>) -> Self {
self.types = types;
self
}
}

impl Default for EndpointDidSov {
Expand All @@ -57,7 +62,9 @@ impl Default for EndpointDidSov {
#[cfg(feature = "general_test")]
mod unit_tests {
use messages::diddoc::aries::diddoc::test_utils::{_routing_keys, _service_endpoint};
use crate::common::ledger::service_didsov::EndpointDidSov;

use crate::common::ledger::service_didsov::{DidSovServiceType, EndpointDidSov};
use crate::utils::devsetup::SetupEmpty;

#[test]
fn test_service_comparison() {
Expand All @@ -81,4 +88,45 @@ mod unit_tests {
assert_eq!(service1, service4);
assert_ne!(service1, service3);
}

#[test]
fn test_didsov_service_serialization() {
SetupEmpty::init();
let service1 = EndpointDidSov::create()
.set_service_endpoint(_service_endpoint())
.set_routing_keys(Some(_routing_keys()))
.set_types(Some(vec![DidSovServiceType::Endpoint, DidSovServiceType::DidCommunication, DidSovServiceType::DIDComm]));

let expected = json!({
"endpoint": "http://localhost:8080",
"routingKeys": [
"Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR",
"3LYuxJBJkngDbvJj4zjx13DBUdZ2P96eNybwd2n9L9AU"
],
"types": ["endpoint", "did-communication", "DIDComm"]
});
assert_eq!(expected, json!(&service1));
}

#[test]
fn test_didsov_service_deserialization() {
SetupEmpty::init();

let data = json!({
"endpoint": "http://localhost:8080",
"routingKeys": [
"Hezce2UWMZ3wUhVkh2LfKSs8nDzWwzs2Win7EzNN3YaR",
"3LYuxJBJkngDbvJj4zjx13DBUdZ2P96eNybwd2n9L9AU"
],
"types": ["endpoint", "did-communication", "DIDComm", "foobar"]
}).to_string();

let deserialized: EndpointDidSov = serde_json::from_str(&data).unwrap();
assert_eq!(deserialized.endpoint, _service_endpoint());
assert_eq!(deserialized.routing_keys, Some(_routing_keys()));
assert_eq!(deserialized.types, Some(vec![DidSovServiceType::Endpoint,
DidSovServiceType::DidCommunication,
DidSovServiceType::DIDComm,
DidSovServiceType::Unknown]));
}
}

0 comments on commit 36509b3

Please sign in to comment.