test_pagure_lib_drop_issue.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. from mock import patch, MagicMock
  15. sys.path.insert(0, os.path.join(os.path.dirname(
  16. os.path.abspath(__file__)), '..'))
  17. import pagure
  18. import pagure.lib
  19. import pagure.lib.model
  20. import tests
  21. class PagureLibDropIssuetests(tests.Modeltests):
  22. """ Tests for pagure.lib.drop_issue """
  23. @patch('pagure.lib.git.update_git')
  24. @patch('pagure.lib.notify.send_email')
  25. def setUp(self, p_send_email, p_ugt):
  26. """ Create a couple of tickets and add tag to the project so we can
  27. play with them later.
  28. """
  29. super(PagureLibDropIssuetests, self).setUp()
  30. p_send_email.return_value = True
  31. p_ugt.return_value = True
  32. tests.create_projects(self.session)
  33. repo = pagure.lib.get_authorized_project(self.session, 'test')
  34. # Before
  35. issues = pagure.lib.search_issues(self.session, repo)
  36. self.assertEqual(len(issues), 0)
  37. self.assertEqual(repo.open_tickets, 0)
  38. self.assertEqual(repo.open_tickets_public, 0)
  39. # Create two issues to play with
  40. msg = pagure.lib.new_issue(
  41. session=self.session,
  42. repo=repo,
  43. title='Test issue',
  44. content='We should work on this',
  45. user='pingou',
  46. )
  47. self.session.commit()
  48. self.assertEqual(msg.title, 'Test issue')
  49. self.assertEqual(repo.open_tickets, 1)
  50. self.assertEqual(repo.open_tickets_public, 1)
  51. msg = pagure.lib.new_issue(
  52. session=self.session,
  53. repo=repo,
  54. title='Test issue #2',
  55. content='We should work on this for the second time',
  56. user='foo',
  57. status='Open',
  58. )
  59. self.session.commit()
  60. self.assertEqual(msg.title, 'Test issue #2')
  61. self.assertEqual(repo.open_tickets, 2)
  62. self.assertEqual(repo.open_tickets_public, 2)
  63. # After
  64. issues = pagure.lib.search_issues(self.session, repo)
  65. self.assertEqual(len(issues), 2)
  66. # Add tag to the project
  67. pagure.lib.new_tag(
  68. self.session,
  69. 'red',
  70. 'red tag',
  71. '#ff0000',
  72. repo.id
  73. )
  74. self.session.commit()
  75. repo = pagure.lib.get_authorized_project(self.session, 'test')
  76. self.assertEqual(
  77. str(repo.tags_colored),
  78. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  79. )
  80. @patch('pagure.lib.git.update_git')
  81. @patch('pagure.lib.notify.send_email')
  82. @patch('pagure.lib.git._maybe_wait', tests.definitely_wait)
  83. def test_drop_issue(self, p_send_email, p_ugt):
  84. """ Test the drop_issue of pagure.lib.
  85. We had an issue where we could not delete issue that had been tagged
  86. with this test, we create two issues, tag one of them and delete
  87. it, ensuring it all goes well.
  88. """
  89. p_send_email.return_value = True
  90. p_ugt.return_value = True
  91. repo = pagure.lib.get_authorized_project(self.session, 'test')
  92. # Add tag to the second issue
  93. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  94. msgs = pagure.lib.update_tags(
  95. self.session,
  96. issue,
  97. tags=['red'],
  98. username='pingou',
  99. )
  100. self.session.commit()
  101. self.assertEqual(msgs, ['Issue tagged with: red'])
  102. repo = pagure.lib.get_authorized_project(self.session, 'test')
  103. self.assertEqual(len(repo.issues), 2)
  104. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  105. self.assertEqual(
  106. str(issue.tags),
  107. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  108. )
  109. # Drop the issue #2
  110. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  111. pagure.lib.drop_issue(
  112. self.session, issue, user='pingou')
  113. self.session.commit()
  114. repo = pagure.lib.get_authorized_project(self.session, 'test')
  115. self.assertEqual(len(repo.issues), 1)
  116. @patch('pagure.lib.git.update_git')
  117. @patch('pagure.lib.notify.send_email')
  118. @patch('pagure.lib.git._maybe_wait', tests.definitely_wait)
  119. def test_drop_issue_two_issues_one_tag(self, p_send_email, p_ugt):
  120. """ Test the drop_issue of pagure.lib.
  121. We had an issue where we could not delete issue that had been tagged
  122. with this test, we create two issues, tag them both and delete one
  123. then we check that the other issue is still tagged.
  124. """
  125. p_send_email.return_value = True
  126. p_ugt.return_value = True
  127. repo = pagure.lib.get_authorized_project(self.session, 'test')
  128. # Add the tag to both issues
  129. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  130. msgs = pagure.lib.update_tags(
  131. self.session,
  132. issue,
  133. tags=['red'],
  134. username='pingou',
  135. )
  136. self.session.commit()
  137. self.assertEqual(msgs, ['Issue tagged with: red'])
  138. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  139. msgs = pagure.lib.update_tags(
  140. self.session,
  141. issue,
  142. tags=['red'],
  143. username='pingou',
  144. )
  145. self.session.commit()
  146. self.assertEqual(msgs, ['Issue tagged with: red'])
  147. repo = pagure.lib.get_authorized_project(self.session, 'test')
  148. self.assertEqual(len(repo.issues), 2)
  149. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  150. self.assertEqual(
  151. str(issue.tags),
  152. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  153. )
  154. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  155. self.assertEqual(
  156. str(issue.tags),
  157. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  158. )
  159. # Drop the issue #2
  160. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  161. pagure.lib.drop_issue(
  162. self.session, issue, user='pingou')
  163. self.session.commit()
  164. repo = pagure.lib.get_authorized_project(self.session, 'test')
  165. self.assertEqual(len(repo.issues), 1)
  166. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  167. self.assertEqual(
  168. str(issue.tags),
  169. '[TagColored(id: 1, tag:red, tag_description:red tag, color:#ff0000)]'
  170. )
  171. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  172. self.assertIsNone(issue)
  173. if __name__ == '__main__':
  174. unittest.main(verbosity=2)