test_pagure_flask_dump_load_ticket.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. def test_dumping_reloading_ticket(self, send_email):
  29. """Test dumping a ticket into a JSON blob."""
  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, issue=issue, assignee="pingou", user="pingou"
  87. )
  88. self.session.commit()
  89. self.assertEqual(msg, "Issue assigned to pingou")
  90. # Add a couple of tags on the ticket
  91. msg = pagure.lib.query.add_tag_obj(
  92. session=self.session,
  93. obj=issue,
  94. tags=[" feature ", "future "],
  95. user="pingou",
  96. )
  97. self.session.commit()
  98. self.assertEqual(msg, "Issue tagged with: feature, future")
  99. # Add dependencies
  100. msg = pagure.lib.query.add_issue_dependency(
  101. session=self.session,
  102. issue=issue,
  103. issue_blocked=issue2,
  104. user="pingou",
  105. )
  106. self.session.commit()
  107. self.assertEqual(msg, "Issue marked as depending on: #2")
  108. msg = pagure.lib.query.add_issue_dependency(
  109. session=self.session, issue=issue3, issue_blocked=issue, user="foo"
  110. )
  111. self.session.commit()
  112. self.assertEqual(msg, "Issue marked as depending on: #1")
  113. # Dump the JSON
  114. pagure.lib.git.update_git(issue, repo).wait()
  115. repo = pygit2.Repository(self.gitrepo)
  116. cnt = len(
  117. [
  118. commit
  119. for commit in repo.walk(
  120. repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL
  121. )
  122. ]
  123. )
  124. self.assertIn(cnt, (9, 10, 11))
  125. last_commit = repo.revparse_single("HEAD")
  126. patch = pagure.lib.git.commit_to_patch(repo, last_commit)
  127. for line in patch.split("\n"):
  128. if line.startswith("--- a/"):
  129. fileid = line.split("--- a/")[1]
  130. break
  131. newpath = tempfile.mkdtemp(prefix="pagure-dump-load")
  132. clone_repo = pygit2.clone_repository(self.gitrepo, newpath)
  133. self.assertEqual(len(os.listdir(newpath)), 4)
  134. ticket_json = os.path.join(self.path, "test_ticket.json")
  135. self.assertFalse(os.path.exists(ticket_json))
  136. shutil.copyfile(os.path.join(newpath, fileid), ticket_json)
  137. self.assertTrue(os.path.exists(ticket_json))
  138. jsondata = None
  139. with open(ticket_json) as stream:
  140. jsondata = json.load(stream)
  141. self.assertNotEqual(jsondata, None)
  142. shutil.rmtree(newpath)
  143. # Test reloading the JSON
  144. # Re-create the DB from scratch
  145. self.session.rollback()
  146. self._clear_database()
  147. self.db_session.close()
  148. del self.session
  149. del self.db_session
  150. os.unlink(os.path.join(self.dbfolder, "db.sqlite"))
  151. self.db_session = pagure.lib.model.create_tables(
  152. self.dbpath,
  153. acls=pagure_config.get("ACLS", {}),
  154. )
  155. self._prepare_db()
  156. tests.create_projects(self.session)
  157. # Create repo
  158. self.gitrepo = os.path.join(self.path, "tickets", "test.git")
  159. repopath = os.path.join(self.path, "tickets")
  160. os.makedirs(self.gitrepo)
  161. pygit2.init_repository(self.gitrepo, bare=True)
  162. pagure.lib.git.update_ticket_from_git(
  163. self.session,
  164. reponame="test",
  165. namespace=None,
  166. username=None,
  167. issue_uid="foobar",
  168. json_data=jsondata,
  169. agent="pingou",
  170. )
  171. # Post loading
  172. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  173. self.assertEqual(len(repo.issues), 1)
  174. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  175. # Check after re-loading
  176. self.assertEqual(len(issue.comments), 3)
  177. self.assertEqual(len(issue.tags), 2)
  178. self.assertEqual(
  179. sorted(issue.tags_text), sorted(["future", "feature"])
  180. )
  181. self.assertEqual(issue.assignee.username, "pingou")
  182. self.assertEqual(issue.children, [])
  183. self.assertEqual(issue.parents, [])
  184. self.assertEqual(issue.status, "Open")
  185. if __name__ == "__main__":
  186. unittest.main(verbosity=2)