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

View File

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

View File

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

View File

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

View File

@ -294,12 +294,10 @@ pub fn ap_url(url: &str) -> String {
#[cfg(test)]
#[macro_use]
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 plume_common::utils::random_hex;
use scheduled_thread_pool::ScheduledThreadPool;
use std::env::temp_dir;
use std::sync::Arc;
#[macro_export]
macro_rules! part_eq {
@ -329,15 +327,6 @@ mod tests {
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;

View File

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

View File

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

View File

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