2 use diesel::{dsl::*, result::Error, PgConnection, *};
3 use lemmy_db_schema::{schema::password_reset_request::dsl::*, source::password_reset_request::*};
4 use sha2::{Digest, Sha256};
6 impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
7 fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
9 .find(password_reset_request_id)
12 fn create(conn: &PgConnection, form: &PasswordResetRequestForm) -> Result<Self, Error> {
13 insert_into(password_reset_request)
15 .get_result::<Self>(conn)
19 password_reset_request_id: i32,
20 form: &PasswordResetRequestForm,
21 ) -> Result<Self, Error> {
22 diesel::update(password_reset_request.find(password_reset_request_id))
24 .get_result::<Self>(conn)
28 pub trait PasswordResetRequest_ {
33 ) -> Result<PasswordResetRequest, Error>;
34 fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error>;
37 impl PasswordResetRequest_ for PasswordResetRequest {
42 ) -> Result<PasswordResetRequest, Error> {
43 let mut hasher = Sha256::new();
45 let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
47 let form = PasswordResetRequestForm {
48 user_id: from_user_id,
49 token_encrypted: token_hash,
52 Self::create(&conn, &form)
54 fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error> {
55 let mut hasher = Sha256::new();
57 let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
58 password_reset_request
59 .filter(token_encrypted.eq(token_hash))
60 .filter(published.gt(now - 1.days()))
65 fn bytes_to_hex(bytes: Vec<u8>) -> String {
66 let mut str = String::new();
68 str = format!("{}{:02x}", str, byte);
76 establish_unpooled_connection,
77 source::password_reset_request::PasswordResetRequest_,
82 use lemmy_db_schema::source::{password_reset_request::PasswordResetRequest, user::*};
83 use serial_test::serial;
88 let conn = establish_unpooled_connection();
90 let new_user = UserForm {
91 name: "thommy prw".into(),
92 preferred_username: None,
93 password_encrypted: "nope".into(),
103 theme: "browser".into(),
104 default_sort_type: SortType::Hot as i16,
105 default_listing_type: ListingType::Subscribed as i16,
106 lang: "browser".into(),
108 send_notifications_to_email: false,
114 last_refreshed_at: None,
116 shared_inbox_url: None,
119 let inserted_user = User_::create(&conn, &new_user).unwrap();
122 let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
124 let inserted_password_reset_request =
125 PasswordResetRequest::create_token(&conn, inserted_user.id, token).unwrap();
127 let expected_password_reset_request = PasswordResetRequest {
128 id: inserted_password_reset_request.id,
129 user_id: inserted_user.id,
130 token_encrypted: token_encrypted_.to_string(),
131 published: inserted_password_reset_request.published,
134 let read_password_reset_request = PasswordResetRequest::read_from_token(&conn, token).unwrap();
135 let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
137 assert_eq!(expected_password_reset_request, read_password_reset_request);
139 expected_password_reset_request,
140 inserted_password_reset_request
142 assert_eq!(1, num_deleted);