auth_test.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. use std::collections::HashMap;
  2. use flowy_test::event_builder::EventBuilder;
  3. use flowy_test::FlowyCoreTest;
  4. use flowy_user::entities::{
  5. AuthTypePB, ThirdPartyAuthPB, UpdateUserProfilePayloadPB, UserProfilePB,
  6. };
  7. use flowy_user::errors::ErrorCode;
  8. use flowy_user::event_map::UserEvent::*;
  9. use crate::util::*;
  10. #[tokio::test]
  11. async fn third_party_sign_up_test() {
  12. if get_supabase_config().is_some() {
  13. let test = FlowyCoreTest::new();
  14. let mut map = HashMap::new();
  15. map.insert("uuid".to_string(), uuid::Uuid::new_v4().to_string());
  16. let payload = ThirdPartyAuthPB {
  17. map,
  18. auth_type: AuthTypePB::Supabase,
  19. };
  20. let response = EventBuilder::new(test.clone())
  21. .event(ThirdPartyAuth)
  22. .payload(payload)
  23. .async_send()
  24. .await
  25. .parse::<UserProfilePB>();
  26. dbg!(&response);
  27. }
  28. }
  29. #[tokio::test]
  30. async fn sign_up_as_guest_and_then_update_to_new_cloud_user_test() {
  31. if get_supabase_config().is_some() {
  32. let test = FlowyCoreTest::new_with_guest_user().await;
  33. let old_views = test
  34. .folder_manager
  35. .get_current_workspace_views()
  36. .await
  37. .unwrap();
  38. let old_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  39. let uuid = uuid::Uuid::new_v4().to_string();
  40. test.supabase_party_sign_up(&uuid).await;
  41. let new_views = test
  42. .folder_manager
  43. .get_current_workspace_views()
  44. .await
  45. .unwrap();
  46. let new_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  47. assert_eq!(old_views.len(), new_views.len());
  48. assert_eq!(old_workspace.name, new_workspace.name);
  49. assert_eq!(old_workspace.views.len(), new_workspace.views.len());
  50. for (index, view) in old_views.iter().enumerate() {
  51. assert_eq!(view.name, new_views[index].name);
  52. assert_eq!(view.id, new_views[index].id);
  53. assert_eq!(view.layout, new_views[index].layout);
  54. assert_eq!(view.create_time, new_views[index].create_time);
  55. }
  56. }
  57. }
  58. #[tokio::test]
  59. async fn sign_up_as_guest_and_then_update_to_existing_cloud_user_test() {
  60. if get_supabase_config().is_some() {
  61. let test = FlowyCoreTest::new_with_guest_user().await;
  62. let historical_users = test.user_session.sign_in_history();
  63. assert_eq!(historical_users.len(), 1);
  64. let uuid = uuid::Uuid::new_v4().to_string();
  65. // The workspace of the guest will be migrated to the new user with given uuid
  66. let user_profile = test.supabase_party_sign_up(&uuid).await;
  67. // let historical_users = test.user_session.sign_in_history(user_profile.id);
  68. // assert_eq!(historical_users.len(), 2);
  69. let old_cloud_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  70. let old_cloud_views = test
  71. .folder_manager
  72. .get_current_workspace_views()
  73. .await
  74. .unwrap();
  75. assert_eq!(old_cloud_views.len(), 1);
  76. assert_eq!(old_cloud_views.first().unwrap().child_views.len(), 1);
  77. // sign out and then sign in as a guest
  78. test.sign_out().await;
  79. // when sign out, the user profile will be not found
  80. let error = test
  81. .user_session
  82. .get_user_profile(user_profile.id, false)
  83. .await
  84. .err()
  85. .unwrap();
  86. assert_eq!(error.code, ErrorCode::RecordNotFound.value());
  87. let _sign_up_context = test.sign_up_as_guest().await;
  88. // assert_eq!(
  89. // test
  90. // .user_session
  91. // .sign_in_history(sign_up_context.user_profile.id)
  92. // .len(),
  93. // 3
  94. // );
  95. let new_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  96. test
  97. .create_view(&new_workspace.id, "new workspace child view".to_string())
  98. .await;
  99. let new_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  100. assert_eq!(new_workspace.views.len(), 2);
  101. // upload to cloud user with given uuid. This time the workspace of the guest will not be merged
  102. // because the cloud user already has a workspace
  103. test.supabase_party_sign_up(&uuid).await;
  104. // assert_eq!(test.user_session.sign_in_history().len(), 3);
  105. let new_cloud_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  106. let new_cloud_views = test
  107. .folder_manager
  108. .get_current_workspace_views()
  109. .await
  110. .unwrap();
  111. assert_eq!(new_cloud_workspace, old_cloud_workspace);
  112. assert_eq!(new_cloud_views, old_cloud_views);
  113. }
  114. }
  115. #[tokio::test]
  116. async fn check_not_exist_user_test() {
  117. if let Some(test) = FlowySupabaseTest::new() {
  118. let err = test
  119. .check_user_with_uuid(&uuid::Uuid::new_v4().to_string())
  120. .await
  121. .unwrap_err();
  122. assert_eq!(err.code, ErrorCode::UserNotExist.value());
  123. }
  124. }
  125. #[tokio::test]
  126. async fn get_user_profile_test() {
  127. if let Some(test) = FlowySupabaseTest::new() {
  128. let uuid = uuid::Uuid::new_v4().to_string();
  129. test.sign_up_with_uuid(&uuid).await;
  130. let result = test.get_user_profile().await;
  131. assert!(result.is_ok());
  132. }
  133. }
  134. #[tokio::test]
  135. async fn update_user_profile_test() {
  136. if let Some(test) = FlowySupabaseTest::new() {
  137. let uuid = uuid::Uuid::new_v4().to_string();
  138. let profile = test.sign_up_with_uuid(&uuid).await;
  139. test
  140. .update_user_profile(UpdateUserProfilePayloadPB::new(profile.id).name("lucas"))
  141. .await;
  142. let new_profile = test.get_user_profile().await.unwrap();
  143. assert_eq!(new_profile.name, "lucas")
  144. }
  145. }