2021-10-16 13:33:38 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::LocalUserId,
|
2024-05-21 18:46:49 +00:00
|
|
|
schema::password_reset_request::dsl::{password_reset_request, published, token},
|
2022-11-19 04:33:54 +00:00
|
|
|
source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2021-03-18 20:25:21 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{
|
2024-05-21 18:46:49 +00:00
|
|
|
delete,
|
2022-11-19 04:33:54 +00:00
|
|
|
dsl::{insert_into, now, IntervalDsl},
|
|
|
|
result::Error,
|
2023-08-24 15:27:00 +00:00
|
|
|
sql_types::Timestamptz,
|
2022-11-19 04:33:54 +00:00
|
|
|
ExpressionMethods,
|
2023-08-24 15:27:00 +00:00
|
|
|
IntoSql,
|
2022-11-19 04:33:54 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2021-10-16 13:33:38 +00:00
|
|
|
impl PasswordResetRequest {
|
2024-05-21 18:46:49 +00:00
|
|
|
pub async fn create(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-03-18 20:25:21 +00:00
|
|
|
from_local_user_id: LocalUserId,
|
2023-08-02 17:02:53 +00:00
|
|
|
token_: String,
|
2020-12-21 13:38:34 +00:00
|
|
|
) -> Result<PasswordResetRequest, Error> {
|
2019-10-30 03:35:39 +00:00
|
|
|
let form = PasswordResetRequestForm {
|
2021-02-26 13:49:58 +00:00
|
|
|
local_user_id: from_local_user_id,
|
2024-05-17 00:41:57 +00:00
|
|
|
token: token_.into(),
|
2019-10-30 03:35:39 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2024-05-21 18:46:49 +00:00
|
|
|
insert_into(password_reset_request)
|
|
|
|
.values(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
2023-06-27 09:20:53 +00:00
|
|
|
|
2024-09-23 15:26:50 +00:00
|
|
|
pub async fn read_and_delete(pool: &mut DbPool<'_>, token_: &str) -> Result<Self, Error> {
|
2023-06-27 09:20:53 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2024-05-21 18:46:49 +00:00
|
|
|
delete(password_reset_request)
|
|
|
|
.filter(token.eq(token_))
|
2023-08-24 15:27:00 +00:00
|
|
|
.filter(published.gt(now.into_sql::<Timestamptz>() - 1.days()))
|
2023-06-27 09:20:53 +00:00
|
|
|
.get_result(conn)
|
|
|
|
.await
|
|
|
|
}
|
2020-12-21 13:38:34 +00:00
|
|
|
}
|
2019-12-22 16:08:03 +00:00
|
|
|
|
2019-10-30 03:35:39 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2020-12-21 16:30:34 +00:00
|
|
|
use crate::{
|
2021-10-16 13:33:38 +00:00
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
instance::Instance,
|
|
|
|
local_user::{LocalUser, LocalUserInsertForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
password_reset_request::PasswordResetRequest,
|
2022-11-19 04:33:54 +00:00
|
|
|
person::{Person, PersonInsertForm},
|
2021-10-16 13:33:38 +00:00
|
|
|
},
|
|
|
|
traits::Crud,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2021-03-11 16:41:04 +00:00
|
|
|
};
|
2024-05-21 18:46:49 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-02-25 19:43:39 +00:00
|
|
|
use serial_test::serial;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2021-02-25 19:43:39 +00:00
|
|
|
#[serial]
|
2024-05-21 18:46:49 +00:00
|
|
|
async fn test_password_reset() -> LemmyResult<()> {
|
2024-11-07 10:49:05 +00:00
|
|
|
let pool = &build_db_pool_for_tests();
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool = &mut pool.into();
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2024-05-21 18:46:49 +00:00
|
|
|
// Setup
|
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
2024-06-06 12:29:18 +00:00
|
|
|
let new_person = PersonInsertForm::test_form(inserted_instance.id, "thommy prw");
|
2024-05-21 18:46:49 +00:00
|
|
|
let inserted_person = Person::create(pool, &new_person).await?;
|
2024-07-16 10:22:47 +00:00
|
|
|
let new_local_user = LocalUserInsertForm::test_form(inserted_person.id);
|
2024-05-21 18:46:49 +00:00
|
|
|
let inserted_local_user = LocalUser::create(pool, &new_local_user, vec![]).await?;
|
2021-03-11 16:41:04 +00:00
|
|
|
|
2024-05-21 18:46:49 +00:00
|
|
|
// Create password reset token
|
2019-12-28 21:06:37 +00:00
|
|
|
let token = "nope";
|
2019-11-02 06:43:21 +00:00
|
|
|
let inserted_password_reset_request =
|
2024-05-21 18:46:49 +00:00
|
|
|
PasswordResetRequest::create(pool, inserted_local_user.id, token.to_string()).await?;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2024-05-21 18:46:49 +00:00
|
|
|
// Read it and verify
|
2024-09-23 15:26:50 +00:00
|
|
|
let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token).await?;
|
2019-11-02 06:43:21 +00:00
|
|
|
assert_eq!(
|
2024-05-21 18:46:49 +00:00
|
|
|
inserted_password_reset_request.id,
|
|
|
|
read_password_reset_request.id
|
2019-11-02 06:43:21 +00:00
|
|
|
);
|
2024-05-21 18:46:49 +00:00
|
|
|
assert_eq!(
|
|
|
|
inserted_password_reset_request.local_user_id,
|
|
|
|
read_password_reset_request.local_user_id
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
inserted_password_reset_request.token,
|
|
|
|
read_password_reset_request.token
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
inserted_password_reset_request.published,
|
|
|
|
read_password_reset_request.published
|
|
|
|
);
|
|
|
|
|
|
|
|
// Cannot reuse same token again
|
2024-09-23 15:26:50 +00:00
|
|
|
let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token).await;
|
|
|
|
assert!(read_password_reset_request.is_err());
|
2024-05-21 18:46:49 +00:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
let num_deleted = Person::delete(pool, inserted_person.id).await?;
|
|
|
|
Instance::delete(pool, inserted_instance.id).await?;
|
2019-10-30 03:35:39 +00:00
|
|
|
assert_eq!(1, num_deleted);
|
2024-05-21 18:46:49 +00:00
|
|
|
Ok(())
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
|
|
|
}
|