1use super::*;
2use gpui::executor::{Background, Deterministic};
3use std::sync::Arc;
4
5#[cfg(test)]
6use pretty_assertions::{assert_eq, assert_ne};
7
8macro_rules! test_both_dbs {
9 ($postgres_test_name:ident, $sqlite_test_name:ident, $db:ident, $body:block) => {
10 #[gpui::test]
11 async fn $postgres_test_name() {
12 let test_db = TestDb::postgres(Deterministic::new(0).build_background());
13 let $db = test_db.db();
14 $body
15 }
16
17 #[gpui::test]
18 async fn $sqlite_test_name() {
19 let test_db = TestDb::sqlite(Deterministic::new(0).build_background());
20 let $db = test_db.db();
21 $body
22 }
23 };
24}
25
26test_both_dbs!(
27 test_get_users_by_ids_postgres,
28 test_get_users_by_ids_sqlite,
29 db,
30 {
31 let mut user_ids = Vec::new();
32 let mut user_metric_ids = Vec::new();
33 for i in 1..=4 {
34 let user = db
35 .create_user(
36 &format!("user{i}@example.com"),
37 false,
38 NewUserParams {
39 github_login: format!("user{i}"),
40 github_user_id: i,
41 invite_count: 0,
42 },
43 )
44 .await
45 .unwrap();
46 user_ids.push(user.user_id);
47 user_metric_ids.push(user.metrics_id);
48 }
49
50 assert_eq!(
51 db.get_users_by_ids(user_ids.clone()).await.unwrap(),
52 vec![
53 User {
54 id: user_ids[0],
55 github_login: "user1".to_string(),
56 github_user_id: Some(1),
57 email_address: Some("user1@example.com".to_string()),
58 admin: false,
59 metrics_id: user_metric_ids[0].parse().unwrap(),
60 ..Default::default()
61 },
62 User {
63 id: user_ids[1],
64 github_login: "user2".to_string(),
65 github_user_id: Some(2),
66 email_address: Some("user2@example.com".to_string()),
67 admin: false,
68 metrics_id: user_metric_ids[1].parse().unwrap(),
69 ..Default::default()
70 },
71 User {
72 id: user_ids[2],
73 github_login: "user3".to_string(),
74 github_user_id: Some(3),
75 email_address: Some("user3@example.com".to_string()),
76 admin: false,
77 metrics_id: user_metric_ids[2].parse().unwrap(),
78 ..Default::default()
79 },
80 User {
81 id: user_ids[3],
82 github_login: "user4".to_string(),
83 github_user_id: Some(4),
84 email_address: Some("user4@example.com".to_string()),
85 admin: false,
86 metrics_id: user_metric_ids[3].parse().unwrap(),
87 ..Default::default()
88 }
89 ]
90 );
91 }
92);
93
94test_both_dbs!(
95 test_get_or_create_user_by_github_account_postgres,
96 test_get_or_create_user_by_github_account_sqlite,
97 db,
98 {
99 let user_id1 = db
100 .create_user(
101 "user1@example.com",
102 false,
103 NewUserParams {
104 github_login: "login1".into(),
105 github_user_id: 101,
106 invite_count: 0,
107 },
108 )
109 .await
110 .unwrap()
111 .user_id;
112 let user_id2 = db
113 .create_user(
114 "user2@example.com",
115 false,
116 NewUserParams {
117 github_login: "login2".into(),
118 github_user_id: 102,
119 invite_count: 0,
120 },
121 )
122 .await
123 .unwrap()
124 .user_id;
125
126 let user = db
127 .get_or_create_user_by_github_account("login1", None, None)
128 .await
129 .unwrap()
130 .unwrap();
131 assert_eq!(user.id, user_id1);
132 assert_eq!(&user.github_login, "login1");
133 assert_eq!(user.github_user_id, Some(101));
134
135 assert!(db
136 .get_or_create_user_by_github_account("non-existent-login", None, None)
137 .await
138 .unwrap()
139 .is_none());
140
141 let user = db
142 .get_or_create_user_by_github_account("the-new-login2", Some(102), None)
143 .await
144 .unwrap()
145 .unwrap();
146 assert_eq!(user.id, user_id2);
147 assert_eq!(&user.github_login, "the-new-login2");
148 assert_eq!(user.github_user_id, Some(102));
149
150 let user = db
151 .get_or_create_user_by_github_account("login3", Some(103), Some("user3@example.com"))
152 .await
153 .unwrap()
154 .unwrap();
155 assert_eq!(&user.github_login, "login3");
156 assert_eq!(user.github_user_id, Some(103));
157 assert_eq!(user.email_address, Some("user3@example.com".into()));
158 }
159);
160
161test_both_dbs!(
162 test_create_access_tokens_postgres,
163 test_create_access_tokens_sqlite,
164 db,
165 {
166 let user = db
167 .create_user(
168 "u1@example.com",
169 false,
170 NewUserParams {
171 github_login: "u1".into(),
172 github_user_id: 1,
173 invite_count: 0,
174 },
175 )
176 .await
177 .unwrap()
178 .user_id;
179
180 let token_1 = db.create_access_token(user, "h1", 2).await.unwrap();
181 let token_2 = db.create_access_token(user, "h2", 2).await.unwrap();
182 assert_eq!(
183 db.get_access_token(token_1).await.unwrap(),
184 access_token::Model {
185 id: token_1,
186 user_id: user,
187 hash: "h1".into(),
188 }
189 );
190 assert_eq!(
191 db.get_access_token(token_2).await.unwrap(),
192 access_token::Model {
193 id: token_2,
194 user_id: user,
195 hash: "h2".into()
196 }
197 );
198
199 let token_3 = db.create_access_token(user, "h3", 2).await.unwrap();
200 assert_eq!(
201 db.get_access_token(token_3).await.unwrap(),
202 access_token::Model {
203 id: token_3,
204 user_id: user,
205 hash: "h3".into()
206 }
207 );
208 assert_eq!(
209 db.get_access_token(token_2).await.unwrap(),
210 access_token::Model {
211 id: token_2,
212 user_id: user,
213 hash: "h2".into()
214 }
215 );
216 assert!(db.get_access_token(token_1).await.is_err());
217
218 let token_4 = db.create_access_token(user, "h4", 2).await.unwrap();
219 assert_eq!(
220 db.get_access_token(token_4).await.unwrap(),
221 access_token::Model {
222 id: token_4,
223 user_id: user,
224 hash: "h4".into()
225 }
226 );
227 assert_eq!(
228 db.get_access_token(token_3).await.unwrap(),
229 access_token::Model {
230 id: token_3,
231 user_id: user,
232 hash: "h3".into()
233 }
234 );
235 assert!(db.get_access_token(token_2).await.is_err());
236 assert!(db.get_access_token(token_1).await.is_err());
237 }
238);
239
240test_both_dbs!(test_add_contacts_postgres, test_add_contacts_sqlite, db, {
241 let mut user_ids = Vec::new();
242 for i in 0..3 {
243 user_ids.push(
244 db.create_user(
245 &format!("user{i}@example.com"),
246 false,
247 NewUserParams {
248 github_login: format!("user{i}"),
249 github_user_id: i,
250 invite_count: 0,
251 },
252 )
253 .await
254 .unwrap()
255 .user_id,
256 );
257 }
258
259 let user_1 = user_ids[0];
260 let user_2 = user_ids[1];
261 let user_3 = user_ids[2];
262
263 // User starts with no contacts
264 assert_eq!(db.get_contacts(user_1).await.unwrap(), &[]);
265
266 // User requests a contact. Both users see the pending request.
267 db.send_contact_request(user_1, user_2).await.unwrap();
268 assert!(!db.has_contact(user_1, user_2).await.unwrap());
269 assert!(!db.has_contact(user_2, user_1).await.unwrap());
270 assert_eq!(
271 db.get_contacts(user_1).await.unwrap(),
272 &[Contact::Outgoing { user_id: user_2 }],
273 );
274 assert_eq!(
275 db.get_contacts(user_2).await.unwrap(),
276 &[Contact::Incoming {
277 user_id: user_1,
278 should_notify: true
279 }]
280 );
281
282 // User 2 dismisses the contact request notification without accepting or rejecting.
283 // We shouldn't notify them again.
284 db.dismiss_contact_notification(user_1, user_2)
285 .await
286 .unwrap_err();
287 db.dismiss_contact_notification(user_2, user_1)
288 .await
289 .unwrap();
290 assert_eq!(
291 db.get_contacts(user_2).await.unwrap(),
292 &[Contact::Incoming {
293 user_id: user_1,
294 should_notify: false
295 }]
296 );
297
298 // User can't accept their own contact request
299 db.respond_to_contact_request(user_1, user_2, true)
300 .await
301 .unwrap_err();
302
303 // User accepts a contact request. Both users see the contact.
304 db.respond_to_contact_request(user_2, user_1, true)
305 .await
306 .unwrap();
307 assert_eq!(
308 db.get_contacts(user_1).await.unwrap(),
309 &[Contact::Accepted {
310 user_id: user_2,
311 should_notify: true,
312 busy: false,
313 }],
314 );
315 assert!(db.has_contact(user_1, user_2).await.unwrap());
316 assert!(db.has_contact(user_2, user_1).await.unwrap());
317 assert_eq!(
318 db.get_contacts(user_2).await.unwrap(),
319 &[Contact::Accepted {
320 user_id: user_1,
321 should_notify: false,
322 busy: false,
323 }]
324 );
325
326 // Users cannot re-request existing contacts.
327 db.send_contact_request(user_1, user_2).await.unwrap_err();
328 db.send_contact_request(user_2, user_1).await.unwrap_err();
329
330 // Users can't dismiss notifications of them accepting other users' requests.
331 db.dismiss_contact_notification(user_2, user_1)
332 .await
333 .unwrap_err();
334 assert_eq!(
335 db.get_contacts(user_1).await.unwrap(),
336 &[Contact::Accepted {
337 user_id: user_2,
338 should_notify: true,
339 busy: false,
340 }]
341 );
342
343 // Users can dismiss notifications of other users accepting their requests.
344 db.dismiss_contact_notification(user_1, user_2)
345 .await
346 .unwrap();
347 assert_eq!(
348 db.get_contacts(user_1).await.unwrap(),
349 &[Contact::Accepted {
350 user_id: user_2,
351 should_notify: false,
352 busy: false,
353 }]
354 );
355
356 // Users send each other concurrent contact requests and
357 // see that they are immediately accepted.
358 db.send_contact_request(user_1, user_3).await.unwrap();
359 db.send_contact_request(user_3, user_1).await.unwrap();
360 assert_eq!(
361 db.get_contacts(user_1).await.unwrap(),
362 &[
363 Contact::Accepted {
364 user_id: user_2,
365 should_notify: false,
366 busy: false,
367 },
368 Contact::Accepted {
369 user_id: user_3,
370 should_notify: false,
371 busy: false,
372 }
373 ]
374 );
375 assert_eq!(
376 db.get_contacts(user_3).await.unwrap(),
377 &[Contact::Accepted {
378 user_id: user_1,
379 should_notify: false,
380 busy: false,
381 }],
382 );
383
384 // User declines a contact request. Both users see that it is gone.
385 db.send_contact_request(user_2, user_3).await.unwrap();
386 db.respond_to_contact_request(user_3, user_2, false)
387 .await
388 .unwrap();
389 assert!(!db.has_contact(user_2, user_3).await.unwrap());
390 assert!(!db.has_contact(user_3, user_2).await.unwrap());
391 assert_eq!(
392 db.get_contacts(user_2).await.unwrap(),
393 &[Contact::Accepted {
394 user_id: user_1,
395 should_notify: false,
396 busy: false,
397 }]
398 );
399 assert_eq!(
400 db.get_contacts(user_3).await.unwrap(),
401 &[Contact::Accepted {
402 user_id: user_1,
403 should_notify: false,
404 busy: false,
405 }],
406 );
407});
408
409test_both_dbs!(test_metrics_id_postgres, test_metrics_id_sqlite, db, {
410 let NewUserResult {
411 user_id: user1,
412 metrics_id: metrics_id1,
413 ..
414 } = db
415 .create_user(
416 "person1@example.com",
417 false,
418 NewUserParams {
419 github_login: "person1".into(),
420 github_user_id: 101,
421 invite_count: 5,
422 },
423 )
424 .await
425 .unwrap();
426 let NewUserResult {
427 user_id: user2,
428 metrics_id: metrics_id2,
429 ..
430 } = db
431 .create_user(
432 "person2@example.com",
433 false,
434 NewUserParams {
435 github_login: "person2".into(),
436 github_user_id: 102,
437 invite_count: 5,
438 },
439 )
440 .await
441 .unwrap();
442
443 assert_eq!(db.get_user_metrics_id(user1).await.unwrap(), metrics_id1);
444 assert_eq!(db.get_user_metrics_id(user2).await.unwrap(), metrics_id2);
445 assert_eq!(metrics_id1.len(), 36);
446 assert_eq!(metrics_id2.len(), 36);
447 assert_ne!(metrics_id1, metrics_id2);
448});
449
450test_both_dbs!(
451 test_project_count_postgres,
452 test_project_count_sqlite,
453 db,
454 {
455 let owner_id = db.create_server("test").await.unwrap().0 as u32;
456
457 let user1 = db
458 .create_user(
459 &format!("admin@example.com"),
460 true,
461 NewUserParams {
462 github_login: "admin".into(),
463 github_user_id: 0,
464 invite_count: 0,
465 },
466 )
467 .await
468 .unwrap();
469 let user2 = db
470 .create_user(
471 &format!("user@example.com"),
472 false,
473 NewUserParams {
474 github_login: "user".into(),
475 github_user_id: 1,
476 invite_count: 0,
477 },
478 )
479 .await
480 .unwrap();
481
482 let room_id = RoomId::from_proto(
483 db.create_room(user1.user_id, ConnectionId { owner_id, id: 0 }, "")
484 .await
485 .unwrap()
486 .id,
487 );
488 db.call(
489 room_id,
490 user1.user_id,
491 ConnectionId { owner_id, id: 0 },
492 user2.user_id,
493 None,
494 )
495 .await
496 .unwrap();
497 db.join_room(
498 room_id,
499 user2.user_id,
500 None,
501 ConnectionId { owner_id, id: 1 },
502 )
503 .await
504 .unwrap();
505 assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0);
506
507 db.share_project(room_id, ConnectionId { owner_id, id: 1 }, &[])
508 .await
509 .unwrap();
510 assert_eq!(db.project_count_excluding_admins().await.unwrap(), 1);
511
512 db.share_project(room_id, ConnectionId { owner_id, id: 1 }, &[])
513 .await
514 .unwrap();
515 assert_eq!(db.project_count_excluding_admins().await.unwrap(), 2);
516
517 // Projects shared by admins aren't counted.
518 db.share_project(room_id, ConnectionId { owner_id, id: 0 }, &[])
519 .await
520 .unwrap();
521 assert_eq!(db.project_count_excluding_admins().await.unwrap(), 2);
522
523 db.leave_room(ConnectionId { owner_id, id: 1 })
524 .await
525 .unwrap();
526 assert_eq!(db.project_count_excluding_admins().await.unwrap(), 0);
527 }
528);
529
530#[test]
531fn test_fuzzy_like_string() {
532 assert_eq!(Database::fuzzy_like_string("abcd"), "%a%b%c%d%");
533 assert_eq!(Database::fuzzy_like_string("x y"), "%x%y%");
534 assert_eq!(Database::fuzzy_like_string(" z "), "%z%");
535}
536
537#[gpui::test]
538async fn test_fuzzy_search_users() {
539 let test_db = TestDb::postgres(build_background_executor());
540 let db = test_db.db();
541 for (i, github_login) in [
542 "California",
543 "colorado",
544 "oregon",
545 "washington",
546 "florida",
547 "delaware",
548 "rhode-island",
549 ]
550 .into_iter()
551 .enumerate()
552 {
553 db.create_user(
554 &format!("{github_login}@example.com"),
555 false,
556 NewUserParams {
557 github_login: github_login.into(),
558 github_user_id: i as i32,
559 invite_count: 0,
560 },
561 )
562 .await
563 .unwrap();
564 }
565
566 assert_eq!(
567 fuzzy_search_user_names(db, "clr").await,
568 &["colorado", "California"]
569 );
570 assert_eq!(
571 fuzzy_search_user_names(db, "ro").await,
572 &["rhode-island", "colorado", "oregon"],
573 );
574
575 async fn fuzzy_search_user_names(db: &Database, query: &str) -> Vec<String> {
576 db.fuzzy_search_users(query, 10)
577 .await
578 .unwrap()
579 .into_iter()
580 .map(|user| user.github_login)
581 .collect::<Vec<_>>()
582 }
583}
584
585#[gpui::test]
586async fn test_invite_codes() {
587 let test_db = TestDb::postgres(build_background_executor());
588 let db = test_db.db();
589
590 let NewUserResult { user_id: user1, .. } = db
591 .create_user(
592 "user1@example.com",
593 false,
594 NewUserParams {
595 github_login: "user1".into(),
596 github_user_id: 0,
597 invite_count: 0,
598 },
599 )
600 .await
601 .unwrap();
602
603 // Initially, user 1 has no invite code
604 assert_eq!(db.get_invite_code_for_user(user1).await.unwrap(), None);
605
606 // Setting invite count to 0 when no code is assigned does not assign a new code
607 db.set_invite_count_for_user(user1, 0).await.unwrap();
608 assert!(db.get_invite_code_for_user(user1).await.unwrap().is_none());
609
610 // User 1 creates an invite code that can be used twice.
611 db.set_invite_count_for_user(user1, 2).await.unwrap();
612 let (invite_code, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
613 assert_eq!(invite_count, 2);
614
615 // User 2 redeems the invite code and becomes a contact of user 1.
616 let user2_invite = db
617 .create_invite_from_code(
618 &invite_code,
619 "user2@example.com",
620 Some("user-2-device-id"),
621 true,
622 )
623 .await
624 .unwrap();
625 let NewUserResult {
626 user_id: user2,
627 inviting_user_id,
628 signup_device_id,
629 metrics_id,
630 } = db
631 .create_user_from_invite(
632 &user2_invite,
633 NewUserParams {
634 github_login: "user2".into(),
635 github_user_id: 2,
636 invite_count: 7,
637 },
638 )
639 .await
640 .unwrap()
641 .unwrap();
642 let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
643 assert_eq!(invite_count, 1);
644 assert_eq!(inviting_user_id, Some(user1));
645 assert_eq!(signup_device_id.unwrap(), "user-2-device-id");
646 assert_eq!(db.get_user_metrics_id(user2).await.unwrap(), metrics_id);
647 assert_eq!(
648 db.get_contacts(user1).await.unwrap(),
649 [Contact::Accepted {
650 user_id: user2,
651 should_notify: true,
652 busy: false,
653 }]
654 );
655 assert_eq!(
656 db.get_contacts(user2).await.unwrap(),
657 [Contact::Accepted {
658 user_id: user1,
659 should_notify: false,
660 busy: false,
661 }]
662 );
663 assert!(db.has_contact(user1, user2).await.unwrap());
664 assert!(db.has_contact(user2, user1).await.unwrap());
665 assert_eq!(
666 db.get_invite_code_for_user(user2).await.unwrap().unwrap().1,
667 7
668 );
669
670 // User 3 redeems the invite code and becomes a contact of user 1.
671 let user3_invite = db
672 .create_invite_from_code(&invite_code, "user3@example.com", None, true)
673 .await
674 .unwrap();
675 let NewUserResult {
676 user_id: user3,
677 inviting_user_id,
678 signup_device_id,
679 ..
680 } = db
681 .create_user_from_invite(
682 &user3_invite,
683 NewUserParams {
684 github_login: "user-3".into(),
685 github_user_id: 3,
686 invite_count: 3,
687 },
688 )
689 .await
690 .unwrap()
691 .unwrap();
692 let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
693 assert_eq!(invite_count, 0);
694 assert_eq!(inviting_user_id, Some(user1));
695 assert!(signup_device_id.is_none());
696 assert_eq!(
697 db.get_contacts(user1).await.unwrap(),
698 [
699 Contact::Accepted {
700 user_id: user2,
701 should_notify: true,
702 busy: false,
703 },
704 Contact::Accepted {
705 user_id: user3,
706 should_notify: true,
707 busy: false,
708 }
709 ]
710 );
711 assert_eq!(
712 db.get_contacts(user3).await.unwrap(),
713 [Contact::Accepted {
714 user_id: user1,
715 should_notify: false,
716 busy: false,
717 }]
718 );
719 assert!(db.has_contact(user1, user3).await.unwrap());
720 assert!(db.has_contact(user3, user1).await.unwrap());
721 assert_eq!(
722 db.get_invite_code_for_user(user3).await.unwrap().unwrap().1,
723 3
724 );
725
726 // Trying to reedem the code for the third time results in an error.
727 db.create_invite_from_code(
728 &invite_code,
729 "user4@example.com",
730 Some("user-4-device-id"),
731 true,
732 )
733 .await
734 .unwrap_err();
735
736 // Invite count can be updated after the code has been created.
737 db.set_invite_count_for_user(user1, 2).await.unwrap();
738 let (latest_code, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
739 assert_eq!(latest_code, invite_code); // Invite code doesn't change when we increment above 0
740 assert_eq!(invite_count, 2);
741
742 // User 4 can now redeem the invite code and becomes a contact of user 1.
743 let user4_invite = db
744 .create_invite_from_code(
745 &invite_code,
746 "user4@example.com",
747 Some("user-4-device-id"),
748 true,
749 )
750 .await
751 .unwrap();
752 let user4 = db
753 .create_user_from_invite(
754 &user4_invite,
755 NewUserParams {
756 github_login: "user-4".into(),
757 github_user_id: 4,
758 invite_count: 5,
759 },
760 )
761 .await
762 .unwrap()
763 .unwrap()
764 .user_id;
765
766 let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
767 assert_eq!(invite_count, 1);
768 assert_eq!(
769 db.get_contacts(user1).await.unwrap(),
770 [
771 Contact::Accepted {
772 user_id: user2,
773 should_notify: true,
774 busy: false,
775 },
776 Contact::Accepted {
777 user_id: user3,
778 should_notify: true,
779 busy: false,
780 },
781 Contact::Accepted {
782 user_id: user4,
783 should_notify: true,
784 busy: false,
785 }
786 ]
787 );
788 assert_eq!(
789 db.get_contacts(user4).await.unwrap(),
790 [Contact::Accepted {
791 user_id: user1,
792 should_notify: false,
793 busy: false,
794 }]
795 );
796 assert!(db.has_contact(user1, user4).await.unwrap());
797 assert!(db.has_contact(user4, user1).await.unwrap());
798 assert_eq!(
799 db.get_invite_code_for_user(user4).await.unwrap().unwrap().1,
800 5
801 );
802
803 // An existing user cannot redeem invite codes.
804 db.create_invite_from_code(
805 &invite_code,
806 "user2@example.com",
807 Some("user-2-device-id"),
808 true,
809 )
810 .await
811 .unwrap_err();
812 let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
813 assert_eq!(invite_count, 1);
814
815 // A newer user can invite an existing one via a different email address
816 // than the one they used to sign up.
817 let user5 = db
818 .create_user(
819 "user5@example.com",
820 false,
821 NewUserParams {
822 github_login: "user5".into(),
823 github_user_id: 5,
824 invite_count: 0,
825 },
826 )
827 .await
828 .unwrap()
829 .user_id;
830 db.set_invite_count_for_user(user5, 5).await.unwrap();
831 let (user5_invite_code, _) = db.get_invite_code_for_user(user5).await.unwrap().unwrap();
832 let user5_invite_to_user1 = db
833 .create_invite_from_code(&user5_invite_code, "user1@different.com", None, true)
834 .await
835 .unwrap();
836 let user1_2 = db
837 .create_user_from_invite(
838 &user5_invite_to_user1,
839 NewUserParams {
840 github_login: "user1".into(),
841 github_user_id: 1,
842 invite_count: 5,
843 },
844 )
845 .await
846 .unwrap()
847 .unwrap()
848 .user_id;
849 assert_eq!(user1_2, user1);
850 assert_eq!(
851 db.get_contacts(user1).await.unwrap(),
852 [
853 Contact::Accepted {
854 user_id: user2,
855 should_notify: true,
856 busy: false,
857 },
858 Contact::Accepted {
859 user_id: user3,
860 should_notify: true,
861 busy: false,
862 },
863 Contact::Accepted {
864 user_id: user4,
865 should_notify: true,
866 busy: false,
867 },
868 Contact::Accepted {
869 user_id: user5,
870 should_notify: false,
871 busy: false,
872 }
873 ]
874 );
875 assert_eq!(
876 db.get_contacts(user5).await.unwrap(),
877 [Contact::Accepted {
878 user_id: user1,
879 should_notify: true,
880 busy: false,
881 }]
882 );
883 assert!(db.has_contact(user1, user5).await.unwrap());
884 assert!(db.has_contact(user5, user1).await.unwrap());
885}
886
887test_both_dbs!(test_channels_postgres, test_channels_sqlite, db, {
888 let a_id = db
889 .create_user(
890 "user1@example.com",
891 false,
892 NewUserParams {
893 github_login: "user1".into(),
894 github_user_id: 5,
895 invite_count: 0,
896 },
897 )
898 .await
899 .unwrap()
900 .user_id;
901
902 let b_id = db
903 .create_user(
904 "user2@example.com",
905 false,
906 NewUserParams {
907 github_login: "user2".into(),
908 github_user_id: 6,
909 invite_count: 0,
910 },
911 )
912 .await
913 .unwrap()
914 .user_id;
915
916 let zed_id = db.create_root_channel("zed", "1", a_id).await.unwrap();
917
918 // Make sure that people cannot read channels they haven't been invited to
919 assert!(db.get_channel(zed_id, b_id).await.unwrap().is_none());
920
921 db.invite_channel_member(zed_id, b_id, a_id, false)
922 .await
923 .unwrap();
924
925 db.respond_to_channel_invite(zed_id, b_id, true)
926 .await
927 .unwrap();
928
929 let crdb_id = db
930 .create_channel("crdb", Some(zed_id), "2", a_id)
931 .await
932 .unwrap();
933 let livestreaming_id = db
934 .create_channel("livestreaming", Some(zed_id), "3", a_id)
935 .await
936 .unwrap();
937 let replace_id = db
938 .create_channel("replace", Some(zed_id), "4", a_id)
939 .await
940 .unwrap();
941
942 let mut members = db.get_channel_members(replace_id).await.unwrap();
943 members.sort();
944 assert_eq!(members, &[a_id, b_id]);
945
946 let rust_id = db.create_root_channel("rust", "5", a_id).await.unwrap();
947 let cargo_id = db
948 .create_channel("cargo", Some(rust_id), "6", a_id)
949 .await
950 .unwrap();
951
952 let cargo_ra_id = db
953 .create_channel("cargo-ra", Some(cargo_id), "7", a_id)
954 .await
955 .unwrap();
956
957 let (channels, _) = db.get_channels_for_user(a_id).await.unwrap();
958 assert_eq!(
959 channels,
960 vec![
961 Channel {
962 id: zed_id,
963 name: "zed".to_string(),
964 parent_id: None,
965 user_is_admin: true,
966 },
967 Channel {
968 id: crdb_id,
969 name: "crdb".to_string(),
970 parent_id: Some(zed_id),
971 user_is_admin: true,
972 },
973 Channel {
974 id: livestreaming_id,
975 name: "livestreaming".to_string(),
976 parent_id: Some(zed_id),
977 user_is_admin: true,
978 },
979 Channel {
980 id: replace_id,
981 name: "replace".to_string(),
982 parent_id: Some(zed_id),
983 user_is_admin: true,
984 },
985 Channel {
986 id: rust_id,
987 name: "rust".to_string(),
988 parent_id: None,
989 user_is_admin: true,
990 },
991 Channel {
992 id: cargo_id,
993 name: "cargo".to_string(),
994 parent_id: Some(rust_id),
995 user_is_admin: true,
996 },
997 Channel {
998 id: cargo_ra_id,
999 name: "cargo-ra".to_string(),
1000 parent_id: Some(cargo_id),
1001 user_is_admin: true,
1002 }
1003 ]
1004 );
1005
1006 let (channels, _) = db.get_channels_for_user(b_id).await.unwrap();
1007 assert_eq!(
1008 channels,
1009 vec![
1010 Channel {
1011 id: zed_id,
1012 name: "zed".to_string(),
1013 parent_id: None,
1014 user_is_admin: false,
1015 },
1016 Channel {
1017 id: crdb_id,
1018 name: "crdb".to_string(),
1019 parent_id: Some(zed_id),
1020 user_is_admin: false,
1021 },
1022 Channel {
1023 id: livestreaming_id,
1024 name: "livestreaming".to_string(),
1025 parent_id: Some(zed_id),
1026 user_is_admin: false,
1027 },
1028 Channel {
1029 id: replace_id,
1030 name: "replace".to_string(),
1031 parent_id: Some(zed_id),
1032 user_is_admin: false,
1033 },
1034 ]
1035 );
1036
1037 // Update member permissions
1038 let set_subchannel_admin = db.set_channel_member_admin(crdb_id, a_id, b_id, true).await;
1039 assert!(set_subchannel_admin.is_err());
1040 let set_channel_admin = db.set_channel_member_admin(zed_id, a_id, b_id, true).await;
1041 assert!(set_channel_admin.is_ok());
1042
1043 let (channels, _) = db.get_channels_for_user(b_id).await.unwrap();
1044 assert_eq!(
1045 channels,
1046 vec![
1047 Channel {
1048 id: zed_id,
1049 name: "zed".to_string(),
1050 parent_id: None,
1051 user_is_admin: true,
1052 },
1053 Channel {
1054 id: crdb_id,
1055 name: "crdb".to_string(),
1056 parent_id: Some(zed_id),
1057 user_is_admin: false,
1058 },
1059 Channel {
1060 id: livestreaming_id,
1061 name: "livestreaming".to_string(),
1062 parent_id: Some(zed_id),
1063 user_is_admin: false,
1064 },
1065 Channel {
1066 id: replace_id,
1067 name: "replace".to_string(),
1068 parent_id: Some(zed_id),
1069 user_is_admin: false,
1070 },
1071 ]
1072 );
1073
1074 // Remove a single channel
1075 db.remove_channel(crdb_id, a_id).await.unwrap();
1076 assert!(db.get_channel(crdb_id, a_id).await.unwrap().is_none());
1077
1078 // Remove a channel tree
1079 let (mut channel_ids, user_ids) = db.remove_channel(rust_id, a_id).await.unwrap();
1080 channel_ids.sort();
1081 assert_eq!(channel_ids, &[rust_id, cargo_id, cargo_ra_id]);
1082 assert_eq!(user_ids, &[a_id]);
1083
1084 assert!(db.get_channel(rust_id, a_id).await.unwrap().is_none());
1085 assert!(db.get_channel(cargo_id, a_id).await.unwrap().is_none());
1086 assert!(db.get_channel(cargo_ra_id, a_id).await.unwrap().is_none());
1087});
1088
1089test_both_dbs!(
1090 test_joining_channels_postgres,
1091 test_joining_channels_sqlite,
1092 db,
1093 {
1094 let owner_id = db.create_server("test").await.unwrap().0 as u32;
1095
1096 let user_1 = db
1097 .create_user(
1098 "user1@example.com",
1099 false,
1100 NewUserParams {
1101 github_login: "user1".into(),
1102 github_user_id: 5,
1103 invite_count: 0,
1104 },
1105 )
1106 .await
1107 .unwrap()
1108 .user_id;
1109 let user_2 = db
1110 .create_user(
1111 "user2@example.com",
1112 false,
1113 NewUserParams {
1114 github_login: "user2".into(),
1115 github_user_id: 6,
1116 invite_count: 0,
1117 },
1118 )
1119 .await
1120 .unwrap()
1121 .user_id;
1122
1123 let channel_1 = db
1124 .create_root_channel("channel_1", "1", user_1)
1125 .await
1126 .unwrap();
1127 let room_1 = db.room_id_for_channel(channel_1).await.unwrap();
1128
1129 // can join a room with membership to its channel
1130 let joined_room = db
1131 .join_room(
1132 room_1,
1133 user_1,
1134 Some(channel_1),
1135 ConnectionId { owner_id, id: 1 },
1136 )
1137 .await
1138 .unwrap();
1139 assert_eq!(joined_room.room.participants.len(), 1);
1140
1141 drop(joined_room);
1142 // cannot join a room without membership to its channel
1143 assert!(db
1144 .join_room(
1145 room_1,
1146 user_2,
1147 Some(channel_1),
1148 ConnectionId { owner_id, id: 1 }
1149 )
1150 .await
1151 .is_err());
1152 }
1153);
1154
1155test_both_dbs!(
1156 test_channel_invites_postgres,
1157 test_channel_invites_sqlite,
1158 db,
1159 {
1160 db.create_server("test").await.unwrap();
1161
1162 let user_1 = db
1163 .create_user(
1164 "user1@example.com",
1165 false,
1166 NewUserParams {
1167 github_login: "user1".into(),
1168 github_user_id: 5,
1169 invite_count: 0,
1170 },
1171 )
1172 .await
1173 .unwrap()
1174 .user_id;
1175 let user_2 = db
1176 .create_user(
1177 "user2@example.com",
1178 false,
1179 NewUserParams {
1180 github_login: "user2".into(),
1181 github_user_id: 6,
1182 invite_count: 0,
1183 },
1184 )
1185 .await
1186 .unwrap()
1187 .user_id;
1188
1189 let user_3 = db
1190 .create_user(
1191 "user3@example.com",
1192 false,
1193 NewUserParams {
1194 github_login: "user3".into(),
1195 github_user_id: 7,
1196 invite_count: 0,
1197 },
1198 )
1199 .await
1200 .unwrap()
1201 .user_id;
1202
1203 let channel_1_1 = db
1204 .create_root_channel("channel_1", "1", user_1)
1205 .await
1206 .unwrap();
1207
1208 let channel_1_2 = db
1209 .create_root_channel("channel_2", "2", user_1)
1210 .await
1211 .unwrap();
1212
1213 db.invite_channel_member(channel_1_1, user_2, user_1, false)
1214 .await
1215 .unwrap();
1216 db.invite_channel_member(channel_1_2, user_2, user_1, false)
1217 .await
1218 .unwrap();
1219 db.invite_channel_member(channel_1_1, user_3, user_1, true)
1220 .await
1221 .unwrap();
1222
1223 let user_2_invites = db
1224 .get_channel_invites_for_user(user_2) // -> [channel_1_1, channel_1_2]
1225 .await
1226 .unwrap()
1227 .into_iter()
1228 .map(|channel| channel.id)
1229 .collect::<Vec<_>>();
1230
1231 assert_eq!(user_2_invites, &[channel_1_1, channel_1_2]);
1232
1233 let user_3_invites = db
1234 .get_channel_invites_for_user(user_3) // -> [channel_1_1]
1235 .await
1236 .unwrap()
1237 .into_iter()
1238 .map(|channel| channel.id)
1239 .collect::<Vec<_>>();
1240
1241 assert_eq!(user_3_invites, &[channel_1_1]);
1242
1243 let members = db
1244 .get_channel_member_details(channel_1_1, user_1)
1245 .await
1246 .unwrap();
1247 assert_eq!(
1248 members,
1249 &[
1250 proto::ChannelMember {
1251 user_id: user_1.to_proto(),
1252 kind: proto::channel_member::Kind::Member.into(),
1253 admin: true,
1254 },
1255 proto::ChannelMember {
1256 user_id: user_2.to_proto(),
1257 kind: proto::channel_member::Kind::Invitee.into(),
1258 admin: false,
1259 },
1260 proto::ChannelMember {
1261 user_id: user_3.to_proto(),
1262 kind: proto::channel_member::Kind::Invitee.into(),
1263 admin: true,
1264 },
1265 ]
1266 );
1267
1268 db.respond_to_channel_invite(channel_1_1, user_2, true)
1269 .await
1270 .unwrap();
1271
1272 let channel_1_3 = db
1273 .create_channel("channel_3", Some(channel_1_1), "1", user_1)
1274 .await
1275 .unwrap();
1276
1277 let members = db
1278 .get_channel_member_details(channel_1_3, user_1)
1279 .await
1280 .unwrap();
1281 assert_eq!(
1282 members,
1283 &[
1284 proto::ChannelMember {
1285 user_id: user_1.to_proto(),
1286 kind: proto::channel_member::Kind::Member.into(),
1287 admin: true,
1288 },
1289 proto::ChannelMember {
1290 user_id: user_2.to_proto(),
1291 kind: proto::channel_member::Kind::AncestorMember.into(),
1292 admin: false,
1293 },
1294 ]
1295 );
1296 }
1297);
1298
1299#[gpui::test]
1300async fn test_multiple_signup_overwrite() {
1301 let test_db = TestDb::postgres(build_background_executor());
1302 let db = test_db.db();
1303
1304 let email_address = "user_1@example.com".to_string();
1305
1306 let initial_signup_created_at_milliseconds = 0;
1307
1308 let initial_signup = NewSignup {
1309 email_address: email_address.clone(),
1310 platform_mac: false,
1311 platform_linux: true,
1312 platform_windows: false,
1313 editor_features: vec!["speed".into()],
1314 programming_languages: vec!["rust".into(), "c".into()],
1315 device_id: Some(format!("device_id")),
1316 added_to_mailing_list: false,
1317 created_at: Some(
1318 DateTime::from_timestamp_millis(initial_signup_created_at_milliseconds).unwrap(),
1319 ),
1320 };
1321
1322 db.create_signup(&initial_signup).await.unwrap();
1323
1324 let initial_signup_from_db = db.get_signup(&email_address).await.unwrap();
1325
1326 assert_eq!(
1327 initial_signup_from_db.clone(),
1328 signup::Model {
1329 email_address: initial_signup.email_address,
1330 platform_mac: initial_signup.platform_mac,
1331 platform_linux: initial_signup.platform_linux,
1332 platform_windows: initial_signup.platform_windows,
1333 editor_features: Some(initial_signup.editor_features),
1334 programming_languages: Some(initial_signup.programming_languages),
1335 added_to_mailing_list: initial_signup.added_to_mailing_list,
1336 ..initial_signup_from_db
1337 }
1338 );
1339
1340 let subsequent_signup = NewSignup {
1341 email_address: email_address.clone(),
1342 platform_mac: true,
1343 platform_linux: false,
1344 platform_windows: true,
1345 editor_features: vec!["git integration".into(), "clean design".into()],
1346 programming_languages: vec!["d".into(), "elm".into()],
1347 device_id: Some(format!("different_device_id")),
1348 added_to_mailing_list: true,
1349 // subsequent signup happens next day
1350 created_at: Some(
1351 DateTime::from_timestamp_millis(
1352 initial_signup_created_at_milliseconds + (1000 * 60 * 60 * 24),
1353 )
1354 .unwrap(),
1355 ),
1356 };
1357
1358 db.create_signup(&subsequent_signup).await.unwrap();
1359
1360 let subsequent_signup_from_db = db.get_signup(&email_address).await.unwrap();
1361
1362 assert_eq!(
1363 subsequent_signup_from_db.clone(),
1364 signup::Model {
1365 platform_mac: subsequent_signup.platform_mac,
1366 platform_linux: subsequent_signup.platform_linux,
1367 platform_windows: subsequent_signup.platform_windows,
1368 editor_features: Some(subsequent_signup.editor_features),
1369 programming_languages: Some(subsequent_signup.programming_languages),
1370 device_id: subsequent_signup.device_id,
1371 added_to_mailing_list: subsequent_signup.added_to_mailing_list,
1372 // shouldn't overwrite their creation Datetime - user shouldn't lose their spot in line
1373 created_at: initial_signup_from_db.created_at,
1374 ..subsequent_signup_from_db
1375 }
1376 );
1377}
1378
1379#[gpui::test]
1380async fn test_signups() {
1381 let test_db = TestDb::postgres(build_background_executor());
1382 let db = test_db.db();
1383
1384 let usernames = (0..8).map(|i| format!("person-{i}")).collect::<Vec<_>>();
1385
1386 let all_signups = usernames
1387 .iter()
1388 .enumerate()
1389 .map(|(i, username)| NewSignup {
1390 email_address: format!("{username}@example.com"),
1391 platform_mac: true,
1392 platform_linux: i % 2 == 0,
1393 platform_windows: i % 4 == 0,
1394 editor_features: vec!["speed".into()],
1395 programming_languages: vec!["rust".into(), "c".into()],
1396 device_id: Some(format!("device_id_{i}")),
1397 added_to_mailing_list: i != 0, // One user failed to subscribe
1398 created_at: Some(DateTime::from_timestamp_millis(i as i64).unwrap()), // Signups are consecutive
1399 })
1400 .collect::<Vec<NewSignup>>();
1401
1402 // people sign up on the waitlist
1403 for signup in &all_signups {
1404 // users can sign up multiple times without issues
1405 for _ in 0..2 {
1406 db.create_signup(&signup).await.unwrap();
1407 }
1408 }
1409
1410 assert_eq!(
1411 db.get_waitlist_summary().await.unwrap(),
1412 WaitlistSummary {
1413 count: 8,
1414 mac_count: 8,
1415 linux_count: 4,
1416 windows_count: 2,
1417 unknown_count: 0,
1418 }
1419 );
1420
1421 // retrieve the next batch of signup emails to send
1422 let signups_batch1 = db.get_unsent_invites(3).await.unwrap();
1423 let addresses = signups_batch1
1424 .iter()
1425 .map(|s| &s.email_address)
1426 .collect::<Vec<_>>();
1427 assert_eq!(
1428 addresses,
1429 &[
1430 all_signups[0].email_address.as_str(),
1431 all_signups[1].email_address.as_str(),
1432 all_signups[2].email_address.as_str()
1433 ]
1434 );
1435 assert_ne!(
1436 signups_batch1[0].email_confirmation_code,
1437 signups_batch1[1].email_confirmation_code
1438 );
1439
1440 // the waitlist isn't updated until we record that the emails
1441 // were successfully sent.
1442 let signups_batch = db.get_unsent_invites(3).await.unwrap();
1443 assert_eq!(signups_batch, signups_batch1);
1444
1445 // once the emails go out, we can retrieve the next batch
1446 // of signups.
1447 db.record_sent_invites(&signups_batch1).await.unwrap();
1448 let signups_batch2 = db.get_unsent_invites(3).await.unwrap();
1449 let addresses = signups_batch2
1450 .iter()
1451 .map(|s| &s.email_address)
1452 .collect::<Vec<_>>();
1453 assert_eq!(
1454 addresses,
1455 &[
1456 all_signups[3].email_address.as_str(),
1457 all_signups[4].email_address.as_str(),
1458 all_signups[5].email_address.as_str()
1459 ]
1460 );
1461
1462 // the sent invites are excluded from the summary.
1463 assert_eq!(
1464 db.get_waitlist_summary().await.unwrap(),
1465 WaitlistSummary {
1466 count: 5,
1467 mac_count: 5,
1468 linux_count: 2,
1469 windows_count: 1,
1470 unknown_count: 0,
1471 }
1472 );
1473
1474 // user completes the signup process by providing their
1475 // github account.
1476 let NewUserResult {
1477 user_id,
1478 inviting_user_id,
1479 signup_device_id,
1480 ..
1481 } = db
1482 .create_user_from_invite(
1483 &Invite {
1484 ..signups_batch1[0].clone()
1485 },
1486 NewUserParams {
1487 github_login: usernames[0].clone(),
1488 github_user_id: 0,
1489 invite_count: 5,
1490 },
1491 )
1492 .await
1493 .unwrap()
1494 .unwrap();
1495 let user = db.get_user_by_id(user_id).await.unwrap().unwrap();
1496 assert!(inviting_user_id.is_none());
1497 assert_eq!(user.github_login, usernames[0]);
1498 assert_eq!(
1499 user.email_address,
1500 Some(all_signups[0].email_address.clone())
1501 );
1502 assert_eq!(user.invite_count, 5);
1503 assert_eq!(signup_device_id.unwrap(), "device_id_0");
1504
1505 // cannot redeem the same signup again.
1506 assert!(db
1507 .create_user_from_invite(
1508 &Invite {
1509 email_address: signups_batch1[0].email_address.clone(),
1510 email_confirmation_code: signups_batch1[0].email_confirmation_code.clone(),
1511 },
1512 NewUserParams {
1513 github_login: "some-other-github_account".into(),
1514 github_user_id: 1,
1515 invite_count: 5,
1516 },
1517 )
1518 .await
1519 .unwrap()
1520 .is_none());
1521
1522 // cannot redeem a signup with the wrong confirmation code.
1523 db.create_user_from_invite(
1524 &Invite {
1525 email_address: signups_batch1[1].email_address.clone(),
1526 email_confirmation_code: "the-wrong-code".to_string(),
1527 },
1528 NewUserParams {
1529 github_login: usernames[1].clone(),
1530 github_user_id: 2,
1531 invite_count: 5,
1532 },
1533 )
1534 .await
1535 .unwrap_err();
1536}
1537
1538fn build_background_executor() -> Arc<Background> {
1539 Deterministic::new(0).build_background()
1540}