mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-22 13:01:08 +00:00
login for fork_article
This commit is contained in:
parent
b72cc51814
commit
e0bd8d2791
4 changed files with 55 additions and 40 deletions
|
@ -145,6 +145,7 @@ pub struct ForkArticleData {
|
||||||
/// how an article should be edited.
|
/// how an article should be edited.
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub(in crate::api) async fn fork_article(
|
pub(in crate::api) async fn fork_article(
|
||||||
|
Extension(_user): Extension<LocalUserView>,
|
||||||
data: Data<MyDataHandle>,
|
data: Data<MyDataHandle>,
|
||||||
Form(fork_form): Form<ForkArticleData>,
|
Form(fork_form): Form<ForkArticleData>,
|
||||||
) -> MyResult<Json<ArticleView>> {
|
) -> MyResult<Json<ArticleView>> {
|
||||||
|
|
|
@ -54,7 +54,7 @@ pub async fn validate(jwt: &str, data: &Data<MyDataHandle>) -> MyResult<LocalUse
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct RegisterUserData {
|
pub struct RegisterUserData {
|
||||||
pub name: String,
|
pub username: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,14 +68,14 @@ pub(in crate::api) async fn register_user(
|
||||||
data: Data<MyDataHandle>,
|
data: Data<MyDataHandle>,
|
||||||
Form(form): Form<RegisterUserData>,
|
Form(form): Form<RegisterUserData>,
|
||||||
) -> MyResult<Json<LoginResponse>> {
|
) -> 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)?))
|
Ok(Json(generate_login_token(user.local_user, &data)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct LoginUserData {
|
pub struct LoginUserData {
|
||||||
name: String,
|
pub username: String,
|
||||||
password: String,
|
pub password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
|
@ -83,7 +83,7 @@ pub(in crate::api) async fn login_user(
|
||||||
data: Data<MyDataHandle>,
|
data: Data<MyDataHandle>,
|
||||||
Form(form): Form<LoginUserData>,
|
Form(form): Form<LoginUserData>,
|
||||||
) -> MyResult<Json<LoginResponse>> {
|
) -> 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)?;
|
let valid = verify(&form.password, &user.local_user.password_encrypted)?;
|
||||||
if !valid {
|
if !valid {
|
||||||
return Err(anyhow!("Invalid login").into());
|
return Err(anyhow!("Invalid login").into());
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use anyhow::anyhow;
|
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::instance::FollowInstance;
|
||||||
use fediwiki::api::user::LoginResponse;
|
|
||||||
use fediwiki::api::user::RegisterUserData;
|
use fediwiki::api::user::RegisterUserData;
|
||||||
|
use fediwiki::api::user::{LoginResponse, LoginUserData};
|
||||||
use fediwiki::api::ResolveObject;
|
use fediwiki::api::ResolveObject;
|
||||||
use fediwiki::database::article::ArticleView;
|
use fediwiki::database::article::ArticleView;
|
||||||
use fediwiki::database::conflict::ApiConflict;
|
use fediwiki::database::conflict::ApiConflict;
|
||||||
|
@ -119,16 +119,12 @@ impl FediwikiInstance {
|
||||||
let handle = tokio::task::spawn(async move {
|
let handle = tokio::task::spawn(async move {
|
||||||
start(&hostname_, &db_url).await.unwrap();
|
start(&hostname_, &db_url).await.unwrap();
|
||||||
});
|
});
|
||||||
let register_form = RegisterUserData {
|
// wait a moment for the server to start
|
||||||
name: username.to_string(),
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
password: "hunter2".to_string(),
|
let register_res = register(&hostname, username, "hunter2").await.unwrap();
|
||||||
};
|
assert!(!register_res.jwt.is_empty());
|
||||||
let register: LoginResponse = post(&hostname, "user/register", ®ister_form)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert!(!register.jwt.is_empty());
|
|
||||||
Self {
|
Self {
|
||||||
jwt: register.jwt,
|
jwt: register_res.jwt,
|
||||||
hostname,
|
hostname,
|
||||||
db_path,
|
db_path,
|
||||||
db_handle: handle,
|
db_handle: handle,
|
||||||
|
@ -214,13 +210,14 @@ where
|
||||||
handle_json_res(req).await
|
handle_json_res(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn post<T: Serialize, R>(hostname: &str, endpoint: &str, form: &T) -> MyResult<R>
|
pub async fn fork_article(
|
||||||
where
|
instance: &FediwikiInstance,
|
||||||
R: for<'de> Deserialize<'de>,
|
form: &ForkArticleData,
|
||||||
{
|
) -> MyResult<ArticleView> {
|
||||||
let req = CLIENT
|
let req = CLIENT
|
||||||
.post(format!("http://{}/api/v1/{}", hostname, endpoint))
|
.post(format!("http://{}/api/v1/article/fork", instance.hostname))
|
||||||
.form(form);
|
.form(form)
|
||||||
|
.bearer_auth(&instance.jwt);
|
||||||
handle_json_res(req).await
|
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())
|
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(®ister_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
|
||||||
|
}
|
||||||
|
|
|
@ -2,14 +2,14 @@ extern crate fediwiki;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
use crate::common::handle_json_res;
|
use crate::common::register;
|
||||||
use crate::common::{
|
use crate::common::{
|
||||||
create_article, edit_article, edit_article_with_conflict, follow_instance, get_article,
|
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 common::get;
|
||||||
use fediwiki::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
|
use fediwiki::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
|
||||||
use fediwiki::api::user::{LoginResponse, RegisterUserData};
|
|
||||||
use fediwiki::api::{ResolveObject, SearchArticleData};
|
use fediwiki::api::{ResolveObject, SearchArticleData};
|
||||||
use fediwiki::database::article::{ArticleView, DbArticle};
|
use fediwiki::database::article::{ArticleView, DbArticle};
|
||||||
use fediwiki::database::conflict::ApiConflict;
|
use fediwiki::database::conflict::ApiConflict;
|
||||||
|
@ -441,7 +441,7 @@ async fn test_fork_article() -> MyResult<()> {
|
||||||
let fork_form = ForkArticleData {
|
let fork_form = ForkArticleData {
|
||||||
article_id: resolved_article.id,
|
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;
|
let forked_article = fork_res.article;
|
||||||
assert_eq!(resolved_article.title, forked_article.title);
|
assert_eq!(resolved_article.title, forked_article.title);
|
||||||
assert_eq!(resolved_article.text, forked_article.text);
|
assert_eq!(resolved_article.text, forked_article.text);
|
||||||
|
@ -470,24 +470,15 @@ async fn test_fork_article() -> MyResult<()> {
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_user_registration_login() -> MyResult<()> {
|
async fn test_user_registration_login() -> MyResult<()> {
|
||||||
let data = TestData::start().await;
|
let data = TestData::start().await;
|
||||||
let register_form = RegisterUserData {
|
let username = "my_user";
|
||||||
name: "my_user".to_string(),
|
let password = "hunter2";
|
||||||
password: "hunter2".to_string(),
|
let register = register(&data.alpha.hostname, username, password).await?;
|
||||||
};
|
|
||||||
let register: LoginResponse =
|
|
||||||
post(&data.alpha.hostname, "user/register", ®ister_form).await?;
|
|
||||||
assert!(!register.jwt.is_empty());
|
assert!(!register.jwt.is_empty());
|
||||||
|
|
||||||
let mut login_form = RegisterUserData {
|
let invalid_login = login(&data.alpha, username, "asd123").await;
|
||||||
name: register_form.name.clone(),
|
|
||||||
password: "asd123".to_string(),
|
|
||||||
};
|
|
||||||
let invalid_login =
|
|
||||||
post::<_, LoginResponse>(&data.alpha.hostname, "user/login", &login_form).await;
|
|
||||||
assert!(invalid_login.is_err());
|
assert!(invalid_login.is_err());
|
||||||
|
|
||||||
login_form.password = register_form.password;
|
let valid_login = login(&data.alpha, username, password).await?;
|
||||||
let valid_login: LoginResponse = post(&data.alpha.hostname, "user/login", &login_form).await?;
|
|
||||||
assert!(!valid_login.jwt.is_empty());
|
assert!(!valid_login.jwt.is_empty());
|
||||||
|
|
||||||
let title = "Manu_Chao".to_string();
|
let title = "Manu_Chao".to_string();
|
||||||
|
|
Loading…
Reference in a new issue