1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-25 19:01:09 +00:00

login for fork_article

This commit is contained in:
Felix Ableitner 2023-12-13 16:58:29 +01:00
parent b72cc51814
commit e0bd8d2791
4 changed files with 55 additions and 40 deletions

View file

@ -145,6 +145,7 @@ pub struct ForkArticleData {
/// how an article should be edited.
#[debug_handler]
pub(in crate::api) async fn fork_article(
Extension(_user): Extension<LocalUserView>,
data: Data<MyDataHandle>,
Form(fork_form): Form<ForkArticleData>,
) -> MyResult<Json<ArticleView>> {

View file

@ -54,7 +54,7 @@ pub async fn validate(jwt: &str, data: &Data<MyDataHandle>) -> MyResult<LocalUse
#[derive(Deserialize, Serialize)]
pub struct RegisterUserData {
pub name: String,
pub username: String,
pub password: String,
}
@ -68,14 +68,14 @@ pub(in crate::api) async fn register_user(
data: Data<MyDataHandle>,
Form(form): Form<RegisterUserData>,
) -> MyResult<Json<LoginResponse>> {
let user = DbPerson::create_local(form.name, form.password, &data)?;
let user = DbPerson::create_local(form.username, form.password, &data)?;
Ok(Json(generate_login_token(user.local_user, &data)?))
}
#[derive(Deserialize, Serialize)]
pub struct LoginUserData {
name: String,
password: String,
pub username: String,
pub password: String,
}
#[debug_handler]
@ -83,7 +83,7 @@ pub(in crate::api) async fn login_user(
data: Data<MyDataHandle>,
Form(form): Form<LoginUserData>,
) -> MyResult<Json<LoginResponse>> {
let user = DbPerson::read_local_from_name(&form.name, &data)?;
let user = DbPerson::read_local_from_name(&form.username, &data)?;
let valid = verify(&form.password, &user.local_user.password_encrypted)?;
if !valid {
return Err(anyhow!("Invalid login").into());

View file

@ -1,8 +1,8 @@
use anyhow::anyhow;
use fediwiki::api::article::{CreateArticleData, EditArticleData, GetArticleData};
use fediwiki::api::article::{CreateArticleData, EditArticleData, ForkArticleData, GetArticleData};
use fediwiki::api::instance::FollowInstance;
use fediwiki::api::user::LoginResponse;
use fediwiki::api::user::RegisterUserData;
use fediwiki::api::user::{LoginResponse, LoginUserData};
use fediwiki::api::ResolveObject;
use fediwiki::database::article::ArticleView;
use fediwiki::database::conflict::ApiConflict;
@ -119,16 +119,12 @@ impl FediwikiInstance {
let handle = tokio::task::spawn(async move {
start(&hostname_, &db_url).await.unwrap();
});
let register_form = RegisterUserData {
name: username.to_string(),
password: "hunter2".to_string(),
};
let register: LoginResponse = post(&hostname, "user/register", &register_form)
.await
.unwrap();
assert!(!register.jwt.is_empty());
// wait a moment for the server to start
tokio::time::sleep(Duration::from_millis(100)).await;
let register_res = register(&hostname, username, "hunter2").await.unwrap();
assert!(!register_res.jwt.is_empty());
Self {
jwt: register.jwt,
jwt: register_res.jwt,
hostname,
db_path,
db_handle: handle,
@ -214,13 +210,14 @@ where
handle_json_res(req).await
}
pub async fn post<T: Serialize, R>(hostname: &str, endpoint: &str, form: &T) -> MyResult<R>
where
R: for<'de> Deserialize<'de>,
{
pub async fn fork_article(
instance: &FediwikiInstance,
form: &ForkArticleData,
) -> MyResult<ArticleView> {
let req = CLIENT
.post(format!("http://{}/api/v1/{}", hostname, endpoint))
.form(form);
.post(format!("http://{}/api/v1/article/fork", instance.hostname))
.form(form)
.bearer_auth(&instance.jwt);
handle_json_res(req).await
}
@ -262,3 +259,29 @@ pub async fn follow_instance(api_instance: &str, follow_instance: &str) -> MyRes
Err(anyhow!("API error: {}", res.text().await?).into())
}
}
pub async fn register(hostname: &str, username: &str, password: &str) -> MyResult<LoginResponse> {
let register_form = RegisterUserData {
username: username.to_string(),
password: password.to_string(),
};
let req = CLIENT
.post(format!("http://{}/api/v1/user/register", hostname))
.form(&register_form);
handle_json_res(req).await
}
pub async fn login(
instance: &FediwikiInstance,
username: &str,
password: &str,
) -> MyResult<LoginResponse> {
let login_form = LoginUserData {
username: username.to_string(),
password: password.to_string(),
};
let req = CLIENT
.post(format!("http://{}/api/v1/user/login", instance.hostname))
.form(&login_form);
handle_json_res(req).await
}

View file

@ -2,14 +2,14 @@ extern crate fediwiki;
mod common;
use crate::common::handle_json_res;
use crate::common::register;
use crate::common::{
create_article, edit_article, edit_article_with_conflict, follow_instance, get_article,
get_query, post, TestData, CLIENT, TEST_ARTICLE_DEFAULT_TEXT,
get_query, TestData, CLIENT, TEST_ARTICLE_DEFAULT_TEXT,
};
use crate::common::{fork_article, handle_json_res, login};
use common::get;
use fediwiki::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
use fediwiki::api::user::{LoginResponse, RegisterUserData};
use fediwiki::api::{ResolveObject, SearchArticleData};
use fediwiki::database::article::{ArticleView, DbArticle};
use fediwiki::database::conflict::ApiConflict;
@ -441,7 +441,7 @@ async fn test_fork_article() -> MyResult<()> {
let fork_form = ForkArticleData {
article_id: resolved_article.id,
};
let fork_res: ArticleView = post(&data.beta.hostname, "article/fork", &fork_form).await?;
let fork_res = fork_article(&data.beta, &fork_form).await?;
let forked_article = fork_res.article;
assert_eq!(resolved_article.title, forked_article.title);
assert_eq!(resolved_article.text, forked_article.text);
@ -470,24 +470,15 @@ async fn test_fork_article() -> MyResult<()> {
#[tokio::test]
async fn test_user_registration_login() -> MyResult<()> {
let data = TestData::start().await;
let register_form = RegisterUserData {
name: "my_user".to_string(),
password: "hunter2".to_string(),
};
let register: LoginResponse =
post(&data.alpha.hostname, "user/register", &register_form).await?;
let username = "my_user";
let password = "hunter2";
let register = register(&data.alpha.hostname, username, password).await?;
assert!(!register.jwt.is_empty());
let mut login_form = RegisterUserData {
name: register_form.name.clone(),
password: "asd123".to_string(),
};
let invalid_login =
post::<_, LoginResponse>(&data.alpha.hostname, "user/login", &login_form).await;
let invalid_login = login(&data.alpha, username, "asd123").await;
assert!(invalid_login.is_err());
login_form.password = register_form.password;
let valid_login: LoginResponse = post(&data.alpha.hostname, "user/login", &login_form).await?;
let valid_login = login(&data.alpha, username, password).await?;
assert!(!valid_login.jwt.is_empty());
let title = "Manu_Chao".to_string();