test_pagure_flask_dump_load_ticket.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 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
  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. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskDumpLoadTicketTests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.lib.SESSION = self.session
  31. pagure.ui.app.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.ui.fork.SESSION = self.session
  34. pagure.ui.repo.SESSION = self.session
  35. pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
  36. pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks')
  37. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  38. tests.HERE, 'tickets')
  39. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  40. tests.HERE, 'docs')
  41. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  42. tests.HERE, 'requests')
  43. self.app = pagure.APP.test_client()
  44. @patch('pagure.lib.notify.send_email')
  45. def test_dumping_ticket(self, send_email):
  46. """ Test dumping a ticket into a JSON blob. """
  47. send_email.return_value = True
  48. tests.create_projects(self.session)
  49. # Create repo
  50. self.gitrepo = os.path.join(tests.HERE, 'tickets', 'test.git')
  51. repopath = os.path.join(tests.HERE, 'tickets')
  52. os.makedirs(self.gitrepo)
  53. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  54. repo = pagure.lib.get_project(self.session, 'test')
  55. # Create an issue to play with
  56. msg = pagure.lib.new_issue(
  57. session=self.session,
  58. repo=repo,
  59. title='Test issue',
  60. content='We should work on this',
  61. user='pingou',
  62. ticketfolder=repopath
  63. )
  64. self.assertEqual(msg.title, 'Test issue')
  65. # Need another two issue to test the dependencie chain
  66. msg = pagure.lib.new_issue(
  67. session=self.session,
  68. repo=repo,
  69. title='Test issue #2',
  70. content='Another bug',
  71. user='pingou',
  72. ticketfolder=repopath
  73. )
  74. self.assertEqual(msg.title, 'Test issue #2')
  75. msg = pagure.lib.new_issue(
  76. session=self.session,
  77. repo=repo,
  78. title='Test issue #3',
  79. content='That would be nice feature no?',
  80. user='foo',
  81. ticketfolder=repopath
  82. )
  83. self.assertEqual(msg.title, 'Test issue #3')
  84. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  85. issue2 = pagure.lib.search_issues(self.session, repo, issueid=2)
  86. issue3 = pagure.lib.search_issues(self.session, repo, issueid=3)
  87. # Add a couple of comment on the ticket
  88. msg = pagure.lib.add_issue_comment(
  89. session=self.session,
  90. issue=issue,
  91. comment='Hey look a comment!',
  92. user='foo',
  93. ticketfolder=repopath,
  94. )
  95. self.session.commit()
  96. self.assertEqual(msg, 'Comment added')
  97. msg = pagure.lib.add_issue_comment(
  98. session=self.session,
  99. issue=issue,
  100. comment='crazy right?',
  101. user='pingou',
  102. ticketfolder=repopath,
  103. )
  104. self.session.commit()
  105. self.assertEqual(msg, 'Comment added')
  106. # Assign the ticket to someone
  107. msg = pagure.lib.add_issue_assignee(
  108. session=self.session,
  109. issue=issue,
  110. assignee='pingou',
  111. user='pingou',
  112. ticketfolder=repopath,
  113. )
  114. self.session.commit()
  115. self.assertEqual(msg, 'Issue assigned')
  116. # Add a couple of tags on the ticket
  117. msg = pagure.lib.add_tag_obj(
  118. session=self.session,
  119. obj=issue,
  120. tags=[' feature ', 'future '],
  121. user='pingou',
  122. ticketfolder=repopath,
  123. )
  124. self.session.commit()
  125. self.assertEqual(msg, 'Tag added: feature, future')
  126. # Add dependencies
  127. msg = pagure.lib.add_issue_dependency(
  128. session=self.session,
  129. issue=issue,
  130. issue_blocked=issue2,
  131. user='pingou',
  132. ticketfolder=repopath,
  133. )
  134. self.session.commit()
  135. self.assertEqual(msg, 'Dependency added')
  136. msg = pagure.lib.add_issue_dependency(
  137. session=self.session,
  138. issue=issue3,
  139. issue_blocked=issue,
  140. user='foo',
  141. ticketfolder=repopath,
  142. )
  143. self.session.commit()
  144. self.assertEqual(msg, 'Dependency added')
  145. # Dump the JSON
  146. pagure.lib.git.update_git(issue, repo, repopath)
  147. repo = pygit2.Repository(self.gitrepo)
  148. cnt = len([commit
  149. for commit in repo.walk(
  150. repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL)])
  151. self.assertEqual(cnt, 10)
  152. last_commit = repo.revparse_single('HEAD')
  153. patch = pagure.lib.git.commit_to_patch(repo, last_commit)
  154. for line in patch.split('\n'):
  155. if line.startswith('--- a/'):
  156. fileid = line.split('--- a/')[1]
  157. break
  158. newpath = tempfile.mkdtemp(prefix='pagure-dump-load')
  159. clone_repo = pygit2.clone_repository(self.gitrepo, newpath)
  160. self.assertEqual(len(os.listdir(newpath)), 4)
  161. ticket_json = os.path.join(tests.HERE, 'test_ticket.json')
  162. self.assertFalse(os.path.exists(ticket_json))
  163. shutil.copyfile(os.path.join(newpath, fileid), ticket_json)
  164. self.assertTrue(os.path.exists(ticket_json))
  165. shutil.rmtree(newpath)
  166. @patch('pagure.lib.notify.send_email')
  167. def test_reload_ticket(self, send_email):
  168. """ Test reloading a ticket from a JSON blob. """
  169. send_email.return_value = True
  170. ticket_json = os.path.join(tests.HERE, 'test_ticket.json')
  171. self.assertTrue(os.path.exists(ticket_json))
  172. tests.create_projects(self.session)
  173. jsondata = None
  174. with open(ticket_json) as stream:
  175. jsondata = json.load(stream)
  176. self.assertNotEqual(jsondata, None)
  177. pagure.lib.git.update_ticket_from_git(
  178. self.session,
  179. reponame='test',
  180. username=None,
  181. issue_uid='foobar',
  182. json_data=jsondata,
  183. )
  184. # Post loading
  185. repo = pagure.lib.get_project(self.session, 'test')
  186. self.assertEqual(len(repo.issues), 1)
  187. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  188. # Check after re-loading
  189. self.assertEqual(len(issue.comments), 2)
  190. self.assertEqual(len(issue.tags), 2)
  191. self.assertEqual(issue.tags_text, ['future', 'feature'])
  192. self.assertEqual(issue.assignee.username, 'pingou')
  193. self.assertEqual(issue.children, [])
  194. self.assertEqual(issue.parents, [])
  195. self.assertTrue(os.path.exists(ticket_json))
  196. os.unlink(ticket_json)
  197. self.assertFalse(os.path.exists(ticket_json))
  198. if __name__ == '__main__':
  199. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  200. PagureFlaskDumpLoadTicketTests)
  201. unittest.TextTestRunner(verbosity=2).run(SUITE)