test_pagure_flask_dump_load_ticket.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2020 - 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. from pagure.config import config as pagure_config, reload_config
  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.query.get_authorized_project(self.session, "test")
  40. # Create an issue to play with
  41. msg = pagure.lib.query.new_issue(
  42. session=self.session,
  43. repo=repo,
  44. title="Test issue",
  45. content="We should work on this",
  46. user="pingou",
  47. )
  48. self.assertEqual(msg.title, "Test issue")
  49. # Need another two issue to test the dependencie chain
  50. msg = pagure.lib.query.new_issue(
  51. session=self.session,
  52. repo=repo,
  53. title="Test issue #2",
  54. content="Another bug",
  55. user="pingou",
  56. )
  57. self.assertEqual(msg.title, "Test issue #2")
  58. msg = pagure.lib.query.new_issue(
  59. session=self.session,
  60. repo=repo,
  61. title="Test issue #3",
  62. content="That would be nice feature no?",
  63. user="foo",
  64. )
  65. self.assertEqual(msg.title, "Test issue #3")
  66. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  67. issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  68. issue3 = pagure.lib.query.search_issues(self.session, repo, issueid=3)
  69. # Add a couple of comment on the ticket
  70. msg = pagure.lib.query.add_issue_comment(
  71. session=self.session,
  72. issue=issue,
  73. comment="Hey look a comment!",
  74. user="foo",
  75. )
  76. self.session.commit()
  77. self.assertEqual(msg, "Comment added")
  78. msg = pagure.lib.query.add_issue_comment(
  79. session=self.session,
  80. issue=issue,
  81. comment="crazy right?",
  82. user="pingou",
  83. )
  84. self.session.commit()
  85. self.assertEqual(msg, "Comment added")
  86. # Assign the ticket to someone
  87. msg = pagure.lib.query.add_issue_assignee(
  88. session=self.session, issue=issue, assignee="pingou", user="pingou"
  89. )
  90. self.session.commit()
  91. self.assertEqual(msg, "Issue assigned to pingou")
  92. # Add a couple of tags on the ticket
  93. msg = pagure.lib.query.add_tag_obj(
  94. session=self.session,
  95. obj=issue,
  96. tags=[" feature ", "future "],
  97. user="pingou",
  98. )
  99. self.session.commit()
  100. self.assertEqual(msg, "Issue tagged with: feature, future")
  101. # Add dependencies
  102. msg = pagure.lib.query.add_issue_dependency(
  103. session=self.session,
  104. issue=issue,
  105. issue_blocked=issue2,
  106. user="pingou",
  107. )
  108. self.session.commit()
  109. self.assertEqual(msg, "Issue marked as depending on: #2")
  110. msg = pagure.lib.query.add_issue_dependency(
  111. session=self.session, issue=issue3, issue_blocked=issue, user="foo"
  112. )
  113. self.session.commit()
  114. self.assertEqual(msg, "Issue marked as depending on: #1")
  115. # Dump the JSON
  116. pagure.lib.git.update_git(issue, repo).wait()
  117. repo = pygit2.Repository(self.gitrepo)
  118. cnt = len(
  119. [
  120. commit
  121. for commit in repo.walk(
  122. repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL
  123. )
  124. ]
  125. )
  126. self.assertIn(cnt, (9, 10))
  127. last_commit = repo.revparse_single("HEAD")
  128. patch = pagure.lib.git.commit_to_patch(repo, last_commit)
  129. for line in patch.split("\n"):
  130. if line.startswith("--- a/"):
  131. fileid = line.split("--- a/")[1]
  132. break
  133. newpath = tempfile.mkdtemp(prefix="pagure-dump-load")
  134. clone_repo = pygit2.clone_repository(self.gitrepo, newpath)
  135. self.assertEqual(len(os.listdir(newpath)), 4)
  136. ticket_json = os.path.join(self.path, "test_ticket.json")
  137. self.assertFalse(os.path.exists(ticket_json))
  138. shutil.copyfile(os.path.join(newpath, fileid), ticket_json)
  139. self.assertTrue(os.path.exists(ticket_json))
  140. jsondata = None
  141. with open(ticket_json) as stream:
  142. jsondata = json.load(stream)
  143. self.assertNotEqual(jsondata, None)
  144. shutil.rmtree(newpath)
  145. # Test reloading the JSON
  146. # Re-create the DB from scratch
  147. self.session.rollback()
  148. self._clear_database()
  149. self.db_session.close()
  150. del self.session
  151. del self.db_session
  152. os.unlink(os.path.join(self.dbfolder, "db.sqlite"))
  153. self.db_session = pagure.lib.model.create_tables(
  154. self.dbpath, acls=pagure_config.get("ACLS", {}),
  155. )
  156. self._prepare_db()
  157. tests.create_projects(self.session)
  158. # Create repo
  159. self.gitrepo = os.path.join(self.path, "tickets", "test.git")
  160. repopath = os.path.join(self.path, "tickets")
  161. os.makedirs(self.gitrepo)
  162. pygit2.init_repository(self.gitrepo, bare=True)
  163. pagure.lib.git.update_ticket_from_git(
  164. self.session,
  165. reponame="test",
  166. namespace=None,
  167. username=None,
  168. issue_uid="foobar",
  169. json_data=jsondata,
  170. agent="pingou",
  171. )
  172. # Post loading
  173. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  174. self.assertEqual(len(repo.issues), 1)
  175. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  176. # Check after re-loading
  177. self.assertEqual(len(issue.comments), 3)
  178. self.assertEqual(len(issue.tags), 2)
  179. self.assertEqual(
  180. sorted(issue.tags_text), sorted(["future", "feature"])
  181. )
  182. self.assertEqual(issue.assignee.username, "pingou")
  183. self.assertEqual(issue.children, [])
  184. self.assertEqual(issue.parents, [])
  185. self.assertEqual(issue.status, "Open")
  186. if __name__ == "__main__":
  187. unittest.main(verbosity=2)