Skip to content

Commit

Permalink
For #26 add test
Browse files Browse the repository at this point in the history
  • Loading branch information
lissa3 committed Aug 6, 2023
1 parent 68ebe72 commit 57ea0f2
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/contacts/tests/test_mail_fail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from smtplib import SMTPException
from unittest import mock

from django.core import mail
from django.test import TestCase

from src.accounts.models import User
from src.contacts.jobs.send_news import Job as SendMailJob
from src.profiles.tests.factories.profile_factory import ProfileFactory

from ..exceptions import * # noqa
from .factories import NewsLetterFactory


@mock.patch("django.core.mail.send_mail")
class MailFailureTests(TestCase):
def test_news_not_send(self, mock_fail):
profile = ProfileFactory(want_news=True) # noqa
letter = NewsLetterFactory(letter_status=1) # noqa

mock_fail.side_effect = SMTPException

send_mail_job = SendMailJob()
send_mail_job.execute()

self.assertEqual(len(mail.outbox), 0)
self.assertTrue(mock_fail.called)
with self.assertRaises(SMTPException):
mock_fail()


class TestSendEmailJob(TestCase):
def test_manager_no_news_inactive_user(self):
"""
letter exists but modal manager method
method filters user is inactive;
ex: user inactive via other route than deleted account
"""
profile = ProfileFactory(want_news=True)
user = User.objects.get(profile=profile)
user.is_active = False
user.save()
letter = NewsLetterFactory(letter_status=1) # noqa
send_mail_job = SendMailJob()
with self.assertRaises(NewsFansNotFoundException) as e:
send_mail_job.execute()
self.assertEqual(str(e.exception), "No profiles not send news")
self.assertEqual(len(mail.outbox), 0)

def test_manager_no_news_fans(self):
"""
letter exists profile modal manager
filters no news fans
"""
profile = ProfileFactory() # noqa
letter = NewsLetterFactory(letter_status=1) # noqa
send_mail_job = SendMailJob()
with self.assertRaises(NewsFansNotFoundException) as e:
send_mail_job.execute()

self.assertEqual(str(e.exception), "No profiles not send news")
self.assertEqual(len(mail.outbox), 0)

def test_no_letter_no_job(self):
"""
if no letter -> no SendMail
"""
profile = ProfileFactory(want_news=True) # noqa
send_mail_job = SendMailJob()
with self.assertRaises(LetterNotFoundException) as e:
send_mail_job.execute()

self.assertEqual(str(e.exception), "No letter to send")
assert len(mail.outbox) == 0

0 comments on commit 57ea0f2

Please sign in to comment.