helper.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. use backend::{
  2. application::{get_connection_pool, Application},
  3. config::{get_configuration, DatabaseSettings},
  4. context::AppContext,
  5. };
  6. use backend::application::init_app_context;
  7. use flowy_document::{
  8. entities::doc::{Doc, QueryDocParams},
  9. prelude::*,
  10. };
  11. use flowy_user::{errors::UserError, prelude::*};
  12. use flowy_workspace::prelude::{server::*, *};
  13. use sqlx::{Connection, Executor, PgConnection, PgPool};
  14. use uuid::Uuid;
  15. pub struct TestUserServer {
  16. pub host: String,
  17. pub port: u16,
  18. pub pg_pool: PgPool,
  19. pub user_token: Option<String>,
  20. pub user_id: Option<String>,
  21. }
  22. impl TestUserServer {
  23. pub async fn new() -> Self {
  24. let mut server: TestUserServer = spawn_server().await.into();
  25. let response = server.register_user().await;
  26. server.user_token = Some(response.token);
  27. server.user_id = Some(response.user_id);
  28. server
  29. }
  30. pub async fn sign_in(&self, params: SignInParams) -> Result<SignInResponse, UserError> {
  31. let url = format!("{}/api/auth", self.http_addr());
  32. user_sign_in_request(params, &url).await
  33. }
  34. pub async fn sign_out(&self) {
  35. let url = format!("{}/api/auth", self.http_addr());
  36. let _ = user_sign_out_request(self.user_token(), &url).await.unwrap();
  37. }
  38. pub fn user_token(&self) -> &str { self.user_token.as_ref().expect("must call register_user first ") }
  39. pub fn user_id(&self) -> &str { self.user_id.as_ref().expect("must call register_user first ") }
  40. pub async fn get_user_profile(&self) -> UserProfile {
  41. let url = format!("{}/api/user", self.http_addr());
  42. let user_profile = get_user_profile_request(self.user_token(), &url).await.unwrap();
  43. user_profile
  44. }
  45. pub async fn update_user_profile(&self, params: UpdateUserParams) -> Result<(), UserError> {
  46. let url = format!("{}/api/user", self.http_addr());
  47. update_user_profile_request(self.user_token(), params, &url).await
  48. }
  49. pub async fn create_workspace(&self, params: CreateWorkspaceParams) -> Workspace {
  50. let url = format!("{}/api/workspace", self.http_addr());
  51. let workspace = create_workspace_request(self.user_token(), params, &url).await.unwrap();
  52. workspace
  53. }
  54. pub async fn read_workspaces(&self, params: QueryWorkspaceParams) -> RepeatedWorkspace {
  55. let url = format!("{}/api/workspace", self.http_addr());
  56. let workspaces = read_workspaces_request(self.user_token(), params, &url).await.unwrap();
  57. workspaces
  58. }
  59. pub async fn update_workspace(&self, params: UpdateWorkspaceParams) {
  60. let url = format!("{}/api/workspace", self.http_addr());
  61. update_workspace_request(self.user_token(), params, &url).await.unwrap();
  62. }
  63. pub async fn delete_workspace(&self, params: DeleteWorkspaceParams) {
  64. let url = format!("{}/api/workspace", self.http_addr());
  65. delete_workspace_request(self.user_token(), params, &url).await.unwrap();
  66. }
  67. pub async fn create_app(&self, params: CreateAppParams) -> App {
  68. let url = format!("{}/api/app", self.http_addr());
  69. let app = create_app_request(self.user_token(), params, &url).await.unwrap();
  70. app
  71. }
  72. pub async fn read_app(&self, params: QueryAppParams) -> Option<App> {
  73. let url = format!("{}/api/app", self.http_addr());
  74. let app = read_app_request(self.user_token(), params, &url).await.unwrap();
  75. app
  76. }
  77. pub async fn update_app(&self, params: UpdateAppParams) {
  78. let url = format!("{}/api/app", self.http_addr());
  79. update_app_request(self.user_token(), params, &url).await.unwrap();
  80. }
  81. pub async fn delete_app(&self, params: DeleteAppParams) {
  82. let url = format!("{}/api/app", self.http_addr());
  83. delete_app_request(self.user_token(), params, &url).await.unwrap();
  84. }
  85. pub async fn create_view(&self, params: CreateViewParams) -> View {
  86. let url = format!("{}/api/view", self.http_addr());
  87. let view = create_view_request(self.user_token(), params, &url).await.unwrap();
  88. view
  89. }
  90. pub async fn read_view(&self, params: QueryViewParams) -> Option<View> {
  91. let url = format!("{}/api/view", self.http_addr());
  92. let view = read_view_request(self.user_token(), params, &url).await.unwrap();
  93. view
  94. }
  95. pub async fn update_view(&self, params: UpdateViewParams) {
  96. let url = format!("{}/api/view", self.http_addr());
  97. update_view_request(self.user_token(), params, &url).await.unwrap();
  98. }
  99. pub async fn delete_view(&self, params: DeleteViewParams) {
  100. let url = format!("{}/api/view", self.http_addr());
  101. delete_view_request(self.user_token(), params, &url).await.unwrap();
  102. }
  103. pub async fn read_doc(&self, params: QueryDocParams) -> Option<Doc> {
  104. let url = format!("{}/api/doc", self.http_addr());
  105. let doc = read_doc_request(self.user_token(), params, &url).await.unwrap();
  106. doc
  107. }
  108. pub async fn register_user(&self) -> SignUpResponse {
  109. let params = SignUpParams {
  110. email: "[email protected]".to_string(),
  111. name: "annie".to_string(),
  112. password: "HelloAppFlowy123!".to_string(),
  113. };
  114. self.register(params).await
  115. }
  116. pub async fn register(&self, params: SignUpParams) -> SignUpResponse {
  117. let url = format!("{}/api/register", self.http_addr());
  118. let response = user_sign_up_request(params, &url).await.unwrap();
  119. response
  120. }
  121. pub fn http_addr(&self) -> String { format!("http://{}", self.host) }
  122. pub fn ws_addr(&self) -> String { format!("ws://{}/ws/{}", self.host, self.user_token.as_ref().unwrap()) }
  123. }
  124. impl std::convert::From<TestServer> for TestUserServer {
  125. fn from(server: TestServer) -> Self {
  126. TestUserServer {
  127. host: server.host,
  128. port: server.port,
  129. pg_pool: server.pg_pool,
  130. user_token: None,
  131. user_id: None,
  132. }
  133. }
  134. }
  135. pub async fn spawn_user_server() -> TestUserServer {
  136. let server: TestUserServer = spawn_server().await.into();
  137. server
  138. }
  139. pub struct TestServer {
  140. pub host: String,
  141. pub port: u16,
  142. pub pg_pool: PgPool,
  143. pub app_ctx: AppContext,
  144. }
  145. pub async fn spawn_server() -> TestServer {
  146. let database_name = format!("{}", Uuid::new_v4().to_string());
  147. let configuration = {
  148. let mut c = get_configuration().expect("Failed to read configuration.");
  149. c.database.database_name = database_name.clone();
  150. // Use a random OS port
  151. c.application.port = 0;
  152. c
  153. };
  154. let _ = configure_database(&configuration.database).await;
  155. let app_ctx = init_app_context(&configuration).await;
  156. let application = Application::build(configuration.clone(), app_ctx.clone())
  157. .await
  158. .expect("Failed to build application.");
  159. let application_port = application.port();
  160. let _ = tokio::spawn(async {
  161. let _ = application.run_until_stopped();
  162. // drop_test_database(database_name).await;
  163. });
  164. TestServer {
  165. host: format!("localhost:{}", application_port),
  166. port: application_port,
  167. pg_pool: get_connection_pool(&configuration.database)
  168. .await
  169. .expect("Failed to connect to the database"),
  170. app_ctx,
  171. }
  172. }
  173. async fn configure_database(config: &DatabaseSettings) -> PgPool {
  174. // Create database
  175. let mut connection = PgConnection::connect_with(&config.without_db())
  176. .await
  177. .expect("Failed to connect to Postgres");
  178. connection
  179. .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
  180. .await
  181. .expect("Failed to create database.");
  182. // Migrate database
  183. let connection_pool = PgPool::connect_with(config.with_db())
  184. .await
  185. .expect("Failed to connect to Postgres.");
  186. sqlx::migrate!("./migrations")
  187. .run(&connection_pool)
  188. .await
  189. .expect("Failed to migrate the database");
  190. connection_pool
  191. }
  192. #[allow(dead_code)]
  193. async fn drop_test_database(database_name: String) {
  194. // https://stackoverflow.com/questions/36502401/postgres-drop-database-error-pq-cannot-drop-the-currently-open-database?rq=1
  195. let configuration = {
  196. let mut c = get_configuration().expect("Failed to read configuration.");
  197. c.database.database_name = "flowy".to_owned();
  198. c.application.port = 0;
  199. c
  200. };
  201. let mut connection = PgConnection::connect_with(&configuration.database.without_db())
  202. .await
  203. .expect("Failed to connect to Postgres");
  204. connection
  205. .execute(&*format!(r#"Drop DATABASE "{}";"#, database_name))
  206. .await
  207. .expect("Failed to drop database.");
  208. }
  209. pub async fn create_test_workspace(server: &TestUserServer) -> Workspace {
  210. let params = CreateWorkspaceParams {
  211. name: "My first workspace".to_string(),
  212. desc: "This is my first workspace".to_string(),
  213. };
  214. let workspace = server.create_workspace(params).await;
  215. workspace
  216. }
  217. pub async fn create_test_app(server: &TestUserServer, workspace_id: &str) -> App {
  218. let params = CreateAppParams {
  219. workspace_id: workspace_id.to_owned(),
  220. name: "My first app".to_string(),
  221. desc: "This is my first app".to_string(),
  222. color_style: ColorStyle::default(),
  223. };
  224. let app = server.create_app(params).await;
  225. app
  226. }
  227. pub async fn create_test_view(application: &TestUserServer, app_id: &str) -> View {
  228. let name = "My first view".to_string();
  229. let desc = "This is my first view".to_string();
  230. let thumbnail = "http://1.png".to_string();
  231. let params = CreateViewParams::new(app_id.to_owned(), name, desc, ViewType::Doc, thumbnail);
  232. let app = application.create_view(params).await;
  233. app
  234. }
  235. pub struct WorkspaceTest {
  236. pub server: TestUserServer,
  237. pub workspace: Workspace,
  238. }
  239. impl WorkspaceTest {
  240. pub async fn new() -> Self {
  241. let server = TestUserServer::new().await;
  242. let workspace = create_test_workspace(&server).await;
  243. Self { server, workspace }
  244. }
  245. pub async fn create_app(&self) -> App { create_test_app(&self.server, &self.workspace.id).await }
  246. }
  247. pub struct AppTest {
  248. pub server: TestUserServer,
  249. pub workspace: Workspace,
  250. pub app: App,
  251. }
  252. impl AppTest {
  253. pub async fn new() -> Self {
  254. let server = TestUserServer::new().await;
  255. let workspace = create_test_workspace(&server).await;
  256. let app = create_test_app(&server, &workspace.id).await;
  257. Self { server, workspace, app }
  258. }
  259. }
  260. pub struct ViewTest {
  261. pub server: TestUserServer,
  262. pub workspace: Workspace,
  263. pub app: App,
  264. pub view: View,
  265. }
  266. impl ViewTest {
  267. pub async fn new() -> Self {
  268. let server = TestUserServer::new().await;
  269. let workspace = create_test_workspace(&server).await;
  270. let app = create_test_app(&server, &workspace.id).await;
  271. let view = create_test_view(&server, &app.id).await;
  272. Self {
  273. server,
  274. workspace,
  275. app,
  276. view,
  277. }
  278. }
  279. }