From 2086ce7ee42d607b6a7a8962b9409d17b04ca1f2 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Wed, 17 Apr 2019 13:42:16 +0200 Subject: [PATCH] Add some constraint to db, and fix list test and refactor other tests to use begin_transaction --- .../2019-04-11-145757_timeline/up.sql | 6 +- .../sqlite/2019-04-11-145757_timeline/up.sql | 6 +- plume-models/src/blogs.rs | 395 ++++++++---------- plume-models/src/follows.rs | 54 ++- plume-models/src/instance.rs | 325 +++++++------- plume-models/src/lib.rs | 4 +- plume-models/src/lists.rs | 69 ++- plume-models/src/medias.rs | 125 +++--- plume-models/src/search/mod.rs | 117 +++--- plume-models/src/users.rs | 225 +++++----- 10 files changed, 638 insertions(+), 688 deletions(-) diff --git a/migrations/postgres/2019-04-11-145757_timeline/up.sql b/migrations/postgres/2019-04-11-145757_timeline/up.sql index f007a17eb..a8a7ae821 100644 --- a/migrations/postgres/2019-04-11-145757_timeline/up.sql +++ b/migrations/postgres/2019-04-11-145757_timeline/up.sql @@ -4,7 +4,8 @@ CREATE TABLE timeline_definition( id SERIAL PRIMARY KEY, user_id integer REFERENCES users ON DELETE CASCADE, name VARCHAR NOT NULL, - query VARCHAR NOT NULL + query VARCHAR NOT NULL, + CONSTRAINT timeline_unique_user_name UNIQUE(user_id, name) ); CREATE TABLE timeline( @@ -17,7 +18,8 @@ CREATE TABLE lists( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, user_id integer REFERENCES users ON DELETE CASCADE, - type integer NOT NULL + type integer NOT NULL, + CONSTRAINT list_unique_user_name UNIQUE(user_id, name) ); CREATE TABLE list_elems( diff --git a/migrations/sqlite/2019-04-11-145757_timeline/up.sql b/migrations/sqlite/2019-04-11-145757_timeline/up.sql index 0779a9914..0b2119971 100644 --- a/migrations/sqlite/2019-04-11-145757_timeline/up.sql +++ b/migrations/sqlite/2019-04-11-145757_timeline/up.sql @@ -4,7 +4,8 @@ CREATE TABLE timeline_definition( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, user_id INTEGER REFERENCES users(id) ON DELETE CASCADE, name VARCHAR NOT NULL, - query VARCHAR NOT NULL + query VARCHAR NOT NULL, + CONSTRAINT timeline_unique_user_name UNIQUE(user_id, name) ); CREATE TABLE timeline( @@ -17,7 +18,8 @@ CREATE TABLE lists( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, user_id integer REFERENCES users(id) ON DELETE CASCADE, - type integer NOT NULL + type integer NOT NULL, + CONSTRAINT timeline_unique_user_name UNIQUE(user_id, name) ); CREATE TABLE list_elems( diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index e61e9d2cb..b022a6173 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -469,7 +469,6 @@ impl NewBlog { pub(crate) mod tests { use super::*; use blog_authors::*; - use diesel::Connection; use instance::tests as instance_tests; use search::tests::get_searcher; use tests::db; @@ -558,262 +557,238 @@ pub(crate) mod tests { #[test] fn get_instance() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - fill_database(conn); + fill_database(conn); - let blog = Blog::insert( - conn, - NewBlog::new_local( - "SomeName".to_owned(), - "Some name".to_owned(), - "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), + let blog = Blog::insert( + conn, + NewBlog::new_local( + "SomeName".to_owned(), + "Some name".to_owned(), + "This is some blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); - - assert_eq!( - blog.get_instance(conn).unwrap().id, - Instance::get_local(conn).unwrap().id - ); - // TODO add tests for remote instance + .unwrap(), + ) + .unwrap(); - Ok(()) - }); + assert_eq!( + blog.get_instance(conn).unwrap().id, + Instance::get_local(conn).unwrap().id + ); + // TODO add tests for remote instance } #[test] fn authors() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let (user, _) = fill_database(conn); + let (user, _) = fill_database(conn); - let b1 = Blog::insert( - conn, - NewBlog::new_local( - "SomeName".to_owned(), - "Some name".to_owned(), - "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), + let b1 = Blog::insert( + conn, + NewBlog::new_local( + "SomeName".to_owned(), + "Some name".to_owned(), + "This is some blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); - let b2 = Blog::insert( - conn, - NewBlog::new_local( - "Blog".to_owned(), - "Blog".to_owned(), - "I've named my blog Blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), + .unwrap(), + ) + .unwrap(); + let b2 = Blog::insert( + conn, + NewBlog::new_local( + "Blog".to_owned(), + "Blog".to_owned(), + "I've named my blog Blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); - let blog = vec![b1, b2]; + .unwrap(), + ) + .unwrap(); + let blog = vec![b1, b2]; - BlogAuthor::insert( - conn, - NewBlogAuthor { - blog_id: blog[0].id, - author_id: user[0].id, - is_owner: true, - }, - ) - .unwrap(); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog[0].id, + author_id: user[0].id, + is_owner: true, + }, + ) + .unwrap(); - BlogAuthor::insert( - conn, - NewBlogAuthor { - blog_id: blog[0].id, - author_id: user[1].id, - is_owner: false, - }, - ) - .unwrap(); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog[0].id, + author_id: user[1].id, + is_owner: false, + }, + ) + .unwrap(); - BlogAuthor::insert( - conn, - NewBlogAuthor { - blog_id: blog[1].id, - author_id: user[0].id, - is_owner: true, - }, - ) - .unwrap(); - - assert!(blog[0] - .list_authors(conn) - .unwrap() - .iter() - .any(|a| a.id == user[0].id)); - assert!(blog[0] - .list_authors(conn) - .unwrap() - .iter() - .any(|a| a.id == user[1].id)); - assert!(blog[1] - .list_authors(conn) - .unwrap() - .iter() - .any(|a| a.id == user[0].id)); - assert!(!blog[1] - .list_authors(conn) - .unwrap() - .iter() - .any(|a| a.id == user[1].id)); - - assert!(Blog::find_for_author(conn, &user[0]) - .unwrap() - .iter() - .any(|b| b.id == blog[0].id)); - assert!(Blog::find_for_author(conn, &user[1]) - .unwrap() - .iter() - .any(|b| b.id == blog[0].id)); - assert!(Blog::find_for_author(conn, &user[0]) - .unwrap() - .iter() - .any(|b| b.id == blog[1].id)); - assert!(!Blog::find_for_author(conn, &user[1]) - .unwrap() - .iter() - .any(|b| b.id == blog[1].id)); - - Ok(()) - }); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog[1].id, + author_id: user[0].id, + is_owner: true, + }, + ) + .unwrap(); + + assert!(blog[0] + .list_authors(conn) + .unwrap() + .iter() + .any(|a| a.id == user[0].id)); + assert!(blog[0] + .list_authors(conn) + .unwrap() + .iter() + .any(|a| a.id == user[1].id)); + assert!(blog[1] + .list_authors(conn) + .unwrap() + .iter() + .any(|a| a.id == user[0].id)); + assert!(!blog[1] + .list_authors(conn) + .unwrap() + .iter() + .any(|a| a.id == user[1].id)); + + assert!(Blog::find_for_author(conn, &user[0]) + .unwrap() + .iter() + .any(|b| b.id == blog[0].id)); + assert!(Blog::find_for_author(conn, &user[1]) + .unwrap() + .iter() + .any(|b| b.id == blog[0].id)); + assert!(Blog::find_for_author(conn, &user[0]) + .unwrap() + .iter() + .any(|b| b.id == blog[1].id)); + assert!(!Blog::find_for_author(conn, &user[1]) + .unwrap() + .iter() + .any(|b| b.id == blog[1].id)); } #[test] fn find_local() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - fill_database(conn); + fill_database(conn); - let blog = Blog::insert( - conn, - NewBlog::new_local( - "SomeName".to_owned(), - "Some name".to_owned(), - "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), + let blog = Blog::insert( + conn, + NewBlog::new_local( + "SomeName".to_owned(), + "Some name".to_owned(), + "This is some blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); - - assert_eq!(Blog::find_by_fqn(conn, "SomeName").unwrap().id, blog.id); + .unwrap(), + ) + .unwrap(); - Ok(()) - }); + assert_eq!(Blog::find_by_fqn(conn, "SomeName").unwrap().id, blog.id); } #[test] fn get_fqn() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - fill_database(conn); + fill_database(conn); - let blog = Blog::insert( - conn, - NewBlog::new_local( - "SomeName".to_owned(), - "Some name".to_owned(), - "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), + let blog = Blog::insert( + conn, + NewBlog::new_local( + "SomeName".to_owned(), + "Some name".to_owned(), + "This is some blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); - - assert_eq!(blog.fqn, "SomeName"); + .unwrap(), + ) + .unwrap(); - Ok(()) - }); + assert_eq!(blog.fqn, "SomeName"); } #[test] fn delete() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let (_, blogs) = fill_database(conn); - - blogs[0].delete(conn, &get_searcher()).unwrap(); - assert!(Blog::get(conn, blogs[0].id).is_err()); + let (_, blogs) = fill_database(conn); - Ok(()) - }); + blogs[0].delete(conn, &get_searcher()).unwrap(); + assert!(Blog::get(conn, blogs[0].id).is_err()); } #[test] fn delete_via_user() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let searcher = get_searcher(); - let (user, _) = fill_database(conn); + let searcher = get_searcher(); + let (user, _) = fill_database(conn); - let b1 = Blog::insert( - conn, - NewBlog::new_local( - "SomeName".to_owned(), - "Some name".to_owned(), - "This is some blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), - ) - .unwrap(); - let b2 = Blog::insert( - conn, - NewBlog::new_local( - "Blog".to_owned(), - "Blog".to_owned(), - "I've named my blog Blog".to_owned(), - Instance::get_local(conn).unwrap().id, - ) - .unwrap(), + let b1 = Blog::insert( + conn, + NewBlog::new_local( + "SomeName".to_owned(), + "Some name".to_owned(), + "This is some blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); - let blog = vec![b1, b2]; - - BlogAuthor::insert( - conn, - NewBlogAuthor { - blog_id: blog[0].id, - author_id: user[0].id, - is_owner: true, - }, + .unwrap(), + ) + .unwrap(); + let b2 = Blog::insert( + conn, + NewBlog::new_local( + "Blog".to_owned(), + "Blog".to_owned(), + "I've named my blog Blog".to_owned(), + Instance::get_local(conn).unwrap().id, ) - .unwrap(); + .unwrap(), + ) + .unwrap(); + let blog = vec![b1, b2]; - BlogAuthor::insert( - conn, - NewBlogAuthor { - blog_id: blog[0].id, - author_id: user[1].id, - is_owner: false, - }, - ) - .unwrap(); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog[0].id, + author_id: user[0].id, + is_owner: true, + }, + ) + .unwrap(); - BlogAuthor::insert( - conn, - NewBlogAuthor { - blog_id: blog[1].id, - author_id: user[0].id, - is_owner: true, - }, - ) - .unwrap(); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog[0].id, + author_id: user[1].id, + is_owner: false, + }, + ) + .unwrap(); - user[0].delete(conn, &searcher).unwrap(); - assert!(Blog::get(conn, blog[0].id).is_ok()); - assert!(Blog::get(conn, blog[1].id).is_err()); - user[1].delete(conn, &searcher).unwrap(); - assert!(Blog::get(conn, blog[0].id).is_err()); + BlogAuthor::insert( + conn, + NewBlogAuthor { + blog_id: blog[1].id, + author_id: user[0].id, + is_owner: true, + }, + ) + .unwrap(); - Ok(()) - }); + user[0].delete(conn, &searcher).unwrap(); + assert!(Blog::get(conn, blog[0].id).is_ok()); + assert!(Blog::get(conn, blog[1].id).is_err()); + user[1].delete(conn, &searcher).unwrap(); + assert!(Blog::get(conn, blog[0].id).is_err()); } } diff --git a/plume-models/src/follows.rs b/plume-models/src/follows.rs index 52c4b66fb..2f18fab96 100644 --- a/plume-models/src/follows.rs +++ b/plume-models/src/follows.rs @@ -190,40 +190,36 @@ impl IntoId for Follow { #[cfg(test)] mod tests { use super::*; - use diesel::Connection; use tests::db; use users::tests as user_tests; #[test] fn test_id() { let conn = db(); - conn.test_transaction::<_, (), _>(|| { - let users = user_tests::fill_database(&conn); - let follow = Follow::insert( - &conn, - NewFollow { - follower_id: users[0].id, - following_id: users[1].id, - ap_url: String::new(), - }, - ) - .expect("Couldn't insert new follow"); - assert_eq!( - follow.ap_url, - format!("https://{}/follows/{}", CONFIG.base_url, follow.id) - ); - - let follow = Follow::insert( - &conn, - NewFollow { - follower_id: users[1].id, - following_id: users[0].id, - ap_url: String::from("https://some.url/"), - }, - ) - .expect("Couldn't insert new follow"); - assert_eq!(follow.ap_url, String::from("https://some.url/")); - Ok(()) - }); + let users = user_tests::fill_database(&conn); + let follow = Follow::insert( + &conn, + NewFollow { + follower_id: users[0].id, + following_id: users[1].id, + ap_url: String::new(), + }, + ) + .expect("Couldn't insert new follow"); + assert_eq!( + follow.ap_url, + format!("https://{}/follows/{}", CONFIG.base_url, follow.id) + ); + + let follow = Follow::insert( + &conn, + NewFollow { + follower_id: users[1].id, + following_id: users[0].id, + ap_url: String::from("https://some.url/"), + }, + ) + .expect("Couldn't insert new follow"); + assert_eq!(follow.ap_url, String::from("https://some.url/")); } } diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index d7c527e53..4afaf178b 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -166,7 +166,6 @@ impl Instance { #[cfg(test)] pub(crate) mod tests { use super::*; - use diesel::Connection; use tests::db; use Connection as Conn; @@ -231,196 +230,176 @@ pub(crate) mod tests { #[test] fn local_instance() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inserted = fill_database(conn) - .into_iter() - .map(|(inserted, _)| inserted) - .find(|inst| inst.local) - .unwrap(); - let res = Instance::get_local(conn).unwrap(); - - part_eq!( - res, - inserted, - [ - default_license, - local, - long_description, - short_description, - name, - open_registrations, - public_domain - ] - ); - assert_eq!( - res.long_description_html.get(), - &inserted.long_description_html - ); - assert_eq!( - res.short_description_html.get(), - &inserted.short_description_html - ); - - Ok(()) - }); + let inserted = fill_database(conn) + .into_iter() + .map(|(inserted, _)| inserted) + .find(|inst| inst.local) + .unwrap(); + let res = Instance::get_local(conn).unwrap(); + + part_eq!( + res, + inserted, + [ + default_license, + local, + long_description, + short_description, + name, + open_registrations, + public_domain + ] + ); + assert_eq!( + res.long_description_html.get(), + &inserted.long_description_html + ); + assert_eq!( + res.short_description_html.get(), + &inserted.short_description_html + ); } #[test] fn remote_instance() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inserted = fill_database(conn); - assert_eq!(Instance::count(conn).unwrap(), inserted.len() as i64); - - let res = Instance::get_remotes(conn).unwrap(); - assert_eq!( - res.len(), - inserted.iter().filter(|(inst, _)| !inst.local).count() - ); + let inserted = fill_database(conn); + assert_eq!(Instance::count(conn).unwrap(), inserted.len() as i64); - inserted - .iter() - .filter(|(newinst, _)| !newinst.local) - .map(|(newinst, inst)| (newinst, res.iter().find(|res| res.id == inst.id).unwrap())) - .for_each(|(newinst, inst)| { - part_eq!( - newinst, - inst, - [ - default_license, - local, - long_description, - short_description, - name, - open_registrations, - public_domain - ] - ); - assert_eq!( - &newinst.long_description_html, - inst.long_description_html.get() - ); - assert_eq!( - &newinst.short_description_html, - inst.short_description_html.get() - ); - }); - - let page = Instance::page(conn, (0, 2)).unwrap(); - assert_eq!(page.len(), 2); - let page1 = &page[0]; - let page2 = &page[1]; - assert!(page1.public_domain <= page2.public_domain); - - let mut last_domaine: String = Instance::page(conn, (0, 1)).unwrap()[0] - .public_domain - .clone(); - for i in 1..inserted.len() as i32 { - let page = Instance::page(conn, (i, i + 1)).unwrap(); - assert_eq!(page.len(), 1); - assert!(last_domaine <= page[0].public_domain); - last_domaine = page[0].public_domain.clone(); - } + let res = Instance::get_remotes(conn).unwrap(); + assert_eq!( + res.len(), + inserted.iter().filter(|(inst, _)| !inst.local).count() + ); - Ok(()) - }); + inserted + .iter() + .filter(|(newinst, _)| !newinst.local) + .map(|(newinst, inst)| (newinst, res.iter().find(|res| res.id == inst.id).unwrap())) + .for_each(|(newinst, inst)| { + part_eq!( + newinst, + inst, + [ + default_license, + local, + long_description, + short_description, + name, + open_registrations, + public_domain + ] + ); + assert_eq!( + &newinst.long_description_html, + inst.long_description_html.get() + ); + assert_eq!( + &newinst.short_description_html, + inst.short_description_html.get() + ); + }); + + let page = Instance::page(conn, (0, 2)).unwrap(); + assert_eq!(page.len(), 2); + let page1 = &page[0]; + let page2 = &page[1]; + assert!(page1.public_domain <= page2.public_domain); + + let mut last_domaine: String = Instance::page(conn, (0, 1)).unwrap()[0] + .public_domain + .clone(); + for i in 1..inserted.len() as i32 { + let page = Instance::page(conn, (i, i + 1)).unwrap(); + assert_eq!(page.len(), 1); + assert!(last_domaine <= page[0].public_domain); + last_domaine = page[0].public_domain.clone(); + } } #[test] fn blocked() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inst_list = fill_database(conn); - let inst = &inst_list[0].1; - let inst_list = &inst_list[1..]; - - let blocked = inst.blocked; - inst.toggle_block(conn).unwrap(); - let inst = Instance::get(conn, inst.id).unwrap(); - assert_eq!(inst.blocked, !blocked); - assert_eq!( - inst_list - .iter() - .filter( - |(_, inst)| inst.blocked != Instance::get(conn, inst.id).unwrap().blocked - ) - .count(), - 0 - ); - assert_eq!( - Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain)) - .unwrap(), - inst.blocked - ); - assert_eq!( - Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain)) - .unwrap(), - Instance::find_by_domain(conn, &format!("{}a", inst.public_domain)) - .map(|inst| inst.blocked) - .unwrap_or(false) - ); - - inst.toggle_block(conn).unwrap(); - let inst = Instance::get(conn, inst.id).unwrap(); - assert_eq!(inst.blocked, blocked); - assert_eq!( - Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain)) - .unwrap(), - inst.blocked - ); - assert_eq!( - Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain)) - .unwrap(), - Instance::find_by_domain(conn, &format!("{}a", inst.public_domain)) - .map(|inst| inst.blocked) - .unwrap_or(false) - ); - assert_eq!( - inst_list - .iter() - .filter( - |(_, inst)| inst.blocked != Instance::get(conn, inst.id).unwrap().blocked - ) - .count(), - 0 - ); + let inst_list = fill_database(conn); + let inst = &inst_list[0].1; + let inst_list = &inst_list[1..]; + + let blocked = inst.blocked; + inst.toggle_block(conn).unwrap(); + let inst = Instance::get(conn, inst.id).unwrap(); + assert_eq!(inst.blocked, !blocked); + assert_eq!( + inst_list + .iter() + .filter(|(_, inst)| inst.blocked != Instance::get(conn, inst.id).unwrap().blocked) + .count(), + 0 + ); + assert_eq!( + Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain)) + .unwrap(), + inst.blocked + ); + assert_eq!( + Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain)) + .unwrap(), + Instance::find_by_domain(conn, &format!("{}a", inst.public_domain)) + .map(|inst| inst.blocked) + .unwrap_or(false) + ); - Ok(()) - }); + inst.toggle_block(conn).unwrap(); + let inst = Instance::get(conn, inst.id).unwrap(); + assert_eq!(inst.blocked, blocked); + assert_eq!( + Instance::is_blocked(conn, &format!("https://{}/something", inst.public_domain)) + .unwrap(), + inst.blocked + ); + assert_eq!( + Instance::is_blocked(conn, &format!("https://{}a/something", inst.public_domain)) + .unwrap(), + Instance::find_by_domain(conn, &format!("{}a", inst.public_domain)) + .map(|inst| inst.blocked) + .unwrap_or(false) + ); + assert_eq!( + inst_list + .iter() + .filter(|(_, inst)| inst.blocked != Instance::get(conn, inst.id).unwrap().blocked) + .count(), + 0 + ); } #[test] fn update() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inst = &fill_database(conn)[0].1; + let inst = &fill_database(conn)[0].1; - inst.update( - conn, - "NewName".to_owned(), - false, - SafeString::new("[short](#link)"), - SafeString::new("[long_description](/with_link)"), - ) - .unwrap(); - let inst = Instance::get(conn, inst.id).unwrap(); - assert_eq!(inst.name, "NewName".to_owned()); - assert_eq!(inst.open_registrations, false); - assert_eq!( - inst.long_description.get(), - "[long_description](/with_link)" - ); - assert_eq!( - inst.long_description_html, - SafeString::new("

long_description

\n") - ); - assert_eq!(inst.short_description.get(), "[short](#link)"); - assert_eq!( - inst.short_description_html, - SafeString::new("

short

\n") - ); - - Ok(()) - }); + inst.update( + conn, + "NewName".to_owned(), + false, + SafeString::new("[short](#link)"), + SafeString::new("[long_description](/with_link)"), + ) + .unwrap(); + let inst = Instance::get(conn, inst.id).unwrap(); + assert_eq!(inst.name, "NewName".to_owned()); + assert_eq!(inst.open_registrations, false); + assert_eq!( + inst.long_description.get(), + "[long_description](/with_link)" + ); + assert_eq!( + inst.long_description_html, + SafeString::new("

long_description

\n") + ); + assert_eq!(inst.short_description.get(), "[short](#link)"); + assert_eq!( + inst.short_description_html, + SafeString::new("

short

\n") + ); } } diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index b34ce1d32..8af0e42b9 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -321,11 +321,13 @@ mod tests { pub fn db() -> Conn { let conn = Conn::establish(CONFIG.database_url.as_str()) .expect("Couldn't connect to the database"); - embedded_migrations::run(&conn).expect("Couldn't run migrations"); #[cfg(feature = "sqlite")] sql_query("PRAGMA foreign_keys = on;") .execute(&conn) .expect("PRAGMA foreign_keys fail"); + conn.begin_test_transaction() + .expect("Couldn't start test transaction"); + embedded_migrations::run(&conn).expect("Couldn't run migrations"); conn } } diff --git a/plume-models/src/lists.rs b/plume-models/src/lists.rs index 919eaf93b..62cb53c5e 100644 --- a/plume-models/src/lists.rs +++ b/plume-models/src/lists.rs @@ -75,23 +75,53 @@ struct NewListElem<'a> { } impl List { + last!(lists); + get!(lists); + fn insert(conn: &Connection, val: NewList) -> Result { diesel::insert_into(lists::table) .values(val) .execute(conn)?; List::last(conn) } - last!(lists); - get!(lists); - list_by!(lists, list_for_user, user_id as Option); - find_by!(lists, find_by_name, user_id as Option, name as &str); - - pub fn new( - conn: &Connection, - name: &str, - user: Option<&User>, - kind: ListType, - ) -> Result { + + pub fn list_for_user(conn: &Connection, user_id: Option) -> Result> { + if let Some(user_id) = user_id { + lists::table + .filter(lists::user_id.eq(user_id)) + .load::(conn) + .map_err(Error::from) + } else { + lists::table + .filter(lists::user_id.is_null()) + .load::(conn) + .map_err(Error::from) + } + } + + pub fn find_by_name(conn: &Connection, user_id: Option, name: &str) -> Result { + if let Some(user_id) = user_id { + lists::table + .filter(lists::user_id.eq(user_id)) + .filter(lists::name.eq(name)) + .limit(1) + .load::(conn)? + .into_iter() + .next() + .ok_or(Error::NotFound) + } else { + lists::table + .filter(lists::user_id.is_null()) + .filter(lists::name.eq(name)) + .limit(1) + .load::(conn)? + .into_iter() + .next() + .ok_or(Error::NotFound) + } + } + + pub fn new(conn: &Connection, name: &str, user: Option<&User>, kind: ListType) -> Result { Self::insert( conn, NewList { @@ -371,8 +401,6 @@ mod tests { use blogs::tests as blog_tests; use tests::db; - use diesel::Connection; - #[test] fn list_type() { for i in 0..4 { @@ -384,13 +412,11 @@ mod tests { #[test] fn list_lists() { let conn = &db(); - conn.begin_test_transaction().unwrap(); - let (users, blogs) = blog_tests::fill_database(conn); + let (users, _) = blog_tests::fill_database(conn); let l1 = List::new(conn, "list1", None, ListType::User).unwrap(); let l2 = List::new(conn, "list2", None, ListType::Blog).unwrap(); let l1u = List::new(conn, "list1", Some(&users[0]), ListType::Word).unwrap(); - // TODO add db constraint (name, user_id) UNIQUE let l_eq = |l1: &List, l2: &List| { assert_eq!(l1.id, l2.id); @@ -406,7 +432,7 @@ mod tests { let l_user = List::list_for_user(conn, Some(users[0].id)).unwrap(); assert_eq!(2, l_inst.len()); assert_eq!(1, l_user.len()); - assert!(l_user[0].id != l1u.id); + assert!(l_inst.iter().all(|l| l.id != l1u.id)); l_eq(&l1u, &l_user[0]); if l_inst[0].id == l1.id { @@ -417,6 +443,13 @@ mod tests { l_eq(&l2, &l_inst[0]); } - //find_by!(lists, find_by_name, user_id as Option, name as &str); + l_eq( + &l1, + &List::find_by_name(conn, l1.user_id, &l1.name).unwrap(), + ); + l_eq( + &&l1u, + &List::find_by_name(conn, l1u.user_id, &l1u.name).unwrap(), + ); } } diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index a1c3897a1..0844392ee 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -242,7 +242,6 @@ impl Media { #[cfg(test)] pub(crate) mod tests { use super::*; - use diesel::Connection; use std::env::{current_dir, set_current_dir}; use std::fs; use std::path::Path; @@ -315,83 +314,75 @@ pub(crate) mod tests { #[test] fn delete() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let user = fill_database(conn).0[0].id; + let user = fill_database(conn).0[0].id; - let path = "static/media/test_deletion".to_owned(); - fs::write(path.clone(), []).unwrap(); + let path = "static/media/test_deletion".to_owned(); + fs::write(path.clone(), []).unwrap(); - let media = Media::insert( - conn, - NewMedia { - file_path: path.clone(), - alt_text: "alt message".to_owned(), - is_remote: false, - remote_url: None, - sensitive: false, - content_warning: None, - owner_id: user, - }, - ) - .unwrap(); - - assert!(Path::new(&path).exists()); - media.delete(conn).unwrap(); - assert!(!Path::new(&path).exists()); + let media = Media::insert( + conn, + NewMedia { + file_path: path.clone(), + alt_text: "alt message".to_owned(), + is_remote: false, + remote_url: None, + sensitive: false, + content_warning: None, + owner_id: user, + }, + ) + .unwrap(); - clean(conn); + assert!(Path::new(&path).exists()); + media.delete(conn).unwrap(); + assert!(!Path::new(&path).exists()); - Ok(()) - }); + clean(conn); } #[test] fn set_owner() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let (users, _) = fill_database(conn); - let u1 = &users[0]; - let u2 = &users[1]; + let (users, _) = fill_database(conn); + let u1 = &users[0]; + let u2 = &users[1]; - let path = "static/media/test_set_owner".to_owned(); - fs::write(path.clone(), []).unwrap(); + let path = "static/media/test_set_owner".to_owned(); + fs::write(path.clone(), []).unwrap(); - let media = Media::insert( - conn, - NewMedia { - file_path: path.clone(), - alt_text: "alt message".to_owned(), - is_remote: false, - remote_url: None, - sensitive: false, - content_warning: None, - owner_id: u1.id, - }, - ) - .unwrap(); - - assert!(Media::for_user(conn, u1.id) - .unwrap() - .iter() - .any(|m| m.id == media.id)); - assert!(!Media::for_user(conn, u2.id) - .unwrap() - .iter() - .any(|m| m.id == media.id)); - media.set_owner(conn, u2).unwrap(); - assert!(!Media::for_user(conn, u1.id) - .unwrap() - .iter() - .any(|m| m.id == media.id)); - assert!(Media::for_user(conn, u2.id) - .unwrap() - .iter() - .any(|m| m.id == media.id)); - - clean(conn); - - Ok(()) - }); + let media = Media::insert( + conn, + NewMedia { + file_path: path.clone(), + alt_text: "alt message".to_owned(), + is_remote: false, + remote_url: None, + sensitive: false, + content_warning: None, + owner_id: u1.id, + }, + ) + .unwrap(); + + assert!(Media::for_user(conn, u1.id) + .unwrap() + .iter() + .any(|m| m.id == media.id)); + assert!(!Media::for_user(conn, u2.id) + .unwrap() + .iter() + .any(|m| m.id == media.id)); + media.set_owner(conn, u2).unwrap(); + assert!(!Media::for_user(conn, u1.id) + .unwrap() + .iter() + .any(|m| m.id == media.id)); + assert!(Media::for_user(conn, u2.id) + .unwrap() + .iter() + .any(|m| m.id == media.id)); + + clean(conn); } } diff --git a/plume-models/src/search/mod.rs b/plume-models/src/search/mod.rs index 48e0dc1b9..f70494826 100644 --- a/plume-models/src/search/mod.rs +++ b/plume-models/src/search/mod.rs @@ -7,7 +7,6 @@ pub use self::searcher::*; #[cfg(test)] pub(crate) mod tests { use super::{Query, Searcher}; - use diesel::Connection; use std::env::temp_dir; use std::str::FromStr; @@ -119,66 +118,62 @@ pub(crate) mod tests { #[test] fn search() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let searcher = get_searcher(); - let blog = &fill_database(conn).1[0]; - let author = &blog.list_authors(conn).unwrap()[0]; - - let title = random_hex()[..8].to_owned(); - - let mut post = Post::insert( - conn, - NewPost { - blog_id: blog.id, - slug: title.clone(), - title: title.clone(), - content: SafeString::new(""), - published: true, - license: "CC-BY-SA".to_owned(), - ap_url: "".to_owned(), - creation_date: None, - subtitle: "".to_owned(), - source: "".to_owned(), - cover_id: None, - }, - &searcher, - ) - .unwrap(); - PostAuthor::insert( - conn, - NewPostAuthor { - post_id: post.id, - author_id: author.id, - }, - ) - .unwrap(); - - searcher.commit(); - assert_eq!( - searcher.search_document(conn, Query::from_str(&title).unwrap(), (0, 1))[0].id, - post.id - ); - - let newtitle = random_hex()[..8].to_owned(); - post.title = newtitle.clone(); - post.update(conn, &searcher).unwrap(); - searcher.commit(); - assert_eq!( - searcher.search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1))[0].id, - post.id - ); - assert!(searcher - .search_document(conn, Query::from_str(&title).unwrap(), (0, 1)) - .is_empty()); - - post.delete(&(conn, &searcher)).unwrap(); - searcher.commit(); - assert!(searcher - .search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1)) - .is_empty()); - - Ok(()) - }); + let searcher = get_searcher(); + let blog = &fill_database(conn).1[0]; + let author = &blog.list_authors(conn).unwrap()[0]; + + let title = random_hex()[..8].to_owned(); + + let mut post = Post::insert( + conn, + NewPost { + blog_id: blog.id, + slug: title.clone(), + title: title.clone(), + content: SafeString::new(""), + published: true, + license: "CC-BY-SA".to_owned(), + ap_url: "".to_owned(), + creation_date: None, + subtitle: "".to_owned(), + source: "".to_owned(), + cover_id: None, + }, + &searcher, + ) + .unwrap(); + PostAuthor::insert( + conn, + NewPostAuthor { + post_id: post.id, + author_id: author.id, + }, + ) + .unwrap(); + + searcher.commit(); + assert_eq!( + searcher.search_document(conn, Query::from_str(&title).unwrap(), (0, 1))[0].id, + post.id + ); + + let newtitle = random_hex()[..8].to_owned(); + post.title = newtitle.clone(); + post.update(conn, &searcher).unwrap(); + searcher.commit(); + assert_eq!( + searcher.search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1))[0].id, + post.id + ); + assert!(searcher + .search_document(conn, Query::from_str(&title).unwrap(), (0, 1)) + .is_empty()); + + post.delete(&(conn, &searcher)).unwrap(); + searcher.commit(); + assert!(searcher + .search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1)) + .is_empty()); } #[test] diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 32d162cad..7214fce84 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -890,7 +890,6 @@ impl NewUser { #[cfg(test)] pub(crate) mod tests { use super::*; - use diesel::Connection; use instance::{tests as instance_tests, Instance}; use search::tests::get_searcher; use tests::db; @@ -934,159 +933,135 @@ pub(crate) mod tests { #[test] fn find_by() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - fill_database(conn); - let test_user = NewUser::new_local( - conn, - "test".to_owned(), - "test user".to_owned(), - false, - "Hello I'm a test", - "test@example.com".to_owned(), - User::hash_pass("test_password").unwrap(), - ) - .unwrap(); + fill_database(conn); + let test_user = NewUser::new_local( + conn, + "test".to_owned(), + "test user".to_owned(), + false, + "Hello I'm a test", + "test@example.com".to_owned(), + User::hash_pass("test_password").unwrap(), + ) + .unwrap(); - assert_eq!( - test_user.id, - User::find_by_name(conn, "test", Instance::get_local(conn).unwrap().id) - .unwrap() - .id - ); - assert_eq!( - test_user.id, - User::find_by_fqn(conn, &test_user.fqn).unwrap().id - ); - assert_eq!( - test_user.id, - User::find_by_email(conn, "test@example.com").unwrap().id - ); - assert_eq!( - test_user.id, - User::find_by_ap_url( - conn, - &format!( - "https://{}/@/{}/", - Instance::get_local(conn).unwrap().public_domain, - "test" - ) - ) + assert_eq!( + test_user.id, + User::find_by_name(conn, "test", Instance::get_local(conn).unwrap().id) .unwrap() .id - ); - - Ok(()) - }); + ); + assert_eq!( + test_user.id, + User::find_by_fqn(conn, &test_user.fqn).unwrap().id + ); + assert_eq!( + test_user.id, + User::find_by_email(conn, "test@example.com").unwrap().id + ); + assert_eq!( + test_user.id, + User::find_by_ap_url( + conn, + &format!( + "https://{}/@/{}/", + Instance::get_local(conn).unwrap().public_domain, + "test" + ) + ) + .unwrap() + .id + ); } #[test] fn delete() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inserted = fill_database(conn); - - assert!(User::get(conn, inserted[0].id).is_ok()); - inserted[0].delete(conn, &get_searcher()).unwrap(); - assert!(User::get(conn, inserted[0].id).is_err()); + let inserted = fill_database(conn); - Ok(()) - }); + assert!(User::get(conn, inserted[0].id).is_ok()); + inserted[0].delete(conn, &get_searcher()).unwrap(); + assert!(User::get(conn, inserted[0].id).is_err()); } #[test] fn admin() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inserted = fill_database(conn); - let local_inst = Instance::get_local(conn).unwrap(); - let mut i = 0; - while local_inst.has_admin(conn).unwrap() { - assert!(i < 100); //prevent from looping indefinitelly - local_inst - .main_admin(conn) - .unwrap() - .revoke_admin_rights(conn) - .unwrap(); - i += 1; - } - inserted[0].grant_admin_rights(conn).unwrap(); - assert_eq!(inserted[0].id, local_inst.main_admin(conn).unwrap().id); - - Ok(()) - }); + let inserted = fill_database(conn); + let local_inst = Instance::get_local(conn).unwrap(); + let mut i = 0; + while local_inst.has_admin(conn).unwrap() { + assert!(i < 100); //prevent from looping indefinitelly + local_inst + .main_admin(conn) + .unwrap() + .revoke_admin_rights(conn) + .unwrap(); + i += 1; + } + inserted[0].grant_admin_rights(conn).unwrap(); + assert_eq!(inserted[0].id, local_inst.main_admin(conn).unwrap().id); } #[test] fn update() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - let inserted = fill_database(conn); - let updated = inserted[0] - .update( - conn, - "new name".to_owned(), - "em@il".to_owned(), - "

summary

".to_owned(), - ) - .unwrap(); - assert_eq!(updated.display_name, "new name"); - assert_eq!(updated.email.unwrap(), "em@il"); - assert_eq!(updated.summary_html.get(), "

summary

"); - - Ok(()) - }); + let inserted = fill_database(conn); + let updated = inserted[0] + .update( + conn, + "new name".to_owned(), + "em@il".to_owned(), + "

summary

".to_owned(), + ) + .unwrap(); + assert_eq!(updated.display_name, "new name"); + assert_eq!(updated.email.unwrap(), "em@il"); + assert_eq!(updated.summary_html.get(), "

summary

"); } #[test] fn auth() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - fill_database(conn); - let test_user = NewUser::new_local( - conn, - "test".to_owned(), - "test user".to_owned(), - false, - "Hello I'm a test", - "test@example.com".to_owned(), - User::hash_pass("test_password").unwrap(), - ) - .unwrap(); - - assert!(test_user.auth("test_password")); - assert!(!test_user.auth("other_password")); + fill_database(conn); + let test_user = NewUser::new_local( + conn, + "test".to_owned(), + "test user".to_owned(), + false, + "Hello I'm a test", + "test@example.com".to_owned(), + User::hash_pass("test_password").unwrap(), + ) + .unwrap(); - Ok(()) - }); + assert!(test_user.auth("test_password")); + assert!(!test_user.auth("other_password")); } #[test] fn get_local_page() { let conn = &db(); - conn.test_transaction::<_, (), _>(|| { - fill_database(conn); - - let page = User::get_local_page(conn, (0, 2)).unwrap(); - assert_eq!(page.len(), 2); - assert!(page[0].username <= page[1].username); - - let mut last_username = User::get_local_page(conn, (0, 1)).unwrap()[0] - .username - .clone(); - for i in 1..User::count_local(conn).unwrap() as i32 { - let page = User::get_local_page(conn, (i, i + 1)).unwrap(); - assert_eq!(page.len(), 1); - assert!(last_username <= page[0].username); - last_username = page[0].username.clone(); - } - assert_eq!( - User::get_local_page(conn, (0, User::count_local(conn).unwrap() as i32 + 10)) - .unwrap() - .len() as i64, - User::count_local(conn).unwrap() - ); - - Ok(()) - }); + fill_database(conn); + + let page = User::get_local_page(conn, (0, 2)).unwrap(); + assert_eq!(page.len(), 2); + assert!(page[0].username <= page[1].username); + + let mut last_username = User::get_local_page(conn, (0, 1)).unwrap()[0] + .username + .clone(); + for i in 1..User::count_local(conn).unwrap() as i32 { + let page = User::get_local_page(conn, (i, i + 1)).unwrap(); + assert_eq!(page.len(), 1); + assert!(last_username <= page[0].username); + last_username = page[0].username.clone(); + } + assert_eq!( + User::get_local_page(conn, (0, User::count_local(conn).unwrap() as i32 + 10)) + .unwrap() + .len() as i64, + User::count_local(conn).unwrap() + ); } }