test_pagure_flask_dump_load_ticket.py 6.8 KB

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