test_pagure_flask_dump_load_ticket.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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 json
  11. import unittest
  12. import shutil
  13. import sys
  14. import tempfile
  15. import time
  16. import os
  17. import pygit2
  18. from mock import patch
  19. sys.path.insert(0, os.path.join(os.path.dirname(
  20. os.path.abspath(__file__)), '..'))
  21. import pagure
  22. import pagure.lib
  23. import tests
  24. class PagureFlaskDumpLoadTicketTests(tests.Modeltests):
  25. """ Tests for flask application for dumping and re-loading the JSON of
  26. a ticket.
  27. """
  28. @patch('pagure.lib.notify.send_email')
  29. @patch('pagure.lib.git._maybe_wait')
  30. def test_dumping_reloading_ticket(self, mw, send_email):
  31. """ Test dumping a ticket into a JSON blob. """
  32. mw.side_effect = lambda result: result.get()
  33. send_email.return_value = True
  34. tests.create_projects(self.session)
  35. # Create repo
  36. self.gitrepo = os.path.join(self.path, 'repos', 'tickets', 'test.git')
  37. repopath = os.path.join(self.path, 'repos', 'tickets')
  38. os.makedirs(self.gitrepo)
  39. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  40. repo = pagure.lib.get_authorized_project(self.session, 'test')
  41. # Create an issue to play with
  42. msg = pagure.lib.new_issue(
  43. session=self.session,
  44. repo=repo,
  45. title='Test issue',
  46. content='We should work on this',
  47. user='pingou',
  48. )
  49. self.assertEqual(msg.title, 'Test issue')
  50. # Need another two issue to test the dependencie chain
  51. msg = pagure.lib.new_issue(
  52. session=self.session,
  53. repo=repo,
  54. title='Test issue #2',
  55. content='Another bug',
  56. user='pingou',
  57. )
  58. self.assertEqual(msg.title, 'Test issue #2')
  59. msg = pagure.lib.new_issue(
  60. session=self.session,
  61. repo=repo,
  62. title='Test issue #3',
  63. content='That would be nice feature no?',
  64. user='foo',
  65. )
  66. self.assertEqual(msg.title, 'Test issue #3')
  67. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  68. issue2 = pagure.lib.search_issues(self.session, repo, issueid=2)
  69. issue3 = pagure.lib.search_issues(self.session, repo, issueid=3)
  70. # Add a couple of comment on the ticket
  71. msg = pagure.lib.add_issue_comment(
  72. session=self.session,
  73. issue=issue,
  74. comment='Hey look a comment!',
  75. user='foo',
  76. )
  77. self.session.commit()
  78. self.assertEqual(msg, 'Comment added')
  79. msg = pagure.lib.add_issue_comment(
  80. session=self.session,
  81. issue=issue,
  82. comment='crazy right?',
  83. user='pingou',
  84. )
  85. self.session.commit()
  86. self.assertEqual(msg, 'Comment added')
  87. # Assign the ticket to someone
  88. msg = pagure.lib.add_issue_assignee(
  89. session=self.session,
  90. issue=issue,
  91. assignee='pingou',
  92. user='pingou',
  93. )
  94. self.session.commit()
  95. self.assertEqual(msg, 'Issue assigned to pingou')
  96. # Add a couple of tags on the ticket
  97. msg = pagure.lib.add_tag_obj(
  98. session=self.session,
  99. obj=issue,
  100. tags=[' feature ', 'future '],
  101. user='pingou',
  102. )
  103. self.session.commit()
  104. self.assertEqual(msg, 'Issue tagged with: feature, future')
  105. # Add dependencies
  106. msg = pagure.lib.add_issue_dependency(
  107. session=self.session,
  108. issue=issue,
  109. issue_blocked=issue2,
  110. user='pingou',
  111. )
  112. self.session.commit()
  113. self.assertEqual(msg, 'Issue marked as depending on: #2')
  114. msg = pagure.lib.add_issue_dependency(
  115. session=self.session,
  116. issue=issue3,
  117. issue_blocked=issue,
  118. user='foo',
  119. )
  120. self.session.commit()
  121. self.assertEqual(msg, 'Issue marked as depending on: #1')
  122. # Dump the JSON
  123. pagure.lib.git.update_git(issue, repo).wait()
  124. repo = pygit2.Repository(self.gitrepo)
  125. cnt = len([commit
  126. for commit in repo.walk(
  127. repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)])
  128. self.assertIn(cnt, (9, 10))
  129. last_commit = repo.revparse_single('HEAD')
  130. patch = pagure.lib.git.commit_to_patch(repo, last_commit)
  131. for line in patch.split('\n'):
  132. if line.startswith('--- a/'):
  133. fileid = line.split('--- a/')[1]
  134. break
  135. newpath = tempfile.mkdtemp(prefix='pagure-dump-load')
  136. clone_repo = pygit2.clone_repository(self.gitrepo, newpath)
  137. self.assertEqual(len(os.listdir(newpath)), 4)
  138. ticket_json = os.path.join(self.path, 'test_ticket.json')
  139. self.assertFalse(os.path.exists(ticket_json))
  140. shutil.copyfile(os.path.join(newpath, fileid), ticket_json)
  141. self.assertTrue(os.path.exists(ticket_json))
  142. jsondata = None
  143. with open(ticket_json) as stream:
  144. jsondata = json.load(stream)
  145. self.assertNotEqual(jsondata, None)
  146. shutil.rmtree(newpath)
  147. # Test reloading the JSON
  148. self.tearDown()
  149. self.setUp()
  150. # Give the worker time to spawn
  151. time.sleep(2)
  152. tests.create_projects(self.session)
  153. # Create repo
  154. self.gitrepo = os.path.join(self.path, 'tickets', 'test.git')
  155. repopath = os.path.join(self.path, 'tickets')
  156. os.makedirs(self.gitrepo)
  157. pygit2.init_repository(self.gitrepo, bare=True)
  158. pagure.lib.git.update_ticket_from_git(
  159. self.session,
  160. reponame='test',
  161. namespace=None,
  162. username=None,
  163. issue_uid='foobar',
  164. json_data=jsondata,
  165. agent='pingou',
  166. )
  167. # Post loading
  168. repo = pagure.lib.get_authorized_project(self.session, 'test')
  169. self.assertEqual(len(repo.issues), 1)
  170. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  171. # Check after re-loading
  172. self.assertEqual(len(issue.comments), 3)
  173. self.assertEqual(len(issue.tags), 2)
  174. self.assertEqual(sorted(issue.tags_text), sorted(['future', 'feature']))
  175. self.assertEqual(issue.assignee.username, 'pingou')
  176. self.assertEqual(issue.children, [])
  177. self.assertEqual(issue.parents, [])
  178. self.assertEqual(issue.status, 'Open')
  179. if __name__ == '__main__':
  180. unittest.main(verbosity=2)