test_pagure_flask_dump_load_ticket.py 6.5 KB

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