auth_test.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. use std::collections::HashMap;
  2. use nanoid::nanoid;
  3. use flowy_server::supabase::define::{USER_EMAIL, USER_UUID};
  4. use flowy_test::event_builder::EventBuilder;
  5. use flowy_test::FlowyCoreTest;
  6. use flowy_user::entities::{
  7. AuthTypePB, ThirdPartyAuthPB, UpdateUserProfilePayloadPB, UserProfilePB,
  8. };
  9. use flowy_user::errors::ErrorCode;
  10. use flowy_user::event_map::UserEvent::*;
  11. use crate::util::*;
  12. #[tokio::test]
  13. async fn third_party_sign_up_test() {
  14. if get_supabase_config().is_some() {
  15. let test = FlowyCoreTest::new();
  16. let mut map = HashMap::new();
  17. map.insert(USER_UUID.to_string(), uuid::Uuid::new_v4().to_string());
  18. map.insert(
  19. USER_EMAIL.to_string(),
  20. format!("{}@appflowy.io", nanoid!(6)),
  21. );
  22. let payload = ThirdPartyAuthPB {
  23. map,
  24. auth_type: AuthTypePB::Supabase,
  25. };
  26. let response = EventBuilder::new(test.clone())
  27. .event(ThirdPartyAuth)
  28. .payload(payload)
  29. .async_send()
  30. .await
  31. .parse::<UserProfilePB>();
  32. dbg!(&response);
  33. }
  34. }
  35. #[tokio::test]
  36. async fn third_party_sign_up_with_duplicated_uuid() {
  37. if get_supabase_config().is_some() {
  38. let test = FlowyCoreTest::new();
  39. let email = format!("{}@appflowy.io", nanoid!(6));
  40. let mut map = HashMap::new();
  41. map.insert(USER_UUID.to_string(), uuid::Uuid::new_v4().to_string());
  42. map.insert(USER_EMAIL.to_string(), email.clone());
  43. let response_1 = EventBuilder::new(test.clone())
  44. .event(ThirdPartyAuth)
  45. .payload(ThirdPartyAuthPB {
  46. map: map.clone(),
  47. auth_type: AuthTypePB::Supabase,
  48. })
  49. .async_send()
  50. .await
  51. .parse::<UserProfilePB>();
  52. dbg!(&response_1);
  53. let response_2 = EventBuilder::new(test.clone())
  54. .event(ThirdPartyAuth)
  55. .payload(ThirdPartyAuthPB {
  56. map: map.clone(),
  57. auth_type: AuthTypePB::Supabase,
  58. })
  59. .async_send()
  60. .await
  61. .parse::<UserProfilePB>();
  62. assert_eq!(response_1, response_2);
  63. };
  64. }
  65. #[tokio::test]
  66. async fn third_party_sign_up_with_duplicated_email() {
  67. if get_supabase_config().is_some() {
  68. let test = FlowyCoreTest::new();
  69. let email = format!("{}@appflowy.io", nanoid!(6));
  70. test
  71. .third_party_sign_up_with_uuid(&uuid::Uuid::new_v4().to_string(), Some(email.clone()))
  72. .await
  73. .unwrap();
  74. let error = test
  75. .third_party_sign_up_with_uuid(&uuid::Uuid::new_v4().to_string(), Some(email.clone()))
  76. .await
  77. .err()
  78. .unwrap();
  79. assert_eq!(error.code, ErrorCode::Conflict.value());
  80. };
  81. }
  82. #[tokio::test]
  83. async fn sign_up_as_guest_and_then_update_to_new_cloud_user_test() {
  84. if get_supabase_config().is_some() {
  85. let test = FlowyCoreTest::new_with_guest_user().await;
  86. let old_views = test
  87. .folder_manager
  88. .get_current_workspace_views()
  89. .await
  90. .unwrap();
  91. let old_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  92. let uuid = uuid::Uuid::new_v4().to_string();
  93. test
  94. .third_party_sign_up_with_uuid(&uuid, None)
  95. .await
  96. .unwrap();
  97. let new_views = test
  98. .folder_manager
  99. .get_current_workspace_views()
  100. .await
  101. .unwrap();
  102. let new_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  103. assert_eq!(old_views.len(), new_views.len());
  104. assert_eq!(old_workspace.name, new_workspace.name);
  105. assert_eq!(old_workspace.views.len(), new_workspace.views.len());
  106. for (index, view) in old_views.iter().enumerate() {
  107. assert_eq!(view.name, new_views[index].name);
  108. assert_eq!(view.id, new_views[index].id);
  109. assert_eq!(view.layout, new_views[index].layout);
  110. assert_eq!(view.create_time, new_views[index].create_time);
  111. }
  112. }
  113. }
  114. #[tokio::test]
  115. async fn sign_up_as_guest_and_then_update_to_existing_cloud_user_test() {
  116. if get_supabase_config().is_some() {
  117. let test = FlowyCoreTest::new_with_guest_user().await;
  118. let uuid = uuid::Uuid::new_v4().to_string();
  119. let email = format!("{}@appflowy.io", nanoid!(6));
  120. // The workspace of the guest will be migrated to the new user with given uuid
  121. let _user_profile = test
  122. .third_party_sign_up_with_uuid(&uuid, Some(email.clone()))
  123. .await
  124. .unwrap();
  125. let old_cloud_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  126. let old_cloud_views = test
  127. .folder_manager
  128. .get_current_workspace_views()
  129. .await
  130. .unwrap();
  131. assert_eq!(old_cloud_views.len(), 1);
  132. assert_eq!(old_cloud_views.first().unwrap().child_views.len(), 1);
  133. // sign out and then sign in as a guest
  134. test.sign_out().await;
  135. let _sign_up_context = test.sign_up_as_guest().await;
  136. let new_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  137. test
  138. .create_view(&new_workspace.id, "new workspace child view".to_string())
  139. .await;
  140. let new_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  141. assert_eq!(new_workspace.views.len(), 2);
  142. // upload to cloud user with given uuid. This time the workspace of the guest will not be merged
  143. // because the cloud user already has a workspace
  144. test
  145. .third_party_sign_up_with_uuid(&uuid, Some(email))
  146. .await
  147. .unwrap();
  148. let new_cloud_workspace = test.folder_manager.get_current_workspace().await.unwrap();
  149. let new_cloud_views = test
  150. .folder_manager
  151. .get_current_workspace_views()
  152. .await
  153. .unwrap();
  154. assert_eq!(new_cloud_workspace, old_cloud_workspace);
  155. assert_eq!(new_cloud_views, old_cloud_views);
  156. }
  157. }
  158. #[tokio::test]
  159. async fn check_not_exist_user_test() {
  160. if let Some(test) = FlowySupabaseTest::new() {
  161. let err = test
  162. .check_user_with_uuid(&uuid::Uuid::new_v4().to_string())
  163. .await
  164. .unwrap_err();
  165. assert_eq!(err.code, ErrorCode::RecordNotFound.value());
  166. }
  167. }
  168. #[tokio::test]
  169. async fn get_user_profile_test() {
  170. if let Some(test) = FlowySupabaseTest::new() {
  171. let uuid = uuid::Uuid::new_v4().to_string();
  172. test
  173. .third_party_sign_up_with_uuid(&uuid, None)
  174. .await
  175. .unwrap();
  176. let result = test.get_user_profile().await;
  177. assert!(result.is_ok());
  178. }
  179. }
  180. #[tokio::test]
  181. async fn update_user_profile_test() {
  182. if let Some(test) = FlowySupabaseTest::new() {
  183. let uuid = uuid::Uuid::new_v4().to_string();
  184. let profile = test
  185. .third_party_sign_up_with_uuid(&uuid, None)
  186. .await
  187. .unwrap();
  188. test
  189. .update_user_profile(UpdateUserProfilePayloadPB::new(profile.id).name("lucas"))
  190. .await;
  191. let new_profile = test.get_user_profile().await.unwrap();
  192. assert_eq!(new_profile.name, "lucas")
  193. }
  194. }
  195. #[tokio::test]
  196. async fn update_user_profile_with_existing_email_test() {
  197. if let Some(test) = FlowySupabaseTest::new() {
  198. let email = format!("{}@appflowy.io", nanoid!(6));
  199. let _ = test
  200. .third_party_sign_up_with_uuid(&uuid::Uuid::new_v4().to_string(), Some(email.clone()))
  201. .await;
  202. let profile = test
  203. .third_party_sign_up_with_uuid(
  204. &uuid::Uuid::new_v4().to_string(),
  205. Some(format!("{}@appflowy.io", nanoid!(6))),
  206. )
  207. .await
  208. .unwrap();
  209. let error = test
  210. .update_user_profile(
  211. UpdateUserProfilePayloadPB::new(profile.id)
  212. .name("lucas")
  213. .email(&email),
  214. )
  215. .await
  216. .unwrap();
  217. assert_eq!(error.code, ErrorCode::Conflict.value());
  218. }
  219. }