Skip to content

Commit

Permalink
Revert "fix: Send verification email to new email on email change req…
Browse files Browse the repository at this point in the history
…uest"

This reverts commit f22fb21.
  • Loading branch information
faucomte97 committed Nov 8, 2023
1 parent f22fb21 commit b51fd42
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 61 deletions.
10 changes: 10 additions & 0 deletions cfl_common/common/email_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ def emailChangeNotificationEmail(request, new_email_address):
}


def emailChangeDuplicateNotificationEmail(request, email):
return {
"subject": f"Duplicate account",
"message": (
f"A user is already registered with this email address: {email}.\n"
f"Please change your email address to something else."
),
}


def userAlreadyRegisteredEmail(request, email, is_independent_student=False):
if is_independent_student:
login_url = reverse("independent_student_login")
Expand Down
81 changes: 32 additions & 49 deletions cfl_common/common/helpers/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from common.email_messages import (
emailChangeNotificationEmail,
emailChangeVerificationEmail,
emailChangeDuplicateNotificationEmail,
emailVerificationNeededEmail,
parentsEmailVerificationNeededEmail,
)
Expand Down Expand Up @@ -60,8 +61,8 @@ def send_email(

if replace_url:
verify_url = replace_url["verify_url"]
verify_replace_url = re.sub(f"(.*/verify_email/)(.*)", f"\\1", verify_url)
html_body = re.sub(f"({verify_url})(.*){verify_url}", f"\\1\\2{verify_replace_url}", original_html_body)
verify_replace_url = re.sub(f'(.*/verify_email/)(.*)', f'\\1', verify_url)
html_body = re.sub(f'({verify_url})(.*){verify_url}', f'\\1\\2{verify_replace_url}', original_html_body)

# make message using templates
message = EmailMultiAlternatives(subject, plaintext_body, sender, recipients)
Expand All @@ -77,7 +78,6 @@ def generate_token(user, new_email="", preverified=False):

return generate_token_for_email(user.email, new_email)


def generate_token_for_email(email: str, new_email: str = ""):
return jwt.encode(
{
Expand Down Expand Up @@ -124,39 +124,18 @@ def send_verification_email(request, user, data, new_email=None, age=None):
# if the user is a teacher
if age is None:
message = emailVerificationNeededEmail(request, verification)
send_email(
VERIFICATION_EMAIL,
[user.email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])

if _newsletter_ticked(data):
add_to_dotmailer(user.first_name, user.last_name, user.email, DotmailerUserType.TEACHER)
# if the user is an independent student
else:
if age < 13:
message = parentsEmailVerificationNeededEmail(request, user, verification)
send_email(
VERIFICATION_EMAIL,
[user.email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])
else:
message = emailVerificationNeededEmail(request, verification)
send_email(
VERIFICATION_EMAIL,
[user.email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])

if _newsletter_ticked(data):
add_to_dotmailer(user.first_name, user.last_name, user.email, DotmailerUserType.STUDENT)
Expand All @@ -165,14 +144,10 @@ def send_verification_email(request, user, data, new_email=None, age=None):
verification = generate_token(user, new_email)

message = emailChangeVerificationEmail(request, verification)
send_email(
VERIFICATION_EMAIL,
[new_email],
message["subject"],
message["message"],
message["subject"],
replace_url=message["url"],
)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"], replace_url=message["url"])

message = emailChangeNotificationEmail(request, new_email)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"])


def add_to_dotmailer(first_name: str, last_name: str, email: str, user_type: DotmailerUserType):
Expand Down Expand Up @@ -280,12 +255,17 @@ def update_indy_email(user, request, data):
if new_email != "" and new_email != user.email:
changing_email = True
users_with_email = User.objects.filter(email=new_email)

message = emailChangeNotificationEmail(request, new_email)
send_email(VERIFICATION_EMAIL, [user.email], message["subject"], message["message"], message["subject"])

# email is available
if not users_with_email.exists():
# email is already taken
if users_with_email.exists():
email_message = emailChangeDuplicateNotificationEmail(request, new_email)
send_email(
NOTIFICATION_EMAIL,
[user.email],
email_message["subject"],
email_message["message"],
email_message["subject"],
)
else:
# new email to set and verify
send_verification_email(request, user, data, new_email)
return changing_email, new_email
Expand All @@ -298,14 +278,17 @@ def update_email(user: Teacher or Student, request, data):
if new_email != "" and new_email != user.new_user.email:
changing_email = True
users_with_email = User.objects.filter(email=new_email)

message = emailChangeNotificationEmail(request, new_email)
send_email(
VERIFICATION_EMAIL, [user.new_user.email], message["subject"], message["message"], message["subject"]
)

# email is available
if not users_with_email.exists():
# email is already taken
if users_with_email.exists():
email_message = emailChangeDuplicateNotificationEmail(request, new_email)
send_email(
NOTIFICATION_EMAIL,
[user.new_user.email],
email_message["subject"],
email_message["message"],
email_message["subject"],
)
else:
# new email to set and verify
send_verification_email(request, user.new_user, data, new_email)
return changing_email, new_email
9 changes: 3 additions & 6 deletions portal/tests/test_independent_student.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Email address update"
assert subject == "Duplicate account"
mail.outbox = []

# Try changing email to an existing teacher's email
Expand All @@ -372,7 +372,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Email address update"
assert subject == "Duplicate account"
mail.outbox = []

page = (
Expand Down Expand Up @@ -401,10 +401,7 @@ def test_change_email(self):

page = page.logout()

subject = str(mail.outbox[0].subject)
assert subject == "Email address update"

page = email_utils.follow_change_email_link_to_independent_dashboard(page, mail.outbox[1])
page = email_utils.follow_change_email_link_to_independent_dashboard(page, mail.outbox[0])
mail.outbox = []

page = page.independent_student_login(new_email, password)
Expand Down
9 changes: 3 additions & 6 deletions portal/tests/test_teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Email address update"
assert subject == "Duplicate account"
mail.outbox = []

# Try changing email to an existing indy student's email, should fail
Expand All @@ -566,7 +566,7 @@ def test_change_email(self):
assert is_email_updated_message_showing(self.selenium)

subject = str(mail.outbox[0].subject)
assert subject == "Email address update"
assert subject == "Duplicate account"
mail.outbox = []

page = self.go_to_homepage()
Expand All @@ -585,10 +585,7 @@ def test_change_email(self):

page = page.logout()

subject = str(mail.outbox[0].subject)
assert subject == "Email address update"

page = email_utils.follow_change_email_link_to_dashboard(page, mail.outbox[1])
page = email_utils.follow_change_email_link_to_dashboard(page, mail.outbox[0])
mail.outbox = []

page = page.login(new_email, password).open_account_tab()
Expand Down

0 comments on commit b51fd42

Please sign in to comment.