import datetime import re from django.contrib.auth.models import User import pytest from . import models from .emails import make_candidate_email, make_comment_email from .models import Candidate, Comment def make_candidate(save=False, **kwargs): defaults = { 'name': 'Test Candidate', 'slug': 'test', 'vendor': 'test vendor', 'device': 'test device', 'release_date': datetime.datetime(2000, 1, 1, tzinfo=datetime.timezone.utc) } merged = defaults | kwargs obj = Candidate(**merged) if save: obj.save() return obj def test_message_id(): assert re.match(r'<.+@.+>', models.gen_message_id()) @pytest.mark.django_db def test_candidate_email(): user = User.objects.create(first_name='Test', last_name='User') candidate = make_candidate(name='Test Candidate', save=True) email = make_candidate_email(candidate, user) assert 'Message-ID' in email.extra_headers assert email.subject == 'Test Candidate' assert 'Test Candidate' in email.body assert 'Test User' in email.body @pytest.mark.django_db def test_comment_knows_comment_its_replying_to(): user = User.objects.create() candidate = make_candidate(name='Test Candidate', save=True) first_comment = Comment.objects.create(user=user, candidate=candidate) second_comment = Comment.objects.create(user=user, candidate=candidate) assert second_comment._find_previous_comment() == first_comment assert second_comment.in_reply_to() == first_comment.email_message_id @pytest.mark.django_db def test_comment_email(): user = User.objects.create(first_name='Test', last_name='User') candidate = make_candidate(name='Test Candidate', save=True) models.Comment.objects.create(candidate=candidate, user=user) second_comment = models.Comment.objects.create( candidate=candidate, user=user, message='Test message', ) email = make_comment_email(second_comment) assert 'Message-ID' in email.extra_headers assert 'In-Reply-To' in email.extra_headers assert email.subject == 'Re: Test Candidate' assert 'Test message' in email.body assert 'Test User' in email.body