diff --git a/README.md b/README.md index e82d8cf..ee8bbfc 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# acapair_chat_api \ No newline at end of file +# Acapair Follow Ban API diff --git a/src/db.rs b/src/db.rs index 8921b3a..f4e655b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,431 +1,2 @@ -use serde::{Deserialize, Serialize}; -use surrealdb::{ - engine::remote::ws::{Client, Ws}, - opt::auth::Root, - sql::Id, - Surreal, -}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Channel { - id: Id, - pub username: String, - pub follower_list: Vec, - pub banned_list: Vec, - pub followed_list: Vec, - pub banned_from_list: Vec, -} - -pub async fn establish_connection() -> Surreal { - let db = Surreal::new::("127.0.0.1:8000").await.unwrap(); - - db.signin(Root { - username: "root", - password: "root", - }) - .await - .unwrap(); - - db.use_ns("test").use_db("test").await.unwrap(); - db -} - -pub async fn search_channel_by_username( - username: &String, - db: &Surreal, -) -> Option { - let searched: Option = db.select(("channel", username)).await.unwrap(); - searched -} -async fn search_channel_by_id(id: &Id, db: &Surreal) -> Option { - let searced: Option = db.select(("channel", id.clone())).await.unwrap(); - searced -} - -pub async fn create_channel(username: &String, db: &Surreal) -> Option> { - match search_channel_by_username(username, db).await { - Some(_) => { - eprintln!("Already Exists"); - return None; - } - None => { - let created: Vec = db - .create("channel") - .content(Channel { - id: Id::uuid(), - username: username.to_string(), - follower_list: vec![], - banned_list: vec![], - followed_list: vec![], - banned_from_list: vec![], - }) - .await - .unwrap(); - return Some(created); - } - } -} - -pub async fn delete_channel(username: &String, db: &Surreal) -> Option { - match search_channel_by_username(username, db).await { - Some(channel) => { - let deleted: Result, surrealdb::Error> = - db.delete(("channel", channel.username)).await; - match deleted { - Ok(channel) => match channel { - Some(channel) => { - remove_follower_artifacts(channel.clone(), db).await; - remove_banned_artifacts(channel, db).await - } - None => { - eprintln!("Error: Channel Not Exists"); - None - } - }, - Err(err_val) => { - eprintln!("Error: Delete | {}", err_val); - None - } - } - } - None => { - eprintln!("Not Exists"); - None - } - } -} -async fn update_channel(channel: Channel, db: &Surreal) -> Option { - match db - .update(("channel", channel.id.clone())) - .content(Channel { - id: channel.id, - username: channel.username, - follower_list: channel.follower_list, - banned_list: channel.banned_list, - followed_list: channel.followed_list, - banned_from_list: channel.banned_from_list, - }) - .await - { - Ok(option_channel) => match option_channel { - Some(channel) => channel, - None => { - eprintln!("Channel Does Not Exists"); - None - } - }, - Err(err_val) => { - eprintln!("Update Failed: {}", err_val); - None - } - } -} -async fn add_id_to_vector(id: Id, mut data: Vec) -> Option> { - data.sort(); - match data.binary_search(&id) { - Ok(_) => { - eprintln!("Error: Already Contains"); - None - } - Err(_) => { - data.push(id); - Some(data) - } - } -} -async fn remove_id_from_vector(id: Id, mut data: Vec) -> Option> { - data.sort(); - match data.binary_search(&id) { - Ok(_) => { - data.retain(|_id| *_id == id); - Some(data) - } - Err(_) => { - eprintln!("Error: Not Contains"); - None - } - } -} -async fn remove_follower_artifacts(mut channel: Channel, db: &Surreal) -> Option { - for id in channel.follower_list.clone() { - match search_channel_by_id(&id, db).await { - Some(follower_channel) => { - match remove_id_from_vector(id.clone(), follower_channel.followed_list).await { - Some(_) => {} - None => {} - } - } - None => { - eprintln!("Error: No Follower Channel by ID"); - } - } - channel.follower_list.retain(|_id| *_id == id); - } - Some(channel) -} -async fn remove_banned_artifacts(mut channel: Channel, db: &Surreal) -> Option { - for id in channel.banned_list.clone() { - match search_channel_by_id(&id, db).await { - Some(banned_channel) => { - match remove_id_from_vector(id.clone(), banned_channel.banned_from_list).await { - Some(_) => {} - None => {} - } - } - None => { - eprintln!("Error: No Banned Channel by ID"); - } - } - channel.banned_list.retain(|_id| *_id == id); - } - Some(channel) -} - -pub async fn update_channel_username( - updated_username: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => { - channel.username = updated_username.to_string(); - update_channel(channel, db).await - } - None => { - eprintln!("Error: Update Username"); - None - } - } -} -pub async fn add_follower( - follower: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(follower, db).await { - Some(follower) => match add_id_to_vector(follower.id, channel.follower_list).await { - Some(follower_list) => { - channel.follower_list = follower_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Add Follower Id"); - None - } - }, - None => { - eprintln!("Error: Follower Not Exists"); - None - } - }, - None => { - eprintln!("Error: Add Follower"); - None - } - } -} -pub async fn remove_follower( - follower: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(follower, db).await { - Some(follower) => { - match remove_id_from_vector(follower.id, channel.follower_list).await { - Some(follower_list) => { - channel.follower_list = follower_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Remove Follower Id"); - None - } - } - } - None => { - eprintln!("Error: Follower Not Exists"); - None - } - }, - None => { - eprintln!("Error: Remove Follower"); - None - } - } -} -pub async fn add_banned( - banned: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(banned, db).await { - Some(banned) => match add_id_to_vector(banned.id, channel.banned_list).await { - Some(banned_list) => { - channel.banned_list = banned_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Add Banned Id"); - None - } - }, - None => { - eprintln!("Error: Banned Not Exists"); - None - } - }, - None => { - eprintln!("Error: Add Banned"); - None - } - } -} -pub async fn remove_banned( - banned: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(banned, db).await { - Some(banned) => match remove_id_from_vector(banned.id, channel.banned_list).await { - Some(banned_list) => { - channel.banned_list = banned_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Remove Banned Id"); - None - } - }, - None => { - eprintln!("Error: Banned Not Exists"); - None - } - }, - None => { - eprintln!("Error: Remove Banned"); - None - } - } -} -pub async fn add_followed( - followed: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(followed, db).await { - Some(followed) => match add_id_to_vector(followed.id, channel.followed_list).await { - Some(followed_list) => { - channel.followed_list = followed_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Add Followed Id"); - None - } - }, - None => { - eprintln!("Error: Followed Not Exists"); - None - } - }, - None => { - eprintln!("Error: Add Followed"); - None - } - } -} -pub async fn remove_followed( - followed: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(followed, db).await { - Some(followed) => { - match remove_id_from_vector(followed.id, channel.followed_list).await { - Some(followed_list) => { - channel.followed_list = followed_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Remove Followed Id"); - None - } - } - } - None => { - eprintln!("Error: Followed Not Exists"); - None - } - }, - None => { - eprintln!("Error: Remove Followed"); - None - } - } -} -pub async fn add_banned_from( - banned_from: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(banned_from, db).await { - Some(banned_from) => { - match add_id_to_vector(banned_from.id, channel.banned_from_list).await { - Some(banned_from_list) => { - channel.banned_from_list = banned_from_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Add Banned from Id"); - None - } - } - } - None => { - eprintln!("Error: Followed Not Exists"); - None - } - }, - None => { - eprintln!("Error: Add Banned From"); - None - } - } -} -pub async fn remove_banned_from( - banned_from: &String, - username: &String, - db: &Surreal, -) -> Option { - match search_channel_by_username(username, db).await { - Some(mut channel) => match search_channel_by_username(banned_from, db).await { - Some(banned_from) => { - match remove_id_from_vector(banned_from.id, channel.banned_from_list).await { - Some(banned_from_list) => { - channel.banned_from_list = banned_from_list; - update_channel(channel, db).await - } - None => { - eprintln!("Error: Remove Banned from Id"); - None - } - } - } - None => { - eprintln!("Error: Banned Not Exists"); - None - } - }, - None => { - eprintln!("Error: Remove Banned From"); - None - } - } -} +pub mod db_operations; +mod db_utils; diff --git a/src/db/db_operations.rs b/src/db/db_operations.rs new file mode 100644 index 0000000..ea4c5bc --- /dev/null +++ b/src/db/db_operations.rs @@ -0,0 +1,102 @@ +use surrealdb::{engine::remote::ws::Client, Surreal}; + +use crate::Channel; + +use super::db_utils::*; + +pub async fn connect() -> Option> { + establish_connection().await +} + +pub async fn create(username: &String, db: &Surreal) -> Option { + create_channel(username, db).await.pop().unwrap() +} + +pub async fn search(username: &String, db: &Surreal) -> Option { + search_channel_by_username(username, db).await +} + +pub async fn delete(username: &String, db: &Surreal) -> Option { + // delete channel should be last for mind sake + // first artifacts + match search_channel_by_username(username, db).await { + Some(channel) => match remove_all_followers(channel.clone(), db).await { + Some(_) => match remove_all_followed(channel.clone(), db).await { + Some(_) => match remove_all_banned(channel.clone(), db).await { + Some(_) => match remove_all_banned_from(channel, db).await { + Some(_) => delete_channel(username, db).await, + None => None, + }, + None => None, + }, + None => None, + }, + None => None, + }, + None => { + eprintln!("Error: Delete | Channel Not Exists"); + None + } + } + // match delete_channel(username, db).await { + // Some(deleted_channel) => { + // match remove_follower_artifacts(deleted_channel.clone(), db).await { + // Some(_) => match remove_banned_artifacts(deleted_channel.clone(), db).await { + // Some(_) => Some(deleted_channel), + // None => None, + // }, + // None => None, + // } + // } + // None => None, + // } +} + +pub async fn change_username( + updated_username: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => { + channel.username = updated_username.to_string(); + update_channel(channel, db).await + } + None => { + eprintln!("Error: Update Username"); + None + } + } +} + +pub async fn follow(follower: &String, followed: &String, db: &Surreal) -> Option { + match add_follower(follower, followed, db).await { + Some(_) => add_followed(followed, follower, db).await, + None => None, + } +} + +pub async fn unfollow( + follower: &String, + followed: &String, + db: &Surreal, +) -> Option { + match remove_follower(follower, followed, db).await { + Some(_) => remove_followed(followed, follower, db).await, + None => None, + } +} + +pub async fn ban(victim: &String, judge: &String, db: &Surreal) -> Option { + match add_banned(victim, judge, db).await { + Some(_) => add_banned_from(judge, victim, db).await, + None => None, + } +} + +pub async fn unban(victim: &String, judge: &String, db: &Surreal) -> Option { + match remove_banned(victim, judge, db).await { + Some(_) => remove_banned_from(judge, victim, db).await, + None => None, + } +} diff --git a/src/db/db_utils.rs b/src/db/db_utils.rs new file mode 100644 index 0000000..c39fcd6 --- /dev/null +++ b/src/db/db_utils.rs @@ -0,0 +1,476 @@ +use crate::Channel; +use surrealdb::{ + engine::remote::ws::{Client, Ws}, + opt::auth::Root, + sql::Id, + Surreal, +}; + +use super::db_operations::{unban, unfollow}; +pub async fn establish_connection() -> Option> { + match Surreal::new::("127.0.0.1:8000").await { + Ok(db) => { + match db + .signin(Root { + username: "root", + password: "root", + }) + .await + { + Ok(_) => match db.use_ns("test").use_db("test").await { + Ok(_) => Some(db), + Err(err_val) => { + eprintln!("Error: DB Use | {}", err_val); + None + } + }, + Err(err_val) => { + eprintln!("Error: DB Login | {}", err_val); + None + } + } + } + Err(err_val) => { + eprintln!("Error: DB Connection | {}", err_val); + None + } + } +} + +async fn search_channel_by_id(id: &Id, db: &Surreal) -> Option { + let searced: Option = db.select(("channel", id.clone())).await.unwrap(); + searced +} +fn id_extractor(channel: &Channel) -> Id { + match channel.id.clone() { + Some(thing) => thing.id, + None => { + eprintln!("Error: Thing Not Exists"); + channel.id.clone().unwrap().id + } + } +} + +pub async fn search_channel_by_username( + username: &String, + db: &Surreal, +) -> Option { + let searched: Vec> = db.select("channel").await.unwrap(); + for element in searched { + match element { + Some(channel) => { + if channel.username == username.to_string() { + return Some(channel); + } + } + None => { + eprintln!("No Content"); + } + } + } + None +} + +pub async fn create_channel(username: &String, db: &Surreal) -> Vec> { + match search_channel_by_username(username, db).await { + Some(_) => { + eprintln!("Already Exists"); + return vec![]; + } + None => { + let created: Vec> = db + .create("channel") + .content(Channel { + id: None, + username: username.to_string(), + follower_list: vec![], + banned_list: vec![], + followed_list: vec![], + banned_from_list: vec![], + }) + .await + .unwrap(); + created + } + } +} + +pub async fn delete_channel(username: &String, db: &Surreal) -> Option { + match search_channel_by_username(username, db).await { + Some(channel) => db + .delete(("channel", id_extractor(&channel))) + .await + .unwrap(), + None => { + eprintln!("Not Exists"); + None + } + } +} + +pub async fn update_channel(channel: Channel, db: &Surreal) -> Option { + match db + .update(("channel", channel.clone().id.unwrap())) + .content(Channel { + id: channel.id, + username: channel.username, + follower_list: channel.follower_list, + banned_list: channel.banned_list, + followed_list: channel.followed_list, + banned_from_list: channel.banned_from_list, + }) + .await + { + Ok(option_channel) => match option_channel { + Some(channel) => channel, + None => { + eprintln!("Channel Does Not Exists"); + None + } + }, + Err(err_val) => { + eprintln!("Update Failed: {}", err_val); + None + } + } +} +fn add_id_to_vector(id: Id, mut data: Vec) -> Option> { + data.sort(); + match data.binary_search(&id) { + Ok(_) => { + eprintln!("Error: Already Contains"); + None + } + Err(_) => { + data.push(id); + Some(data) + } + } +} +fn remove_id_from_vector(id: Id, mut data: Vec) -> Option> { + data.sort(); + match data.binary_search(&id) { + Ok(_) => { + data.retain(|_id| *_id != id); + Some(data) + } + Err(_) => { + eprintln!("Error: Not Contains"); + None + } + } +} +pub async fn add_follower( + follower: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(follower, db).await { + Some(follower) => { + match add_id_to_vector(id_extractor(&follower), channel.follower_list) { + Some(follower_list) => { + channel.follower_list = follower_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Add Follower Id"); + None + } + } + } + None => { + eprintln!("Error: Follower Not Exists"); + None + } + }, + None => { + eprintln!("Error: Add Follower"); + None + } + } +} +pub async fn remove_follower( + follower: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(follower, db).await { + Some(follower) => { + match remove_id_from_vector(id_extractor(&follower), channel.follower_list) { + Some(follower_list) => { + channel.follower_list = follower_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Remove Follower Id"); + None + } + } + } + None => { + eprintln!("Error: Follower Not Exists"); + None + } + }, + None => { + eprintln!("Error: Remove Follower"); + None + } + } +} +pub async fn add_banned( + banned: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(banned, db).await { + Some(banned) => match add_id_to_vector(id_extractor(&banned), channel.banned_list) { + Some(banned_list) => { + channel.banned_list = banned_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Add Banned Id"); + None + } + }, + None => { + eprintln!("Error: Banned Not Exists"); + None + } + }, + None => { + eprintln!("Error: Add Banned"); + None + } + } +} +pub async fn remove_banned( + banned: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(banned, db).await { + Some(banned) => { + match remove_id_from_vector(id_extractor(&banned), channel.banned_list) { + Some(banned_list) => { + channel.banned_list = banned_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Remove Banned Id"); + None + } + } + } + None => { + eprintln!("Error: Banned Not Exists"); + None + } + }, + None => { + eprintln!("Error: Remove Banned"); + None + } + } +} +pub async fn add_followed( + followed: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(followed, db).await { + Some(followed) => { + match add_id_to_vector(id_extractor(&followed), channel.followed_list) { + Some(followed_list) => { + channel.followed_list = followed_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Add Followed Id"); + None + } + } + } + None => { + eprintln!("Error: Followed Not Exists"); + None + } + }, + None => { + eprintln!("Error: Add Followed"); + None + } + } +} +pub async fn remove_followed( + followed: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(followed, db).await { + Some(followed) => { + match remove_id_from_vector(id_extractor(&followed), channel.followed_list) { + Some(followed_list) => { + channel.followed_list = followed_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Remove Followed Id"); + None + } + } + } + None => { + eprintln!("Error: Followed Not Exists"); + None + } + }, + None => { + eprintln!("Error: Remove Followed"); + None + } + } +} +pub async fn add_banned_from( + banned_from: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(banned_from, db).await { + Some(banned_from) => { + match add_id_to_vector(id_extractor(&banned_from), channel.banned_from_list) { + Some(banned_from_list) => { + channel.banned_from_list = banned_from_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Add Banned from Id"); + None + } + } + } + None => { + eprintln!("Error: Followed Not Exists"); + None + } + }, + None => { + eprintln!("Error: Add Banned From"); + None + } + } +} +pub async fn remove_banned_from( + banned_from: &String, + username: &String, + db: &Surreal, +) -> Option { + match search_channel_by_username(username, db).await { + Some(mut channel) => match search_channel_by_username(banned_from, db).await { + Some(banned_from) => { + match remove_id_from_vector(id_extractor(&banned_from), channel.banned_from_list) { + Some(banned_from_list) => { + channel.banned_from_list = banned_from_list; + update_channel(channel, db).await + } + None => { + eprintln!("Error: Remove Banned from Id"); + None + } + } + } + None => { + eprintln!("Error: Banned Not Exists"); + None + } + }, + None => { + eprintln!("Error: Remove Banned From"); + None + } + } +} + +pub async fn remove_all_followers(channel: Channel, db: &Surreal) -> Option { + for id in channel.follower_list { + match search_channel_by_id(&id, db).await { + Some(follower_channel) => { + match unfollow(&follower_channel.username, &channel.username, db).await { + Some(_) => {} + None => { + eprintln!("Error: Can't Remove Follower"); + } + } + } + None => { + eprintln!("Error: Can't Remove Follower, Follower Not Exists"); + } + } + } + search_channel_by_username(&channel.username, db).await +} + +pub async fn remove_all_followed(channel: Channel, db: &Surreal) -> Option { + for id in channel.followed_list { + match search_channel_by_id(&id, db).await { + Some(followed_channel) => { + match unfollow(&channel.username, &followed_channel.username, db).await { + Some(_) => {} + None => { + eprintln!("Error: Can't Remove Followed"); + } + } + } + None => { + eprintln!("Error: Can't Remove Followed, Followed Not Exists"); + } + } + } + search_channel_by_username(&channel.username, db).await +} + +pub async fn remove_all_banned(channel: Channel, db: &Surreal) -> Option { + for id in channel.banned_list { + match search_channel_by_id(&id, db).await { + Some(banned_channel) => { + match unban(&banned_channel.username, &channel.username, db).await { + Some(_) => {} + None => { + eprintln!("Error: Can't Remove Banned"); + } + } + } + None => { + eprintln!("Error: Can't Remove Banned, Banned Not Exists"); + } + } + } + search_channel_by_username(&channel.username, db).await +} + +pub async fn remove_all_banned_from(channel: Channel, db: &Surreal) -> Option { + for id in channel.banned_from_list { + match search_channel_by_id(&id, db).await { + Some(banned_from_channel) => { + match unban(&channel.username, &banned_from_channel.username, db).await { + Some(_) => {} + None => { + eprintln!("Error: Can't Remove Banned From"); + } + } + } + None => { + eprintln!("Error: Can't Remove Banned From, Banned From Not Exists"); + } + } + } + search_channel_by_username(&channel.username, db).await +} diff --git a/src/lib.rs b/src/lib.rs index 466aa79..9e67b58 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,19 @@ +use serde::{Deserialize, Serialize}; +use surrealdb::sql::{Id, Thing}; + pub mod db; pub mod routing; +pub mod tests; #[derive(Debug, Clone)] pub struct AppState {} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Channel { + pub id: Option, + pub username: String, + pub follower_list: Vec, + pub banned_list: Vec, + pub followed_list: Vec, + pub banned_from_list: Vec, +} diff --git a/src/main.rs b/src/main.rs index be22ea9..d8f012e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ -use std::{env, net::SocketAddr}; - -use acapair_chat_api::{routing, AppState}; +use acapair_follow_ban_api::{routing, AppState}; use axum_server::tls_rustls::RustlsConfig; +use std::{env, net::SocketAddr}; fn take_args() -> String { let mut bind_address: String = String::new(); @@ -15,7 +14,6 @@ fn take_args() -> String { #[tokio::main] async fn main() { println!("Hello, world!"); - let config = RustlsConfig::from_pem_file("certificates/fullchain.pem", "certificates/privkey.pem") .await diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..6b9b9f2 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1 @@ +pub mod db_tests; diff --git a/src/tests/db_tests.rs b/src/tests/db_tests.rs new file mode 100644 index 0000000..6817220 --- /dev/null +++ b/src/tests/db_tests.rs @@ -0,0 +1,297 @@ +#[cfg(test)] +use crate::db::db_operations::*; +use tokio::test; + +#[cfg(test)] +async fn create_connection_for_tests( + db_name: &str, +) -> surrealdb::Surreal { + let connection = surrealdb::Surreal::new::("127.0.0.1:8000") + .await + .unwrap(); + connection + .signin(surrealdb::opt::auth::Root { + username: "root", + password: "root", + }) + .await + .unwrap(); + connection.use_ns("test").use_db(db_name).await.unwrap(); + connection +} + +#[test] +async fn test_connect() { + assert_eq!(connect().await.is_some(), true); +} +#[test] +async fn test_create() { + let connection = create_connection_for_tests("test_create").await; + + let name = &"Ahmet".to_string(); + let created = create(name, &connection).await; + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(created.is_some(), true); +} + +#[test] +async fn test_search() { + let connection = create_connection_for_tests("test_search").await; + let name = &"Ahmet".to_string(); + + let created = create(name, &connection).await; + let searched = search(name, &connection).await; + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(created, searched); +} + +#[test] +async fn test_delete() { + let connection = create_connection_for_tests("test_delete").await; + let name = &"Ahmet".to_string(); + + let created = create(name, &connection).await; + let deleted = delete(name, &connection).await; + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(created, deleted); +} + +#[test] +async fn test_change_username() { + let connection = create_connection_for_tests("test_change_username").await; + let name = &"Ahmet".to_string(); + + let created = create(name, &connection).await.unwrap(); + let changed = change_username(&"Kaan".to_string(), name, &connection) + .await + .unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(created.id, changed.clone().id); + assert_eq!(changed.username, "Kaan"); +} + +#[test] +async fn test_follow() { + let connection = create_connection_for_tests("test_follow").await; + let name_follower = &"Ahmet".to_string(); + let name_followed = &"Kaan".to_string(); + + let _follower = create(name_follower, &connection).await.unwrap(); + let _followed = create(name_followed, &connection).await.unwrap(); + + let mut follower = follow(name_follower, name_followed, &connection) + .await + .unwrap(); + let mut followed = search(name_followed, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!( + followed.follower_list.pop().unwrap(), + follower.id.unwrap().id + ); + assert_eq!( + follower.followed_list.pop().unwrap(), + followed.id.unwrap().id + ); +} + +#[test] +async fn test_unfollow() { + let connection = create_connection_for_tests("test_unfollow").await; + let name_follower = &"Ahmet".to_string(); + let name_followed = &"Kaan".to_string(); + + let _follower = create(name_follower, &connection).await.unwrap(); + let _followed = create(name_followed, &connection).await.unwrap(); + + let mut follower = follow(name_follower, name_followed, &connection) + .await + .unwrap(); + let mut followed = search(name_followed, &connection).await.unwrap(); + + assert_eq!( + followed.follower_list.pop().unwrap(), + follower.id.unwrap().id + ); + assert_eq!( + follower.followed_list.pop().unwrap(), + followed.id.unwrap().id + ); + + follower = unfollow(name_follower, name_followed, &connection) + .await + .unwrap(); + followed = search(name_followed, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(followed.follower_list.pop().is_none(), true); + assert_eq!(follower.followed_list.pop().is_none(), true); +} + +#[test] +async fn test_ban() { + let connection = create_connection_for_tests("test_ban").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); + + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); + + let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); + let mut judge = search(name_judge, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); + assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); +} + +#[test] +async fn test_unban() { + let connection = create_connection_for_tests("test_unban").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); + + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); + + let mut victim = ban(name_victim, name_judge, &connection).await.unwrap(); + let mut judge = search(name_judge, &connection).await.unwrap(); + + assert_eq!(victim.banned_from_list.pop().unwrap(), judge.id.unwrap().id); + assert_eq!(judge.banned_list.pop().unwrap(), victim.id.unwrap().id); + + victim = unban(name_victim, name_judge, &connection).await.unwrap(); + judge = search(name_judge, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(victim.banned_from_list.pop().is_none(), true); + assert_eq!(judge.banned_list.pop().is_none(), true); +} + +#[test] +async fn test_delete_follower() { + let connection = create_connection_for_tests("test_delete_follower").await; + let name_follower = &"Ahmet".to_string(); + let name_followed = &"Kaan".to_string(); + + let _follower = create(name_follower, &connection).await.unwrap(); + let _followed = create(name_followed, &connection).await.unwrap(); + + let mut follower = follow(name_follower, name_followed, &connection) + .await + .unwrap(); + let mut followed = search(name_followed, &connection).await.unwrap(); + + assert_eq!( + followed.follower_list.pop().unwrap(), + follower.id.unwrap().id + ); + assert_eq!( + follower.followed_list.pop().unwrap(), + followed.id.unwrap().id + ); + + follower = delete(name_follower, &connection).await.unwrap(); + followed = search(name_followed, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(followed.follower_list.pop().is_none(), true); + assert_eq!(follower.followed_list.pop().is_none(), true); +} + +#[test] +async fn test_delete_followed() { + let connection = create_connection_for_tests("test_delete_followed").await; + let name_follower = &"Ahmet".to_string(); + let name_followed = &"Kaan".to_string(); + + let _follower = create(name_follower, &connection).await.unwrap(); + let _followed = create(name_followed, &connection).await.unwrap(); + + let mut follower = follow(name_follower, name_followed, &connection) + .await + .unwrap(); + let mut followed = search(name_followed, &connection).await.unwrap(); + + assert_eq!( + followed.follower_list.pop().unwrap(), + follower.id.unwrap().id + ); + assert_eq!( + follower.followed_list.pop().unwrap(), + followed.id.unwrap().id + ); + + followed = delete(name_followed, &connection).await.unwrap(); + follower = search(name_follower, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(followed.follower_list.pop().is_none(), true); + assert_eq!(follower.followed_list.pop().is_none(), true); +} + +#[test] +async fn test_delete_victim() { + let connection = create_connection_for_tests("test_delete_victim").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); + + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); + + let mut victim = ban(name_victim, name_judge, &connection) + .await + .unwrap(); + let mut judge = search(name_judge, &connection).await.unwrap(); + + assert_eq!( + judge.banned_list.pop().unwrap(), + victim.id.unwrap().id + ); + assert_eq!( + victim.banned_from_list.pop().unwrap(), + judge.id.unwrap().id + ); + + victim = delete(name_victim, &connection).await.unwrap(); + judge = search(name_judge, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(judge.banned_list.pop().is_none(), true); + assert_eq!(victim.banned_from_list.pop().is_none(), true); +} + +#[test] +async fn test_delete_judge() { + let connection = create_connection_for_tests("test_delete_judge").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); + + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); + + let mut victim = ban(name_victim, name_judge, &connection) + .await + .unwrap(); + let mut judge = search(name_judge, &connection).await.unwrap(); + + assert_eq!( + judge.banned_list.pop().unwrap(), + victim.id.unwrap().id + ); + assert_eq!( + victim.banned_from_list.pop().unwrap(), + judge.id.unwrap().id + ); + + judge = delete(name_judge, &connection).await.unwrap(); + victim = search(name_victim, &connection).await.unwrap(); + + let _cleaning = connection.query("DELETE channel;").await; + assert_eq!(judge.banned_list.pop().is_none(), true); + assert_eq!(victim.banned_from_list.pop().is_none(), true); +}