| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  | use backend::{
 | 
					
						
							|  |  |  |     application::{get_connection_pool, Application},
 | 
					
						
							|  |  |  |     config::{get_configuration, DatabaseSettings},
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:40:34 +08:00
										 |  |  |     context::AppContext,
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2021-08-26 10:19:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:40:34 +08:00
										 |  |  | use backend::application::init_app_context;
 | 
					
						
							| 
									
										
										
										
											2021-11-08 10:57:33 +08:00
										 |  |  | use flowy_backend_api::{user_request::*, workspace_request::*};
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  | use flowy_document::{
 | 
					
						
							| 
									
										
										
										
											2021-10-15 15:52:08 +08:00
										 |  |  |     entities::doc::{Doc, DocIdentifier},
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     prelude::*,
 | 
					
						
							|  |  |  | };
 | 
					
						
							| 
									
										
										
										
											2021-11-08 10:57:33 +08:00
										 |  |  | use flowy_net::errors::ServerError;
 | 
					
						
							|  |  |  | use flowy_user_infra::entities::*;
 | 
					
						
							|  |  |  | use flowy_workspace_infra::entities::prelude::*;
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  | use sqlx::{Connection, Executor, PgConnection, PgPool};
 | 
					
						
							|  |  |  | use uuid::Uuid;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | pub struct TestUserServer {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |     pub host: String,
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |     pub port: u16,
 | 
					
						
							|  |  |  |     pub pg_pool: PgPool,
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub user_token: Option<String>,
 | 
					
						
							|  |  |  |     pub user_id: Option<String>,
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | impl TestUserServer {
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn new() -> Self {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let mut server: TestUserServer = spawn_server().await.into();
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |         let response = server.register_user().await;
 | 
					
						
							|  |  |  |         server.user_token = Some(response.token);
 | 
					
						
							|  |  |  |         server.user_id = Some(response.user_id);
 | 
					
						
							|  |  |  |         server
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 10:57:33 +08:00
										 |  |  |     pub async fn sign_in(&self, params: SignInParams) -> Result<SignInResponse, ServerError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/auth", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-11-08 10:57:33 +08:00
										 |  |  |         let resp = user_sign_in_request(params, &url).await?;
 | 
					
						
							|  |  |  |         Ok(resp)
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn sign_out(&self) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/auth", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let _ = user_sign_out_request(self.user_token(), &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub fn user_token(&self) -> &str { self.user_token.as_ref().expect("must call register_user first ") }
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub fn user_id(&self) -> &str { self.user_id.as_ref().expect("must call register_user first ") }
 | 
					
						
							| 
									
										
										
										
											2021-08-31 17:25:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn get_user_profile(&self) -> UserProfile {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/user", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let user_profile = get_user_profile_request(self.user_token(), &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-09-04 16:53:58 +08:00
										 |  |  |         user_profile
 | 
					
						
							| 
									
										
										
										
											2021-08-31 17:56:38 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 10:57:33 +08:00
										 |  |  |     pub async fn update_user_profile(&self, params: UpdateUserParams) -> Result<(), ServerError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/user", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-11-08 10:57:33 +08:00
										 |  |  |         let _ = update_user_profile_request(self.user_token(), params, &url).await?;
 | 
					
						
							|  |  |  |         Ok(())
 | 
					
						
							| 
									
										
										
										
											2021-09-01 16:08:32 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn create_workspace(&self, params: CreateWorkspaceParams) -> Workspace {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/workspace", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let workspace = create_workspace_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  |         workspace
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 19:19:02 +08:00
										 |  |  |     pub async fn read_workspaces(&self, params: WorkspaceIdentifier) -> RepeatedWorkspace {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/workspace", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let workspaces = read_workspaces_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-27 23:53:53 +08:00
										 |  |  |         workspaces
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn update_workspace(&self, params: UpdateWorkspaceParams) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/workspace", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         update_workspace_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 23:39:51 +08:00
										 |  |  |     pub async fn delete_workspace(&self, params: WorkspaceIdentifier) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/workspace", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         delete_workspace_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn create_app(&self, params: CreateAppParams) -> App {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/app", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let app = create_app_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  |         app
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 12:24:02 +08:00
										 |  |  |     pub async fn read_app(&self, params: AppIdentifier) -> Option<App> {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/app", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let app = read_app_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  |         app
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn update_app(&self, params: UpdateAppParams) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/app", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         update_app_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 17:19:50 +08:00
										 |  |  |     pub async fn delete_app(&self, params: AppIdentifier) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/app", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         delete_app_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn create_view(&self, params: CreateViewParams) -> View {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/view", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let view = create_view_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 21:33:29 +08:00
										 |  |  |         view
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:45:52 +08:00
										 |  |  |     pub async fn read_view(&self, params: ViewIdentifier) -> Option<View> {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/view", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let view = read_view_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 21:33:29 +08:00
										 |  |  |         view
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub async fn update_view(&self, params: UpdateViewParams) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/view", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         update_view_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 21:33:29 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 13:04:09 +08:00
										 |  |  |     pub async fn delete_view(&self, params: ViewIdentifiers) {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/view", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         delete_view_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-25 21:33:29 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-17 22:44:51 +08:00
										 |  |  |     pub async fn create_view_trash(&self, view_id: &str) {
 | 
					
						
							|  |  |  |         let identifier = TrashIdentifier {
 | 
					
						
							|  |  |  |             id: view_id.to_string(),
 | 
					
						
							|  |  |  |             ty: TrashType::View,
 | 
					
						
							|  |  |  |         };
 | 
					
						
							| 
									
										
										
										
											2021-10-16 12:24:02 +08:00
										 |  |  |         let url = format!("{}/api/trash", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-10-17 22:44:51 +08:00
										 |  |  |         create_trash_request(self.user_token(), vec![identifier].into(), &url)
 | 
					
						
							|  |  |  |             .await
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-10-16 12:24:02 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 22:46:05 +08:00
										 |  |  |     pub async fn delete_view_trash(&self, trash_identifiers: TrashIdentifiers) {
 | 
					
						
							| 
									
										
										
										
											2021-10-16 12:24:02 +08:00
										 |  |  |         let url = format!("{}/api/trash", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-10-17 22:44:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 22:46:05 +08:00
										 |  |  |         delete_trash_request(self.user_token(), trash_identifiers, &url)
 | 
					
						
							| 
									
										
										
										
											2021-10-17 22:44:51 +08:00
										 |  |  |             .await
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-10-16 12:24:02 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pub async fn read_trash(&self) -> RepeatedTrash {
 | 
					
						
							|  |  |  |         let url = format!("{}/api/trash", self.http_addr());
 | 
					
						
							|  |  |  |         read_trash_request(self.user_token(), &url).await.unwrap()
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 15:52:08 +08:00
										 |  |  |     pub async fn read_doc(&self, params: DocIdentifier) -> Option<Doc> {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/doc", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let doc = read_doc_request(self.user_token(), params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-09-11 14:26:30 +08:00
										 |  |  |         doc
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 23:21:10 +08:00
										 |  |  |     pub async fn register_user(&self) -> SignUpResponse {
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  |         let params = SignUpParams {
 | 
					
						
							|  |  |  |             email: "annie@appflowy.io".to_string(),
 | 
					
						
							|  |  |  |             name: "annie".to_string(),
 | 
					
						
							|  |  |  |             password: "HelloAppFlowy123!".to_string(),
 | 
					
						
							|  |  |  |         };
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |         self.register(params).await
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 23:21:10 +08:00
										 |  |  |     pub async fn register(&self, params: SignUpParams) -> SignUpResponse {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         let url = format!("{}/api/register", self.http_addr());
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |         let response = user_sign_up_request(params, &url).await.unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  |         response
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-09-17 19:03:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-19 23:21:10 +08:00
										 |  |  |     pub fn http_addr(&self) -> String { format!("http://{}", self.host) }
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub fn ws_addr(&self) -> String { format!("ws://{}/ws/{}", self.host, self.user_token.as_ref().unwrap()) }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl std::convert::From<TestServer> for TestUserServer {
 | 
					
						
							|  |  |  |     fn from(server: TestServer) -> Self {
 | 
					
						
							|  |  |  |         TestUserServer {
 | 
					
						
							|  |  |  |             host: server.host,
 | 
					
						
							|  |  |  |             port: server.port,
 | 
					
						
							|  |  |  |             pg_pool: server.pg_pool,
 | 
					
						
							|  |  |  |             user_token: None,
 | 
					
						
							|  |  |  |             user_id: None,
 | 
					
						
							|  |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2021-09-17 19:03:46 +08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | pub async fn spawn_user_server() -> TestUserServer {
 | 
					
						
							|  |  |  |     let server: TestUserServer = spawn_server().await.into();
 | 
					
						
							|  |  |  |     server
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct TestServer {
 | 
					
						
							|  |  |  |     pub host: String,
 | 
					
						
							|  |  |  |     pub port: u16,
 | 
					
						
							|  |  |  |     pub pg_pool: PgPool,
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:40:34 +08:00
										 |  |  |     pub app_ctx: AppContext,
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  | pub async fn spawn_server() -> TestServer {
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  |     let database_name = format!("{}", Uuid::new_v4().to_string());
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |     let configuration = {
 | 
					
						
							|  |  |  |         let mut c = get_configuration().expect("Failed to read configuration.");
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  |         c.database.database_name = database_name.clone();
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |         // Use a random OS port
 | 
					
						
							|  |  |  |         c.application.port = 0;
 | 
					
						
							|  |  |  |         c
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let _ = configure_database(&configuration.database).await;
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:40:34 +08:00
										 |  |  |     let app_ctx = init_app_context(&configuration).await;
 | 
					
						
							|  |  |  |     let application = Application::build(configuration.clone(), app_ctx.clone())
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to build application.");
 | 
					
						
							|  |  |  |     let application_port = application.port();
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  |     let _ = tokio::spawn(async {
 | 
					
						
							|  |  |  |         let _ = application.run_until_stopped();
 | 
					
						
							| 
									
										
										
										
											2021-09-09 17:34:01 +08:00
										 |  |  |         // drop_test_database(database_name).await;
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  |     });
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     TestServer {
 | 
					
						
							| 
									
										
										
										
											2021-09-18 22:32:00 +08:00
										 |  |  |         host: format!("localhost:{}", application_port),
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |         port: application_port,
 | 
					
						
							|  |  |  |         pg_pool: get_connection_pool(&configuration.database)
 | 
					
						
							|  |  |  |             .await
 | 
					
						
							|  |  |  |             .expect("Failed to connect to the database"),
 | 
					
						
							| 
									
										
										
										
											2021-09-29 17:40:34 +08:00
										 |  |  |         app_ctx,
 | 
					
						
							| 
									
										
										
										
											2021-08-23 22:10:36 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async fn configure_database(config: &DatabaseSettings) -> PgPool {
 | 
					
						
							|  |  |  |     // Create database
 | 
					
						
							|  |  |  |     let mut connection = PgConnection::connect_with(&config.without_db())
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to connect to Postgres");
 | 
					
						
							|  |  |  |     connection
 | 
					
						
							|  |  |  |         .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to create database.");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Migrate database
 | 
					
						
							|  |  |  |     let connection_pool = PgPool::connect_with(config.with_db())
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to connect to Postgres.");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sqlx::migrate!("./migrations")
 | 
					
						
							|  |  |  |         .run(&connection_pool)
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to migrate the database");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     connection_pool
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 17:34:01 +08:00
										 |  |  | #[allow(dead_code)]
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  | async fn drop_test_database(database_name: String) {
 | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     // https://stackoverflow.com/questions/36502401/postgres-drop-database-error-pq-cannot-drop-the-currently-open-database?rq=1
 | 
					
						
							| 
									
										
										
										
											2021-09-06 14:24:22 +08:00
										 |  |  |     let configuration = {
 | 
					
						
							|  |  |  |         let mut c = get_configuration().expect("Failed to read configuration.");
 | 
					
						
							|  |  |  |         c.database.database_name = "flowy".to_owned();
 | 
					
						
							|  |  |  |         c.application.port = 0;
 | 
					
						
							|  |  |  |         c
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let mut connection = PgConnection::connect_with(&configuration.database.without_db())
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to connect to Postgres");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     connection
 | 
					
						
							|  |  |  |         .execute(&*format!(r#"Drop DATABASE "{}";"#, database_name))
 | 
					
						
							|  |  |  |         .await
 | 
					
						
							|  |  |  |         .expect("Failed to drop database.");
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | pub async fn create_test_workspace(server: &TestUserServer) -> Workspace {
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     let params = CreateWorkspaceParams {
 | 
					
						
							|  |  |  |         name: "My first workspace".to_string(),
 | 
					
						
							|  |  |  |         desc: "This is my first workspace".to_string(),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let workspace = server.create_workspace(params).await;
 | 
					
						
							|  |  |  |     workspace
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | pub async fn create_test_app(server: &TestUserServer, workspace_id: &str) -> App {
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     let params = CreateAppParams {
 | 
					
						
							|  |  |  |         workspace_id: workspace_id.to_owned(),
 | 
					
						
							|  |  |  |         name: "My first app".to_string(),
 | 
					
						
							|  |  |  |         desc: "This is my first app".to_string(),
 | 
					
						
							|  |  |  |         color_style: ColorStyle::default(),
 | 
					
						
							|  |  |  |     };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let app = server.create_app(params).await;
 | 
					
						
							|  |  |  |     app
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  | pub async fn create_test_view(application: &TestUserServer, app_id: &str) -> View {
 | 
					
						
							| 
									
										
										
										
											2021-09-14 16:22:44 +08:00
										 |  |  |     let name = "My first view".to_string();
 | 
					
						
							|  |  |  |     let desc = "This is my first view".to_string();
 | 
					
						
							|  |  |  |     let thumbnail = "http://1.png".to_string();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let params = CreateViewParams::new(app_id.to_owned(), name, desc, ViewType::Doc, thumbnail);
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     let app = application.create_view(params).await;
 | 
					
						
							|  |  |  |     app
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct WorkspaceTest {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub server: TestUserServer,
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub workspace: Workspace,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl WorkspaceTest {
 | 
					
						
							|  |  |  |     pub async fn new() -> Self {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let server = TestUserServer::new().await;
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |         let workspace = create_test_workspace(&server).await;
 | 
					
						
							|  |  |  |         Self { server, workspace }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub async fn create_app(&self) -> App { create_test_app(&self.server, &self.workspace.id).await }
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct AppTest {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub server: TestUserServer,
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub workspace: Workspace,
 | 
					
						
							|  |  |  |     pub app: App,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl AppTest {
 | 
					
						
							|  |  |  |     pub async fn new() -> Self {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let server = TestUserServer::new().await;
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |         let workspace = create_test_workspace(&server).await;
 | 
					
						
							|  |  |  |         let app = create_test_app(&server, &workspace.id).await;
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         Self { server, workspace, app }
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub struct ViewTest {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |     pub server: TestUserServer,
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |     pub workspace: Workspace,
 | 
					
						
							|  |  |  |     pub app: App,
 | 
					
						
							|  |  |  |     pub view: View,
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl ViewTest {
 | 
					
						
							|  |  |  |     pub async fn new() -> Self {
 | 
					
						
							| 
									
										
										
										
											2021-09-27 23:23:23 +08:00
										 |  |  |         let server = TestUserServer::new().await;
 | 
					
						
							| 
									
										
										
										
											2021-09-10 18:21:35 +08:00
										 |  |  |         let workspace = create_test_workspace(&server).await;
 | 
					
						
							|  |  |  |         let app = create_test_app(&server, &workspace.id).await;
 | 
					
						
							|  |  |  |         let view = create_test_view(&server, &app.id).await;
 | 
					
						
							|  |  |  |         Self {
 | 
					
						
							|  |  |  |             server,
 | 
					
						
							|  |  |  |             workspace,
 | 
					
						
							|  |  |  |             app,
 | 
					
						
							|  |  |  |             view,
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |