helper.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, mut params: CreateWorkspaceParams) -> Workspace {
  27. let url = format!("{}/api/workspace", self.address);
  28. let response = self.register_test_user().await;
  29. params.user_id = Some(response.uid);
  30. let workspace = create_workspace_request(params, &url).await.unwrap();
  31. workspace
  32. }
  33. pub async fn read_workspace(&self, params: QueryWorkspaceParams) -> Workspace {
  34. let url = format!("{}/api/workspace", self.address);
  35. let workspace = read_workspace_request(params, &url).await.unwrap();
  36. workspace
  37. }
  38. async fn register_test_user(&self) -> SignUpResponse {
  39. let params = SignUpParams {
  40. email: "[email protected]".to_string(),
  41. name: "annie".to_string(),
  42. password: "HelloAppFlowy123!".to_string(),
  43. };
  44. let response = self.register_user(params).await;
  45. response
  46. }
  47. }
  48. pub async fn spawn_app() -> TestApp {
  49. let configuration = {
  50. let mut c = get_configuration().expect("Failed to read configuration.");
  51. c.database.database_name = Uuid::new_v4().to_string();
  52. // Use a random OS port
  53. c.application.port = 0;
  54. c
  55. };
  56. let _ = configure_database(&configuration.database).await;
  57. let application = Application::build(configuration.clone())
  58. .await
  59. .expect("Failed to build application.");
  60. let application_port = application.port();
  61. let _ = tokio::spawn(application.run_until_stopped());
  62. TestApp {
  63. address: format!("http://localhost:{}", application_port),
  64. port: application_port,
  65. pg_pool: get_connection_pool(&configuration.database)
  66. .await
  67. .expect("Failed to connect to the database"),
  68. }
  69. }
  70. async fn configure_database(config: &DatabaseSettings) -> PgPool {
  71. // Create database
  72. let mut connection = PgConnection::connect_with(&config.without_db())
  73. .await
  74. .expect("Failed to connect to Postgres");
  75. connection
  76. .execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
  77. .await
  78. .expect("Failed to create database.");
  79. // Migrate database
  80. let connection_pool = PgPool::connect_with(config.with_db())
  81. .await
  82. .expect("Failed to connect to Postgres.");
  83. sqlx::migrate!("./migrations")
  84. .run(&connection_pool)
  85. .await
  86. .expect("Failed to migrate the database");
  87. connection_pool
  88. }