1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-12-23 08:41:24 +00:00

Fix tests

This commit is contained in:
Felix Ableitner 2024-11-11 16:02:48 +01:00
parent 34068cffa1
commit d720a003e2
16 changed files with 50 additions and 144 deletions

View file

@ -28,6 +28,7 @@ hydrate = [
"leptos_router/hydrate", "leptos_router/hydrate",
"katex/wasm-js", "katex/wasm-js",
] ]
diesel-derive-newtype = ["dep:diesel-derive-newtype"]
# This profile significantly speeds up build time. If debug info is needed you can comment the line # This profile significantly speeds up build time. If debug info is needed you can comment the line
# out temporarily, but make sure to leave this in the main branch. # out temporarily, but make sure to leave this in the main branch.

View file

@ -2,7 +2,7 @@
# see https://diesel.rs/guides/configuring-diesel-cli # see https://diesel.rs/guides/configuring-diesel-cli
[print_schema] [print_schema]
file = "src/database/schema.rs" file = "src/backend/database/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId"] custom_type_derives = ["diesel::query_builder::QueryId"]
[migrations_directory] [migrations_directory]

View file

@ -0,0 +1,2 @@
ALTER TABLE conflict DROP CONSTRAINT conflict_creator_id_fkey;
ALTER TABLE conflict ADD CONSTRAINT conflict_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES local_user(id) ON UPDATE CASCADE ON DELETE CASCADE;

View file

@ -0,0 +1,2 @@
ALTER TABLE conflict DROP CONSTRAINT conflict_creator_id_fkey;
ALTER TABLE conflict ADD CONSTRAINT conflict_creator_id_fkey FOREIGN KEY (creator_id) REFERENCES person(id) ON UPDATE CASCADE ON DELETE CASCADE;

View file

@ -116,7 +116,12 @@ pub(in crate::backend::api) async fn edit_article(
edit_form.new_text.push('\n'); edit_form.new_text.push('\n');
} }
dbg!(
&edit_form.previous_version_id,
&original_article.latest_version
);
if edit_form.previous_version_id == original_article.latest_version { if edit_form.previous_version_id == original_article.latest_version {
dbg!("no conflict");
// No intermediate changes, simply submit new version // No intermediate changes, simply submit new version
submit_article_update( submit_article_update(
edit_form.new_text.clone(), edit_form.new_text.clone(),
@ -140,7 +145,7 @@ pub(in crate::backend::api) async fn edit_article(
hash: EditVersion::new(&patch.to_string()), hash: EditVersion::new(&patch.to_string()),
diff: patch.to_string(), diff: patch.to_string(),
summary: edit_form.summary.clone(), summary: edit_form.summary.clone(),
creator_id: user.local_user.id, creator_id: user.person.id,
article_id: original_article.article.id, article_id: original_article.article.id,
previous_version_id: previous_version.hash, previous_version_id: previous_version.hash,
}; };

View file

@ -92,7 +92,7 @@ async fn edit_conflicts(
Extension(user): Extension<LocalUserView>, Extension(user): Extension<LocalUserView>,
data: Data<IbisData>, data: Data<IbisData>,
) -> MyResult<Json<Vec<ApiConflict>>> { ) -> MyResult<Json<Vec<ApiConflict>>> {
let conflicts = DbConflict::list(&user.local_user, &data)?; let conflicts = DbConflict::list(&user.person, &data)?;
let conflicts: Vec<ApiConflict> = try_join_all(conflicts.into_iter().map(|c| { let conflicts: Vec<ApiConflict> = try_join_all(conflicts.into_iter().map(|c| {
let data = data.reset_request_count(); let data = data.reset_request_count();
async move { c.to_api_conflict(&data).await } async move { c.to_api_conflict(&data).await }

View file

@ -5,7 +5,7 @@ use crate::{
federation::activities::submit_article_update, federation::activities::submit_article_update,
utils::generate_article_version, utils::generate_article_version,
}, },
common::{ApiConflict, DbArticle, DbEdit, DbLocalUser, EditVersion}, common::{ApiConflict, DbArticle, DbEdit, DbPerson, EditVersion, PersonId},
}; };
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use diesel::{ use diesel::{
@ -32,7 +32,7 @@ pub struct DbConflict {
pub hash: EditVersion, pub hash: EditVersion,
pub diff: String, pub diff: String,
pub summary: String, pub summary: String,
pub creator_id: i32, pub creator_id: PersonId,
pub article_id: i32, pub article_id: i32,
pub previous_version_id: EditVersion, pub previous_version_id: EditVersion,
} }
@ -43,7 +43,7 @@ pub struct DbConflictForm {
pub hash: EditVersion, pub hash: EditVersion,
pub diff: String, pub diff: String,
pub summary: String, pub summary: String,
pub creator_id: i32, pub creator_id: PersonId,
pub article_id: i32, pub article_id: i32,
pub previous_version_id: EditVersion, pub previous_version_id: EditVersion,
} }
@ -56,10 +56,10 @@ impl DbConflict {
.get_result(conn.deref_mut())?) .get_result(conn.deref_mut())?)
} }
pub fn list(local_user: &DbLocalUser, data: &IbisData) -> MyResult<Vec<Self>> { pub fn list(person: &DbPerson, data: &IbisData) -> MyResult<Vec<Self>> {
let mut conn = data.db_pool.get()?; let mut conn = data.db_pool.get()?;
Ok(conflict::table Ok(conflict::table
.filter(conflict::dsl::creator_id.eq(local_user.id)) .filter(conflict::dsl::creator_id.eq(person.id))
.get_results(conn.deref_mut())?) .get_results(conn.deref_mut())?)
} }

View file

@ -4,7 +4,7 @@ use crate::{
error::MyResult, error::MyResult,
IbisData, IbisData,
}, },
common::{DbArticle, DbEdit, EditVersion, EditView}, common::{DbArticle, DbEdit, EditVersion, EditView, PersonId},
}; };
use activitypub_federation::fetch::object_id::ObjectId; use activitypub_federation::fetch::object_id::ObjectId;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
@ -15,7 +15,7 @@ use std::ops::DerefMut;
#[derive(Debug, Clone, Insertable, AsChangeset)] #[derive(Debug, Clone, Insertable, AsChangeset)]
#[diesel(table_name = edit, check_for_backend(diesel::pg::Pg))] #[diesel(table_name = edit, check_for_backend(diesel::pg::Pg))]
pub struct DbEditForm { pub struct DbEditForm {
pub creator_id: i32, pub creator_id: PersonId,
pub hash: EditVersion, pub hash: EditVersion,
pub ap_id: ObjectId<DbEdit>, pub ap_id: ObjectId<DbEdit>,
pub diff: String, pub diff: String,
@ -28,7 +28,7 @@ pub struct DbEditForm {
impl DbEditForm { impl DbEditForm {
pub fn new( pub fn new(
original_article: &DbArticle, original_article: &DbArticle,
creator_id: i32, creator_id: PersonId,
updated_text: &str, updated_text: &str,
summary: String, summary: String,
previous_version_id: EditVersion, previous_version_id: EditVersion,

View file

@ -102,7 +102,7 @@ diesel::table! {
diesel::joinable!(article -> instance (instance_id)); diesel::joinable!(article -> instance (instance_id));
diesel::joinable!(conflict -> article (article_id)); diesel::joinable!(conflict -> article (article_id));
diesel::joinable!(conflict -> local_user (creator_id)); diesel::joinable!(conflict -> person (creator_id));
diesel::joinable!(edit -> article (article_id)); diesel::joinable!(edit -> article (article_id));
diesel::joinable!(edit -> person (creator_id)); diesel::joinable!(edit -> person (creator_id));
diesel::joinable!(instance_follow -> instance (instance_id)); diesel::joinable!(instance_follow -> instance (instance_id));

View file

@ -6,7 +6,14 @@ use crate::{
}, },
error::MyResult, error::MyResult,
}, },
common::{utils::http_protocol_str, DbInstance, DbLocalUser, DbPerson, LocalUserView}, common::{
utils::http_protocol_str,
DbInstance,
DbLocalUser,
DbPerson,
LocalUserView,
PersonId,
},
}; };
use activitypub_federation::{ use activitypub_federation::{
config::Data, config::Data,
@ -31,7 +38,7 @@ use std::ops::DerefMut;
#[diesel(table_name = local_user, check_for_backend(diesel::pg::Pg))] #[diesel(table_name = local_user, check_for_backend(diesel::pg::Pg))]
pub struct DbLocalUserForm { pub struct DbLocalUserForm {
pub password_encrypted: String, pub password_encrypted: String,
pub person_id: i32, pub person_id: PersonId,
pub admin: bool, pub admin: bool,
} }
@ -58,7 +65,7 @@ impl DbPerson {
.get_result::<DbPerson>(conn.deref_mut())?) .get_result::<DbPerson>(conn.deref_mut())?)
} }
pub fn read(id: i32, data: &Data<IbisData>) -> MyResult<DbPerson> { pub fn read(id: PersonId, data: &Data<IbisData>) -> MyResult<DbPerson> {
let mut conn = data.db_pool.get()?; let mut conn = data.db_pool.get()?;
Ok(person::table.find(id).get_result(conn.deref_mut())?) Ok(person::table.find(id).get_result(conn.deref_mut())?)
} }
@ -155,7 +162,7 @@ impl DbPerson {
}) })
} }
fn read_following(id_: i32, data: &Data<IbisData>) -> MyResult<Vec<DbInstance>> { fn read_following(id_: PersonId, data: &Data<IbisData>) -> MyResult<Vec<DbInstance>> {
use instance_follow::dsl::{follower_id, instance_id}; use instance_follow::dsl::{follower_id, instance_id};
let mut conn = data.db_pool.get()?; let mut conn = data.db_pool.get()?;
Ok(instance_follow::table Ok(instance_follow::table

View file

@ -7,7 +7,7 @@ use crate::{
update_remote_article::UpdateRemoteArticle, update_remote_article::UpdateRemoteArticle,
}, },
}, },
common::{DbArticle, DbEdit, DbInstance, EditVersion}, common::{DbArticle, DbEdit, DbInstance, EditVersion, PersonId},
}; };
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use chrono::Utc; use chrono::Utc;
@ -24,7 +24,7 @@ pub async fn submit_article_update(
summary: String, summary: String,
previous_version: EditVersion, previous_version: EditVersion,
original_article: &DbArticle, original_article: &DbArticle,
creator_id: i32, creator_id: PersonId,
data: &Data<IbisData>, data: &Data<IbisData>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let form = DbEditForm::new( let form = DbEditForm::new(

View file

@ -46,7 +46,7 @@ pub fn generate_article_version(edits: &Vec<EditView>, version: &EditVersion) ->
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::common::{DbEdit, DbPerson}; use crate::common::{DbEdit, DbPerson, PersonId};
use activitypub_federation::fetch::object_id::ObjectId; use activitypub_federation::fetch::object_id::ObjectId;
use chrono::Utc; use chrono::Utc;
use diffy::create_patch; use diffy::create_patch;
@ -57,7 +57,7 @@ mod test {
Ok(EditView { Ok(EditView {
edit: DbEdit { edit: DbEdit {
id: 0, id: 0,
creator_id: 0, creator_id: PersonId(0),
hash: EditVersion::new(&diff), hash: EditVersion::new(&diff),
ap_id: ObjectId::parse("http://example.com")?, ap_id: ObjectId::parse("http://example.com")?,
diff, diff,
@ -67,7 +67,7 @@ mod test {
created: Utc::now(), created: Utc::now(),
}, },
creator: DbPerson { creator: DbPerson {
id: 0, id: PersonId(0),
username: "".to_string(), username: "".to_string(),
ap_id: ObjectId::parse("http://example.com")?, ap_id: ObjectId::parse("http://example.com")?,
inbox_url: "".to_string(), inbox_url: "".to_string(),

View file

@ -67,7 +67,7 @@ pub struct DbEdit {
// the same edit is used for multiple articles // the same edit is used for multiple articles
pub id: i32, pub id: i32,
#[serde(skip)] #[serde(skip)]
pub creator_id: i32, pub creator_id: PersonId,
/// UUID built from sha224 hash of diff /// UUID built from sha224 hash of diff
pub hash: EditVersion, pub hash: EditVersion,
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
@ -150,12 +150,17 @@ pub struct DbLocalUser {
pub admin: bool, pub admin: bool,
} }
#[derive(
Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize, DieselNewType,
)]
pub struct PersonId(pub i32);
/// Federation related data from a local or remote user. /// Federation related data from a local or remote user.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "ssr", derive(Queryable, Selectable, Identifiable))] #[cfg_attr(feature = "ssr", derive(Queryable, Selectable, Identifiable))]
#[cfg_attr(feature = "ssr", diesel(table_name = person, check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "ssr", diesel(table_name = person, check_for_backend(diesel::pg::Pg)))]
pub struct DbPerson { pub struct DbPerson {
pub id: i32, pub id: PersonId,
pub username: String, pub username: String,
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
pub ap_id: ObjectId<DbPerson>, pub ap_id: ObjectId<DbPerson>,

View file

@ -1,121 +0,0 @@
// @generated automatically by Diesel CLI.
diesel::table! {
article (id) {
id -> Int4,
title -> Text,
text -> Text,
#[max_length = 255]
ap_id -> Varchar,
instance_id -> Int4,
local -> Bool,
protected -> Bool,
}
}
diesel::table! {
conflict (id) {
id -> Int4,
hash -> Uuid,
diff -> Text,
summary -> Text,
creator_id -> Int4,
article_id -> Int4,
previous_version_id -> Uuid,
}
}
diesel::table! {
edit (id) {
id -> Int4,
creator_id -> Int4,
hash -> Uuid,
#[max_length = 255]
ap_id -> Varchar,
diff -> Text,
summary -> Text,
article_id -> Int4,
previous_version_id -> Uuid,
created -> Timestamptz,
}
}
diesel::table! {
instance (id) {
id -> Int4,
domain -> Text,
#[max_length = 255]
ap_id -> Varchar,
description -> Nullable<Text>,
#[max_length = 255]
articles_url -> Nullable<Varchar>,
#[max_length = 255]
inbox_url -> Varchar,
public_key -> Text,
private_key -> Nullable<Text>,
last_refreshed_at -> Timestamptz,
local -> Bool,
#[max_length = 255]
instances_url -> Nullable<Varchar>,
}
}
diesel::table! {
instance_follow (id) {
id -> Int4,
instance_id -> Int4,
follower_id -> Int4,
pending -> Bool,
}
}
diesel::table! {
jwt_secret (id) {
id -> Int4,
secret -> Varchar,
}
}
diesel::table! {
local_user (id) {
id -> Int4,
password_encrypted -> Text,
person_id -> Int4,
admin -> Bool,
}
}
diesel::table! {
person (id) {
id -> Int4,
username -> Text,
#[max_length = 255]
ap_id -> Varchar,
#[max_length = 255]
inbox_url -> Varchar,
public_key -> Text,
private_key -> Nullable<Text>,
last_refreshed_at -> Timestamptz,
local -> Bool,
}
}
diesel::joinable!(article -> instance (instance_id));
diesel::joinable!(conflict -> article (article_id));
diesel::joinable!(conflict -> local_user (creator_id));
diesel::joinable!(edit -> article (article_id));
diesel::joinable!(edit -> person (creator_id));
diesel::joinable!(instance_follow -> instance (instance_id));
diesel::joinable!(instance_follow -> person (follower_id));
diesel::joinable!(local_user -> person (person_id));
diesel::allow_tables_to_appear_in_same_query!(
article,
conflict,
edit,
instance,
instance_follow,
jwt_secret,
local_user,
person,
);

View file

@ -1,3 +1,6 @@
#[macro_use]
extern crate diesel_derive_newtype;
#[cfg(feature = "ssr")] #[cfg(feature = "ssr")]
pub mod backend; pub mod backend;
pub mod common; pub mod common;

View file

@ -358,6 +358,7 @@ async fn test_local_edit_conflict() -> MyResult<()> {
previous_version_id: create_res.latest_version.clone(), previous_version_id: create_res.latest_version.clone(),
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
dbg!("first edit");
let edit_res = data.alpha.edit_article(&edit_form).await?; let edit_res = data.alpha.edit_article(&edit_form).await?;
assert_eq!(edit_res.article.text, edit_form.new_text); assert_eq!(edit_res.article.text, edit_form.new_text);
assert_eq!(2, edit_res.edits.len()); assert_eq!(2, edit_res.edits.len());
@ -370,6 +371,7 @@ async fn test_local_edit_conflict() -> MyResult<()> {
previous_version_id: create_res.latest_version, previous_version_id: create_res.latest_version,
resolve_conflict_id: None, resolve_conflict_id: None,
}; };
dbg!("second edit");
let edit_res = data let edit_res = data
.alpha .alpha
.edit_article_with_conflict(&edit_form) .edit_article_with_conflict(&edit_form)