Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
chore: update test
Nathan530e076838d3
diff --git a/frontend/rust-lib/event-integration-test/tests/user/af_cloud_test/workspace_test.rs b/frontend/rust-lib/event-integration-test/tests/user/af_cloud_test/workspace_test.rsindex ce50575954..d390c0558e 100644--- a/frontend/rust-lib/event-integration-test/tests/user/af_cloud_test/workspace_test.rs+++ b/frontend/rust-lib/event-integration-test/tests/user/af_cloud_test/workspace_test.rs@@ -6,6 +6,7 @@ use collab_folder::Folder;use event_integration_test::user_event::use_localhost_af_cloud;use event_integration_test::EventIntegrationTest;use flowy_user::entities::AFRolePB;+use flowy_user_pub::cloud::UserCloudServiceProvider;use flowy_user_pub::entities::AuthType;use std::time::Duration;use tokio::task::LocalSet;@@ -151,7 +152,7 @@ async fn af_cloud_open_workspace_test() {test.open_workspace(&first_workspace.workspace_id,- first_workspace.workspace_auth_type.clone(),+ first_workspace.workspace_auth_type,).await;sleep(Duration::from_millis(300)).await;@@ -162,7 +163,7 @@ async fn af_cloud_open_workspace_test() {test.open_workspace(&second_workspace.workspace_id,- second_workspace.workspace_auth_type.clone(),+ second_workspace.workspace_auth_type,).await;sleep(Duration::from_millis(200)).await;@@ -175,7 +176,7 @@ async fn af_cloud_open_workspace_test() {test.open_workspace(&first_workspace.workspace_id,- first_workspace.workspace_auth_type.clone(),+ first_workspace.workspace_auth_type,).await;let views_1 = test.get_all_workspace_views().await;@@ -187,7 +188,7 @@ async fn af_cloud_open_workspace_test() {test.open_workspace(&second_workspace.workspace_id,- second_workspace.workspace_auth_type.clone(),+ second_workspace.workspace_auth_type,).await;let views_2 = test.get_all_workspace_views().await;@@ -246,10 +247,7 @@ async fn af_cloud_different_open_same_workspace_test() {let index = i % 2;let iter_workspace_id = &all_workspaces[index].workspace_id;client- .open_workspace(- iter_workspace_id,- all_workspaces[index].workspace_auth_type.clone(),- )+ .open_workspace(iter_workspace_id, all_workspaces[index].workspace_auth_type).await;if iter_workspace_id == &cloned_shared_workspace_id {let views = client.get_all_workspace_views().await;@@ -296,42 +294,127 @@ async fn af_cloud_different_open_same_workspace_test() {#[tokio::test]async fn af_cloud_create_local_workspace_test() {+ // Setup: Initialize test environment with AppFlowyClouduse_localhost_af_cloud().await;let test = EventIntegrationTest::new().await;let _ = test.af_cloud_sign_up().await;- let workspaces = test.get_all_workspaces().await.items;- assert_eq!(workspaces.len(), 1);+ // Verify initial state: User should have one default workspace+ let initial_workspaces = test.get_all_workspaces().await.items;+ assert_eq!(+ initial_workspaces.len(),+ 1,+ "User should start with one default workspace"+ );- let created_workspace = test+ // make sure the workspaces order is consistent+ // tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;++ // Test: Create a local workspace+ let local_workspace = test.create_workspace("my local workspace", AuthType::Local).await;- assert_eq!(created_workspace.name, "my local workspace");- let workspaces = test.get_all_workspaces().await.items;- assert_eq!(workspaces.len(), 2);- assert_eq!(workspaces[1].name, "my local workspace");+ // Verify: Local workspace was created correctly+ assert_eq!(local_workspace.name, "my local workspace");+ let updated_workspaces = test.get_all_workspaces().await.items;+ assert_eq!(+ updated_workspaces.len(),+ 2,+ "Should now have two workspaces"+ );+ dbg!(&updated_workspaces);++ // Find local workspace by name instead of using index+ let found_local_workspace = updated_workspaces+ .iter()+ .find(|workspace| workspace.name == "my local workspace")+ .expect("Local workspace should exist");+ assert_eq!(found_local_workspace.name, "my local workspace");+ // Test: Open the local workspacetest.open_workspace(- &created_workspace.workspace_id,- created_workspace.workspace_auth_type,+ &local_workspace.workspace_id,+ local_workspace.workspace_auth_type,).await;+ // Verify: Views in the local workspacelet views = test.get_all_views().await;- assert_eq!(views.len(), 2);- assert!(views- .iter()- .any(|view| view.parent_view_id == workspaces[1].workspace_id));-+ assert_eq!(+ views.len(),+ 2,+ "Local workspace should have 2 default views"+ );+ assert!(+ views+ .iter()+ .any(|view| view.parent_view_id == local_workspace.workspace_id),+ "Views should belong to the local workspace"+ );++ // Verify: Can access all viewsfor view in views {test.get_view(&view.id).await;}+ // Verify: Local workspace memberslet members = test- .get_workspace_members(&created_workspace.workspace_id)+ .get_workspace_members(&local_workspace.workspace_id).await;- assert_eq!(members.len(), 1);- assert_eq!(members[0].role, AFRolePB::Owner);+ assert_eq!(+ members.len(),+ 1,+ "Local workspace should have only one member"+ );+ assert_eq!(members[0].role, AFRolePB::Owner, "User should be the owner");++ // Test: Create a server workspace+ let server_workspace = test+ .create_workspace("my server workspace", AuthType::AppFlowyCloud)+ .await;++ // Verify: Server workspace was created correctly+ assert_eq!(server_workspace.name, "my server workspace");+ let final_workspaces = test.get_all_workspaces().await.items;+ assert_eq!(+ final_workspaces.len(),+ 3,+ "Should now have three workspaces"+ );++ dbg!(&final_workspaces);++ // Find workspaces by name instead of using indices+ let found_local_workspace = final_workspaces+ .iter()+ .find(|workspace| workspace.name == "my local workspace")+ .expect("Local workspace should exist");+ assert_eq!(found_local_workspace.name, "my local workspace");++ let found_server_workspace = final_workspaces+ .iter()+ .find(|workspace| workspace.name == "my server workspace")+ .expect("Server workspace should exist");+ assert_eq!(found_server_workspace.name, "my server workspace");++ // Verify: Server-side only recognizes cloud workspaces (not local ones)+ let user_profile = test.get_user_profile().await.unwrap();+ test+ .server_provider+ .set_server_auth_type(&AuthType::AppFlowyCloud, Some(user_profile.token.clone()))+ .unwrap();+ test.server_provider.set_token(&user_profile.token).unwrap();++ let user_service = test.server_provider.get_server().unwrap().user_service();+ let server_workspaces = user_service+ .get_all_workspace(user_profile.id)+ .await+ .unwrap();+ assert_eq!(+ server_workspaces.len(),+ 2,+ "Server should only see 2 workspaces (the default and server workspace, not the local one)"+ );}diff --git a/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs b/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rsindex 6c9581d4a7..c49757f735 100644--- a/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs+++ b/frontend/rust-lib/flowy-core/src/deps_resolve/cloud_service_impl.rs@@ -161,6 +161,7 @@ impl StorageCloudService for ServerProvider {impl UserCloudServiceProvider for ServerProvider {fn set_token(&self, token: &str) -> Result<(), FlowyError> {let server = self.get_server()?;+ info!("Set token");server.set_token(token)?;Ok(())}@@ -191,8 +192,12 @@ impl UserCloudServiceProvider for ServerProvider {/// to create a new [AppFlowyServer] if it doesn't exist. Once the [AuthType] is set,/// it will be used when user open the app again.///- fn set_server_auth_type(&self, auth_type: &AuthType) {+ fn set_server_auth_type(&self, auth_type: &AuthType, token: Option<String>) -> FlowyResult<()> {self.set_auth_type(*auth_type);+ if let Some(token) = token {+ self.set_token(&token)?;+ }+ Ok(())}fn get_server_auth_type(&self) -> AuthType {diff --git a/frontend/rust-lib/flowy-user-pub/src/cloud.rs b/frontend/rust-lib/flowy-user-pub/src/cloud.rsindex 743d39ed4f..a99e8b8672 100644--- a/frontend/rust-lib/flowy-user-pub/src/cloud.rs+++ b/frontend/rust-lib/flowy-user-pub/src/cloud.rs@@ -84,7 +84,11 @@ pub trait UserCloudServiceProvider: Send + Sync {/// * `enable_sync`: A boolean indicating whether synchronization should be enabled or disabled.fn set_enable_sync(&self, uid: i64, enable_sync: bool);- fn set_server_auth_type(&self, auth_type: &AuthType);+ fn set_server_auth_type(+ &self,+ auth_type: &AuthType,+ token: Option<String>,+ ) -> Result<(), FlowyError>;fn get_server_auth_type(&self) -> AuthType;diff --git a/frontend/rust-lib/flowy-user-pub/src/sql/user_sql.rs b/frontend/rust-lib/flowy-user-pub/src/sql/user_sql.rsindex a78172aa9f..ca117300f2 100644--- a/frontend/rust-lib/flowy-user-pub/src/sql/user_sql.rs+++ b/frontend/rust-lib/flowy-user-pub/src/sql/user_sql.rs@@ -165,6 +165,11 @@ pub fn select_user_auth_type(Ok(AuthType::from(row.auth_type))}+pub fn select_user_token(uid: i64, conn: &mut SqliteConnection) -> Result<String, FlowyError> {+ let row = select_user_table_row(uid, conn)?;+ Ok(row.token)+}+pub fn upsert_user(user: UserTable, mut conn: DBConnection) -> FlowyResult<()> {conn.immediate_transaction(|conn| {// delete old user if existsdiff --git a/frontend/rust-lib/flowy-user/src/entities/workspace.rs b/frontend/rust-lib/flowy-user/src/entities/workspace.rsindex 0f7b7f4587..860bda3be7 100644--- a/frontend/rust-lib/flowy-user/src/entities/workspace.rs+++ b/frontend/rust-lib/flowy-user/src/entities/workspace.rs@@ -242,7 +242,7 @@ pub struct CreateWorkspacePB {pub auth_type: AuthTypePB,}-#[derive(ProtoBuf_Enum, Default, Debug, Clone, Eq, PartialEq)]+#[derive(ProtoBuf_Enum, Copy, Default, Debug, Clone, Eq, PartialEq)]#[repr(u8)]pub enum AuthTypePB {#[default]diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager.rsindex 2118362a8b..b95ac3baaf 100644--- a/frontend/rust-lib/flowy-user/src/user_manager/manager.rs+++ b/frontend/rust-lib/flowy-user/src/user_manager/manager.rs@@ -131,7 +131,8 @@ impl UserManager {.get_user_profile_from_disk(session.user_id, &session.user_workspace.id).await?;let auth_type = user.workspace_auth_type;- self.cloud_service.set_server_auth_type(&auth_type);+ let token = self.token_from_auth_type(&auth_type)?;+ self.cloud_service.set_server_auth_type(&auth_type, token)?;event!(tracing::Level::INFO,@@ -326,7 +327,7 @@ impl UserManager {params: SignInParams,auth_type: AuthType,) -> Result<UserProfile, FlowyError> {- self.cloud_service.set_server_auth_type(&auth_type);+ self.cloud_service.set_server_auth_type(&auth_type, None)?;let response: AuthResponse = self.cloud_service@@ -374,7 +375,7 @@ impl UserManager {auth_type: AuthType,params: BoxAny,) -> Result<UserProfile, FlowyError> {- self.cloud_service.set_server_auth_type(&auth_type);+ self.cloud_service.set_server_auth_type(&auth_type, None)?;// sign out the current user if there is onelet migration_user = self.get_migration_user(&auth_type).await;@@ -600,6 +601,16 @@ impl UserManager {self.authenticate_user.user_paths.user_data_dir(uid)}+ pub fn token_from_auth_type(&self, auth_type: &AuthType) -> FlowyResult<Option<String>> {+ match auth_type {+ AuthType::Local => Ok(None),+ AuthType::AppFlowyCloud => {+ let uid = self.user_id()?;+ let mut conn = self.db_connection(uid)?;+ Ok(select_user_token(uid, &mut conn).ok())+ },+ }+ }pub fn user_setting(&self) -> Result<UserSettingPB, FlowyError> {let session = self.get_session()?;let user_setting = UserSettingPB {@@ -639,7 +650,9 @@ impl UserManager {authenticator: &AuthType,email: &str,) -> Result<String, FlowyError> {- self.cloud_service.set_server_auth_type(authenticator);+ self+ .cloud_service+ .set_server_auth_type(authenticator, None)?;let auth_service = self.cloud_service.get_user_service()?;let url = auth_service.generate_sign_in_url_with_email(email).await?;@@ -654,7 +667,7 @@ impl UserManager {) -> Result<GotrueTokenResponse, FlowyError> {self.cloud_service- .set_server_auth_type(&AuthType::AppFlowyCloud);+ .set_server_auth_type(&AuthType::AppFlowyCloud, None)?;let auth_service = self.cloud_service.get_user_service()?;let response = auth_service.sign_in_with_password(email, password).await?;Ok(response)@@ -668,7 +681,7 @@ impl UserManager {) -> Result<(), FlowyError> {self.cloud_service- .set_server_auth_type(&AuthType::AppFlowyCloud);+ .set_server_auth_type(&AuthType::AppFlowyCloud, None)?;let auth_service = self.cloud_service.get_user_service()?;auth_service.sign_in_with_magic_link(email, redirect_to)@@ -684,7 +697,7 @@ impl UserManager {) -> Result<GotrueTokenResponse, FlowyError> {self.cloud_service- .set_server_auth_type(&AuthType::AppFlowyCloud);+ .set_server_auth_type(&AuthType::AppFlowyCloud, None)?;let auth_service = self.cloud_service.get_user_service()?;let response = auth_service.sign_in_with_passcode(email, passcode).await?;Ok(response)@@ -697,7 +710,7 @@ impl UserManager {) -> Result<String, FlowyError> {self.cloud_service- .set_server_auth_type(&AuthType::AppFlowyCloud);+ .set_server_auth_type(&AuthType::AppFlowyCloud, None)?;let auth_service = self.cloud_service.get_user_service()?;let url = auth_service.generate_oauth_url_with_provider(oauth_provider)diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rsindex 78a13d7229..b78d635133 100644--- a/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs+++ b/frontend/rust-lib/flowy-user/src/user_manager/manager_user_workspace.rs@@ -155,7 +155,8 @@ impl UserManager {pub async fn open_workspace(&self, workspace_id: &Uuid, auth_type: AuthType) -> FlowyResult<()> {info!("open workspace: {}, auth type:{}", workspace_id, auth_type);let workspace_id_str = workspace_id.to_string();- self.cloud_service.set_server_auth_type(&auth_type);+ let token = self.token_from_auth_type(&auth_type)?;+ self.cloud_service.set_server_auth_type(&auth_type, token)?;let uid = self.user_id()?;let profile = self@@ -227,7 +228,8 @@ impl UserManager {workspace_name: &str,auth_type: AuthType,) -> FlowyResult<UserWorkspace> {- self.cloud_service.set_server_auth_type(&auth_type);+ let token = self.token_from_auth_type(&auth_type)?;+ self.cloud_service.set_server_auth_type(&auth_type, token)?;let new_workspace = self.cloud_service