From 652ead1c2ba45667c3df0408269d81a6d188949c Mon Sep 17 00:00:00 2001 From: Fabrice Bellamy <12b@distrilab.fr> Date: Mon, 21 Oct 2024 17:50:55 +0200 Subject: [PATCH] remove the notes model added by loco app template and everything related to it --- nixin_farm_ssr/migration/src/lib.rs | 2 - .../migration/src/m20231103_114510_notes.rs | 34 ----- nixin_farm_ssr/src/app.rs | 10 +- nixin_farm_ssr/src/controllers/mod.rs | 1 - nixin_farm_ssr/src/controllers/notes.rs | 75 ----------- nixin_farm_ssr/src/fixtures/notes.yaml | 11 -- nixin_farm_ssr/src/fixtures/users.yaml | 13 +- nixin_farm_ssr/src/models/_entities/mod.rs | 1 - nixin_farm_ssr/src/models/_entities/notes.rs | 18 --- .../src/models/_entities/prelude.rs | 1 - nixin_farm_ssr/src/models/mod.rs | 1 - nixin_farm_ssr/src/models/notes.rs | 7 - nixin_farm_ssr/tests/requests/auth.rs | 12 +- nixin_farm_ssr/tests/requests/mod.rs | 1 - nixin_farm_ssr/tests/requests/notes.rs | 123 ------------------ nixin_farm_ssr/tests/requests/prepare_data.rs | 7 +- .../snapshots/can_add_note@notes_request.snap | 8 -- .../can_delete_note@notes_request.snap | 8 -- .../snapshots/can_get_note@notes_request.snap | 8 -- .../can_get_notes@notes_request.snap | 8 -- 20 files changed, 19 insertions(+), 330 deletions(-) delete mode 100644 nixin_farm_ssr/migration/src/m20231103_114510_notes.rs delete mode 100644 nixin_farm_ssr/src/controllers/notes.rs delete mode 100644 nixin_farm_ssr/src/fixtures/notes.yaml delete mode 100644 nixin_farm_ssr/src/models/_entities/notes.rs delete mode 100644 nixin_farm_ssr/src/models/notes.rs delete mode 100644 nixin_farm_ssr/tests/requests/notes.rs delete mode 100644 nixin_farm_ssr/tests/requests/snapshots/can_add_note@notes_request.snap delete mode 100644 nixin_farm_ssr/tests/requests/snapshots/can_delete_note@notes_request.snap delete mode 100644 nixin_farm_ssr/tests/requests/snapshots/can_get_note@notes_request.snap delete mode 100644 nixin_farm_ssr/tests/requests/snapshots/can_get_notes@notes_request.snap diff --git a/nixin_farm_ssr/migration/src/lib.rs b/nixin_farm_ssr/migration/src/lib.rs index 5b28008..c62898e 100644 --- a/nixin_farm_ssr/migration/src/lib.rs +++ b/nixin_farm_ssr/migration/src/lib.rs @@ -3,7 +3,6 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_users; -mod m20231103_114510_notes; mod m20241016_181828_servers; mod m20241021_121449_bundles; @@ -16,7 +15,6 @@ impl MigratorTrait for Migrator { fn migrations() -> Vec> { vec![ Box::new(m20220101_000001_users::Migration), - Box::new(m20231103_114510_notes::Migration), Box::new(m20241016_181828_servers::Migration), Box::new(m20241021_121449_bundles::Migration), Box::new(m20241021_121806_services::Migration), diff --git a/nixin_farm_ssr/migration/src/m20231103_114510_notes.rs b/nixin_farm_ssr/migration/src/m20231103_114510_notes.rs deleted file mode 100644 index d0d8a5a..0000000 --- a/nixin_farm_ssr/migration/src/m20231103_114510_notes.rs +++ /dev/null @@ -1,34 +0,0 @@ -use loco_rs::schema::table_auto_tz; -use sea_orm_migration::{prelude::*, schema::*}; - -#[derive(DeriveMigrationName)] -pub struct Migration; - -#[async_trait::async_trait] -impl MigrationTrait for Migration { - async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .create_table( - table_auto_tz(Notes::Table) - .col(pk_auto(Notes::Id)) - .col(string_null(Notes::Title)) - .col(string_null(Notes::Content)) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table(Table::drop().table(Notes::Table).to_owned()) - .await - } -} - -#[derive(DeriveIden)] -enum Notes { - Table, - Id, - Title, - Content, -} diff --git a/nixin_farm_ssr/src/app.rs b/nixin_farm_ssr/src/app.rs index 81d0c80..f1d2b8d 100644 --- a/nixin_farm_ssr/src/app.rs +++ b/nixin_farm_ssr/src/app.rs @@ -16,7 +16,7 @@ use sea_orm::DatabaseConnection; use crate::{ controllers, initializers, - models::_entities::{notes, users}, + models::_entities::{servers, bundles, services, bundles_services, users}, tasks, workers::downloader::DownloadWorker, }; @@ -53,7 +53,6 @@ impl Hooks for App { .add_route(controllers::service::routes()) .add_route(controllers::bundle::routes()) .add_route(controllers::server::routes()) - .add_route(controllers::notes::routes()) .add_route(controllers::auth::routes()) .add_route(controllers::user::routes()) } @@ -69,13 +68,16 @@ impl Hooks for App { async fn truncate(db: &DatabaseConnection) -> Result<()> { truncate_table(db, users::Entity).await?; - truncate_table(db, notes::Entity).await?; + truncate_table(db, bundles_services::Entity).await?; + truncate_table(db, services::Entity).await?; + truncate_table(db, bundles::Entity).await?; + truncate_table(db, servers::Entity).await?; Ok(()) } async fn seed(db: &DatabaseConnection, base: &Path) -> Result<()> { db::seed::(db, &base.join("users.yaml").display().to_string()).await?; - db::seed::(db, &base.join("notes.yaml").display().to_string()).await?; + // ToDo: add seeds for other entities Ok(()) } } \ No newline at end of file diff --git a/nixin_farm_ssr/src/controllers/mod.rs b/nixin_farm_ssr/src/controllers/mod.rs index 3a2ad1b..ae02a00 100644 --- a/nixin_farm_ssr/src/controllers/mod.rs +++ b/nixin_farm_ssr/src/controllers/mod.rs @@ -1,5 +1,4 @@ pub mod auth; -pub mod notes; pub mod user; pub mod server; diff --git a/nixin_farm_ssr/src/controllers/notes.rs b/nixin_farm_ssr/src/controllers/notes.rs deleted file mode 100644 index c95aa3e..0000000 --- a/nixin_farm_ssr/src/controllers/notes.rs +++ /dev/null @@ -1,75 +0,0 @@ -#![allow(clippy::missing_errors_doc)] -#![allow(clippy::unnecessary_struct_initialization)] -#![allow(clippy::unused_async)] -use axum::debug_handler; -use loco_rs::prelude::*; -use serde::{Deserialize, Serialize}; - -use crate::models::_entities::notes::{ActiveModel, Entity, Model}; - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Params { - pub title: Option, - pub content: Option, -} - -impl Params { - fn update(&self, item: &mut ActiveModel) { - item.title = Set(self.title.clone()); - item.content = Set(self.content.clone()); - } -} - -async fn load_item(ctx: &AppContext, id: i32) -> Result { - let item = Entity::find_by_id(id).one(&ctx.db).await?; - item.ok_or_else(|| Error::NotFound) -} - -#[debug_handler] -pub async fn list(State(ctx): State) -> Result { - format::json(Entity::find().all(&ctx.db).await?) -} - -#[debug_handler] -pub async fn add(State(ctx): State, Json(params): Json) -> Result { - let mut item = ActiveModel { - ..Default::default() - }; - params.update(&mut item); - let item = item.insert(&ctx.db).await?; - format::json(item) -} - -#[debug_handler] -pub async fn update( - Path(id): Path, - State(ctx): State, - Json(params): Json, -) -> Result { - let item = load_item(&ctx, id).await?; - let mut item = item.into_active_model(); - params.update(&mut item); - let item = item.update(&ctx.db).await?; - format::json(item) -} - -#[debug_handler] -pub async fn remove(Path(id): Path, State(ctx): State) -> Result { - load_item(&ctx, id).await?.delete(&ctx.db).await?; - format::empty() -} - -#[debug_handler] -pub async fn get_one(Path(id): Path, State(ctx): State) -> Result { - format::json(load_item(&ctx, id).await?) -} - -pub fn routes() -> Routes { - Routes::new() - .prefix("api/notes") - .add("/", get(list)) - .add("/", post(add)) - .add("/:id", get(get_one)) - .add("/:id", delete(remove)) - .add("/:id", post(update)) -} diff --git a/nixin_farm_ssr/src/fixtures/notes.yaml b/nixin_farm_ssr/src/fixtures/notes.yaml deleted file mode 100644 index 0e66c95..0000000 --- a/nixin_farm_ssr/src/fixtures/notes.yaml +++ /dev/null @@ -1,11 +0,0 @@ ---- -- id: 1 - title: Loco note 1 - content: Loco note 1 content - created_at: "2023-11-12T12:34:56.789Z" - updated_at: "2023-11-12T12:34:56.789Z" -- id: 2 - title: Loco note 2 - content: Loco note 2 content - created_at: "2023-11-12T12:34:56.789Z" - updated_at: "2023-11-12T12:34:56.789Z" diff --git a/nixin_farm_ssr/src/fixtures/users.yaml b/nixin_farm_ssr/src/fixtures/users.yaml index 8f5b5ed..7982273 100644 --- a/nixin_farm_ssr/src/fixtures/users.yaml +++ b/nixin_farm_ssr/src/fixtures/users.yaml @@ -1,17 +1,10 @@ --- - id: 1 pid: 11111111-1111-1111-1111-111111111111 - email: user1@example.com + email: test@nixin.local.com password: "$argon2id$v=19$m=19456,t=2,p=1$ETQBx4rTgNAZhSaeYZKOZg$eYTdH26CRT6nUJtacLDEboP0li6xUwUF/q5nSlQ8uuc" api_key: lo-95ec80d7-cb60-4b70-9b4b-9ef74cb88758 - name: user1 - created_at: "2023-11-12T12:34:56.789Z" - updated_at: "2023-11-12T12:34:56.789Z" -- id: 2 - pid: 22222222-2222-2222-2222-222222222222 - email: user2@example.com - password: "$argon2id$v=19$m=19456,t=2,p=1$ETQBx4rTgNAZhSaeYZKOZg$eYTdH26CRT6nUJtacLDEboP0li6xUwUF/q5nSlQ8uuc" - api_key: lo-153561ca-fa84-4e1b-813a-c62526d0a77e - name: user2 + name: test created_at: "2023-11-12T12:34:56.789Z" updated_at: "2023-11-12T12:34:56.789Z" + diff --git a/nixin_farm_ssr/src/models/_entities/mod.rs b/nixin_farm_ssr/src/models/_entities/mod.rs index 94770ef..09f7167 100644 --- a/nixin_farm_ssr/src/models/_entities/mod.rs +++ b/nixin_farm_ssr/src/models/_entities/mod.rs @@ -4,7 +4,6 @@ pub mod prelude; pub mod bundles; pub mod bundles_services; -pub mod notes; pub mod servers; pub mod services; pub mod users; diff --git a/nixin_farm_ssr/src/models/_entities/notes.rs b/nixin_farm_ssr/src/models/_entities/notes.rs deleted file mode 100644 index 0bf2b7c..0000000 --- a/nixin_farm_ssr/src/models/_entities/notes.rs +++ /dev/null @@ -1,18 +0,0 @@ -//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 - -use sea_orm::entity::prelude::*; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] -#[sea_orm(table_name = "notes")] -pub struct Model { - pub created_at: DateTimeWithTimeZone, - pub updated_at: DateTimeWithTimeZone, - #[sea_orm(primary_key)] - pub id: i32, - pub title: Option, - pub content: Option, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} diff --git a/nixin_farm_ssr/src/models/_entities/prelude.rs b/nixin_farm_ssr/src/models/_entities/prelude.rs index b3143e7..8b9bf25 100644 --- a/nixin_farm_ssr/src/models/_entities/prelude.rs +++ b/nixin_farm_ssr/src/models/_entities/prelude.rs @@ -2,7 +2,6 @@ pub use super::bundles::Entity as Bundles; pub use super::bundles_services::Entity as BundlesServices; -pub use super::notes::Entity as Notes; pub use super::servers::Entity as Servers; pub use super::services::Entity as Services; pub use super::users::Entity as Users; diff --git a/nixin_farm_ssr/src/models/mod.rs b/nixin_farm_ssr/src/models/mod.rs index 476b1fc..5389997 100644 --- a/nixin_farm_ssr/src/models/mod.rs +++ b/nixin_farm_ssr/src/models/mod.rs @@ -1,5 +1,4 @@ pub mod _entities; -pub mod notes; pub mod users; pub mod servers; pub mod bundles; diff --git a/nixin_farm_ssr/src/models/notes.rs b/nixin_farm_ssr/src/models/notes.rs deleted file mode 100644 index 1102598..0000000 --- a/nixin_farm_ssr/src/models/notes.rs +++ /dev/null @@ -1,7 +0,0 @@ -use sea_orm::entity::prelude::*; - -use super::_entities::notes::ActiveModel; - -impl ActiveModelBehavior for ActiveModel { - // extend activemodel below (keep comment for generators) -} diff --git a/nixin_farm_ssr/tests/requests/auth.rs b/nixin_farm_ssr/tests/requests/auth.rs index 608c84c..6410973 100644 --- a/nixin_farm_ssr/tests/requests/auth.rs +++ b/nixin_farm_ssr/tests/requests/auth.rs @@ -23,9 +23,9 @@ async fn can_register() { configure_insta!(); testing::request::(|request, ctx| async move { - let email = "test@loco.com"; + let email = "test@nixin.local"; let payload = serde_json::json!({ - "name": "loco", + "name": "test", "email": email, "password": "12341234" }); @@ -57,9 +57,9 @@ async fn can_login_with_verify(#[case] test_name: &str, #[case] password: &str) configure_insta!(); testing::request::(|request, ctx| async move { - let email = "test@loco.com"; + let email = "test@nixin.local"; let register_payload = serde_json::json!({ - "name": "loco", + "name": "test", "email": email, "password": "12341234" }); @@ -107,10 +107,10 @@ async fn can_login_without_verify() { configure_insta!(); testing::request::(|request, _ctx| async move { - let email = "test@loco.com"; + let email = "test@nixin.local"; let password = "12341234"; let register_payload = serde_json::json!({ - "name": "loco", + "name": "test", "email": email, "password": password }); diff --git a/nixin_farm_ssr/tests/requests/mod.rs b/nixin_farm_ssr/tests/requests/mod.rs index 81ed68f..b26f447 100644 --- a/nixin_farm_ssr/tests/requests/mod.rs +++ b/nixin_farm_ssr/tests/requests/mod.rs @@ -1,4 +1,3 @@ mod auth; -mod notes; mod prepare_data; mod user; diff --git a/nixin_farm_ssr/tests/requests/notes.rs b/nixin_farm_ssr/tests/requests/notes.rs deleted file mode 100644 index 9f334d9..0000000 --- a/nixin_farm_ssr/tests/requests/notes.rs +++ /dev/null @@ -1,123 +0,0 @@ -use insta::{assert_debug_snapshot, with_settings}; -use loco_rs::testing; -use nixin_farm_ssr::{app::App, models::_entities::notes::Entity}; -use sea_orm::entity::prelude::*; -use serial_test::serial; - -// TODO: see how to dedup / extract this to app-local test utils -// not to framework, because that would require a runtime dep on insta -macro_rules! configure_insta { - ($($expr:expr),*) => { - let mut settings = insta::Settings::clone_current(); - settings.set_prepend_module_to_snapshot(false); - settings.set_snapshot_suffix("notes_request"); - let _guard = settings.bind_to_scope(); - }; -} - -#[tokio::test] -#[serial] -async fn can_get_notes() { - configure_insta!(); - - testing::request::(|request, ctx| async move { - testing::seed::(&ctx.db).await.unwrap(); - - let notes = request.get("/api/notes").await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (notes.status_code(), notes.text()) - ); - }); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_add_note() { - configure_insta!(); - - testing::request::(|request, _ctx| async move { - let payload = serde_json::json!({ - "title": "loco", - "content": "loco note test", - }); - - let add_note_request = request.post("/api/notes").json(&payload).await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (add_note_request.status_code(), add_note_request.text()) - ); - }); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_get_note() { - configure_insta!(); - - testing::request::(|request, ctx| async move { - testing::seed::(&ctx.db).await.unwrap(); - - let add_note_request = request.get("/api/notes/1").await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (add_note_request.status_code(), add_note_request.text()) - ); - }); - }) - .await; -} - -#[tokio::test] -#[serial] -async fn can_delete_note() { - configure_insta!(); - - testing::request::(|request, ctx| async move { - testing::seed::(&ctx.db).await.unwrap(); - - let count_before_delete = Entity::find().all(&ctx.db).await.unwrap().len(); - let delete_note_request = request.delete("/api/notes/1").await; - - with_settings!({ - filters => { - let mut combined_filters = testing::CLEANUP_DATE.to_vec(); - combined_filters.extend(vec![(r#"\"id\\":\d+"#, r#""id\":ID"#)]); - combined_filters - } - }, { - assert_debug_snapshot!( - (delete_note_request.status_code(), delete_note_request.text()) - ); - }); - - let count_after_delete = Entity::find().all(&ctx.db).await.unwrap().len(); - assert_eq!(count_after_delete, count_before_delete - 1); - }) - .await; -} diff --git a/nixin_farm_ssr/tests/requests/prepare_data.rs b/nixin_farm_ssr/tests/requests/prepare_data.rs index de0c572..3ebe94a 100644 --- a/nixin_farm_ssr/tests/requests/prepare_data.rs +++ b/nixin_farm_ssr/tests/requests/prepare_data.rs @@ -2,8 +2,9 @@ use axum::http::{HeaderName, HeaderValue}; use loco_rs::{app::AppContext, TestServer}; use nixin_farm_ssr::{models::users, views::auth::LoginResponse}; -const USER_EMAIL: &str = "test@loco.com"; -const USER_PASSWORD: &str = "1234"; +const USER_NAME: &str = "test"; +const USER_EMAIL: &str = "test@nixin.local"; +const USER_PASSWORD: &str = "Test"; pub struct LoggedInUser { pub user: users::Model, @@ -12,7 +13,7 @@ pub struct LoggedInUser { pub async fn init_user_login(request: &TestServer, ctx: &AppContext) -> LoggedInUser { let register_payload = serde_json::json!({ - "name": "loco", + "name": USER_NAME, "email": USER_EMAIL, "password": USER_PASSWORD }); diff --git a/nixin_farm_ssr/tests/requests/snapshots/can_add_note@notes_request.snap b/nixin_farm_ssr/tests/requests/snapshots/can_add_note@notes_request.snap deleted file mode 100644 index f8457d7..0000000 --- a/nixin_farm_ssr/tests/requests/snapshots/can_add_note@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(add_note_request.status_code(), add_note_request.text())" ---- -( - 200, - "{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"loco\",\"content\":\"loco note test\"}", -) diff --git a/nixin_farm_ssr/tests/requests/snapshots/can_delete_note@notes_request.snap b/nixin_farm_ssr/tests/requests/snapshots/can_delete_note@notes_request.snap deleted file mode 100644 index 3481fc3..0000000 --- a/nixin_farm_ssr/tests/requests/snapshots/can_delete_note@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(delete_note_request.status_code(), delete_note_request.text())" ---- -( - 200, - "", -) diff --git a/nixin_farm_ssr/tests/requests/snapshots/can_get_note@notes_request.snap b/nixin_farm_ssr/tests/requests/snapshots/can_get_note@notes_request.snap deleted file mode 100644 index 8af1604..0000000 --- a/nixin_farm_ssr/tests/requests/snapshots/can_get_note@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(add_note_request.status_code(), add_note_request.text())" ---- -( - 200, - "{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"Loco note 1 content\"}", -) diff --git a/nixin_farm_ssr/tests/requests/snapshots/can_get_notes@notes_request.snap b/nixin_farm_ssr/tests/requests/snapshots/can_get_notes@notes_request.snap deleted file mode 100644 index 014b75c..0000000 --- a/nixin_farm_ssr/tests/requests/snapshots/can_get_notes@notes_request.snap +++ /dev/null @@ -1,8 +0,0 @@ ---- -source: tests/requests/notes.rs -expression: "(notes.status_code(), notes.text())" ---- -( - 200, - "[{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"Loco note 1 content\"},{\"created_at\":\"DATEZ\",\"updated_at\":\"DATEZ\",\"id\":ID,\"title\":\"Loco note 2\",\"content\":\"Loco note 2 content\"}]", -)