From b28a356cb41077ccee12a9c52f4ef2054e76cac6 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 9 Jan 2026 19:58:41 +0000 Subject: chore: cargo fmt --- tests/state_authorization.rs | 242 +++++++++++++++++++++---------------------- 1 file changed, 119 insertions(+), 123 deletions(-) (limited to 'tests/state_authorization.rs') diff --git a/tests/state_authorization.rs b/tests/state_authorization.rs index a5dfa2d..d443005 100644 --- a/tests/state_authorization.rs +++ b/tests/state_authorization.rs @@ -13,30 +13,27 @@ use nostr_sdk::prelude::*; async fn test_reject_state_without_announcement() { // Start test relay let relay = TestRelay::start().await; - + // Create test keypair let keys = Keys::generate(); - + // Create a state event without any announcement - let state_event = EventBuilder::new( - Kind::RepoState, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), - ]) - .sign_with_keys(&keys) - .unwrap(); - + let state_event = EventBuilder::new(Kind::RepoState, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), + ]) + .sign_with_keys(&keys) + .unwrap(); + // Connect to relay let client = Client::default(); client.add_relay(relay.url()).await.unwrap(); client.connect().await; - + // Try to send state event let result = client.send_event(&state_event).await; - + // Should be rejected match result { Ok(output) => { @@ -45,22 +42,26 @@ async fn test_reject_state_without_announcement() { "Event should be processed" ); // Check if any relay rejected it - let rejected = output.failed.values().any(|err| { - err.to_string().contains("no announcement exists") - }); - assert!(rejected, "Event should be rejected due to missing announcement"); + let rejected = output + .failed + .values() + .any(|err| err.to_string().contains("no announcement exists")); + assert!( + rejected, + "Event should be rejected due to missing announcement" + ); } Err(e) => { // Also acceptable - relay rejected the event assert!( - e.to_string().contains("no announcement exists") || - e.to_string().contains("rejected"), + e.to_string().contains("no announcement exists") + || e.to_string().contains("rejected"), "Error should indicate missing announcement: {}", e ); } } - + relay.stop().await; } @@ -68,67 +69,67 @@ async fn test_reject_state_without_announcement() { async fn test_reject_state_from_unauthorized_author() { // Start test relay let relay = TestRelay::start().await; - + // Create two keypairs: one for announcement, one for unauthorized state let announcement_keys = Keys::generate(); let unauthorized_keys = Keys::generate(); - + // Create announcement - let announcement = EventBuilder::new( - Kind::GitRepoAnnouncement, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("clone"), [format!("https://{}/test.git", relay.domain())]), - Tag::custom(TagKind::custom("relays"), [relay.url()]), - ]) - .sign_with_keys(&announcement_keys) - .unwrap(); - + let announcement = EventBuilder::new(Kind::GitRepoAnnouncement, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom( + TagKind::custom("clone"), + [format!("https://{}/test.git", relay.domain())], + ), + Tag::custom(TagKind::custom("relays"), [relay.url()]), + ]) + .sign_with_keys(&announcement_keys) + .unwrap(); + // Connect to relay let client = Client::default(); client.add_relay(relay.url()).await.unwrap(); client.connect().await; - + // Send announcement client.send_event(&announcement).await.unwrap(); - + // Wait for announcement to be processed tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; - + // Try to send state event from unauthorized author - let state_event = EventBuilder::new( - Kind::RepoState, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), - ]) - .sign_with_keys(&unauthorized_keys) - .unwrap(); - + let state_event = EventBuilder::new(Kind::RepoState, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), + ]) + .sign_with_keys(&unauthorized_keys) + .unwrap(); + let result = client.send_event(&state_event).await; - + // Should be rejected match result { Ok(output) => { - let rejected = output.failed.values().any(|err| { - err.to_string().contains("not authorized") - }); - assert!(rejected, "Event should be rejected due to unauthorized author"); + let rejected = output + .failed + .values() + .any(|err| err.to_string().contains("not authorized")); + assert!( + rejected, + "Event should be rejected due to unauthorized author" + ); } Err(e) => { assert!( - e.to_string().contains("not authorized") || - e.to_string().contains("rejected"), + e.to_string().contains("not authorized") || e.to_string().contains("rejected"), "Error should indicate unauthorized author: {}", e ); } } - + relay.stop().await; } @@ -136,48 +137,45 @@ async fn test_reject_state_from_unauthorized_author() { async fn test_accept_state_from_announcement_author() { // Start test relay let relay = TestRelay::start().await; - + // Create keypair let keys = Keys::generate(); - + // Create announcement - let announcement = EventBuilder::new( - Kind::GitRepoAnnouncement, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("clone"), [format!("https://{}/test.git", relay.domain())]), - Tag::custom(TagKind::custom("relays"), [relay.url()]), - ]) - .sign_with_keys(&keys) - .unwrap(); - + let announcement = EventBuilder::new(Kind::GitRepoAnnouncement, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom( + TagKind::custom("clone"), + [format!("https://{}/test.git", relay.domain())], + ), + Tag::custom(TagKind::custom("relays"), [relay.url()]), + ]) + .sign_with_keys(&keys) + .unwrap(); + // Connect to relay let client = Client::default(); client.add_relay(relay.url()).await.unwrap(); client.connect().await; - + // Send announcement client.send_event(&announcement).await.unwrap(); - + // Wait for announcement to be processed tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; - + // Send state event from same author (should be accepted or go to purgatory) - let state_event = EventBuilder::new( - Kind::RepoState, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), - ]) - .sign_with_keys(&keys) - .unwrap(); - + let state_event = EventBuilder::new(Kind::RepoState, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), + ]) + .sign_with_keys(&keys) + .unwrap(); + let result = client.send_event(&state_event).await; - + // Should be accepted or go to purgatory (not permanently rejected) match result { Ok(output) => { @@ -194,14 +192,13 @@ async fn test_accept_state_from_announcement_author() { Err(e) => { // Purgatory is acceptable assert!( - e.to_string().contains("purgatory") || - e.to_string().contains("waiting for git"), + e.to_string().contains("purgatory") || e.to_string().contains("waiting for git"), "Error should be about purgatory, not authorization: {}", e ); } } - + relay.stop().await; } @@ -209,50 +206,50 @@ async fn test_accept_state_from_announcement_author() { async fn test_accept_state_from_maintainer() { // Start test relay let relay = TestRelay::start().await; - + // Create two keypairs: owner and maintainer let owner_keys = Keys::generate(); let maintainer_keys = Keys::generate(); - + // Create announcement with maintainer - let announcement = EventBuilder::new( - Kind::GitRepoAnnouncement, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("clone"), [format!("https://{}/test.git", relay.domain())]), - Tag::custom(TagKind::custom("relays"), [relay.url()]), - Tag::custom(TagKind::custom("maintainers"), [maintainer_keys.public_key().to_hex()]), - ]) - .sign_with_keys(&owner_keys) - .unwrap(); - + let announcement = EventBuilder::new(Kind::GitRepoAnnouncement, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom( + TagKind::custom("clone"), + [format!("https://{}/test.git", relay.domain())], + ), + Tag::custom(TagKind::custom("relays"), [relay.url()]), + Tag::custom( + TagKind::custom("maintainers"), + [maintainer_keys.public_key().to_hex()], + ), + ]) + .sign_with_keys(&owner_keys) + .unwrap(); + // Connect to relay let client = Client::default(); client.add_relay(relay.url()).await.unwrap(); client.connect().await; - + // Send announcement client.send_event(&announcement).await.unwrap(); - + // Wait for announcement to be processed tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; - + // Send state event from maintainer - let state_event = EventBuilder::new( - Kind::RepoState, - "", - ) - .tags([ - Tag::custom(TagKind::custom("d"), ["test-repo"]), - Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), - ]) - .sign_with_keys(&maintainer_keys) - .unwrap(); - + let state_event = EventBuilder::new(Kind::RepoState, "") + .tags([ + Tag::custom(TagKind::custom("d"), ["test-repo"]), + Tag::custom(TagKind::custom("refs/heads/main"), ["abc123"]), + ]) + .sign_with_keys(&maintainer_keys) + .unwrap(); + let result = client.send_event(&state_event).await; - + // Should be accepted or go to purgatory (not permanently rejected) match result { Ok(output) => { @@ -268,13 +265,12 @@ async fn test_accept_state_from_maintainer() { Err(e) => { // Purgatory is acceptable assert!( - e.to_string().contains("purgatory") || - e.to_string().contains("waiting for git"), + e.to_string().contains("purgatory") || e.to_string().contains("waiting for git"), "Error should be about purgatory, not authorization: {}", e ); } } - + relay.stop().await; } -- cgit v1.2.3