Skip to content

Commit

Permalink
chore: refactor update and delete tests (#255)
Browse files Browse the repository at this point in the history
* chore: refactor update

* chore: refactor subscriptions changed

* chore: refactor delete watch subscriptions changed

* fix: flaky test

* chore: refactor delete
  • Loading branch information
chris13524 authored Jan 5, 2024
1 parent 0ff5084 commit 0c2db96
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 255 deletions.
8 changes: 4 additions & 4 deletions src/services/websocket_server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ pub struct NotifySubscribe {

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct NotifyUpdate {
update_auth: String,
pub struct NotifyUpdate {
pub update_auth: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct NotifyDelete {
delete_auth: String,
pub struct NotifyDelete {
pub delete_auth: String,
}
4 changes: 4 additions & 0 deletions src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub const NOTIFY_SUBSCRIPTIONS_CHANGED_ACT: &str = "notify_subscriptions_changed
pub const NOTIFY_SUBSCRIPTIONS_CHANGED_RESPONE_ACT: &str = "notify_subscriptions_changed_response";
pub const NOTIFY_SUBSCRIBE_ACT: &str = "notify_subscription";
pub const NOTIFY_SUBSCRIBE_RESPONSE_ACT: &str = "notify_subscription_response";
pub const NOTIFY_UPDATE_ACT: &str = "notify_update";
pub const NOTIFY_UPDATE_RESPONSE_ACT: &str = "notify_update_response";
pub const NOTIFY_DELETE_ACT: &str = "notify_delete";
pub const NOTIFY_DELETE_RESPONSE_ACT: &str = "notify_delete_response";

#[cfg(test)]
mod tests {
Expand Down
28 changes: 28 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ pub fn parse_scope(scope: &str) -> std::result::Result<HashSet<Uuid>, uuid::Erro
Ok(parsed_scope)
}

pub fn encode_scope(notification_types: &HashSet<Uuid>) -> String {
notification_types
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(" ")
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -178,4 +186,24 @@ mod test {
HashSet::from([scope1, scope2])
);
}

#[test]
fn encode_empty_scope() {
assert_eq!(encode_scope(&HashSet::new()), "");
}

#[test]
fn encode_one_scope() {
let scope1 = Uuid::new_v4();
assert_eq!(encode_scope(&HashSet::from([scope1])), scope1.to_string());
}

#[test]
fn encode_two_scopes() {
let scope1 = Uuid::new_v4();
let scope2 = Uuid::new_v4();
let encoded = encode_scope(&HashSet::from([scope1, scope2]));
// need to check both orders because HashSet is non-deterministic
assert!(encoded == format!("{scope1} {scope2}") || encoded == format!("{scope2} {scope1}"));
}
}
14 changes: 3 additions & 11 deletions tests/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use {
NOTIFY_WATCH_SUBSCRIPTIONS_RESPONSE_TAG, NOTIFY_WATCH_SUBSCRIPTIONS_TAG,
NOTIFY_WATCH_SUBSCRIPTIONS_TTL,
},
types::{Envelope, EnvelopeType0, EnvelopeType1, Notification},
types::{encode_scope, Envelope, EnvelopeType0, EnvelopeType1, Notification},
utils::topic_from_key,
},
rand::{rngs::StdRng, SeedableRng},
Expand Down Expand Up @@ -493,11 +493,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
},
ksu: vars.keys_server_url.to_string(),
sub: did_pkh.clone(),
scp: notification_types
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(" "),
scp: encode_scope(&notification_types),
app: DidWeb::from_domain(app_domain.clone()),
};

Expand Down Expand Up @@ -766,11 +762,7 @@ async fn run_test(statement: String, watch_subscriptions_all_domains: bool) {
},
ksu: vars.keys_server_url.to_string(),
sub: did_pkh.clone(),
scp: notification_types
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(" "),
scp: encode_scope(&notification_types),
app: DidWeb::from_domain(app_domain.clone()),
};

Expand Down
Loading

0 comments on commit 0c2db96

Please sign in to comment.