Skip to content

Commit

Permalink
test(messages): Add first round of tests, accompanying fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Naian <[email protected]>
  • Loading branch information
nain-F49FF806 committed Oct 18, 2023
1 parent 50cba2b commit fc9a27c
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 1 deletion.
27 changes: 27 additions & 0 deletions messages/src/msg_fields/protocols/pickup/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,34 @@ pub struct Transport {
#[derive(Clone, Debug, Deserialize, Serialize, Default, PartialEq)]
pub enum ReturnRoute {
#[default]
#[serde(rename = "none")]
None,
#[serde(rename = "all")]
All,
#[serde(rename = "thread")]
Thread,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::misc::test_utils;

#[test]
fn test_transport_decorator() {
let transport = Transport::builder().return_route(ReturnRoute::All).build();
let decorators = PickupDecoratorsCommon::builder()
.transport(transport)
.build();
let expected = json!({
"~transport": {
"return_route": "all"
}
});
test_utils::test_serde(decorators, expected);
}
}
48 changes: 48 additions & 0 deletions messages/src/msg_fields/protocols/pickup/delivery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,51 @@ pub struct DeliveryAttachData {
#[serde_as(as = "Base64")]
pub base64: Vec<u8>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{
decorators::thread::Thread, misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0,
};
#[test]
fn test_delivery() {
let expected = json!(
{
"@id": "123456781",
"~thread": {
"thid": "<message id of delivery-request message>"
},
"@type": "https://didcomm.org/messagepickup/2.0/delivery",
"recipient_key": "<key for messages>",
"~attach": [{
"@id": "<messageid>",
"data": {
"base64": ""
}
}]
}
);
let attach = DeliveryAttach::builder()
.id("<messageid>".to_owned())
.data(DeliveryAttachData::builder().base64("".into()).build())
.build();
let content = DeliveryContent::builder()
.recipient_key("<key for messages>".to_owned())
.attach(vec![attach])
.build();
let decorators = PickupDecoratorsCommon::builder()
.thread(
Thread::builder()
.thid("<message id of delivery-request message>".to_owned())
.build(),
)
.build();

test_utils::test_msg(content, decorators, PickupTypeV2_0::Delivery, expected);
}
}
33 changes: 33 additions & 0 deletions messages/src/msg_fields/protocols/pickup/delivery_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,36 @@ pub struct DeliveryRequestContent {
#[serde(skip_serializing_if = "Option::is_none")]
pub recipient_key: Option<String>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};
#[test]
fn test_delivery_request() {
let expected = json!(
{
"@id": "123456781",
"@type": "https://didcomm.org/messagepickup/2.0/delivery-request",
"limit": 10,
"recipient_key": "<key for messages>"
}
);
let content = DeliveryRequestContent::builder()
.recipient_key("<key for messages>".to_owned())
.limit(10)
.build();
let decorators = PickupDecoratorsCommon::builder().build();

test_utils::test_msg(
content,
decorators,
PickupTypeV2_0::DeliveryRequest,
expected,
);
}
}
30 changes: 30 additions & 0 deletions messages/src/msg_fields/protocols/pickup/live_delivery_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,33 @@ pub type LiveDeliveryChange = MsgParts<LiveDeliveryChangeContent, PickupDecorato
pub struct LiveDeliveryChangeContent {
pub live_delivery: bool,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};
#[test]
fn test_live_delivery_change() {
let expected = json!(
{
"@type": "https://didcomm.org/messagepickup/2.0/live-delivery-change",
"live_delivery": true
}
);
let content = LiveDeliveryChangeContent::builder()
.live_delivery(true)
.build();
let decorators = PickupDecoratorsCommon::builder().build();

test_utils::test_msg(
content,
decorators,
PickupTypeV2_0::LiveDeliveryChange,
expected,
);
}
}
31 changes: 31 additions & 0 deletions messages/src/msg_fields/protocols/pickup/messages_received.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,34 @@ pub type MessagesReceived = MsgParts<MessagesReceivedContent, PickupDecoratorsCo
pub struct MessagesReceivedContent {
pub message_id_list: Vec<String>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};
#[test]
fn test_messages_received() {
let expected = json!(
{
"@type": "https://didcomm.org/messagepickup/2.0/messages-received",
"message_id_list": ["123","456"]
}

);
let content = MessagesReceivedContent::builder()
.message_id_list(vec!["123".to_string(), "456".to_string()])
.build();
let decorators = PickupDecoratorsCommon::builder().build();

test_utils::test_msg(
content,
decorators,
PickupTypeV2_0::MessagesReceived,
expected,
);
}
}
14 changes: 13 additions & 1 deletion messages/src/msg_fields/protocols/pickup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,20 @@ impl DelayedSerde for Pickup {
};

match kind.map_err(D::Error::custom)? {
PickupTypeV2_0::StatusRequest => {
StatusRequest::deserialize(deserializer).map(From::from)
}
PickupTypeV2_0::Status => Status::deserialize(deserializer).map(From::from),
_ => todo!(),
PickupTypeV2_0::DeliveryRequest => {
DeliveryRequest::deserialize(deserializer).map(From::from)
}
PickupTypeV2_0::Delivery => Delivery::deserialize(deserializer).map(From::from),
PickupTypeV2_0::MessagesReceived => {
MessagesReceived::deserialize(deserializer).map(From::from)
}
PickupTypeV2_0::LiveDeliveryChange => {
LiveDeliveryChange::deserialize(deserializer).map(From::from)
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions messages/src/msg_fields/protocols/pickup/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,32 @@ pub struct StatusContent {
#[serde(skip_serializing_if = "Option::is_none")]
pub recipient_key: Option<String>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};

#[test]
fn test_status() {
let expected = json!(
{
"@id": "123456781",
"@type": "https://didcomm.org/messagepickup/2.0/status",
"recipient_key": "<key for messages>",
"message_count": 7,
}
);
let content = StatusContent::builder()
.recipient_key("<key for messages>".to_owned())
.message_count(7)
.build();
let decorators = PickupDecoratorsCommon::builder().build();

test_utils::test_msg(content, decorators, PickupTypeV2_0::Status, expected);
}
}
27 changes: 27 additions & 0 deletions messages/src/msg_fields/protocols/pickup/status_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,30 @@ pub struct StatusRequestContent {
#[serde(skip_serializing_if = "Option::is_none")]
pub recipient_key: Option<String>,
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
use serde_json::json;

use super::*;
use crate::{misc::test_utils, msg_types::protocols::pickup::PickupTypeV2_0};

#[test]
fn test_status_request() {
let expected = json!(
{
"@id": "123456781",
"@type": "https://didcomm.org/messagepickup/2.0/status-request",
"recipient_key": "<key for messages>"
}
);
let content = StatusRequestContent::builder()
.recipient_key("<key for messages>".to_owned())
.build();
let decorators = PickupDecoratorsCommon::builder().build();

test_utils::test_msg(content, decorators, PickupTypeV2_0::StatusRequest, expected);
}
}

0 comments on commit fc9a27c

Please sign in to comment.