test_pagure_flask_dump_load_ticket.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(
  18. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  19. )
  20. import pagure.lib.query
  21. import tests
  22. class PagureFlaskDumpLoadTicketTests(tests.Modeltests):
  23. """ Tests for flask application for dumping and re-loading the JSON of
  24. a ticket.
  25. """
  26. @patch("pagure.lib.notify.send_email")
  27. @patch("pagure.lib.git._maybe_wait")
  28. def test_dumping_reloading_ticket(self, mw, send_email):
  29. """ Test dumping a ticket into a JSON blob. """
  30. mw.side_effect = lambda result: result.get()
  31. send_email.return_value = True
  32. tests.create_projects(self.session)
  33. # Create repo
  34. self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git")
  35. repopath = os.path.join(self.path, "repos", "tickets")
  36. os.makedirs(self.gitrepo)
  37. repo_obj = pygit2.init_repository(self.gitrepo, bare=True)
  38. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  39. # Create an issue to play with
  40. msg = pagure.lib.query.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.assertEqual(msg.title, "Test issue")
  48. # Need another two issue to test the dependencie chain
  49. msg = pagure.lib.query.new_issue(
  50. session=self.session,
  51. repo=repo,
  52. title="Test issue #2",
  53. content="Another bug",
  54. user="pingou",
  55. )
  56. self.assertEqual(msg.title, "Test issue #2")
  57. msg = pagure.lib.query.new_issue(
  58. session=self.session,
  59. repo=repo,
  60. title="Test issue #3",
  61. content="That would be nice feature no?",
  62. user="foo",
  63. )
  64. self.assertEqual(msg.title, "Test issue #3")
  65. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  66. issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  67. issue3 = pagure.lib.query.search_issues(self.session, repo, issueid=3)
  68. # Add a couple of comment on the ticket
  69. msg = pagure.lib.query.add_issue_comment(
  70. session=self.session,
  71. issue=issue,
  72. comment="Hey look a comment!",
  73. user="foo",
  74. )
  75. self.session.commit()
  76. self.assertEqual(msg, "Comment added")
  77. msg = pagure.lib.query.add_issue_comment(
  78. session=self.session,
  79. issue=issue,
  80. comment="crazy right?",
  81. user="pingou",
  82. )
  83. self.session.commit()
  84. self.assertEqual(msg, "Comment added")
  85. # Assign the ticket to someone
  86. msg = pagure.lib.query.add_issue_assignee(
  87. session=self.session, issue=issue, assignee="pingou", user="pingou"
  88. )
  89. self.session.commit()
  90. self.assertEqual(msg, "Issue assigned to pingou")
  91. # Add a couple of tags on the ticket
  92. msg = pagure.lib.query.add_tag_obj(
  93. session=self.session,
  94. obj=issue,
  95. tags=[" feature ", "future "],
  96. user="pingou",
  97. )
  98. self.session.commit()
  99. self.assertEqual(msg, "Issue tagged with: feature, future")
  100. # Add dependencies
  101. msg = pagure.lib.query.add_issue_dependency(
  102. session=self.session,
  103. issue=issue,
  104. issue_blocked=issue2,
  105. user="pingou",
  106. )
  107. self.session.commit()
  108. self.assertEqual(msg, "Issue marked as depending on: #2")
  109. msg = pagure.lib.query.add_issue_dependency(
  110. session=self.session, issue=issue3, issue_blocked=issue, user="foo"
  111. )
  112. self.session.commit()
  113. self.assertEqual(msg, "Issue marked as depending on: #1")
  114. # Dump the JSON
  115. pagure.lib.git.update_git(issue, repo).wait()
  116. repo = pygit2.Repository(self.gitrepo)
  117. cnt = len(
  118. [
  119. commit
  120. for commit in repo.walk(
  121. repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL
  122. )
  123. ]
  124. )
  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(
  170. sorted(issue.tags_text), sorted(["future", "feature"])
  171. )
  172. self.assertEqual(issue.assignee.username, "pingou")
  173. self.assertEqual(issue.children, [])
  174. self.assertEqual(issue.parents, [])
  175. self.assertEqual(issue.status, "Open")
  176. if __name__ == "__main__":
  177. unittest.main(verbosity=2)