util.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. use std::ops::Deref;
  2. use std::time::Duration;
  3. use tokio::sync::mpsc::Receiver;
  4. use tokio::time::timeout;
  5. use flowy_server_config::supabase_config::SupabaseConfiguration;
  6. use flowy_test::event_builder::EventBuilder;
  7. use flowy_test::FlowyCoreTest;
  8. use flowy_user::entities::{
  9. AuthTypePB, UpdateUserProfilePayloadPB, UserCredentialsPB, UserProfilePB,
  10. };
  11. use flowy_user::errors::FlowyError;
  12. use flowy_user::event_map::UserCloudServiceProvider;
  13. use flowy_user::event_map::UserEvent::*;
  14. use flowy_user::services::AuthType;
  15. /// In order to run this test, you need to create a .env.test file in the root directory of this project
  16. /// and add the following environment variables:
  17. /// - SUPABASE_URL
  18. /// - SUPABASE_ANON_KEY
  19. /// - SUPABASE_KEY
  20. /// - SUPABASE_JWT_SECRET
  21. /// - SUPABASE_DB
  22. /// - SUPABASE_DB_USER
  23. /// - SUPABASE_DB_PORT
  24. /// - SUPABASE_DB_PASSWORD
  25. ///
  26. /// the .env.test file should look like this:
  27. /// SUPABASE_URL=https://<your-supabase-url>.supabase.co
  28. /// SUPABASE_ANON_KEY=<your-supabase-anon-key>
  29. /// SUPABASE_KEY=<your-supabase-key>
  30. /// SUPABASE_JWT_SECRET=<your-supabase-jwt-secret>
  31. /// SUPABASE_DB=db.xxx.supabase.co
  32. /// SUPABASE_DB_USER=<your-supabase-db-user>
  33. /// SUPABASE_DB_PORT=<your-supabase-db-port>
  34. /// SUPABASE_DB_PASSWORD=<your-supabase-db-password>
  35. ///
  36. pub fn get_supabase_config() -> Option<SupabaseConfiguration> {
  37. dotenv::from_path(".env.test").ok()?;
  38. SupabaseConfiguration::from_env().ok()
  39. }
  40. pub struct FlowySupabaseTest {
  41. inner: FlowyCoreTest,
  42. }
  43. impl FlowySupabaseTest {
  44. pub fn new() -> Option<Self> {
  45. let _ = get_supabase_config()?;
  46. let test = FlowyCoreTest::new();
  47. test.set_auth_type(AuthTypePB::Supabase);
  48. test.server_provider.set_auth_type(AuthType::Supabase);
  49. Some(Self { inner: test })
  50. }
  51. pub async fn check_user_with_uuid(&self, uuid: &str) -> Result<(), FlowyError> {
  52. match EventBuilder::new(self.inner.clone())
  53. .event(CheckUser)
  54. .payload(UserCredentialsPB::from_uuid(uuid))
  55. .async_send()
  56. .await
  57. .error()
  58. {
  59. None => Ok(()),
  60. Some(error) => Err(error),
  61. }
  62. }
  63. pub async fn get_user_profile(&self) -> Result<UserProfilePB, FlowyError> {
  64. EventBuilder::new(self.inner.clone())
  65. .event(GetUserProfile)
  66. .async_send()
  67. .await
  68. .try_parse::<UserProfilePB>()
  69. }
  70. pub async fn update_user_profile(&self, payload: UpdateUserProfilePayloadPB) {
  71. EventBuilder::new(self.inner.clone())
  72. .event(UpdateUserProfile)
  73. .payload(payload)
  74. .async_send()
  75. .await;
  76. }
  77. }
  78. impl Deref for FlowySupabaseTest {
  79. type Target = FlowyCoreTest;
  80. fn deref(&self) -> &Self::Target {
  81. &self.inner
  82. }
  83. }
  84. pub async fn receive_with_timeout<T>(
  85. receiver: &mut Receiver<T>,
  86. duration: Duration,
  87. ) -> Result<T, Box<dyn std::error::Error>> {
  88. let res = timeout(duration, receiver.recv())
  89. .await?
  90. .ok_or(anyhow::anyhow!("recv timeout"))?;
  91. Ok(res)
  92. }