feat: add_user and user_exists with accompanying test suites

DESCRIPTION
    add_user
	Adds new user to DB. Implementers must ensure username
	uniqueness scoped at forge instance level. When applicable, HTML
	link to profile photo may be stored as we..
    user_exists
	Checks if a user exists in database. Optionally provide hostname
	of forge instance to get forge-specific results.
This commit is contained in:
Aravinth Manivannan 2022-04-20 16:23:38 +05:30
parent b8be794c19
commit 5102531a70
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 31 additions and 2 deletions

View file

@ -40,6 +40,7 @@ pub mod ops;
#[cfg(feature = "test")]
pub mod tests;
use dev::*;
pub use ops::GetConnection;
pub mod prelude {
@ -65,7 +66,20 @@ pub struct CreateForge<'a> {
pub forge_type: ForgeImplementation,
}
use dev::*;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// add new user to database
pub struct AddUser<'a> {
/// hostname of the forge instance: with scheme but remove trailing slash
/// hostname can be derived from html_link also, but used to link to user's forge instance
pub hostname: &'a str,
/// username of the user
pub username: &'a str,
/// html link to the user profile
pub html_link: &'a str,
/// OPTIONAL: html link to the user's profile photo
pub profile_photo: Option<&'a str>,
}
#[async_trait]
/// Starchart's database requirements. To implement support for $Database, kindly implement this
/// trait.
@ -84,6 +98,13 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
/// check if forge type exists
async fn forge_type_exists(&self, forge_type: &ForgeImplementation) -> DBResult<bool>;
/// add new user to database
async fn add_user(&self, u: &AddUser) -> DBResult<()>;
/// check if an user exists. When hostname of a forge instace is provided, username search is
/// done only on that forge
async fn user_exists(&self, username: &str, hostname: Option<&str>) -> DBResult<bool>;
}
/// Trait to clone SCDatabase

View file

@ -19,13 +19,21 @@
use crate::prelude::*;
/// adding forge works
pub async fn adding_forge_works<T: SCDatabase>(db: &T, create_forge_msg: CreateForge<'static>) {
pub async fn adding_forge_works<T: SCDatabase>(
db: &T,
create_forge_msg: CreateForge<'static>,
add_user_msg: AddUser<'static>,
add_user_msg2: AddUser<'static>,
) {
let _ = db.delete_forge_instance(&create_forge_msg.hostname).await;
db.create_forge_isntance(&create_forge_msg).await.unwrap();
assert!(
db.forge_exists(create_forge_msg.hostname).await.unwrap(),
"forge creation failed, forge existance check failure"
);
db.add_user(&add_user_msg).await.unwrap();
db.add_user(&add_user_msg2).await.unwrap();
}
/// test if all forge type implementations are loaded into DB