Skip to content

Commit

Permalink
user mails refactoring/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Feb 26, 2021
1 parent 2cca218 commit 97cb483
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions core/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from celery.utils.log import get_task_logger
from celery_once import QueueOnce
from django.apps import apps
from django.conf import settings
from django.core.mail import EmailMessage
from django.core.management import call_command
from django.template.loader import render_to_string
from django_elasticsearch_dsl.registries import registry
from pydash import get

from core.celery import app
from core.common.constants import CONFIRM_EMAIL_ADDRESS_MAIL_SUBJECT, PASSWORD_RESET_MAIL_SUBJECT
Expand Down Expand Up @@ -212,8 +214,9 @@ def send_user_verification_email(user_id):
)
mail = EmailMessage(subject=CONFIRM_EMAIL_ADDRESS_MAIL_SUBJECT, body=html_body, to=[user.email])
mail.content_subtype = "html"
mail.send()
return mail
res = mail.send()

return mail if get(settings, 'TEST_MODE', False) else res


@app.task
Expand All @@ -232,8 +235,9 @@ def send_user_reset_password_email(user_id):
)
mail = EmailMessage(subject=PASSWORD_RESET_MAIL_SUBJECT, body=html_body, to=[user.email])
mail.content_subtype = "html"
mail.send()
return mail
res = mail.send()

return mail if get(settings, 'TEST_MODE', False) else res


@app.task(bind=True)
Expand Down
4 changes: 2 additions & 2 deletions core/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ def orgs_count(self):
return self.organizations.count()

def send_verification_email(self):
return send_user_verification_email.apply_async((self.id, ))
return send_user_verification_email.delay(self.id)

def send_reset_password_email(self):
return send_user_reset_password_email.apply_async((self.id,))
return send_user_reset_password_email.delay(self.id)

@property
def email_verification_url(self):
Expand Down
4 changes: 2 additions & 2 deletions core/users/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ def test_send_verification_email(self, mail_mock): # pylint: disable=no-self-us
user = UserProfile(id=189)
user.send_verification_email()

mail_mock.apply_async.assert_called_once_with((189, ))
mail_mock.delay.assert_called_once_with(189)

@patch('core.users.models.send_user_reset_password_email')
def test_send_reset_password_email(self, mail_mock): # pylint: disable=no-self-use
user = UserProfile(id=189)
user.send_reset_password_email()

mail_mock.apply_async.assert_called_once_with((189, ))
mail_mock.delay.assert_called_once_with(189)

def test_email_verification_url(self):
user = UserProfile(id=189, username='foobar', verification_token='some-token')
Expand Down

0 comments on commit 97cb483

Please sign in to comment.