rusmes-jmap 0.1.2

JMAP server for RusMES — RFC 8620/8621 HTTP/JSON mail API with Email, Mailbox, Thread, Blob, EventSource push, and VacationResponse support
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Per-method account ownership tests for JMAP.
//!
//! Every method handler that accepts an `accountId` argument MUST reject
//! requests where the requested account does not belong to the authenticated
//! [`Principal`] (RFC 8621 §1.6 + RFC 8620 §3.6 conventions). These tests
//! exercise representative handlers across the dispatch surface.

use rusmes_core::transport::NullMailTransport;
use rusmes_jmap::methods::identity::FileIdentityStore;
use rusmes_jmap::methods::submission::FileSubmissionStore;
use rusmes_jmap::methods::vacation::FileVacationStore;
use rusmes_jmap::methods::{
    email::email_get, email::email_query, email::email_set, email_advanced::email_changes,
    email_advanced::email_copy, email_advanced::email_import, email_advanced::email_parse,
    email_advanced::email_query_changes, identity::identity_changes, identity::identity_get,
    identity::identity_set, mailbox::mailbox_changes, mailbox::mailbox_get, mailbox::mailbox_query,
    mailbox::mailbox_query_changes, mailbox::mailbox_set, search_snippet::search_snippet_get,
    submission::email_submission_changes, submission::email_submission_get,
    submission::email_submission_query, submission::email_submission_set,
    submission::SubmissionContext, thread::thread_changes, thread::thread_get,
    vacation::vacation_response_get, vacation::vacation_response_set,
};
use rusmes_jmap::methods::{
    email_advanced::{
        EmailChangesRequest, EmailCopyObject, EmailCopyRequest, EmailImportObject,
        EmailImportRequest, EmailParseRequest, EmailQueryChangesRequest,
    },
    identity::{IdentityChangesRequest, IdentityGetRequest, IdentitySetRequest},
    mailbox::{
        MailboxChangesRequest, MailboxGetRequest, MailboxQueryChangesRequest, MailboxQueryRequest,
        MailboxSetRequest,
    },
    search_snippet::SearchSnippetGetRequest,
    submission::{
        EmailSubmissionChangesRequest, EmailSubmissionGetRequest, EmailSubmissionQueryRequest,
        EmailSubmissionSetRequest,
    },
    thread::{ThreadChangesRequest, ThreadGetRequest},
    vacation::{VacationResponseGetRequest, VacationResponseSetRequest},
};
use rusmes_jmap::types::{
    EmailGetRequest, EmailQueryRequest, EmailSetRequest, JmapErrorType, Principal,
};
use rusmes_jmap::BlobStorage;
use rusmes_storage::{backends::filesystem::FilesystemBackend, MessageStore, StorageBackend};
use std::collections::HashMap;
use std::sync::Arc;

fn store() -> Arc<dyn MessageStore> {
    let backend = FilesystemBackend::new(std::env::temp_dir().join("rusmes-jmap-binding-test"));
    backend.message_store()
}

fn empty_blobs() -> BlobStorage {
    BlobStorage::new()
}

fn identity_store() -> FileIdentityStore {
    FileIdentityStore::new(std::env::temp_dir().join("rusmes-jmap-binding-identity-test"))
}

fn vacation_store() -> FileVacationStore {
    FileVacationStore::new(std::env::temp_dir().join("rusmes-jmap-binding-vacation-test"))
}

fn alice() -> Principal {
    // Owns ONLY "account-alice" — no admin scope.
    Principal {
        username: "alice".to_string(),
        account_id: "account-alice".to_string(),
        scopes: vec![],
    }
}

/// Assert that a returned `anyhow::Error` carries the JMAP `forbidden` error
/// type. `forbidden_error` from `methods::mod` boxes a `ForbiddenError` whose
/// Display includes the canonical URN; checking the URN is the contract.
fn assert_forbidden(err: anyhow::Error) {
    let msg = err.to_string();
    assert!(
        msg.contains(JmapErrorType::Forbidden.as_str()),
        "expected forbidden error, got: {}",
        msg
    );
}

#[tokio::test]
async fn email_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailGetRequest {
        account_id: "account-bob".to_string(),
        ids: Some(vec![]),
        properties: None,
        body_properties: None,
        fetch_text_body_values: None,
        fetch_html_body_values: None,
        fetch_all_body_values: None,
        max_body_value_bytes: None,
    };
    let err = email_get(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_set_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailSetRequest {
        account_id: "account-bob".to_string(),
        if_in_state: None,
        create: None,
        update: None,
        destroy: None,
    };
    let err = email_set(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_query_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailQueryRequest {
        account_id: "account-bob".to_string(),
        filter: None,
        sort: None,
        position: None,
        anchor: None,
        anchor_offset: None,
        limit: None,
        calculate_total: None,
    };
    let err = email_query(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailChangesRequest {
        account_id: "account-bob".to_string(),
        since_state: "0".to_string(),
        max_changes: None,
    };
    let err = email_changes(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_query_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailQueryChangesRequest {
        account_id: "account-bob".to_string(),
        since_query_state: "0".to_string(),
        filter: None,
        sort: None,
        max_changes: None,
        up_to_id: None,
        calculate_total: None,
    };
    let err = email_query_changes(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_copy_rejects_foreign_destination() {
    let principal = alice();
    let store = store();
    let req = EmailCopyRequest {
        from_account_id: "account-alice".to_string(),
        account_id: "account-bob".to_string(),
        if_from_in_state: None,
        if_in_state: None,
        create: HashMap::new(),
        on_success_destroy_original: None,
        destroy_from_if_in_state: None,
    };
    let err = email_copy(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_copy_rejects_foreign_source() {
    let principal = alice();
    let store = store();
    let mut create = HashMap::new();
    create.insert(
        "c1".to_string(),
        EmailCopyObject {
            id: "msg1".to_string(),
            mailbox_ids: HashMap::new(),
            keywords: None,
            received_at: None,
        },
    );
    let req = EmailCopyRequest {
        from_account_id: "account-bob".to_string(),
        account_id: "account-alice".to_string(),
        if_from_in_state: None,
        if_in_state: None,
        create,
        on_success_destroy_original: None,
        destroy_from_if_in_state: None,
    };
    let err = email_copy(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_import_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let mut emails = HashMap::new();
    emails.insert(
        "i1".to_string(),
        EmailImportObject {
            blob_id: "blob".to_string(),
            mailbox_ids: HashMap::new(),
            keywords: None,
            received_at: None,
        },
    );
    let req = EmailImportRequest {
        account_id: "account-bob".to_string(),
        if_in_state: None,
        emails,
    };
    let err = email_import(req, store.as_ref(), &empty_blobs(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_parse_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailParseRequest {
        account_id: "account-bob".to_string(),
        blob_ids: vec![],
        properties: None,
        body_properties: None,
        fetch_text_body_values: None,
        fetch_html_body_values: None,
        fetch_all_body_values: None,
        max_body_value_bytes: None,
    };
    let err = email_parse(req, store.as_ref(), &empty_blobs(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn mailbox_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = MailboxGetRequest {
        account_id: "account-bob".to_string(),
        ids: None,
        properties: None,
    };
    let err = mailbox_get(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn mailbox_set_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = MailboxSetRequest {
        account_id: "account-bob".to_string(),
        if_in_state: None,
        create: None,
        update: None,
        destroy: None,
        on_destroy_remove_emails: None,
    };
    let err = mailbox_set(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn mailbox_query_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = MailboxQueryRequest {
        account_id: "account-bob".to_string(),
        filter: None,
        sort: None,
        position: None,
        limit: None,
        calculate_total: None,
    };
    let err = mailbox_query(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn mailbox_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = MailboxChangesRequest {
        account_id: "account-bob".to_string(),
        since_state: "0".to_string(),
        max_changes: None,
    };
    let err = mailbox_changes(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn mailbox_query_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = MailboxQueryChangesRequest {
        account_id: "account-bob".to_string(),
        since_query_state: "0".to_string(),
        filter: None,
        sort: None,
        max_changes: None,
        up_to_id: None,
        calculate_total: None,
    };
    let err = mailbox_query_changes(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn thread_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = ThreadGetRequest {
        account_id: "account-bob".to_string(),
        ids: None,
        properties: None,
    };
    let err = thread_get(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn thread_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = ThreadChangesRequest {
        account_id: "account-bob".to_string(),
        since_state: "0".to_string(),
        max_changes: None,
    };
    let err = thread_changes(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn search_snippet_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = SearchSnippetGetRequest {
        account_id: "account-bob".to_string(),
        email_ids: vec![],
        filter: None,
    };
    let err = search_snippet_get(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn identity_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let id_store = identity_store();
    let req = IdentityGetRequest {
        account_id: "account-bob".to_string(),
        ids: None,
        properties: None,
    };
    let err = identity_get(req, store.as_ref(), &id_store, &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn identity_set_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let id_store = identity_store();
    let req = IdentitySetRequest {
        account_id: "account-bob".to_string(),
        if_in_state: None,
        create: None,
        update: None,
        destroy: None,
    };
    let err = identity_set(req, store.as_ref(), &id_store, &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn identity_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let id_store = identity_store();
    let req = IdentityChangesRequest {
        account_id: "account-bob".to_string(),
        since_state: "0".to_string(),
        max_changes: None,
    };
    let err = identity_changes(req, store.as_ref(), &id_store, &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn vacation_response_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let vstore = vacation_store();
    let req = VacationResponseGetRequest {
        account_id: "account-bob".to_string(),
        ids: None,
        properties: None,
    };
    let err = vacation_response_get(req, store.as_ref(), &principal, &vstore)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn vacation_response_set_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let vstore = vacation_store();
    let req = VacationResponseSetRequest {
        account_id: "account-bob".to_string(),
        if_in_state: None,
        update: None,
    };
    let err = vacation_response_set(req, store.as_ref(), &principal, &vstore)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_submission_get_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailSubmissionGetRequest {
        account_id: "account-bob".to_string(),
        ids: None,
        properties: None,
    };
    let err = email_submission_get(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_submission_set_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailSubmissionSetRequest {
        account_id: "account-bob".to_string(),
        if_in_state: None,
        create: None,
        update: None,
        destroy: None,
        on_success_update_email: None,
        on_success_destroy_email: None,
    };
    let sstore = FileSubmissionStore::new(std::env::temp_dir().join("rusmes-jmap-binding-test"));
    let istore = FileIdentityStore::new(std::env::temp_dir().join("rusmes-jmap-binding-test"));
    let transport = NullMailTransport;
    let ctx = SubmissionContext {
        message_store: store.as_ref(),
        submission_store: &sstore,
        identity_store: &istore,
        mail_transport: &transport,
    };
    let err = email_submission_set(req, &principal, &ctx)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_submission_query_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailSubmissionQueryRequest {
        account_id: "account-bob".to_string(),
        filter: None,
        sort: None,
        position: None,
        limit: None,
        calculate_total: None,
    };
    let err = email_submission_query(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

#[tokio::test]
async fn email_submission_changes_rejects_foreign_account() {
    let principal = alice();
    let store = store();
    let req = EmailSubmissionChangesRequest {
        account_id: "account-bob".to_string(),
        since_state: "0".to_string(),
        max_changes: None,
    };
    let err = email_submission_changes(req, store.as_ref(), &principal)
        .await
        .expect_err("should reject");
    assert_forbidden(err);
}

// ---------------------------------------------------------------------------
// Positive tests — owning principal succeeds.
// ---------------------------------------------------------------------------

#[tokio::test]
async fn owning_principal_can_call_mailbox_get() {
    let principal = alice();
    let store = store();
    let req = MailboxGetRequest {
        account_id: "account-alice".to_string(),
        ids: None,
        properties: None,
    };
    let resp = mailbox_get(req, store.as_ref(), &principal)
        .await
        .expect("owning principal succeeds");
    assert_eq!(resp.account_id, "account-alice");
}