dittolive-ditto 4.2.2

Ditto is a peer to peer cross-platform database that allows mobile, web, IoT and server apps to sync with or without an internet connection.
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
mod common;

use std::{
    sync::{
        atomic::{AtomicU8, Ordering},
        mpsc, Arc,
    },
    time::Duration,
};

use common::*;

// These tests are all ignored by default because they involve authenticating with the Ditto Cloud.
// Find and remove all of the #[ignore = "Auth tests disabled by default"] attributes to run them.

/// App ID from the Ditto Cloud Portal. Must be a valid UUID.
static APP_ID: &str = "6eff5920-8ab1-43f4-8803-8187f0d389fe";

/// Provider to use when attempting a login.
static PROVIDER: &str = "portal";

/// Token from "Playground Mode Details" section on Settings page for the app ID above.
static PLAYGROUND_TOKEN: &str = "3f03f821-9f0a-4424-8186-3aaaaa23b05c";

/// JWT token scraped from portal request to endpoint:
/// `https://<appID>.cloud.ditto.live/_ditto/auth/login`
static LOGIN_TOKEN: &str = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik9UZEJSVFJDUVRSRU1qTTJNakpHTlVORU1UUTVPRVU0TXpoRU1qazVRa0k0UkRRMk5EVTJNZyJ9.eyJpc3MiOiJodHRwczovL2RpdHRvLmF1dGgwLmNvbS8iLCJzdWIiOiJnb29nbGUtb2F1dGgyfDEwNDc5ODQ2OTkzNzk4NTQ5MjY5MiIsImF1ZCI6WyJodHRwczovL3BvcnRhbGFwaS5kaXR0by5saXZlIiwiaHR0cHM6Ly9kaXR0by5hdXRoMC5jb20vdXNlcmluZm8iXSwiaWF0IjoxNjgwNTEzNzE2LCJleHAiOjE2ODA2MDAxMTYsImF6cCI6ImVnTjNMRU9OZVUyR1hzemZ2RVkwc1NTbWM5RzcyZHB5Iiwic2NvcGUiOiJvcGVuaWQgcHJvZmlsZSBlbWFpbCJ9.lkvRobdxCHcjze9OvOTHfdeMEdvz4lhpi6kRYQSq3vhs6rznzarbsivefzHaPKiwoZPRgaRf-EO7BRBbpIz6fOFlKhFmMdNWOqhqNr4STRmC8K4nXx1CM1us8LRu5AZlzm8HjszHIpzHlNfneEXXV7QvDLAZnN_SoEh-Sc_Ue9_16UxSsZna6qaE7q-ZTaz7MBoDYi-B0PLn79t2VuzrsrQfsyhdj7Hoy7hXG1CzrLAlM0RXQMXHtZDELiDVftaqnYvc2EuuPbY496-UBSiGGHxRrHucHrzPPqgzyWprzcOfsUyCqWb3mbiS0GYkWOBx2fKpppQZpvpNstEUk-vvCQ";

/// An expired token for testing auth errors.
static EXPIRED_LOGIN_TOKEN: &str = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik9UZEJSVFJDUVRSRU1qTTJNakpHTlVORU1UUTVPRVU0TXpoRU1qazVRa0k0UkRRMk5EVTJNZyJ9.eyJpc3MiOiJodHRwczovL2RpdHRvLmF1dGgwLmNvbS8iLCJzdWIiOiJhdXRoMHw2MTU1NDhkYjE4MGUxNjAwNmFmNWE3NTkiLCJhdWQiOlsiaHR0cHM6Ly9wb3J0YWxhcGkuZGl0dG8ubGl2ZSIsImh0dHBzOi8vZGl0dG8uYXV0aDAuY29tL3VzZXJpbmZvIl0sImlhdCI6MTY0MjQ0MTA3NywiZXhwIjoxNjQyNTI3NDc3LCJhenAiOiJlZ04zTEVPTmVVMkdYc3pmdkVZMHNTU21jOUc3MmRweSIsInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwifQ.mCQC3pILToK9MhZXsoe3DCw4f1uD0KEpfj2i7yRCrt6R81-7yWUZb7F5OXDGkVApX4FzI12w13L_Bzk6Nx1NxJVFa_8Tmb40Fthy5UW7JjANL0itEIBM15QH_rb9Z1YVXfnSHXn3rTRaqhZeRyPZjoIMeP43WB1mDzg-JLPsxw_BHhmTeS1kkvcDATxyT21H2IyE4xZXwQHwxpiXRv9UaE_rtP4qL13oHT46wm3Isa6rXT2wJmPoq9eXFkdZXvvNJgE_xCeXlAz_GURfNsnJQn0BExzzI8k3Ti66ac-q-48vXbzv9zESfWiJQnsHXfFAbAn2pQacXThb9NYKR-hPPQ";

#[ignore = "Auth tests disabled by default"]
#[test]
fn login_with_token_logs_in_given_a_valid_token() {
    let (auth_required_tx, auth_required_rx) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());

    auth_required_rx.recv().unwrap();

    let res = ditto
        .authenticator()
        .unwrap()
        .login_with_token_and_feedback(LOGIN_TOKEN, PROVIDER);
    assert!(res.is_ok());

    assert!(ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_some());
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn login_with_token_fails_if_token_is_expired() {
    let (auth_required_tx, auth_required_rx) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());

    auth_required_rx.recv().unwrap();

    let res = ditto
        .authenticator()
        .unwrap()
        .login_with_token_and_feedback(EXPIRED_LOGIN_TOKEN, PROVIDER);
    assert!(res.is_err());

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn login_with_token_fails_if_token_is_invalid() {
    let (auth_required_tx, auth_required_rx) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());

    auth_required_rx.recv().unwrap();

    let res = ditto
        .authenticator()
        .unwrap()
        .login_with_token_and_feedback("an_invalid_token", PROVIDER);
    assert!(res.is_err());

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn login_with_token_fails_if_provider_is_invalid() {
    let (auth_required_tx, auth_required_rx) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());

    auth_required_rx.recv().unwrap();

    let res = ditto
        .authenticator()
        .unwrap()
        .login_with_token_and_feedback(LOGIN_TOKEN, "invalid_provider");
    assert!(res.is_err());

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn logout() {
    let (auth_required_tx, auth_required_rx) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());

    auth_required_rx.recv().unwrap();

    let res = ditto
        .authenticator()
        .unwrap()
        .login_with_token_and_feedback(LOGIN_TOKEN, PROVIDER);
    assert!(res.is_ok());

    assert!(ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_some());

    let cleanup_block_times_called = Arc::new(AtomicU8::new(0));
    let cleanup_block_times_called_1 = cleanup_block_times_called.clone();
    let res = ditto.authenticator().unwrap().logout(|_| {
        cleanup_block_times_called_1.fetch_add(1, Ordering::SeqCst);
    });
    assert!(res.is_ok());
    assert_eq!(cleanup_block_times_called.load(Ordering::SeqCst), 1);

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn logout_calls_cleanup_block_even_if_not_logged_in() {
    let (auth_required_tx, _) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(!ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_none());

    let cleanup_block_times_called = Arc::new(AtomicU8::new(0));
    let cleanup_block_times_called_1 = cleanup_block_times_called.clone();
    let res = ditto.authenticator().unwrap().logout(|_| {
        cleanup_block_times_called_1.fetch_add(1, Ordering::SeqCst);
    });
    assert!(res.is_ok());
    assert_eq!(cleanup_block_times_called.load(Ordering::SeqCst), 1);
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn status_is_authenticated_when_starting_an_instance_reusing_an_authenticated_directory() {
    let ditto_dir = tempfile::tempdir().unwrap();
    let ditto_dir_path = ditto_dir.path();
    {
        let (auth_required_tx, auth_required_rx) = mpsc::sync_channel::<()>(1);
        let auth_event_handler = AuthEventHandler::new(auth_required_tx);
        let ditto = Ditto::builder()
            .with_root(Arc::new(PersistentRoot::new(ditto_dir_path).unwrap()))
            .with_identity(|ditto_root| {
                identity::OnlineWithAuthentication::new(
                    ditto_root,
                    AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                    auth_event_handler.clone(),
                    true,
                    None,
                )
            })
            .unwrap()
            .with_minimum_log_level(LogLevel::Error)
            .build()
            .unwrap();

        assert!(!ditto.authenticator().unwrap().is_authenticated());
        assert!(ditto.authenticator().unwrap().user_id().is_none());

        auth_required_rx.recv().unwrap();

        let res = ditto
            .authenticator()
            .unwrap()
            .login_with_token_and_feedback(LOGIN_TOKEN, PROVIDER);
        assert!(res.is_ok());

        assert!(ditto.authenticator().unwrap().is_authenticated());
        assert!(ditto.authenticator().unwrap().user_id().is_some());
    }

    // We've got a new `AuthEventHandler` so the `authentication_required_times_called` counter is
    // reset
    let (auth_required_tx, _) = mpsc::sync_channel::<()>(1);
    let auth_event_handler = AuthEventHandler::new(auth_required_tx);
    let ditto = Ditto::builder()
        .with_root(Arc::new(PersistentRoot::new(ditto_dir_path).unwrap()))
        .with_identity(|ditto_root| {
            identity::OnlineWithAuthentication::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                auth_event_handler.clone(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    assert!(ditto.authenticator().unwrap().is_authenticated());
    assert!(ditto.authenticator().unwrap().user_id().is_some());
    assert_eq!(
        auth_event_handler
            .authentication_required_times_called
            .load(Ordering::SeqCst),
        0
    );
}

#[ignore = "Auth tests disabled by default"]
#[test]
fn online_playground_authenticates_and_syncs() {
    let ditto1 = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlinePlayground::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                PLAYGROUND_TOKEN.to_string(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    let ditto2 = Ditto::builder()
        .with_temp_dir()
        .with_identity(|ditto_root| {
            identity::OnlinePlayground::new(
                ditto_root,
                AppId::from_uuid(uuid::Uuid::parse_str(APP_ID).unwrap()),
                PLAYGROUND_TOKEN.to_string(),
                true,
                None,
            )
        })
        .unwrap()
        .with_minimum_log_level(LogLevel::Error)
        .build()
        .unwrap();

    let unique_collection_name = uuid::Uuid::new_v4().to_string();

    let collection1 = ditto1
        .store()
        .collection(unique_collection_name.as_str())
        .unwrap();
    let collection2 = ditto2
        .store()
        .collection(unique_collection_name.as_str())
        .unwrap();

    assert!(ditto1.start_sync().is_ok());
    assert!(ditto2.start_sync().is_ok());

    let now = std::time::Instant::now();
    while now.elapsed() < std::time::Duration::from_secs(10) {
        if ditto1.authenticator().unwrap().is_authenticated()
            && ditto2.authenticator().unwrap().is_authenticated()
        {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }

    assert!(
        ditto1.authenticator().unwrap().is_authenticated()
            && ditto2.authenticator().unwrap().is_authenticated(),
        "at least one of the Ditto instances failed to authenticate"
    );

    let subscription1 = collection1.find_all().subscribe();
    let subscription2 = collection2.find_all().subscribe();

    let yellow_car_id_string = uuid::Uuid::new_v4().to_string();
    let yellow_car_doc_id = DocumentId::new(&yellow_car_id_string.as_str()).unwrap();

    assert!(collection1
        .find_by_id(yellow_car_doc_id.clone())
        .exec()
        .is_err());
    assert!(collection2
        .find_by_id(yellow_car_doc_id.clone())
        .exec()
        .is_err());

    let _doc_id = collection1
        .upsert(serde_json::json!({"color": "yellow", "_id": yellow_car_doc_id}))
        .unwrap();

    let found_doc = collection1
        .find_by_id(yellow_car_doc_id.clone())
        .exec()
        .unwrap();
    assert_eq!(found_doc.get::<String>("color").unwrap(), "yellow");

    let (tx, rx) = mpsc::sync_channel(1);

    let handler = move |doc: Option<BoxedDocument>, _event: SingleDocumentLiveQueryEvent| {
        if doc.is_some() {
            tx.send(()).unwrap();
        }
    };
    let car_at_ditto2_live_query = collection2
        .find_by_id(yellow_car_doc_id.clone())
        .observe_local(handler)
        .unwrap();

    rx.recv_timeout(Duration::from_secs(10)).unwrap();

    let found_doc = collection2.find_by_id(yellow_car_doc_id).exec().unwrap();
    assert_eq!(found_doc.get::<String>("color").unwrap(), "yellow");

    subscription1.stop();
    subscription2.stop();
    car_at_ditto2_live_query.stop();
}

#[derive(Clone)]
pub struct AuthEventHandler {
    /// Count of how many times `authentication_required` has been called
    pub authentication_required_times_called: Arc<AtomicU8>,

    /// Count of how many times `authentication_expiring_soon` has been called
    pub authentication_expiring_soon_times_called: Arc<AtomicU8>,

    /// A sync_channel sender to send a message to when there has been a call 1to
    /// `authentication_required`
    auth_required_tx: Arc<mpsc::SyncSender<()>>,
}

impl AuthEventHandler {
    pub fn new(auth_required_tx: mpsc::SyncSender<()>) -> Self {
        Self {
            authentication_required_times_called: Arc::new(AtomicU8::new(0)),
            authentication_expiring_soon_times_called: Arc::new(AtomicU8::new(0)),
            auth_required_tx: Arc::new(auth_required_tx),
        }
    }
}

impl DittoAuthenticationEventHandler for AuthEventHandler {
    fn authentication_required(&self, _auth: DittoAuthenticator) {
        self.authentication_required_times_called
            .fetch_add(1, Ordering::SeqCst);
        self.auth_required_tx.send(()).unwrap();
    }

    fn authentication_expiring_soon(
        &self,
        _auth: DittoAuthenticator,
        _seconds_remaining: Duration,
    ) {
        self.authentication_expiring_soon_times_called
            .fetch_add(1, Ordering::SeqCst);
    }
}