auth_test.rs 7.8 KB

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