Skip to content

Commit 3be2b55

Browse files
committed
retain original email
- retain email registered with when github email is modified
1 parent fc01ad4 commit 3be2b55

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/models/user.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ impl<'a> NewUser<'a> {
5454
email: Option<&'a str>,
5555
conn: &PgConnection,
5656
) -> QueryResult<User> {
57-
use crate::schema::emails::columns::user_id;
5857
use crate::schema::users::dsl::*;
5958
use diesel::dsl::sql;
6059
use diesel::insert_into;
@@ -91,9 +90,7 @@ impl<'a> NewUser<'a> {
9190

9291
let token = insert_into(emails::table)
9392
.values(&new_email)
94-
.on_conflict(user_id)
95-
.do_update()
96-
.set(&new_email)
93+
.on_conflict_do_nothing()
9794
.returning(emails::token)
9895
.get_result::<String>(conn)
9996
.optional()?;

src/tests/user.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,13 @@ fn test_confirm_user_email() {
514514

515515
// Simulate logging in via GitHub. Don't use app.db_new_user because it inserts a verified
516516
// email directly into the database and we want to test the verification flow here.
517+
let email = "cow@mammals@milk";
518+
517519
let user = app.db(|conn| {
518520
let u = NewUser {
519521
..new_user("arbitrary_username")
520522
};
521-
let u = u.create_or_update(None, conn).unwrap();
523+
let u = u.create_or_update(Some(email), conn).unwrap();
522524
MockCookieUser::new(&app, u)
523525
});
524526
let user_model = user.as_model();
@@ -533,7 +535,7 @@ fn test_confirm_user_email() {
533535
user.confirm_email(&email_token);
534536

535537
let json = user.show_me();
536-
assert_eq!(json.user.email.unwrap(), "potato2@example.com");
538+
assert_eq!(json.user.email.unwrap(), "cow@mammals@milk");
537539
assert!(json.user.email_verified);
538540
assert!(json.user.email_verification_sent);
539541
}
@@ -552,11 +554,12 @@ fn test_existing_user_email() {
552554

553555
// Simulate logging in via GitHub. Don't use app.db_new_user because it inserts a verified
554556
// email directly into the database and we want to test the verification flow here.
557+
let email = "potahto@example.com";
555558
let user = app.db(|conn| {
556559
let u = NewUser {
557560
..new_user("arbitrary_username")
558561
};
559-
let u = u.create_or_update(None, conn).unwrap();
562+
let u = u.create_or_update(Some(email), conn).unwrap();
560563
update(Email::belonging_to(&u))
561564
// Users created before we added verification will have
562565
// `NULL` in the `token_generated_at` column.

src/tests/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl TestApp {
135135
use diesel::prelude::*;
136136

137137
let user = self.db(|conn| {
138-
let email = "cow@mammals.milk";
138+
let email = "something@example.com";
139139

140140
let user = crate::new_user(username)
141141
.create_or_update(None, conn)

0 commit comments

Comments
 (0)