tests_mail_news.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2013 The Distro Tracker Developers
  3. # See the COPYRIGHT file at the top-level directory of this distribution and
  4. # at http://deb.li/DTAuthors
  5. #
  6. # This file is part of Distro Tracker. It is subject to the license terms
  7. # in the LICENSE file found in the top-level directory of this
  8. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  9. # including this file, may be copied, modified, propagated, or distributed
  10. # except according to the terms contained in the LICENSE file.
  11. """
  12. Tests for the :mod:`distro_tracker.mail.mail_news` app.
  13. """
  14. from __future__ import unicode_literals
  15. from distro_tracker.test import TestCase, SimpleTestCase
  16. from django.utils import six
  17. from django.utils.six.moves import mock
  18. from django.utils.encoding import force_bytes
  19. from distro_tracker.core.models import SourcePackageName, SourcePackage
  20. from distro_tracker.core.models import News
  21. from distro_tracker.mail.mail_news import process
  22. from distro_tracker.mail.management.commands.tracker_receive_news import (
  23. Command as MailNewsCommand)
  24. from email.message import Message
  25. class BasicNewsGeneration(TestCase):
  26. def setUp(self):
  27. self.package_name = \
  28. SourcePackageName.objects.create(name='dummy-package')
  29. self.package = SourcePackage.objects.create(
  30. source_package_name=self.package_name,
  31. version='1.0.0')
  32. self.message = Message()
  33. def set_subject(self, subject):
  34. if 'Subject' in self.message:
  35. del self.message['Subject']
  36. self.message['Subject'] = subject
  37. def add_header(self, header_name, header_value):
  38. self.message[header_name] = header_value
  39. def set_message_content(self, content):
  40. self.message.set_payload(content)
  41. def process_mail(self):
  42. process(force_bytes(self.message.as_string(), 'utf-8'))
  43. def test_creates_news_from_email(self):
  44. """
  45. Tets that a news is created from an email with the correct header
  46. information.
  47. """
  48. subject = 'Some message'
  49. content = 'Some message content'
  50. self.set_subject(subject)
  51. self.add_header('X-Distro-Tracker-Package', self.package.name)
  52. self.set_message_content(content)
  53. self.process_mail()
  54. # A news item is created
  55. self.assertEqual(1, News.objects.count())
  56. news = News.objects.all()[0]
  57. # The title of the news is set correctly.
  58. self.assertEqual(subject, news.title)
  59. self.assertIn(content, news.content.decode('utf-8'))
  60. # The content type is set to render email messages
  61. self.assertEqual(news.content_type, 'message/rfc822')
  62. def test_create_news_url_from_email(self):
  63. """
  64. Tests that when an X-Distro-Tracker-Url header is given the news
  65. content is the URL, not the email message.
  66. """
  67. subject = 'Some message'
  68. content = 'Some message content'
  69. self.set_subject(subject)
  70. self.add_header('X-Distro-Tracker-Package', self.package.name)
  71. url = 'http://some-url.com'
  72. self.add_header('X-Distro-Tracker-Url', url)
  73. self.set_message_content(content)
  74. self.process_mail()
  75. # A news item is created
  76. self.assertEqual(1, News.objects.count())
  77. news = News.objects.all()[0]
  78. # The title of the news is set correctly.
  79. self.assertEqual(url, news.title)
  80. self.assertIn(url, news.content.strip())
  81. def test_create_news_package_does_not_exist(self):
  82. """
  83. Tests that when the package given in X-Distro-Tracker-Package does
  84. not exist, no news items are created.
  85. """
  86. subject = 'Some message'
  87. content = 'Some message content'
  88. self.set_subject(subject)
  89. self.add_header('X-Distro-Tracker-Package', 'no-exist')
  90. self.set_message_content(content)
  91. # Sanity check - there are no news at the beginning
  92. self.assertEqual(0, News.objects.count())
  93. self.process_mail()
  94. # There are still no news
  95. self.assertEqual(0, News.objects.count())
  96. @mock.patch('distro_tracker.mail.mail_news.vendor.call')
  97. def test_create_news_calls_vendor_function(self, mock_vendor_call):
  98. """
  99. Tests that the vendor-provided function is called during the processing
  100. of the news.
  101. """
  102. subject = 'Some message'
  103. content = 'Some message content'
  104. # Do not add any headers.
  105. self.set_subject(subject)
  106. self.set_message_content(content)
  107. # Make it look like the vendor does not implement the function
  108. mock_vendor_call.return_value = (None, False)
  109. self.process_mail()
  110. # The function was called?
  111. self.assertTrue(mock_vendor_call.called)
  112. # The correct vendor function was asked for?
  113. self.assertEqual(mock_vendor_call.call_args[0][0],
  114. 'create_news_from_email_message')
  115. class MailNewsManagementCommandTest(SimpleTestCase):
  116. """
  117. Tests that the
  118. :mod:`distro_tracker.mail.management.commands.tracker_receive_news`
  119. management command calls the correct function.
  120. """
  121. @mock.patch(
  122. 'distro_tracker.mail.management.commands.tracker_receive_news.process')
  123. def test_calls_process(self, mock_process):
  124. cmd = MailNewsCommand()
  125. cmd.input_file = mock.create_autospec(six.BytesIO)
  126. cmd.handle()
  127. self.assertTrue(mock_process.called)