helper.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. use backend::{
  2. application::{get_connection_pool, Application},
  3. config::{get_configuration, DatabaseSettings},
  4. };
  5. use flowy_net::request::HttpRequestBuilder;
  6. use flowy_user::prelude::*;
  7. use flowy_workspace::prelude::*;
  8. use sqlx::{Connection, Executor, PgConnection, PgPool};
  9. use uuid::Uuid;
  10. pub struct TestApp {
  11. pub address: String,
  12. pub port: u16,
  13. pub pg_pool: PgPool,
  14. }
  15. impl TestApp {
  16. pub async fn register_user(&self, params: SignUpParams) -> SignUpResponse {
  17. let url = format!("{}/api/register", self.address);
  18. let resp = user_sign_up(params, &url).await.unwrap();
  19. resp
  20. }
  21. pub async fn sign_in(&self, params: SignInParams) -> SignInResponse {
  22. let url = format!("{}/api/auth", self.address);
  23. let resp = user_sign_in(params, &url).await.unwrap();
  24. resp
  25. }
  26. pub async fn create_workspace(&self, params: CreateWorkspaceParams) -> Workspace {
  27. let url = format!("{}/api/workspace", self.address);
  28. let workspace = create_workspace_request(params, &url).await.unwrap();
  29. workspace
  30. }
  31. pub async fn read_workspace(&self, params: QueryWorkspaceParams) -> Option<Workspace> {
  32. let url = format!("{}/api/workspace", self.address);
  33. let workspace = read_workspace_request(params, &url).await.unwrap();
  34. workspace
  35. }
  36. pub async fn update_workspace(&self, params: UpdateWorkspaceParams) {
  37. let url = format!("{}/api/workspace", self.address);
  38. update_workspace_request(params, &url).await.unwrap();
  39. }
  40. pub async fn delete_workspace(&self, params: DeleteWorkspaceParams) {
  41. let url = format!("{}/api/workspace", self.address);
  42. delete_workspace_request(params, &url).await.unwrap();
  43. }
  44. pub async fn create_app(&self, params: CreateAppParams) -> App {
  45. let url = format!("{}/api/app", self.address);
  46. let app = create_app_request(params, &url).await.unwrap();
  47. app
  48. }
  49. pub async fn read_app(&self, params: QueryAppParams) -> Option<App> {
  50. let url = format!("{}/api/app", self.address);
  51. let app = read_app_request(params, &url).await.unwrap();
  52. app
  53. }
  54. pub async fn update_app(&self, params: UpdateAppParams) {
  55. let url = format!("{}/api/app", self.address);
  56. update_app_request(params, &url).await.unwrap();
  57. }
  58. pub async fn delete_app(&self, params: DeleteAppParams) {
  59. let url = format!("{}/api/app", self.address);
  60. delete_app_request(params, &url).await.unwrap();
  61. }
  62. pub async fn create_view(&self, params: CreateViewParams) -> View {
  63. let url = format!("{}/api/view", self.address);
  64. let view = create_view_request(params, &url).await.unwrap();
  65. view
  66. }
  67. pub async fn read_view(&self, params: QueryViewParams) -> Option<View> {
  68. let url = format!("{}/api/view", self.address);
  69. let view = read_view_request(params, &url).await.unwrap();
  70. view
  71. }
  72. pub async fn update_view(&self, params: UpdateViewParams) {
  73. let url = format!("{}/api/view", self.address);
  74. update_view_request(params, &url).await.unwrap();
  75. }
  76. pub async fn delete_view(&self, params: DeleteViewParams) {
  77. let url = format!("{}/api/view", self.address);
  78. delete_view_request(params, &url).await.unwrap();
  79. }
  80. pub(crate) async fn register_test_user(&self) -> SignUpResponse {
  81. let params = SignUpParams {
  82. email: "[email protected]".to_string(),
  83. name: "annie".to_string(),
  84. password: "HelloAppFlowy123!".to_string(),
  85. };
  86. let response = self.register_user(params).await;
  87. response
  88. }
  89. }
  90. pub async fn spawn_app() -> TestApp {
  91. let configuration = {
  92. let mut c = get_configuration().expect("Failed to read configuration.");
  93. c.database.database_name = Uuid::new_v4().to_string();
  94. // Use a random OS port
  95. c.application.port = 0;
  96. c
  97. };
  98. let _ = configure_database(&configuration.database).await;
  99. let application = Application::build(configuration.clone())
  100. .await
  101. .expect("Failed to build application.");
  102. let application_port = application.port();
  103. let _ = tokio::spawn(application.run_until_stopped());
  104. TestApp {
  105. address: format!("http://localhost:{}", application_port),
  106. port: application_port,
  107. pg_pool: get_connection_pool(&configuration.database)
  108. .await
  109. .expect("Failed to connect to the database"),
  110. }
  111. }
  112. async fn configure_database(config: &DatabaseSettings) -> PgPool {
  113. // Create database
  114. let mut connection = PgConnection::connect_with(&config.without_db())
  115. .await
  116. .expect("Failed to connect to Postgres");
  117. connection
  118. .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
  119. .await
  120. .expect("Failed to create database.");
  121. // Migrate database
  122. let connection_pool = PgPool::connect_with(config.with_db())
  123. .await
  124. .expect("Failed to connect to Postgres.");
  125. sqlx::migrate!("./migrations")
  126. .run(&connection_pool)
  127. .await
  128. .expect("Failed to migrate the database");
  129. connection_pool
  130. }