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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Copyright 2024 New Vector Ltd.
// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

//! A module containing PostgreSQL implementation of repositories for the
//! compatibility layer

mod access_token;
mod refresh_token;
mod session;
mod sso_login;

pub use self::{
    access_token::PgCompatAccessTokenRepository, refresh_token::PgCompatRefreshTokenRepository,
    session::PgCompatSessionRepository, sso_login::PgCompatSsoLoginRepository,
};

#[cfg(test)]
mod tests {
    use chrono::Duration;
    use mas_data_model::{Device, UserAgent};
    use mas_storage::{
        clock::MockClock,
        compat::{
            CompatAccessTokenRepository, CompatRefreshTokenRepository, CompatSessionFilter,
            CompatSessionRepository, CompatSsoLoginFilter,
        },
        user::UserRepository,
        Clock, Pagination, RepositoryAccess,
    };
    use rand::SeedableRng;
    use rand_chacha::ChaChaRng;
    use sqlx::PgPool;
    use ulid::Ulid;

    use crate::PgRepository;

    #[sqlx::test(migrator = "crate::MIGRATOR")]
    async fn test_session_repository(pool: PgPool) {
        let mut rng = ChaChaRng::seed_from_u64(42);
        let clock = MockClock::default();
        let mut repo = PgRepository::from_pool(&pool).await.unwrap();

        // Create a user
        let user = repo
            .user()
            .add(&mut rng, &clock, "john".to_owned())
            .await
            .unwrap();

        let all = CompatSessionFilter::new().for_user(&user);
        let active = all.active_only();
        let finished = all.finished_only();
        let pagination = Pagination::first(10);

        assert_eq!(repo.compat_session().count(all).await.unwrap(), 0);
        assert_eq!(repo.compat_session().count(active).await.unwrap(), 0);
        assert_eq!(repo.compat_session().count(finished).await.unwrap(), 0);

        let full_list = repo.compat_session().list(all, pagination).await.unwrap();
        assert!(full_list.edges.is_empty());
        let active_list = repo
            .compat_session()
            .list(active, pagination)
            .await
            .unwrap();
        assert!(active_list.edges.is_empty());
        let finished_list = repo
            .compat_session()
            .list(finished, pagination)
            .await
            .unwrap();
        assert!(finished_list.edges.is_empty());

        // Start a compat session for that user
        let device = Device::generate(&mut rng);
        let device_str = device.as_str().to_owned();
        let session = repo
            .compat_session()
            .add(&mut rng, &clock, &user, device.clone(), None, false)
            .await
            .unwrap();
        assert_eq!(session.user_id, user.id);
        assert_eq!(session.device.as_str(), device_str);
        assert!(session.is_valid());
        assert!(!session.is_finished());

        assert_eq!(repo.compat_session().count(all).await.unwrap(), 1);
        assert_eq!(repo.compat_session().count(active).await.unwrap(), 1);
        assert_eq!(repo.compat_session().count(finished).await.unwrap(), 0);

        let full_list = repo.compat_session().list(all, pagination).await.unwrap();
        assert_eq!(full_list.edges.len(), 1);
        assert_eq!(full_list.edges[0].0.id, session.id);
        let active_list = repo
            .compat_session()
            .list(active, pagination)
            .await
            .unwrap();
        assert_eq!(active_list.edges.len(), 1);
        assert_eq!(active_list.edges[0].0.id, session.id);
        let finished_list = repo
            .compat_session()
            .list(finished, pagination)
            .await
            .unwrap();
        assert!(finished_list.edges.is_empty());

        // Lookup the session and check it didn't change
        let session_lookup = repo
            .compat_session()
            .lookup(session.id)
            .await
            .unwrap()
            .expect("compat session not found");
        assert_eq!(session_lookup.id, session.id);
        assert_eq!(session_lookup.user_id, user.id);
        assert_eq!(session_lookup.device.as_str(), device_str);
        assert!(session_lookup.is_valid());
        assert!(!session_lookup.is_finished());

        // Record a user-agent for the session
        assert!(session_lookup.user_agent.is_none());
        let session = repo
            .compat_session()
            .record_user_agent(session_lookup, UserAgent::parse("Mozilla/5.0".to_owned()))
            .await
            .unwrap();
        assert_eq!(session.user_agent.as_deref(), Some("Mozilla/5.0"));

        // Reload the session and check again
        let session_lookup = repo
            .compat_session()
            .lookup(session.id)
            .await
            .unwrap()
            .expect("compat session not found");
        assert_eq!(session_lookup.user_agent.as_deref(), Some("Mozilla/5.0"));

        // Look up the session by device
        let list = repo
            .compat_session()
            .list(
                CompatSessionFilter::new()
                    .for_user(&user)
                    .for_device(&device),
                pagination,
            )
            .await
            .unwrap();
        assert_eq!(list.edges.len(), 1);
        let session_lookup = &list.edges[0].0;
        assert_eq!(session_lookup.id, session.id);
        assert_eq!(session_lookup.user_id, user.id);
        assert_eq!(session_lookup.device.as_str(), device_str);
        assert!(session_lookup.is_valid());
        assert!(!session_lookup.is_finished());

        // Finish the session
        let session = repo.compat_session().finish(&clock, session).await.unwrap();
        assert!(!session.is_valid());
        assert!(session.is_finished());

        assert_eq!(repo.compat_session().count(all).await.unwrap(), 1);
        assert_eq!(repo.compat_session().count(active).await.unwrap(), 0);
        assert_eq!(repo.compat_session().count(finished).await.unwrap(), 1);

        let full_list = repo.compat_session().list(all, pagination).await.unwrap();
        assert_eq!(full_list.edges.len(), 1);
        assert_eq!(full_list.edges[0].0.id, session.id);
        let active_list = repo
            .compat_session()
            .list(active, pagination)
            .await
            .unwrap();
        assert!(active_list.edges.is_empty());
        let finished_list = repo
            .compat_session()
            .list(finished, pagination)
            .await
            .unwrap();
        assert_eq!(finished_list.edges.len(), 1);
        assert_eq!(finished_list.edges[0].0.id, session.id);

        // Reload the session and check again
        let session_lookup = repo
            .compat_session()
            .lookup(session.id)
            .await
            .unwrap()
            .expect("compat session not found");
        assert!(!session_lookup.is_valid());
        assert!(session_lookup.is_finished());

        // Now add another session, with an SSO login this time
        let unknown_session = session;
        // Start a new SSO login
        let login = repo
            .compat_sso_login()
            .add(
                &mut rng,
                &clock,
                "login-token".to_owned(),
                "https://example.com/callback".parse().unwrap(),
            )
            .await
            .unwrap();
        assert!(login.is_pending());

        // Start a compat session for that user
        let device = Device::generate(&mut rng);
        let sso_login_session = repo
            .compat_session()
            .add(&mut rng, &clock, &user, device, None, false)
            .await
            .unwrap();

        // Associate the login with the session
        let login = repo
            .compat_sso_login()
            .fulfill(&clock, login, &sso_login_session)
            .await
            .unwrap();
        assert!(login.is_fulfilled());

        // Now query the session list with both the unknown and SSO login session type
        // filter
        let all = CompatSessionFilter::new().for_user(&user);
        let sso_login = all.sso_login_only();
        let unknown = all.unknown_only();
        assert_eq!(repo.compat_session().count(all).await.unwrap(), 2);
        assert_eq!(repo.compat_session().count(sso_login).await.unwrap(), 1);
        assert_eq!(repo.compat_session().count(unknown).await.unwrap(), 1);

        let list = repo
            .compat_session()
            .list(sso_login, pagination)
            .await
            .unwrap();
        assert_eq!(list.edges.len(), 1);
        assert_eq!(list.edges[0].0.id, sso_login_session.id);
        let list = repo
            .compat_session()
            .list(unknown, pagination)
            .await
            .unwrap();
        assert_eq!(list.edges.len(), 1);
        assert_eq!(list.edges[0].0.id, unknown_session.id);

        // Check that combining the two filters works
        // At this point, there is one active SSO login session and one finished unknown
        // session
        assert_eq!(
            repo.compat_session()
                .count(all.sso_login_only().active_only())
                .await
                .unwrap(),
            1
        );
        assert_eq!(
            repo.compat_session()
                .count(all.sso_login_only().finished_only())
                .await
                .unwrap(),
            0
        );
        assert_eq!(
            repo.compat_session()
                .count(all.unknown_only().active_only())
                .await
                .unwrap(),
            0
        );
        assert_eq!(
            repo.compat_session()
                .count(all.unknown_only().finished_only())
                .await
                .unwrap(),
            1
        );

        // Check that we can batch finish sessions
        let affected = repo
            .compat_session()
            .finish_bulk(&clock, all.sso_login_only().active_only())
            .await
            .unwrap();
        assert_eq!(affected, 1);
        assert_eq!(repo.compat_session().count(finished).await.unwrap(), 2);
        assert_eq!(repo.compat_session().count(active).await.unwrap(), 0);
    }

    #[sqlx::test(migrator = "crate::MIGRATOR")]
    async fn test_access_token_repository(pool: PgPool) {
        const FIRST_TOKEN: &str = "first_access_token";
        const SECOND_TOKEN: &str = "second_access_token";
        let mut rng = ChaChaRng::seed_from_u64(42);
        let clock = MockClock::default();
        let mut repo = PgRepository::from_pool(&pool).await.unwrap().boxed();

        // Create a user
        let user = repo
            .user()
            .add(&mut rng, &clock, "john".to_owned())
            .await
            .unwrap();

        // Start a compat session for that user
        let device = Device::generate(&mut rng);
        let session = repo
            .compat_session()
            .add(&mut rng, &clock, &user, device, None, false)
            .await
            .unwrap();

        // Add an access token to that session
        let token = repo
            .compat_access_token()
            .add(
                &mut rng,
                &clock,
                &session,
                FIRST_TOKEN.to_owned(),
                Some(Duration::try_minutes(1).unwrap()),
            )
            .await
            .unwrap();
        assert_eq!(token.session_id, session.id);
        assert_eq!(token.token, FIRST_TOKEN);

        // Commit the txn and grab a new transaction, to test a conflict
        repo.save().await.unwrap();

        {
            let mut repo = PgRepository::from_pool(&pool).await.unwrap().boxed();
            // Adding the same token a second time should conflict
            assert!(repo
                .compat_access_token()
                .add(
                    &mut rng,
                    &clock,
                    &session,
                    FIRST_TOKEN.to_owned(),
                    Some(Duration::try_minutes(1).unwrap()),
                )
                .await
                .is_err());
            repo.cancel().await.unwrap();
        }

        // Grab a new repo
        let mut repo = PgRepository::from_pool(&pool).await.unwrap().boxed();

        // Looking up via ID works
        let token_lookup = repo
            .compat_access_token()
            .lookup(token.id)
            .await
            .unwrap()
            .expect("compat access token not found");
        assert_eq!(token.id, token_lookup.id);
        assert_eq!(token_lookup.session_id, session.id);

        // Looking up via the token value works
        let token_lookup = repo
            .compat_access_token()
            .find_by_token(FIRST_TOKEN)
            .await
            .unwrap()
            .expect("compat access token not found");
        assert_eq!(token.id, token_lookup.id);
        assert_eq!(token_lookup.session_id, session.id);

        // Token is currently valid
        assert!(token.is_valid(clock.now()));

        clock.advance(Duration::try_minutes(1).unwrap());
        // Token should have expired
        assert!(!token.is_valid(clock.now()));

        // Add a second access token, this time without expiration
        let token = repo
            .compat_access_token()
            .add(&mut rng, &clock, &session, SECOND_TOKEN.to_owned(), None)
            .await
            .unwrap();
        assert_eq!(token.session_id, session.id);
        assert_eq!(token.token, SECOND_TOKEN);

        // Token is currently valid
        assert!(token.is_valid(clock.now()));

        // Make it expire
        repo.compat_access_token()
            .expire(&clock, token)
            .await
            .unwrap();

        // Reload it
        let token = repo
            .compat_access_token()
            .find_by_token(SECOND_TOKEN)
            .await
            .unwrap()
            .expect("compat access token not found");

        // Token is not valid anymore
        assert!(!token.is_valid(clock.now()));

        repo.save().await.unwrap();
    }

    #[sqlx::test(migrator = "crate::MIGRATOR")]
    async fn test_refresh_token_repository(pool: PgPool) {
        const ACCESS_TOKEN: &str = "access_token";
        const REFRESH_TOKEN: &str = "refresh_token";
        let mut rng = ChaChaRng::seed_from_u64(42);
        let clock = MockClock::default();
        let mut repo = PgRepository::from_pool(&pool).await.unwrap().boxed();

        // Create a user
        let user = repo
            .user()
            .add(&mut rng, &clock, "john".to_owned())
            .await
            .unwrap();

        // Start a compat session for that user
        let device = Device::generate(&mut rng);
        let session = repo
            .compat_session()
            .add(&mut rng, &clock, &user, device, None, false)
            .await
            .unwrap();

        // Add an access token to that session
        let access_token = repo
            .compat_access_token()
            .add(&mut rng, &clock, &session, ACCESS_TOKEN.to_owned(), None)
            .await
            .unwrap();

        let refresh_token = repo
            .compat_refresh_token()
            .add(
                &mut rng,
                &clock,
                &session,
                &access_token,
                REFRESH_TOKEN.to_owned(),
            )
            .await
            .unwrap();
        assert_eq!(refresh_token.session_id, session.id);
        assert_eq!(refresh_token.access_token_id, access_token.id);
        assert_eq!(refresh_token.token, REFRESH_TOKEN);
        assert!(refresh_token.is_valid());
        assert!(!refresh_token.is_consumed());

        // Look it up by ID and check everything matches
        let refresh_token_lookup = repo
            .compat_refresh_token()
            .lookup(refresh_token.id)
            .await
            .unwrap()
            .expect("refresh token not found");
        assert_eq!(refresh_token_lookup.id, refresh_token.id);
        assert_eq!(refresh_token_lookup.session_id, session.id);
        assert_eq!(refresh_token_lookup.access_token_id, access_token.id);
        assert_eq!(refresh_token_lookup.token, REFRESH_TOKEN);
        assert!(refresh_token_lookup.is_valid());
        assert!(!refresh_token_lookup.is_consumed());

        // Look it up by token and check everything matches
        let refresh_token_lookup = repo
            .compat_refresh_token()
            .find_by_token(REFRESH_TOKEN)
            .await
            .unwrap()
            .expect("refresh token not found");
        assert_eq!(refresh_token_lookup.id, refresh_token.id);
        assert_eq!(refresh_token_lookup.session_id, session.id);
        assert_eq!(refresh_token_lookup.access_token_id, access_token.id);
        assert_eq!(refresh_token_lookup.token, REFRESH_TOKEN);
        assert!(refresh_token_lookup.is_valid());
        assert!(!refresh_token_lookup.is_consumed());

        // Consume it
        let refresh_token = repo
            .compat_refresh_token()
            .consume(&clock, refresh_token)
            .await
            .unwrap();
        assert!(!refresh_token.is_valid());
        assert!(refresh_token.is_consumed());

        // Reload it and check again
        let refresh_token_lookup = repo
            .compat_refresh_token()
            .find_by_token(REFRESH_TOKEN)
            .await
            .unwrap()
            .expect("refresh token not found");
        assert!(!refresh_token_lookup.is_valid());
        assert!(refresh_token_lookup.is_consumed());

        // Consuming it again should not work
        assert!(repo
            .compat_refresh_token()
            .consume(&clock, refresh_token)
            .await
            .is_err());

        repo.save().await.unwrap();
    }

    #[sqlx::test(migrator = "crate::MIGRATOR")]
    async fn test_compat_sso_login_repository(pool: PgPool) {
        let mut rng = ChaChaRng::seed_from_u64(42);
        let clock = MockClock::default();
        let mut repo = PgRepository::from_pool(&pool).await.unwrap().boxed();

        // Create a user
        let user = repo
            .user()
            .add(&mut rng, &clock, "john".to_owned())
            .await
            .unwrap();

        // Lookup an unknown SSO login
        let login = repo.compat_sso_login().lookup(Ulid::nil()).await.unwrap();
        assert_eq!(login, None);

        let all = CompatSsoLoginFilter::new();
        let for_user = all.for_user(&user);
        let pending = all.pending_only();
        let fulfilled = all.fulfilled_only();
        let exchanged = all.exchanged_only();

        // Check the initial counts
        assert_eq!(repo.compat_sso_login().count(all).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(for_user).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(pending).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(fulfilled).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(exchanged).await.unwrap(), 0);

        // Lookup an unknown login token
        let login = repo
            .compat_sso_login()
            .find_by_token("login-token")
            .await
            .unwrap();
        assert_eq!(login, None);

        // Start a new SSO login
        let login = repo
            .compat_sso_login()
            .add(
                &mut rng,
                &clock,
                "login-token".to_owned(),
                "https://example.com/callback".parse().unwrap(),
            )
            .await
            .unwrap();
        assert!(login.is_pending());

        // Check the counts
        assert_eq!(repo.compat_sso_login().count(all).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(for_user).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(pending).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(fulfilled).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(exchanged).await.unwrap(), 0);

        // Lookup the login by ID
        let login_lookup = repo
            .compat_sso_login()
            .lookup(login.id)
            .await
            .unwrap()
            .expect("login not found");
        assert_eq!(login_lookup, login);

        // Find the login by token
        let login_lookup = repo
            .compat_sso_login()
            .find_by_token("login-token")
            .await
            .unwrap()
            .expect("login not found");
        assert_eq!(login_lookup, login);

        // Exchanging before fulfilling should not work
        // Note: It should also not poison the SQL transaction
        let res = repo
            .compat_sso_login()
            .exchange(&clock, login.clone())
            .await;
        assert!(res.is_err());

        // Start a compat session for that user
        let device = Device::generate(&mut rng);
        let session = repo
            .compat_session()
            .add(&mut rng, &clock, &user, device, None, false)
            .await
            .unwrap();

        // Associate the login with the session
        let login = repo
            .compat_sso_login()
            .fulfill(&clock, login, &session)
            .await
            .unwrap();
        assert!(login.is_fulfilled());

        // Check the counts
        assert_eq!(repo.compat_sso_login().count(all).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(for_user).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(pending).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(fulfilled).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(exchanged).await.unwrap(), 0);

        // Fulfilling again should not work
        // Note: It should also not poison the SQL transaction
        let res = repo
            .compat_sso_login()
            .fulfill(&clock, login.clone(), &session)
            .await;
        assert!(res.is_err());

        // Exchange that login
        let login = repo
            .compat_sso_login()
            .exchange(&clock, login)
            .await
            .unwrap();
        assert!(login.is_exchanged());

        // Check the counts
        assert_eq!(repo.compat_sso_login().count(all).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(for_user).await.unwrap(), 1);
        assert_eq!(repo.compat_sso_login().count(pending).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(fulfilled).await.unwrap(), 0);
        assert_eq!(repo.compat_sso_login().count(exchanged).await.unwrap(), 1);

        // Exchange again should not work
        // Note: It should also not poison the SQL transaction
        let res = repo
            .compat_sso_login()
            .exchange(&clock, login.clone())
            .await;
        assert!(res.is_err());

        // Fulfilling after exchanging should not work
        // Note: It should also not poison the SQL transaction
        let res = repo
            .compat_sso_login()
            .fulfill(&clock, login.clone(), &session)
            .await;
        assert!(res.is_err());

        let pagination = Pagination::first(10);

        // List all logins
        let logins = repo.compat_sso_login().list(all, pagination).await.unwrap();
        assert!(!logins.has_next_page);
        assert_eq!(logins.edges, &[login.clone()]);

        // List the logins for the user
        let logins = repo
            .compat_sso_login()
            .list(for_user, pagination)
            .await
            .unwrap();
        assert!(!logins.has_next_page);
        assert_eq!(logins.edges, &[login.clone()]);

        // List only the pending logins for the user
        let logins = repo
            .compat_sso_login()
            .list(for_user.pending_only(), pagination)
            .await
            .unwrap();
        assert!(!logins.has_next_page);
        assert!(logins.edges.is_empty());

        // List only the fulfilled logins for the user
        let logins = repo
            .compat_sso_login()
            .list(for_user.fulfilled_only(), pagination)
            .await
            .unwrap();
        assert!(!logins.has_next_page);
        assert!(logins.edges.is_empty());

        // List only the exchanged logins for the user
        let logins = repo
            .compat_sso_login()
            .list(for_user.exchanged_only(), pagination)
            .await
            .unwrap();
        assert!(!logins.has_next_page);
        assert_eq!(logins.edges, &[login]);
    }
}