test_pagure_flask_ui_pr_no_sources.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - 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, MagicMock
  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.lib.repo import PagureRepo
  23. class PagureFlaskPrNoSourcestests(tests.Modeltests):
  24. """ Tests PR in pagure when the source is gone """
  25. maxDiff = None
  26. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  27. @patch("pagure.lib.notify.fedmsg_publish", MagicMock(return_value=True))
  28. def setUp(self):
  29. """ Set up the environnment, ran before every tests. """
  30. super(PagureFlaskPrNoSourcestests, self).setUp()
  31. tests.create_projects(self.session)
  32. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  33. # Create foo's fork of pingou's test project
  34. item = pagure.lib.model.Project(
  35. user_id=2, # foo
  36. name="test",
  37. description="test project #1",
  38. hook_token="aaabbb",
  39. is_fork=True,
  40. parent_id=1,
  41. )
  42. self.session.add(item)
  43. self.session.commit()
  44. # Create the fork's git repo
  45. repo_path = os.path.join(self.path, "repos", item.path)
  46. pygit2.init_repository(repo_path, bare=True)
  47. project = pagure.lib.query.get_authorized_project(self.session, "test")
  48. fork = pagure.lib.query.get_authorized_project(
  49. self.session, "test", user="foo"
  50. )
  51. self.set_up_git_repo(repo=project, fork=fork)
  52. # Ensure things got setup straight
  53. project = pagure.lib.query.get_authorized_project(self.session, "test")
  54. self.assertEqual(len(project.requests), 1)
  55. # wait for the worker to process the task
  56. path = os.path.join(
  57. self.path, "repos", "test.git", "refs", "pull", "1", "head"
  58. )
  59. self.assertTrue(os.path.exists(path))
  60. def set_up_git_repo(self, repo, fork, branch_from="feature"):
  61. """ Set up the git repo and create the corresponding PullRequest
  62. object.
  63. """
  64. req = tests.add_pull_request_git_repo(
  65. self.path, self.session, repo, fork, branch_from
  66. )
  67. self.assertEqual(req.id, 1)
  68. self.assertEqual(req.title, "PR from the %s branch" % branch_from)
  69. tests.clean_pull_requests_path()
  70. def test_request_pull_reference(self):
  71. """ Test if there is a reference created for a new PR. """
  72. project = pagure.lib.query.get_authorized_project(self.session, "test")
  73. self.assertEqual(len(project.requests), 1)
  74. gitrepo = os.path.join(self.path, "repos", "test.git")
  75. repo = pygit2.Repository(gitrepo)
  76. self.assertEqual(
  77. list(repo.listall_references()),
  78. ["refs/heads/master", "refs/pull/1/head"],
  79. )
  80. def test_request_pull_fork_reference(self):
  81. """ Test if there the references created on the fork. """
  82. project = pagure.lib.query.get_authorized_project(
  83. self.session, "test", user="foo"
  84. )
  85. self.assertEqual(len(project.requests), 0)
  86. gitrepo = os.path.join(self.path, "repos", project.path)
  87. repo = pygit2.Repository(gitrepo)
  88. self.assertEqual(
  89. list(repo.listall_references()), ["refs/heads/feature"]
  90. )
  91. def test_accessing_pr_fork_deleted(self):
  92. """ Test accessing the PR if the fork has been deleted. """
  93. # Delete fork on disk
  94. project = pagure.lib.query.get_authorized_project(
  95. self.session, "test", user="foo"
  96. )
  97. repo_path = os.path.join(self.path, "repos", project.path)
  98. self.assertTrue(os.path.exists(repo_path))
  99. shutil.rmtree(repo_path)
  100. self.assertFalse(os.path.exists(repo_path))
  101. # Delete fork in the DB
  102. self.session.delete(project)
  103. self.session.commit()
  104. # View the pull-request
  105. output2 = self.app.get("/test/pull-request/1")
  106. self.assertEqual(output2.status_code, 200)
  107. def test_accessing_pr_patch_fork_deleted(self):
  108. """ Test accessing the PR's patch if the fork has been deleted. """
  109. # Delete fork on disk
  110. project = pagure.lib.query.get_authorized_project(
  111. self.session, "test", user="foo"
  112. )
  113. repo_path = os.path.join(self.path, "repos", project.path)
  114. self.assertTrue(os.path.exists(repo_path))
  115. shutil.rmtree(repo_path)
  116. self.assertFalse(os.path.exists(repo_path))
  117. # Delete fork in the DB
  118. self.session.delete(project)
  119. self.session.commit()
  120. # View the pull-request
  121. output = self.app.get("/test/pull-request/1.patch")
  122. self.assertEqual(output.status_code, 200)
  123. self.assertIn(
  124. "--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@",
  125. output.get_data(as_text=True),
  126. )
  127. def test_accessing_pr_branch_deleted(self):
  128. """ Test accessing the PR if branch it originates from has been
  129. deleted. """
  130. project = pagure.lib.query.get_authorized_project(
  131. self.session, "test", user="foo"
  132. )
  133. # Check the branches before
  134. gitrepo = os.path.join(self.path, "repos", project.path)
  135. repo = pygit2.Repository(gitrepo)
  136. self.assertEqual(
  137. list(repo.listall_references()), ["refs/heads/feature"]
  138. )
  139. # Delete branch of the fork
  140. user = tests.FakeUser(username="foo")
  141. with tests.user_set(self.app.application, user):
  142. output = self.app.post(
  143. "/fork/foo/test/b/feature/delete", follow_redirects=True
  144. )
  145. self.assertEqual(output.status_code, 200)
  146. # Check the branches after
  147. gitrepo = os.path.join(self.path, "repos", project.path)
  148. repo = pygit2.Repository(gitrepo)
  149. self.assertEqual(list(repo.listall_references()), [])
  150. # View the pull-request
  151. output2 = self.app.get("/test/pull-request/1")
  152. self.assertEqual(output2.status_code, 200)
  153. def test_accessing_pr_patch_branch_deleted(self):
  154. """ Test accessing the PR's patch if branch it originates from has
  155. been deleted. """
  156. project = pagure.lib.query.get_authorized_project(
  157. self.session, "test", user="foo"
  158. )
  159. # Check the branches before
  160. gitrepo = os.path.join(self.path, "repos", project.path)
  161. repo = pygit2.Repository(gitrepo)
  162. self.assertEqual(
  163. list(repo.listall_references()), ["refs/heads/feature"]
  164. )
  165. # Delete branch of the fork
  166. user = tests.FakeUser(username="foo")
  167. with tests.user_set(self.app.application, user):
  168. output = self.app.post(
  169. "/fork/foo/test/b/feature/delete", follow_redirects=True
  170. )
  171. self.assertEqual(output.status_code, 200)
  172. # Check the branches after
  173. gitrepo = os.path.join(self.path, "repos", project.path)
  174. repo = pygit2.Repository(gitrepo)
  175. self.assertEqual(list(repo.listall_references()), [])
  176. # View the pull-request
  177. output = self.app.get("/test/pull-request/1.patch")
  178. self.assertEqual(output.status_code, 200)
  179. self.assertIn(
  180. "--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@",
  181. output.get_data(as_text=True),
  182. )
  183. if __name__ == "__main__":
  184. unittest.main(verbosity=2)