/ptsapp/tests.py

https://bitbucket.org/mgill25/debian-pts · Python · 130 lines · 103 code · 22 blank · 5 comment · 0 complexity · 059d3818a1666d0af6e12777ce809f89 MD5 · raw file

  1. """
  2. This file demonstrates writing tests using the unittest module. These will pass
  3. when you run "manage.py test".
  4. Replace this with more appropriate tests for your application.
  5. """
  6. import email
  7. from django.test import TestCase
  8. from django.test.utils import setup_test_environment
  9. from django.test.client import Client
  10. from ptsapp.models import Subscription, Tag
  11. from ptsapp.dispatch import *
  12. setup_test_environment()
  13. class SimpleTest(TestCase):
  14. def test_basic_addition(self):
  15. """
  16. Tests that 1 + 1 always equals 2.
  17. """
  18. self.assertEqual(1 + 1, 2)
  19. class DispatchTest(TestCase):
  20. def test_get_package_name(self):
  21. os.environ['LOCAL_PART'] = "Foo"
  22. os.environ['LOCAL_PART_SUFFIX'] = "_BAr"
  23. self.assertEqual(get_package_name(), "foo_bar")
  24. del os.environ['LOCAL_PART']
  25. del os.environ['LOCAL_PART_SUFFIX']
  26. def test_get_package_keyword(self):
  27. os.environ['LOCAL_PART'] = "Foo"
  28. os.environ['LOCAL_PART_SUFFIX'] = "_BAr"
  29. self.assertEqual(get_package_keyword(), ('foo', 'bar'))
  30. del os.environ['LOCAL_PART']
  31. del os.environ['LOCAL_PART_SUFFIX']
  32. def test_filter_tags_default(self):
  33. os.environ['LOCAL_PART'] = "Foo"
  34. os.environ['LOCAL_PART_SUFFIX'] = "default"
  35. p = Package(name='MyPackage'); p.save()
  36. s = Subscription.objects.get_or_create(address='hello@debian.org', package=p)[0]
  37. s.generate_default_tags(); s.save()
  38. self.assertTrue(filter_tags(s, 'default'))
  39. self.assertTrue(filter_tags(s, 'bts'))
  40. self.assertTrue(filter_tags(s, 'bts-control'))
  41. self.assertTrue(filter_tags(s, 'katie-other'))
  42. self.assertTrue(filter_tags(s, 'contact'))
  43. self.assertTrue(filter_tags(s, 'summary'))
  44. self.assertTrue(filter_tags(s, 'upload-source'))
  45. self.assertTrue(filter_tags(s, 'buildd'))
  46. del os.environ['LOCAL_PART']
  47. del os.environ['LOCAL_PART_SUFFIX']
  48. def test_parse_email(self):
  49. message = """From: mgill25@outlook.com
  50. Test Message."""
  51. msg = email.message_from_string(message)
  52. parsed_output = parse_mail(msg)
  53. self.assertEqual(parsed_output[1], 'no-msgid-present@localhost')
  54. self.assertEqual(parsed_output[2], 'mgill25@outlook.com')
  55. def test_parse_email_fail_xloop(self):
  56. message = """From: mgill25@outlook.com\nX-Loop: python@packages.qa.debian.org
  57. Test Message.
  58. """
  59. msg = email.message_from_string(message)
  60. self.assertEqual(None, parse_mail(msg))
  61. def test_inject_headers(self):
  62. message = """From: mgill25@outlook.com
  63. Hello, World."""
  64. msg = email.message_from_string(message)
  65. injected_output = inject_headers(msg)
  66. self.assertEqual(injected_output['From'], 'mgill25@outlook.com')
  67. self.assertEqual(injected_output['X-Loop'], 'python_bts@packages.qa.debian.org')
  68. self.assertEqual(injected_output['X-Debian'], 'PTS')
  69. self.assertEqual(injected_output['X-Debian-Package'], 'python_bts')
  70. self.assertEqual(injected_output['X-PTS-Package'], 'python_bts')
  71. self.assertEqual(injected_output['X-PTS-Keyword'], 'bts')
  72. self.assertEqual(injected_output['List-Unsubscribe'], '<mailto:pts@qa.debian.org?body=unsubscribe%20python_bts>')
  73. def test_filter_tags_address_package(self):
  74. os.environ['LOCAL_PART'] = 'hello'
  75. os.environ['LOCAL_PART_SUFFIX'] = 'katie-other'
  76. p = Package(name='MyPackage'); p.save()
  77. s = Subscription(address='hello@debian.org', package=p); s.save()
  78. s.set_tag("summary")
  79. s.save()
  80. self.assertTrue(filter_tags(s, "summary"))
  81. del os.environ['LOCAL_PART']
  82. del os.environ['LOCAL_PART_SUFFIX']
  83. def test_update_keyword(self):
  84. p = Package(name='MyPackage'); p.save()
  85. s = Subscription.objects.get_or_create(address='hello@debian.org', package=p)[0]
  86. s.save()
  87. message = """From: mgill25@outlook.com\nX-Loop: owner@bugs.debian.org\nX-Debian-PR-Message: transcript"""
  88. msg = email.message_from_string(message)
  89. self.assertEqual(update_keyword(msg), 'bts-control')
  90. message = """From: mgill25@outlook.com\nX-Loop: owner@bugs.debian.org\nX-Debian-PR-Message: Heyo!"""
  91. msg = email.message_from_string(message)
  92. self.assertEqual(update_keyword(msg), 'bts')
  93. message = """From: mgill25@outlook.com\nSubject: Accepted\nX-DAK: Foo\n
  94. Source Package URL: http://cdn.debian.net/debian/pool/main/f/figlet/figlet_2.2.2-1.dsc"""
  95. msg = email.message_from_string(message)
  96. self.assertEqual(update_keyword(msg), 'upload-source')
  97. message = """From: mgill25@outlook.com\nSubject: INSTALLED\nX-DAK: Foo\n
  98. Binary Package Installed!!!"""
  99. msg = email.message_from_string(message)
  100. self.assertEqual(update_keyword(msg), 'upload-binary')
  101. message = """From: mgill25@outlook.com\nSubject: Comments Regarding MyPackage.changes\nX-DAK: Foo\n
  102. Binary Package Installed!!!"""
  103. msg = email.message_from_string(message)
  104. self.assertEqual(update_keyword(msg), 'katie-other')