1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
//! Tests for state event authorization
//!
//! Verifies that state events are properly rejected when:
//! 1. No announcement exists for the repository
//! 2. Author is not in the maintainer set
mod common;
use common::relay::TestRelay;
use nostr_sdk::prelude::*;
#[tokio::test]
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();
// 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) => {
assert!(
!output.success.is_empty() || !output.failed.is_empty(),
"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"
);
}
Err(e) => {
// Also acceptable - relay rejected the event
assert!(
e.to_string().contains("no announcement exists")
|| e.to_string().contains("rejected"),
"Error should indicate missing announcement: {}",
e
);
}
}
relay.stop().await;
}
#[tokio::test]
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();
// 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 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"
);
}
Err(e) => {
assert!(
e.to_string().contains("not authorized") || e.to_string().contains("rejected"),
"Error should indicate unauthorized author: {}",
e
);
}
}
relay.stop().await;
}
#[tokio::test]
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();
// 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 result = client.send_event(&state_event).await;
// Should be accepted or go to purgatory (not permanently rejected)
match result {
Ok(output) => {
// Check that it wasn't permanently rejected
let permanently_rejected = output.failed.values().any(|err| {
let err_str = err.to_string();
err_str.contains("not authorized") || err_str.contains("no announcement exists")
});
assert!(
!permanently_rejected,
"Event should not be permanently rejected when author is authorized"
);
}
Err(e) => {
// Purgatory is acceptable
assert!(
e.to_string().contains("purgatory") || e.to_string().contains("waiting for git"),
"Error should be about purgatory, not authorization: {}",
e
);
}
}
relay.stop().await;
}
#[tokio::test]
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();
// 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 result = client.send_event(&state_event).await;
// Should be accepted or go to purgatory (not permanently rejected)
match result {
Ok(output) => {
let permanently_rejected = output.failed.values().any(|err| {
let err_str = err.to_string();
err_str.contains("not authorized") || err_str.contains("no announcement exists")
});
assert!(
!permanently_rejected,
"Event should not be permanently rejected when maintainer is authorized"
);
}
Err(e) => {
// Purgatory is acceptable
assert!(
e.to_string().contains("purgatory") || e.to_string().contains("waiting for git"),
"Error should be about purgatory, not authorization: {}",
e
);
}
}
relay.stop().await;
}
|