Make tests follow API changes

This commit is contained in:
Kitaiti Makoto 2021-01-30 23:15:07 +09:00
parent 25e52788c5
commit 78be49d57d
8 changed files with 305 additions and 338 deletions

View File

@ -87,7 +87,7 @@ impl BlocklistedEmail {
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
use crate::{instance::tests as instance_tests, tests::rockets, Connection as Conn}; use crate::{instance::tests as instance_tests, tests::db, Connection as Conn};
use diesel::Connection; use diesel::Connection;
pub(crate) fn fill_database(conn: &Conn) -> Vec<BlocklistedEmail> { pub(crate) fn fill_database(conn: &Conn) -> Vec<BlocklistedEmail> {
@ -106,29 +106,28 @@ pub(crate) mod tests {
} }
#[test] #[test]
fn test_match() { fn test_match() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let various = fill_database(conn); let various = fill_database(&conn);
let match1 = "user1@bad-actor.com"; let match1 = "user1@bad-actor.com";
let match2 = "spammer@lax-administration.com"; let match2 = "spammer@lax-administration.com";
let no_match = "happy-user@lax-administration.com"; let no_match = "happy-user@lax-administration.com";
assert_eq!( assert_eq!(
BlocklistedEmail::matches_blocklist(conn, match1) BlocklistedEmail::matches_blocklist(&conn, match1)
.unwrap() .unwrap()
.unwrap() .unwrap()
.id, .id,
various[0].id various[0].id
); );
assert_eq!( assert_eq!(
BlocklistedEmail::matches_blocklist(conn, match2) BlocklistedEmail::matches_blocklist(&conn, match2)
.unwrap() .unwrap()
.unwrap() .unwrap()
.id, .id,
various[1].id various[1].id
); );
assert_eq!( assert_eq!(
BlocklistedEmail::matches_blocklist(conn, no_match) BlocklistedEmail::matches_blocklist(&conn, no_match)
.unwrap() .unwrap()
.is_none(), .is_none(),
true true

View File

@ -496,12 +496,8 @@ impl NewBlog {
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
use crate::{ use crate::{
blog_authors::*, blog_authors::*, instance::tests as instance_tests, medias::NewMedia, tests::db,
instance::tests as instance_tests, users::tests as usersTests, Connection as Conn,
medias::NewMedia,
tests::{db, rockets},
users::tests as usersTests,
Connection as Conn,
}; };
use diesel::Connection; use diesel::Connection;
@ -588,10 +584,10 @@ pub(crate) mod tests {
fn get_instance() { fn get_instance() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
fill_database(conn); fill_database(&conn);
let blog = Blog::insert( let blog = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"SomeName".to_owned(), "SomeName".to_owned(),
"Some name".to_owned(), "Some name".to_owned(),
@ -603,7 +599,7 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
blog.get_instance(conn).unwrap().id, blog.get_instance(&conn).unwrap().id,
Instance::get_local().unwrap().id Instance::get_local().unwrap().id
); );
// TODO add tests for remote instance // TODO add tests for remote instance
@ -615,10 +611,10 @@ pub(crate) mod tests {
fn authors() { fn authors() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (user, _) = fill_database(conn); let (user, _) = fill_database(&conn);
let b1 = Blog::insert( let b1 = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"SomeName".to_owned(), "SomeName".to_owned(),
"Some name".to_owned(), "Some name".to_owned(),
@ -629,7 +625,7 @@ pub(crate) mod tests {
) )
.unwrap(); .unwrap();
let b2 = Blog::insert( let b2 = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"Blog".to_owned(), "Blog".to_owned(),
"Blog".to_owned(), "Blog".to_owned(),
@ -642,7 +638,7 @@ pub(crate) mod tests {
let blog = vec![b1, b2]; let blog = vec![b1, b2];
BlogAuthor::insert( BlogAuthor::insert(
conn, &conn,
NewBlogAuthor { NewBlogAuthor {
blog_id: blog[0].id, blog_id: blog[0].id,
author_id: user[0].id, author_id: user[0].id,
@ -652,7 +648,7 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
BlogAuthor::insert( BlogAuthor::insert(
conn, &conn,
NewBlogAuthor { NewBlogAuthor {
blog_id: blog[0].id, blog_id: blog[0].id,
author_id: user[1].id, author_id: user[1].id,
@ -662,7 +658,7 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
BlogAuthor::insert( BlogAuthor::insert(
conn, &conn,
NewBlogAuthor { NewBlogAuthor {
blog_id: blog[1].id, blog_id: blog[1].id,
author_id: user[0].id, author_id: user[0].id,
@ -672,39 +668,39 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
assert!(blog[0] assert!(blog[0]
.list_authors(conn) .list_authors(&conn)
.unwrap() .unwrap()
.iter() .iter()
.any(|a| a.id == user[0].id)); .any(|a| a.id == user[0].id));
assert!(blog[0] assert!(blog[0]
.list_authors(conn) .list_authors(&conn)
.unwrap() .unwrap()
.iter() .iter()
.any(|a| a.id == user[1].id)); .any(|a| a.id == user[1].id));
assert!(blog[1] assert!(blog[1]
.list_authors(conn) .list_authors(&conn)
.unwrap() .unwrap()
.iter() .iter()
.any(|a| a.id == user[0].id)); .any(|a| a.id == user[0].id));
assert!(!blog[1] assert!(!blog[1]
.list_authors(conn) .list_authors(&conn)
.unwrap() .unwrap()
.iter() .iter()
.any(|a| a.id == user[1].id)); .any(|a| a.id == user[1].id));
assert!(Blog::find_for_author(conn, &user[0]) assert!(Blog::find_for_author(&conn, &user[0])
.unwrap() .unwrap()
.iter() .iter()
.any(|b| b.id == blog[0].id)); .any(|b| b.id == blog[0].id));
assert!(Blog::find_for_author(conn, &user[1]) assert!(Blog::find_for_author(&conn, &user[1])
.unwrap() .unwrap()
.iter() .iter()
.any(|b| b.id == blog[0].id)); .any(|b| b.id == blog[0].id));
assert!(Blog::find_for_author(conn, &user[0]) assert!(Blog::find_for_author(&conn, &user[0])
.unwrap() .unwrap()
.iter() .iter()
.any(|b| b.id == blog[1].id)); .any(|b| b.id == blog[1].id));
assert!(!Blog::find_for_author(conn, &user[1]) assert!(!Blog::find_for_author(&conn, &user[1])
.unwrap() .unwrap()
.iter() .iter()
.any(|b| b.id == blog[1].id)); .any(|b| b.id == blog[1].id));
@ -714,13 +710,12 @@ pub(crate) mod tests {
#[test] #[test]
fn find_local() { fn find_local() {
let r = rockets(); let conn = &db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
fill_database(conn); fill_database(&conn);
let blog = Blog::insert( let blog = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"SomeName".to_owned(), "SomeName".to_owned(),
"Some name".to_owned(), "Some name".to_owned(),
@ -731,7 +726,7 @@ pub(crate) mod tests {
) )
.unwrap(); .unwrap();
assert_eq!(Blog::find_by_fqn(&r, "SomeName").unwrap().id, blog.id); assert_eq!(Blog::find_by_fqn(&conn, "SomeName").unwrap().id, blog.id);
Ok(()) Ok(())
}) })
} }
@ -740,10 +735,10 @@ pub(crate) mod tests {
fn get_fqn() { fn get_fqn() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
fill_database(conn); fill_database(&conn);
let blog = Blog::insert( let blog = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"SomeName".to_owned(), "SomeName".to_owned(),
"Some name".to_owned(), "Some name".to_owned(),
@ -763,10 +758,10 @@ pub(crate) mod tests {
fn delete() { fn delete() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, blogs) = fill_database(conn); let (_, blogs) = fill_database(&conn);
blogs[0].delete(conn).unwrap(); blogs[0].delete(&conn).unwrap();
assert!(Blog::get(conn, blogs[0].id).is_err()); assert!(Blog::get(&conn, blogs[0].id).is_err());
Ok(()) Ok(())
}) })
} }
@ -775,10 +770,10 @@ pub(crate) mod tests {
fn delete_via_user() { fn delete_via_user() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (user, _) = fill_database(conn); let (user, _) = fill_database(&conn);
let b1 = Blog::insert( let b1 = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"SomeName".to_owned(), "SomeName".to_owned(),
"Some name".to_owned(), "Some name".to_owned(),
@ -789,7 +784,7 @@ pub(crate) mod tests {
) )
.unwrap(); .unwrap();
let b2 = Blog::insert( let b2 = Blog::insert(
conn, &conn,
NewBlog::new_local( NewBlog::new_local(
"Blog".to_owned(), "Blog".to_owned(),
"Blog".to_owned(), "Blog".to_owned(),
@ -802,7 +797,7 @@ pub(crate) mod tests {
let blog = vec![b1, b2]; let blog = vec![b1, b2];
BlogAuthor::insert( BlogAuthor::insert(
conn, &conn,
NewBlogAuthor { NewBlogAuthor {
blog_id: blog[0].id, blog_id: blog[0].id,
author_id: user[0].id, author_id: user[0].id,
@ -812,7 +807,7 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
BlogAuthor::insert( BlogAuthor::insert(
conn, &conn,
NewBlogAuthor { NewBlogAuthor {
blog_id: blog[0].id, blog_id: blog[0].id,
author_id: user[1].id, author_id: user[1].id,
@ -822,7 +817,7 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
BlogAuthor::insert( BlogAuthor::insert(
conn, &conn,
NewBlogAuthor { NewBlogAuthor {
blog_id: blog[1].id, blog_id: blog[1].id,
author_id: user[0].id, author_id: user[0].id,
@ -831,24 +826,23 @@ pub(crate) mod tests {
) )
.unwrap(); .unwrap();
user[0].delete(conn).unwrap(); user[0].delete(&conn).unwrap();
assert!(Blog::get(conn, blog[0].id).is_ok()); assert!(Blog::get(&conn, blog[0].id).is_ok());
assert!(Blog::get(conn, blog[1].id).is_err()); assert!(Blog::get(&conn, blog[1].id).is_err());
user[1].delete(conn).unwrap(); user[1].delete(&conn).unwrap();
assert!(Blog::get(conn, blog[0].id).is_err()); assert!(Blog::get(&conn, blog[0].id).is_err());
Ok(()) Ok(())
}) })
} }
#[test] #[test]
fn self_federation() { fn self_federation() {
let r = rockets(); let conn = &db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, mut blogs) = fill_database(conn); let (users, mut blogs) = fill_database(&conn);
blogs[0].icon_id = Some( blogs[0].icon_id = Some(
Media::insert( Media::insert(
conn, &conn,
NewMedia { NewMedia {
file_path: "aaa.png".into(), file_path: "aaa.png".into(),
alt_text: String::new(), alt_text: String::new(),
@ -864,7 +858,7 @@ pub(crate) mod tests {
); );
blogs[0].banner_id = Some( blogs[0].banner_id = Some(
Media::insert( Media::insert(
conn, &conn,
NewMedia { NewMedia {
file_path: "bbb.png".into(), file_path: "bbb.png".into(),
alt_text: String::new(), alt_text: String::new(),
@ -878,11 +872,11 @@ pub(crate) mod tests {
.unwrap() .unwrap()
.id, .id,
); );
let _: Blog = blogs[0].save_changes(conn).unwrap(); let _: Blog = blogs[0].save_changes(&**conn).unwrap();
let ap_repr = blogs[0].to_activity(conn).unwrap(); let ap_repr = blogs[0].to_activity(&conn).unwrap();
blogs[0].delete(conn).unwrap(); blogs[0].delete(&conn).unwrap();
let blog = Blog::from_activity(&r, ap_repr).unwrap(); let blog = Blog::from_activity(&conn, ap_repr).unwrap();
assert_eq!(blog.actor_id, blogs[0].actor_id); assert_eq!(blog.actor_id, blogs[0].actor_id);
assert_eq!(blog.title, blogs[0].title); assert_eq!(blog.title, blogs[0].title);
@ -894,8 +888,8 @@ pub(crate) mod tests {
assert_eq!(blog.public_key, blogs[0].public_key); assert_eq!(blog.public_key, blogs[0].public_key);
assert_eq!(blog.fqn, blogs[0].fqn); assert_eq!(blog.fqn, blogs[0].fqn);
assert_eq!(blog.summary_html, blogs[0].summary_html); assert_eq!(blog.summary_html, blogs[0].summary_html);
assert_eq!(blog.icon_url(conn), blogs[0].icon_url(conn)); assert_eq!(blog.icon_url(&conn), blogs[0].icon_url(&conn));
assert_eq!(blog.banner_url(conn), blogs[0].banner_url(conn)); assert_eq!(blog.banner_url(&conn), blogs[0].banner_url(&conn));
Ok(()) Ok(())
}) })

View File

@ -392,17 +392,16 @@ mod tests {
use super::*; use super::*;
use crate::inbox::{inbox, tests::fill_database, InboxResult}; use crate::inbox::{inbox, tests::fill_database, InboxResult};
use crate::safe_string::SafeString; use crate::safe_string::SafeString;
use crate::tests::rockets; use crate::tests::db;
use diesel::Connection; use diesel::Connection;
// creates a post, get it's Create activity, delete the post, // creates a post, get it's Create activity, delete the post,
// "send" the Create to the inbox, and check it works // "send" the Create to the inbox, and check it works
#[test] #[test]
fn self_federation() { fn self_federation() {
let r = rockets(); let conn = &db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let original_comm = Comment::insert( let original_comm = Comment::insert(
conn, conn,
@ -418,14 +417,14 @@ mod tests {
}, },
) )
.unwrap(); .unwrap();
let act = original_comm.create_activity(&r).unwrap(); let act = original_comm.create_activity(&conn).unwrap();
inbox( inbox(
&r, &conn,
serde_json::to_value(original_comm.build_delete(conn).unwrap()).unwrap(), serde_json::to_value(original_comm.build_delete(&conn).unwrap()).unwrap(),
) )
.unwrap(); .unwrap();
match inbox(&r, serde_json::to_value(act).unwrap()).unwrap() { match inbox(&conn, serde_json::to_value(act).unwrap()).unwrap() {
InboxResult::Commented(c) => { InboxResult::Commented(c) => {
// TODO: one is HTML, the other markdown: assert_eq!(c.content, original_comm.content); // TODO: one is HTML, the other markdown: assert_eq!(c.content, original_comm.content);
assert_eq!(c.in_response_to_id, original_comm.in_response_to_id); assert_eq!(c.in_response_to_id, original_comm.in_response_to_id);

View File

@ -67,13 +67,13 @@ pub fn inbox(conn: DbConn, act: serde_json::Value) -> Result<InboxResult, Error>
pub(crate) mod tests { pub(crate) mod tests {
use super::InboxResult; use super::InboxResult;
use crate::blogs::tests::fill_database as blog_fill_db; use crate::blogs::tests::fill_database as blog_fill_db;
use crate::db_conn::DbConn;
use crate::safe_string::SafeString; use crate::safe_string::SafeString;
use crate::tests::rockets; use crate::tests::db;
use crate::PlumeRocket;
use diesel::Connection; use diesel::Connection;
pub fn fill_database( pub fn fill_database(
rockets: &PlumeRocket, conn: &DbConn,
) -> ( ) -> (
Vec<crate::posts::Post>, Vec<crate::posts::Post>,
Vec<crate::users::User>, Vec<crate::users::User>,
@ -82,9 +82,9 @@ pub(crate) mod tests {
use crate::post_authors::*; use crate::post_authors::*;
use crate::posts::*; use crate::posts::*;
let (users, blogs) = blog_fill_db(&rockets.conn); let (users, blogs) = blog_fill_db(&conn);
let post = Post::insert( let post = Post::insert(
&rockets.conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "testing".to_owned(), slug: "testing".to_owned(),
@ -102,7 +102,7 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
PostAuthor::insert( PostAuthor::insert(
&rockets.conn, &conn,
NewPostAuthor { NewPostAuthor {
post_id: post.id, post_id: post.id,
author_id: users[0].id, author_id: users[0].id,
@ -115,10 +115,9 @@ pub(crate) mod tests {
#[test] #[test]
fn announce_post() { fn announce_post() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/announce/1", "id": "https://plu.me/announce/1",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -126,7 +125,7 @@ pub(crate) mod tests {
"type": "Announce", "type": "Announce",
}); });
match super::inbox(&r, act).unwrap() { match super::inbox(&conn, act).unwrap() {
super::InboxResult::Reshared(r) => { super::InboxResult::Reshared(r) => {
assert_eq!(r.post_id, posts[0].id); assert_eq!(r.post_id, posts[0].id);
assert_eq!(r.user_id, users[0].id); assert_eq!(r.user_id, users[0].id);
@ -140,10 +139,9 @@ pub(crate) mod tests {
#[test] #[test]
fn create_comment() { fn create_comment() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -158,7 +156,7 @@ pub(crate) mod tests {
"type": "Create", "type": "Create",
}); });
match super::inbox(&r, act).unwrap() { match super::inbox(&conn, act).unwrap() {
super::InboxResult::Commented(c) => { super::InboxResult::Commented(c) => {
assert_eq!(c.author_id, users[0].id); assert_eq!(c.author_id, users[0].id);
assert_eq!(c.post_id, posts[0].id); assert_eq!(c.post_id, posts[0].id);
@ -174,10 +172,9 @@ pub(crate) mod tests {
#[test] #[test]
fn spoof_comment() { fn spoof_comment() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -193,7 +190,7 @@ pub(crate) mod tests {
}); });
assert!(matches!( assert!(matches!(
super::inbox(&r, act.clone()), super::inbox(&conn, act.clone()),
Err(super::Error::Inbox( Err(super::Error::Inbox(
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_), box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
)) ))
@ -204,10 +201,9 @@ pub(crate) mod tests {
#[test] #[test]
fn spoof_comment_by_object_with_id() { fn spoof_comment_by_object_with_id() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -225,7 +221,7 @@ pub(crate) mod tests {
}); });
assert!(matches!( assert!(matches!(
super::inbox(&r, act.clone()), super::inbox(&conn, act.clone()),
Err(super::Error::Inbox( Err(super::Error::Inbox(
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_), box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
)) ))
@ -235,10 +231,9 @@ pub(crate) mod tests {
} }
#[test] #[test]
fn spoof_comment_by_object_without_id() { fn spoof_comment_by_object_without_id() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -254,7 +249,7 @@ pub(crate) mod tests {
}); });
assert!(matches!( assert!(matches!(
super::inbox(&r, act.clone()), super::inbox(&conn, act.clone()),
Err(super::Error::Inbox( Err(super::Error::Inbox(
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_), box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
)) ))
@ -265,10 +260,9 @@ pub(crate) mod tests {
#[test] #[test]
fn create_post() { fn create_post() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, blogs) = fill_database(&r); let (_, users, blogs) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -289,9 +283,9 @@ pub(crate) mod tests {
"type": "Create", "type": "Create",
}); });
match super::inbox(&r, act).unwrap() { match super::inbox(&conn, act).unwrap() {
super::InboxResult::Post(p) => { super::InboxResult::Post(p) => {
assert!(p.is_author(conn, users[0].id).unwrap()); assert!(p.is_author(&conn, users[0].id).unwrap());
assert_eq!(p.source, "Hello.".to_owned()); assert_eq!(p.source, "Hello.".to_owned());
assert_eq!(p.blog_id, blogs[0].id); assert_eq!(p.blog_id, blogs[0].id);
assert_eq!(p.content, SafeString::new("Hello.")); assert_eq!(p.content, SafeString::new("Hello."));
@ -306,10 +300,9 @@ pub(crate) mod tests {
#[test] #[test]
fn spoof_post() { fn spoof_post() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, blogs) = fill_database(&r); let (_, users, blogs) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -331,7 +324,7 @@ pub(crate) mod tests {
}); });
assert!(matches!( assert!(matches!(
super::inbox(&r, act.clone()), super::inbox(&conn, act.clone()),
Err(super::Error::Inbox( Err(super::Error::Inbox(
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_), box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
)) ))
@ -342,10 +335,9 @@ pub(crate) mod tests {
#[test] #[test]
fn spoof_post_by_object_with_id() { fn spoof_post_by_object_with_id() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, blogs) = fill_database(&r); let (_, users, blogs) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -370,7 +362,7 @@ pub(crate) mod tests {
}); });
assert!(matches!( assert!(matches!(
super::inbox(&r, act.clone()), super::inbox(&conn, act.clone()),
Err(super::Error::Inbox( Err(super::Error::Inbox(
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_), box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
)) ))
@ -381,10 +373,9 @@ pub(crate) mod tests {
#[test] #[test]
fn spoof_post_by_object_without_id() { fn spoof_post_by_object_without_id() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, blogs) = fill_database(&r); let (_, users, blogs) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/comment/1/activity", "id": "https://plu.me/comment/1/activity",
"actor": users[0].ap_url, "actor": users[0].ap_url,
@ -406,7 +397,7 @@ pub(crate) mod tests {
}); });
assert!(matches!( assert!(matches!(
super::inbox(&r, act.clone()), super::inbox(&conn, act.clone()),
Err(super::Error::Inbox( Err(super::Error::Inbox(
box plume_common::activity_pub::inbox::InboxError::InvalidObject(_), box plume_common::activity_pub::inbox::InboxError::InvalidObject(_),
)) ))
@ -419,12 +410,11 @@ pub(crate) mod tests {
fn delete_comment() { fn delete_comment() {
use crate::comments::*; use crate::comments::*;
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
Comment::insert( Comment::insert(
conn, &conn,
NewComment { NewComment {
content: SafeString::new("My comment"), content: SafeString::new("My comment"),
in_response_to_id: None, in_response_to_id: None,
@ -444,7 +434,7 @@ pub(crate) mod tests {
"object": "https://plu.me/comment/1", "object": "https://plu.me/comment/1",
"type": "Delete", "type": "Delete",
}); });
assert!(super::inbox(&r, fail_act).is_err()); assert!(super::inbox(&conn, fail_act).is_err());
let ok_act = json!({ let ok_act = json!({
"id": "https://plu.me/comment/1/delete", "id": "https://plu.me/comment/1/delete",
@ -452,17 +442,16 @@ pub(crate) mod tests {
"object": "https://plu.me/comment/1", "object": "https://plu.me/comment/1",
"type": "Delete", "type": "Delete",
}); });
assert!(super::inbox(&r, ok_act).is_ok()); assert!(super::inbox(&conn, ok_act).is_ok());
Ok(()) Ok(())
}) })
} }
#[test] #[test]
fn delete_post() { fn delete_post() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let fail_act = json!({ let fail_act = json!({
"id": "https://plu.me/comment/1/delete", "id": "https://plu.me/comment/1/delete",
@ -470,7 +459,7 @@ pub(crate) mod tests {
"object": posts[0].ap_url, "object": posts[0].ap_url,
"type": "Delete", "type": "Delete",
}); });
assert!(super::inbox(&r, fail_act).is_err()); assert!(super::inbox(&conn, fail_act).is_err());
let ok_act = json!({ let ok_act = json!({
"id": "https://plu.me/comment/1/delete", "id": "https://plu.me/comment/1/delete",
@ -478,17 +467,16 @@ pub(crate) mod tests {
"object": posts[0].ap_url, "object": posts[0].ap_url,
"type": "Delete", "type": "Delete",
}); });
assert!(super::inbox(&r, ok_act).is_ok()); assert!(super::inbox(&conn, ok_act).is_ok());
Ok(()) Ok(())
}); });
} }
#[test] #[test]
fn delete_user() { fn delete_user() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, _) = fill_database(&r); let (_, users, _) = fill_database(&conn);
let fail_act = json!({ let fail_act = json!({
"id": "https://plu.me/@/Admin#delete", "id": "https://plu.me/@/Admin#delete",
@ -496,7 +484,7 @@ pub(crate) mod tests {
"object": users[0].ap_url, "object": users[0].ap_url,
"type": "Delete", "type": "Delete",
}); });
assert!(super::inbox(&r, fail_act).is_err()); assert!(super::inbox(&conn, fail_act).is_err());
let ok_act = json!({ let ok_act = json!({
"id": "https://plu.me/@/Admin#delete", "id": "https://plu.me/@/Admin#delete",
@ -504,8 +492,8 @@ pub(crate) mod tests {
"object": users[0].ap_url, "object": users[0].ap_url,
"type": "Delete", "type": "Delete",
}); });
assert!(super::inbox(&r, ok_act).is_ok()); assert!(super::inbox(&conn, ok_act).is_ok());
assert!(crate::users::User::get(conn, users[0].id).is_err()); assert!(crate::users::User::get(&conn, users[0].id).is_err());
Ok(()) Ok(())
}); });
@ -513,10 +501,9 @@ pub(crate) mod tests {
#[test] #[test]
fn follow() { fn follow() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, _) = fill_database(&r); let (_, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/follow/1", "id": "https://plu.me/follow/1",
@ -524,7 +511,7 @@ pub(crate) mod tests {
"object": users[1].ap_url, "object": users[1].ap_url,
"type": "Follow", "type": "Follow",
}); });
match super::inbox(&r, act).unwrap() { match super::inbox(&conn, act).unwrap() {
InboxResult::Followed(f) => { InboxResult::Followed(f) => {
assert_eq!(f.follower_id, users[0].id); assert_eq!(f.follower_id, users[0].id);
assert_eq!(f.following_id, users[1].id); assert_eq!(f.following_id, users[1].id);
@ -538,10 +525,9 @@ pub(crate) mod tests {
#[test] #[test]
fn like() { fn like() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/like/1", "id": "https://plu.me/like/1",
@ -549,7 +535,7 @@ pub(crate) mod tests {
"object": posts[0].ap_url, "object": posts[0].ap_url,
"type": "Like", "type": "Like",
}); });
match super::inbox(&r, act).unwrap() { match super::inbox(&conn, act).unwrap() {
InboxResult::Liked(l) => { InboxResult::Liked(l) => {
assert_eq!(l.user_id, users[1].id); assert_eq!(l.user_id, users[1].id);
assert_eq!(l.post_id, posts[0].id); assert_eq!(l.post_id, posts[0].id);
@ -565,13 +551,12 @@ pub(crate) mod tests {
fn undo_reshare() { fn undo_reshare() {
use crate::reshares::*; use crate::reshares::*;
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let announce = Reshare::insert( let announce = Reshare::insert(
conn, &conn,
NewReshare { NewReshare {
post_id: posts[0].id, post_id: posts[0].id,
user_id: users[1].id, user_id: users[1].id,
@ -586,7 +571,7 @@ pub(crate) mod tests {
"object": announce.ap_url, "object": announce.ap_url,
"type": "Undo", "type": "Undo",
}); });
assert!(super::inbox(&r, fail_act).is_err()); assert!(super::inbox(&conn, fail_act).is_err());
let ok_act = json!({ let ok_act = json!({
"id": "https://plu.me/undo/1", "id": "https://plu.me/undo/1",
@ -594,7 +579,7 @@ pub(crate) mod tests {
"object": announce.ap_url, "object": announce.ap_url,
"type": "Undo", "type": "Undo",
}); });
assert!(super::inbox(&r, ok_act).is_ok()); assert!(super::inbox(&conn, ok_act).is_ok());
Ok(()) Ok(())
}); });
} }
@ -603,13 +588,12 @@ pub(crate) mod tests {
fn undo_follow() { fn undo_follow() {
use crate::follows::*; use crate::follows::*;
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, _) = fill_database(&r); let (_, users, _) = fill_database(&conn);
let follow = Follow::insert( let follow = Follow::insert(
conn, &conn,
NewFollow { NewFollow {
follower_id: users[0].id, follower_id: users[0].id,
following_id: users[1].id, following_id: users[1].id,
@ -624,7 +608,7 @@ pub(crate) mod tests {
"object": follow.ap_url, "object": follow.ap_url,
"type": "Undo", "type": "Undo",
}); });
assert!(super::inbox(&r, fail_act).is_err()); assert!(super::inbox(&conn, fail_act).is_err());
let ok_act = json!({ let ok_act = json!({
"id": "https://plu.me/undo/1", "id": "https://plu.me/undo/1",
@ -632,7 +616,7 @@ pub(crate) mod tests {
"object": follow.ap_url, "object": follow.ap_url,
"type": "Undo", "type": "Undo",
}); });
assert!(super::inbox(&r, ok_act).is_ok()); assert!(super::inbox(&conn, ok_act).is_ok());
Ok(()) Ok(())
}); });
} }
@ -641,13 +625,12 @@ pub(crate) mod tests {
fn undo_like() { fn undo_like() {
use crate::likes::*; use crate::likes::*;
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let like = Like::insert( let like = Like::insert(
conn, &conn,
NewLike { NewLike {
post_id: posts[0].id, post_id: posts[0].id,
user_id: users[1].id, user_id: users[1].id,
@ -662,7 +645,7 @@ pub(crate) mod tests {
"object": like.ap_url, "object": like.ap_url,
"type": "Undo", "type": "Undo",
}); });
assert!(super::inbox(&r, fail_act).is_err()); assert!(super::inbox(&conn, fail_act).is_err());
let ok_act = json!({ let ok_act = json!({
"id": "https://plu.me/undo/1", "id": "https://plu.me/undo/1",
@ -670,17 +653,16 @@ pub(crate) mod tests {
"object": like.ap_url, "object": like.ap_url,
"type": "Undo", "type": "Undo",
}); });
assert!(super::inbox(&r, ok_act).is_ok()); assert!(super::inbox(&conn, ok_act).is_ok());
Ok(()) Ok(())
}); });
} }
#[test] #[test]
fn update_post() { fn update_post() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (posts, users, _) = fill_database(&r); let (posts, users, _) = fill_database(&conn);
let act = json!({ let act = json!({
"id": "https://plu.me/update/1", "id": "https://plu.me/update/1",
@ -699,7 +681,7 @@ pub(crate) mod tests {
"type": "Update", "type": "Update",
}); });
super::inbox(&r, act).unwrap(); super::inbox(&conn, act).unwrap();
Ok(()) Ok(())
}); });
} }

View File

@ -294,12 +294,10 @@ pub fn ap_url(url: &str) -> String {
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
mod tests { mod tests {
use crate::{db_conn, migrations::IMPORTED_MIGRATIONS, search, Connection as Conn, CONFIG}; use crate::{db_conn, migrations::IMPORTED_MIGRATIONS, Connection as Conn, CONFIG};
use diesel::r2d2::ConnectionManager; use diesel::r2d2::ConnectionManager;
use plume_common::utils::random_hex; use plume_common::utils::random_hex;
use scheduled_thread_pool::ScheduledThreadPool;
use std::env::temp_dir; use std::env::temp_dir;
use std::sync::Arc;
#[macro_export] #[macro_export]
macro_rules! part_eq { macro_rules! part_eq {
@ -329,15 +327,6 @@ mod tests {
pool pool
}; };
} }
pub fn rockets() -> super::PlumeRocket {
super::PlumeRocket {
conn: db_conn::DbConn((*DB_POOL).get().unwrap()),
searcher: Arc::new(search::tests::get_searcher(&CONFIG.search_tokenizers)),
worker: Arc::new(ScheduledThreadPool::new(2)),
user: None,
}
}
} }
pub mod admin; pub mod admin;

View File

@ -880,19 +880,18 @@ mod tests {
use super::*; use super::*;
use crate::inbox::{inbox, tests::fill_database, InboxResult}; use crate::inbox::{inbox, tests::fill_database, InboxResult};
use crate::safe_string::SafeString; use crate::safe_string::SafeString;
use crate::tests::rockets; use crate::tests::db;
use diesel::Connection; use diesel::Connection;
// creates a post, get it's Create activity, delete the post, // creates a post, get it's Create activity, delete the post,
// "send" the Create to the inbox, and check it works // "send" the Create to the inbox, and check it works
#[test] #[test]
fn self_federation() { fn self_federation() {
let r = rockets(); let conn = &db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (_, users, blogs) = fill_database(&r); let (_, users, blogs) = fill_database(&conn);
let post = Post::insert( let post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "yo".into(), slug: "yo".into(),
@ -909,19 +908,19 @@ mod tests {
) )
.unwrap(); .unwrap();
PostAuthor::insert( PostAuthor::insert(
conn, &conn,
NewPostAuthor { NewPostAuthor {
post_id: post.id, post_id: post.id,
author_id: users[0].id, author_id: users[0].id,
}, },
) )
.unwrap(); .unwrap();
let create = post.create_activity(conn).unwrap(); let create = post.create_activity(&conn).unwrap();
post.delete(conn).unwrap(); post.delete(&conn).unwrap();
match inbox(&r, serde_json::to_value(create).unwrap()).unwrap() { match inbox(&conn, serde_json::to_value(create).unwrap()).unwrap() {
InboxResult::Post(p) => { InboxResult::Post(p) => {
assert!(p.is_author(conn, users[0].id).unwrap()); assert!(p.is_author(&conn, users[0].id).unwrap());
assert_eq!(p.source, "Hello".to_owned()); assert_eq!(p.source, "Hello".to_owned());
assert_eq!(p.blog_id, blogs[0].id); assert_eq!(p.blog_id, blogs[0].id);
assert_eq!(p.content, SafeString::new("Hello")); assert_eq!(p.content, SafeString::new("Hello"));

View File

@ -249,7 +249,7 @@ mod tests {
posts::NewPost, posts::NewPost,
safe_string::SafeString, safe_string::SafeString,
tags::Tag, tags::Tag,
tests::{db, rockets}, tests::db,
users::tests as userTests, users::tests as userTests,
}; };
use diesel::Connection; use diesel::Connection;
@ -258,63 +258,69 @@ mod tests {
fn test_timeline() { fn test_timeline() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let users = userTests::fill_database(conn); let users = userTests::fill_database(&conn);
let mut tl1_u1 = Timeline::new_for_user( let mut tl1_u1 = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"my timeline".to_owned(), "my timeline".to_owned(),
"all".to_owned(), "all".to_owned(),
) )
.unwrap(); .unwrap();
List::new(conn, "languages I speak", Some(&users[1]), ListType::Prefix).unwrap(); List::new(
&conn,
"languages I speak",
Some(&users[1]),
ListType::Prefix,
)
.unwrap();
let tl2_u1 = Timeline::new_for_user( let tl2_u1 = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"another timeline".to_owned(), "another timeline".to_owned(),
"followed".to_owned(), "followed".to_owned(),
) )
.unwrap(); .unwrap();
let tl1_u2 = Timeline::new_for_user( let tl1_u2 = Timeline::new_for_user(
conn, &conn,
users[1].id, users[1].id,
"english posts".to_owned(), "english posts".to_owned(),
"lang in \"languages I speak\"".to_owned(), "lang in \"languages I speak\"".to_owned(),
) )
.unwrap(); .unwrap();
let tl1_instance = Timeline::new_for_instance( let tl1_instance = Timeline::new_for_instance(
conn, &conn,
"english posts".to_owned(), "english posts".to_owned(),
"license in [cc]".to_owned(), "license in [cc]".to_owned(),
) )
.unwrap(); .unwrap();
assert_eq!(tl1_u1, Timeline::get(conn, tl1_u1.id).unwrap()); assert_eq!(tl1_u1, Timeline::get(&conn, tl1_u1.id).unwrap());
assert_eq!( assert_eq!(
tl2_u1, tl2_u1,
Timeline::find_for_user_by_name(conn, Some(users[0].id), "another timeline") Timeline::find_for_user_by_name(&conn, Some(users[0].id), "another timeline")
.unwrap() .unwrap()
); );
assert_eq!( assert_eq!(
tl1_instance, tl1_instance,
Timeline::find_for_user_by_name(conn, None, "english posts").unwrap() Timeline::find_for_user_by_name(&conn, None, "english posts").unwrap()
); );
let tl_u1 = Timeline::list_for_user(conn, Some(users[0].id)).unwrap(); let tl_u1 = Timeline::list_for_user(&conn, Some(users[0].id)).unwrap();
assert_eq!(3, tl_u1.len()); // it is not 2 because there is a "Your feed" tl created for each user automatically assert_eq!(3, tl_u1.len()); // it is not 2 because there is a "Your feed" tl created for each user automatically
assert!(tl_u1.iter().fold(false, |res, tl| { res || *tl == tl1_u1 })); assert!(tl_u1.iter().fold(false, |res, tl| { res || *tl == tl1_u1 }));
assert!(tl_u1.iter().fold(false, |res, tl| { res || *tl == tl2_u1 })); assert!(tl_u1.iter().fold(false, |res, tl| { res || *tl == tl2_u1 }));
let tl_instance = Timeline::list_for_user(conn, None).unwrap(); let tl_instance = Timeline::list_for_user(&conn, None).unwrap();
assert_eq!(3, tl_instance.len()); // there are also the local and federated feed by default assert_eq!(3, tl_instance.len()); // there are also the local and federated feed by default
assert!(tl_instance assert!(tl_instance
.iter() .iter()
.fold(false, |res, tl| { res || *tl == tl1_instance })); .fold(false, |res, tl| { res || *tl == tl1_instance }));
tl1_u1.name = "My Super TL".to_owned(); tl1_u1.name = "My Super TL".to_owned();
let new_tl1_u2 = tl1_u2.update(conn).unwrap(); let new_tl1_u2 = tl1_u2.update(&conn).unwrap();
let tl_u2 = Timeline::list_for_user(conn, Some(users[1].id)).unwrap(); let tl_u2 = Timeline::list_for_user(&conn, Some(users[1].id)).unwrap();
assert_eq!(2, tl_u2.len()); // same here assert_eq!(2, tl_u2.len()); // same here
assert!(tl_u2 assert!(tl_u2
.iter() .iter()
@ -328,48 +334,48 @@ mod tests {
fn test_timeline_creation_error() { fn test_timeline_creation_error() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let users = userTests::fill_database(conn); let users = userTests::fill_database(&conn);
assert!(Timeline::new_for_user( assert!(Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"my timeline".to_owned(), "my timeline".to_owned(),
"invalid keyword".to_owned(), "invalid keyword".to_owned(),
) )
.is_err()); .is_err());
assert!(Timeline::new_for_instance( assert!(Timeline::new_for_instance(
conn, &conn,
"my timeline".to_owned(), "my timeline".to_owned(),
"invalid keyword".to_owned(), "invalid keyword".to_owned(),
) )
.is_err()); .is_err());
assert!(Timeline::new_for_user( assert!(Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"my timeline".to_owned(), "my timeline".to_owned(),
"author in non_existant_list".to_owned(), "author in non_existant_list".to_owned(),
) )
.is_err()); .is_err());
assert!(Timeline::new_for_instance( assert!(Timeline::new_for_instance(
conn, &conn,
"my timeline".to_owned(), "my timeline".to_owned(),
"lang in dont-exist".to_owned(), "lang in dont-exist".to_owned(),
) )
.is_err()); .is_err());
List::new(conn, "friends", Some(&users[0]), ListType::User).unwrap(); List::new(&conn, "friends", Some(&users[0]), ListType::User).unwrap();
List::new(conn, "idk", None, ListType::Blog).unwrap(); List::new(&conn, "idk", None, ListType::Blog).unwrap();
assert!(Timeline::new_for_user( assert!(Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"my timeline".to_owned(), "my timeline".to_owned(),
"blog in friends".to_owned(), "blog in friends".to_owned(),
) )
.is_err()); .is_err());
assert!(Timeline::new_for_instance( assert!(Timeline::new_for_instance(
conn, &conn,
"my timeline".to_owned(), "my timeline".to_owned(),
"not author in idk".to_owned(), "not author in idk".to_owned(),
) )
@ -381,13 +387,12 @@ mod tests {
#[test] #[test]
fn test_simple_match() { fn test_simple_match() {
let r = &rockets(); let conn = &db();
let conn = &r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, blogs) = blogTests::fill_database(conn); let (users, blogs) = blogTests::fill_database(&conn);
let gnu_tl = Timeline::new_for_user( let gnu_tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"GNU timeline".to_owned(), "GNU timeline".to_owned(),
"license in [AGPL, LGPL, GPL]".to_owned(), "license in [AGPL, LGPL, GPL]".to_owned(),
@ -395,7 +400,7 @@ mod tests {
.unwrap(); .unwrap();
let gnu_post = Post::insert( let gnu_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug".to_string(), slug: "slug".to_string(),
@ -411,10 +416,10 @@ mod tests {
}, },
) )
.unwrap(); .unwrap();
assert!(gnu_tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(gnu_tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
let non_free_post = Post::insert( let non_free_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug2".to_string(), slug: "slug2".to_string(),
@ -430,7 +435,9 @@ mod tests {
}, },
) )
.unwrap(); .unwrap();
assert!(!gnu_tl.matches(r, &non_free_post, Kind::Original).unwrap()); assert!(!gnu_tl
.matches(&conn, &non_free_post, Kind::Original)
.unwrap());
Ok(()) Ok(())
}); });
@ -438,12 +445,11 @@ mod tests {
#[test] #[test]
fn test_complex_match() { fn test_complex_match() {
let r = &rockets(); let conn = &db();
let conn = &r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, blogs) = blogTests::fill_database(conn); let (users, blogs) = blogTests::fill_database(&conn);
Follow::insert( Follow::insert(
conn, &conn,
NewFollow { NewFollow {
follower_id: users[0].id, follower_id: users[0].id,
following_id: users[1].id, following_id: users[1].id,
@ -453,11 +459,11 @@ mod tests {
.unwrap(); .unwrap();
let fav_blogs_list = let fav_blogs_list =
List::new(conn, "fav_blogs", Some(&users[0]), ListType::Blog).unwrap(); List::new(&conn, "fav_blogs", Some(&users[0]), ListType::Blog).unwrap();
fav_blogs_list.add_blogs(conn, &[blogs[0].id]).unwrap(); fav_blogs_list.add_blogs(&conn, &[blogs[0].id]).unwrap();
let my_tl = Timeline::new_for_user( let my_tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"My timeline".to_owned(), "My timeline".to_owned(),
"blog in fav_blogs and not has_cover or local and followed exclude likes" "blog in fav_blogs and not has_cover or local and followed exclude likes"
@ -466,7 +472,7 @@ mod tests {
.unwrap(); .unwrap();
let post = Post::insert( let post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "about-linux".to_string(), slug: "about-linux".to_string(),
@ -482,10 +488,10 @@ mod tests {
}, },
) )
.unwrap(); .unwrap();
assert!(my_tl.matches(r, &post, Kind::Original).unwrap()); // matches because of "blog in fav_blogs" (and there is no cover) assert!(my_tl.matches(&conn, &post, Kind::Original).unwrap()); // matches because of "blog in fav_blogs" (and there is no cover)
let post = Post::insert( let post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[1].id, blog_id: blogs[1].id,
slug: "about-linux-2".to_string(), slug: "about-linux-2".to_string(),
@ -503,7 +509,7 @@ mod tests {
}, },
) )
.unwrap(); .unwrap();
assert!(!my_tl.matches(r, &post, Kind::Like(&users[1])).unwrap()); assert!(!my_tl.matches(&conn, &post, Kind::Like(&users[1])).unwrap());
Ok(()) Ok(())
}); });
@ -511,20 +517,19 @@ mod tests {
#[test] #[test]
fn test_add_to_all_timelines() { fn test_add_to_all_timelines() {
let r = &rockets(); let conn = &db();
let conn = &r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, blogs) = blogTests::fill_database(conn); let (users, blogs) = blogTests::fill_database(&conn);
let gnu_tl = Timeline::new_for_user( let gnu_tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"GNU timeline".to_owned(), "GNU timeline".to_owned(),
"license in [AGPL, LGPL, GPL]".to_owned(), "license in [AGPL, LGPL, GPL]".to_owned(),
) )
.unwrap(); .unwrap();
let non_gnu_tl = Timeline::new_for_user( let non_gnu_tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Stallman disapproved timeline".to_owned(), "Stallman disapproved timeline".to_owned(),
"not license in [AGPL, LGPL, GPL]".to_owned(), "not license in [AGPL, LGPL, GPL]".to_owned(),
@ -532,7 +537,7 @@ mod tests {
.unwrap(); .unwrap();
let gnu_post = Post::insert( let gnu_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug".to_string(), slug: "slug".to_string(),
@ -550,7 +555,7 @@ mod tests {
.unwrap(); .unwrap();
let non_free_post = Post::insert( let non_free_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug2".to_string(), slug: "slug2".to_string(),
@ -567,13 +572,13 @@ mod tests {
) )
.unwrap(); .unwrap();
Timeline::add_to_all_timelines(r, &gnu_post, Kind::Original).unwrap(); Timeline::add_to_all_timelines(&conn, &gnu_post, Kind::Original).unwrap();
Timeline::add_to_all_timelines(r, &non_free_post, Kind::Original).unwrap(); Timeline::add_to_all_timelines(&conn, &non_free_post, Kind::Original).unwrap();
let res = gnu_tl.get_latest(conn, 2).unwrap(); let res = gnu_tl.get_latest(&conn, 2).unwrap();
assert_eq!(res.len(), 1); assert_eq!(res.len(), 1);
assert_eq!(res[0].id, gnu_post.id); assert_eq!(res[0].id, gnu_post.id);
let res = non_gnu_tl.get_latest(conn, 2).unwrap(); let res = non_gnu_tl.get_latest(&conn, 2).unwrap();
assert_eq!(res.len(), 1); assert_eq!(res.len(), 1);
assert_eq!(res[0].id, non_free_post.id); assert_eq!(res[0].id, non_free_post.id);
@ -583,13 +588,12 @@ mod tests {
#[test] #[test]
fn test_matches_lists_direct() { fn test_matches_lists_direct() {
let r = &rockets(); let conn = &db();
let conn = &r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, blogs) = blogTests::fill_database(conn); let (users, blogs) = blogTests::fill_database(&conn);
let gnu_post = Post::insert( let gnu_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug".to_string(), slug: "slug".to_string(),
@ -606,61 +610,63 @@ mod tests {
) )
.unwrap(); .unwrap();
gnu_post gnu_post
.update_tags(conn, vec![Tag::build_activity("free".to_owned()).unwrap()]) .update_tags(&conn, vec![Tag::build_activity("free".to_owned()).unwrap()])
.unwrap(); .unwrap();
PostAuthor::insert( PostAuthor::insert(
conn, &conn,
NewPostAuthor { NewPostAuthor {
post_id: gnu_post.id, post_id: gnu_post.id,
author_id: blogs[0].list_authors(conn).unwrap()[0].id, author_id: blogs[0].list_authors(&conn).unwrap()[0].id,
}, },
) )
.unwrap(); .unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"blog timeline".to_owned(), "blog timeline".to_owned(),
format!("blog in [{}]", blogs[0].fqn), format!("blog in [{}]", blogs[0].fqn),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"blog timeline".to_owned(), "blog timeline".to_owned(),
"blog in [no_one@nowhere]".to_owned(), "blog in [no_one@nowhere]".to_owned(),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"author timeline".to_owned(), "author timeline".to_owned(),
format!( format!(
"author in [{}]", "author in [{}]",
blogs[0].list_authors(conn).unwrap()[0].fqn blogs[0].list_authors(&conn).unwrap()[0].fqn
), ),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"author timeline".to_owned(), "author timeline".to_owned(),
format!("author in [{}]", users[2].fqn), format!("author in [{}]", users[2].fqn),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
assert!(tl.matches(r, &gnu_post, Kind::Reshare(&users[2])).unwrap()); assert!(tl
assert!(!tl.matches(r, &gnu_post, Kind::Like(&users[2])).unwrap()); .matches(&conn, &gnu_post, Kind::Reshare(&users[2]))
tl.delete(conn).unwrap(); .unwrap());
assert!(!tl.matches(&conn, &gnu_post, Kind::Like(&users[2])).unwrap());
tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"author timeline".to_owned(), "author timeline".to_owned(),
format!( format!(
@ -669,48 +675,50 @@ mod tests {
), ),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
assert!(!tl.matches(r, &gnu_post, Kind::Reshare(&users[2])).unwrap()); assert!(!tl
assert!(tl.matches(r, &gnu_post, Kind::Like(&users[2])).unwrap()); .matches(&conn, &gnu_post, Kind::Reshare(&users[2]))
tl.delete(conn).unwrap(); .unwrap());
assert!(tl.matches(&conn, &gnu_post, Kind::Like(&users[2])).unwrap());
tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"tag timeline".to_owned(), "tag timeline".to_owned(),
"tags in [free]".to_owned(), "tags in [free]".to_owned(),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"tag timeline".to_owned(), "tag timeline".to_owned(),
"tags in [private]".to_owned(), "tags in [private]".to_owned(),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"english timeline".to_owned(), "english timeline".to_owned(),
"lang in [en]".to_owned(), "lang in [en]".to_owned(),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"franco-italian timeline".to_owned(), "franco-italian timeline".to_owned(),
"lang in [fr, it]".to_owned(), "lang in [fr, it]".to_owned(),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
Ok(()) Ok(())
}); });
@ -720,12 +728,12 @@ mod tests {
#[test] #[test]
fn test_matches_lists_saved() { fn test_matches_lists_saved() {
let r = &rockets(); let r = &rockets();
let conn = &r.conn; let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, blogs) = blogTests::fill_database(conn); let (users, blogs) = blogTests::fill_database(&conn);
let gnu_post = Post::insert( let gnu_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug".to_string(), slug: "slug".to_string(),
@ -741,8 +749,8 @@ mod tests {
}, },
) )
.unwrap(); .unwrap();
gnu_post.update_tags(conn, vec![Tag::build_activity("free".to_owned()).unwrap()]).unwrap(); gnu_post.update_tags(&conn, vec![Tag::build_activity("free".to_owned()).unwrap()]).unwrap();
PostAuthor::insert(conn, NewPostAuthor {post_id: gnu_post.id, author_id: blogs[0].list_authors(conn).unwrap()[0].id}).unwrap(); PostAuthor::insert(&conn, NewPostAuthor {post_id: gnu_post.id, author_id: blogs[0].list_authors(&conn).unwrap()[0].id}).unwrap();
unimplemented!(); unimplemented!();
@ -752,13 +760,12 @@ mod tests {
#[test] #[test]
fn test_matches_keyword() { fn test_matches_keyword() {
let r = &rockets(); let conn = &db();
let conn = &r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let (users, blogs) = blogTests::fill_database(conn); let (users, blogs) = blogTests::fill_database(&conn);
let gnu_post = Post::insert( let gnu_post = Post::insert(
conn, &conn,
NewPost { NewPost {
blog_id: blogs[0].id, blog_id: blogs[0].id,
slug: "slug".to_string(), slug: "slug".to_string(),
@ -776,61 +783,61 @@ mod tests {
.unwrap(); .unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Linux title".to_owned(), "Linux title".to_owned(),
"title contains Linux".to_owned(), "title contains Linux".to_owned(),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Microsoft title".to_owned(), "Microsoft title".to_owned(),
"title contains Microsoft".to_owned(), "title contains Microsoft".to_owned(),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Linux subtitle".to_owned(), "Linux subtitle".to_owned(),
"subtitle contains Stallman".to_owned(), "subtitle contains Stallman".to_owned(),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Microsoft subtitle".to_owned(), "Microsoft subtitle".to_owned(),
"subtitle contains Nadella".to_owned(), "subtitle contains Nadella".to_owned(),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Linux content".to_owned(), "Linux content".to_owned(),
"content contains Linux".to_owned(), "content contains Linux".to_owned(),
) )
.unwrap(); .unwrap();
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
let tl = Timeline::new_for_user( let tl = Timeline::new_for_user(
conn, &conn,
users[0].id, users[0].id,
"Microsoft content".to_owned(), "Microsoft content".to_owned(),
"subtitle contains Windows".to_owned(), "subtitle contains Windows".to_owned(),
) )
.unwrap(); .unwrap();
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap()); assert!(!tl.matches(&conn, &gnu_post, Kind::Original).unwrap());
tl.delete(conn).unwrap(); tl.delete(&conn).unwrap();
Ok(()) Ok(())
}); });

View File

@ -1131,7 +1131,7 @@ pub(crate) mod tests {
use super::*; use super::*;
use crate::{ use crate::{
instance::{tests as instance_tests, Instance}, instance::{tests as instance_tests, Instance},
tests::{db, rockets}, tests::db,
Connection as Conn, Connection as Conn,
}; };
use diesel::Connection; use diesel::Connection;
@ -1173,12 +1173,11 @@ pub(crate) mod tests {
#[test] #[test]
fn find_by() { fn find_by() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
fill_database(conn); fill_database(&conn);
let test_user = NewUser::new_local( let test_user = NewUser::new_local(
conn, &conn,
"test".to_owned(), "test".to_owned(),
"test user".to_owned(), "test user".to_owned(),
Role::Normal, Role::Normal,
@ -1189,22 +1188,22 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
test_user.id, test_user.id,
User::find_by_name(conn, "test", Instance::get_local().unwrap().id) User::find_by_name(&conn, "test", Instance::get_local().unwrap().id)
.unwrap() .unwrap()
.id .id
); );
assert_eq!( assert_eq!(
test_user.id, test_user.id,
User::find_by_fqn(&r, &test_user.fqn).unwrap().id User::find_by_fqn(&conn, &test_user.fqn).unwrap().id
); );
assert_eq!( assert_eq!(
test_user.id, test_user.id,
User::find_by_email(conn, "test@example.com").unwrap().id User::find_by_email(&conn, "test@example.com").unwrap().id
); );
assert_eq!( assert_eq!(
test_user.id, test_user.id,
User::find_by_ap_url( User::find_by_ap_url(
conn, &conn,
&format!( &format!(
"https://{}/@/{}/", "https://{}/@/{}/",
Instance::get_local().unwrap().public_domain, Instance::get_local().unwrap().public_domain,
@ -1222,11 +1221,11 @@ pub(crate) mod tests {
fn delete() { fn delete() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let inserted = fill_database(conn); let inserted = fill_database(&conn);
assert!(User::get(conn, inserted[0].id).is_ok()); assert!(User::get(&conn, inserted[0].id).is_ok());
inserted[0].delete(conn).unwrap(); inserted[0].delete(&conn).unwrap();
assert!(User::get(conn, inserted[0].id).is_err()); assert!(User::get(&conn, inserted[0].id).is_err());
Ok(()) Ok(())
}); });
} }
@ -1235,20 +1234,20 @@ pub(crate) mod tests {
fn admin() { fn admin() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let inserted = fill_database(conn); let inserted = fill_database(&conn);
let local_inst = Instance::get_local().unwrap(); let local_inst = Instance::get_local().unwrap();
let mut i = 0; let mut i = 0;
while local_inst.has_admin(conn).unwrap() { while local_inst.has_admin(&conn).unwrap() {
assert!(i < 100); //prevent from looping indefinitelly assert!(i < 100); //prevent from looping indefinitelly
local_inst local_inst
.main_admin(conn) .main_admin(&conn)
.unwrap() .unwrap()
.set_role(conn, Role::Normal) .set_role(&conn, Role::Normal)
.unwrap(); .unwrap();
i += 1; i += 1;
} }
inserted[0].set_role(conn, Role::Admin).unwrap(); inserted[0].set_role(&conn, Role::Admin).unwrap();
assert_eq!(inserted[0].id, local_inst.main_admin(conn).unwrap().id); assert_eq!(inserted[0].id, local_inst.main_admin(&conn).unwrap().id);
Ok(()) Ok(())
}); });
} }
@ -1257,9 +1256,9 @@ pub(crate) mod tests {
fn auth() { fn auth() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
fill_database(conn); fill_database(&conn);
let test_user = NewUser::new_local( let test_user = NewUser::new_local(
conn, &conn,
"test".to_owned(), "test".to_owned(),
"test user".to_owned(), "test user".to_owned(),
Role::Normal, Role::Normal,
@ -1270,10 +1269,10 @@ pub(crate) mod tests {
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
User::login(conn, "test", "test_password").unwrap().id, User::login(&conn, "test", "test_password").unwrap().id,
test_user.id test_user.id
); );
assert!(User::login(conn, "test", "other_password").is_err()); assert!(User::login(&conn, "test", "other_password").is_err());
Ok(()) Ok(())
}); });
} }
@ -1282,26 +1281,26 @@ pub(crate) mod tests {
fn get_local_page() { fn get_local_page() {
let conn = &db(); let conn = &db();
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
fill_database(conn); fill_database(&conn);
let page = User::get_local_page(conn, (0, 2)).unwrap(); let page = User::get_local_page(&conn, (0, 2)).unwrap();
assert_eq!(page.len(), 2); assert_eq!(page.len(), 2);
assert!(page[0].username <= page[1].username); assert!(page[0].username <= page[1].username);
let mut last_username = User::get_local_page(conn, (0, 1)).unwrap()[0] let mut last_username = User::get_local_page(&conn, (0, 1)).unwrap()[0]
.username .username
.clone(); .clone();
for i in 1..User::count_local(conn).unwrap() as i32 { for i in 1..User::count_local(&conn).unwrap() as i32 {
let page = User::get_local_page(conn, (i, i + 1)).unwrap(); let page = User::get_local_page(&conn, (i, i + 1)).unwrap();
assert_eq!(page.len(), 1); assert_eq!(page.len(), 1);
assert!(last_username <= page[0].username); assert!(last_username <= page[0].username);
last_username = page[0].username.clone(); last_username = page[0].username.clone();
} }
assert_eq!( assert_eq!(
User::get_local_page(conn, (0, User::count_local(conn).unwrap() as i32 + 10)) User::get_local_page(&conn, (0, User::count_local(&conn).unwrap() as i32 + 10))
.unwrap() .unwrap()
.len() as i64, .len() as i64,
User::count_local(conn).unwrap() User::count_local(&conn).unwrap()
); );
Ok(()) Ok(())
}); });
@ -1309,14 +1308,13 @@ pub(crate) mod tests {
#[test] #[test]
fn self_federation() { fn self_federation() {
let r = rockets(); let conn = db();
let conn = &*r.conn;
conn.test_transaction::<_, (), _>(|| { conn.test_transaction::<_, (), _>(|| {
let users = fill_database(conn); let users = fill_database(&conn);
let ap_repr = users[0].to_activity(conn).unwrap(); let ap_repr = users[0].to_activity(&conn).unwrap();
users[0].delete(conn).unwrap(); users[0].delete(&conn).unwrap();
let user = User::from_activity(&r, ap_repr).unwrap(); let user = User::from_activity(&conn, ap_repr).unwrap();
assert_eq!(user.username, users[0].username); assert_eq!(user.username, users[0].username);
assert_eq!(user.display_name, users[0].display_name); assert_eq!(user.display_name, users[0].display_name);
@ -1327,7 +1325,7 @@ pub(crate) mod tests {
assert_eq!(user.public_key, users[0].public_key); assert_eq!(user.public_key, users[0].public_key);
assert_eq!(user.shared_inbox_url, users[0].shared_inbox_url); assert_eq!(user.shared_inbox_url, users[0].shared_inbox_url);
assert_eq!(user.followers_endpoint, users[0].followers_endpoint); assert_eq!(user.followers_endpoint, users[0].followers_endpoint);
assert_eq!(user.avatar_url(conn), users[0].avatar_url(conn)); assert_eq!(user.avatar_url(&conn), users[0].avatar_url(&conn));
assert_eq!(user.fqn, users[0].fqn); assert_eq!(user.fqn, users[0].fqn);
assert_eq!(user.summary_html, users[0].summary_html); assert_eq!(user.summary_html, users[0].summary_html);
Ok(()) Ok(())