test_pagure_flask_ui_old_commit.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-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 unittest
  9. import sys
  10. import os
  11. import pygit2
  12. from mock import patch
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.query
  17. import tests
  18. class PagureFlaskRepoOldUrltests(tests.SimplePagureTest):
  19. """Tests for flask app controller of pagure"""
  20. def setUp(self):
  21. """Set up the environnment, ran before every tests."""
  22. super(PagureFlaskRepoOldUrltests, self).setUp()
  23. pagure.config.config["EMAIL_SEND"] = False
  24. pagure.config.config["UPLOAD_FOLDER_PATH"] = os.path.join(
  25. self.path, "releases"
  26. )
  27. @patch.dict("pagure.config.config", {"OLD_VIEW_COMMIT_ENABLED": True})
  28. def test_view_commit_old_with_bogus_url(self):
  29. """Test the view_commit_old endpoint."""
  30. tests.create_projects(self.session)
  31. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  32. # Add a README to the git repo - First commit
  33. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  34. pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  35. # View first commit
  36. output = self.app.get("/apple-touch-icon-152x152-precomposed.png")
  37. self.assertEqual(output.status_code, 404)
  38. @patch.dict("pagure.config.config", {"OLD_VIEW_COMMIT_ENABLED": True})
  39. def test_view_commit_old(self):
  40. """Test the view_commit_old endpoint."""
  41. tests.create_projects(self.session)
  42. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  43. # Add a README to the git repo - First commit
  44. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  45. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  46. commit = repo.revparse_single("HEAD")
  47. # View first commit
  48. output = self.app.get("/test/%s" % commit.oid.hex)
  49. self.assertEqual(output.status_code, 302)
  50. output = self.app.get(
  51. "/test/%s" % commit.oid.hex, follow_redirects=True
  52. )
  53. self.assertEqual(output.status_code, 200)
  54. self.assertTrue(
  55. 'href="#commit-overview-collapse"' in output.get_data(as_text=True)
  56. )
  57. self.assertTrue(
  58. "Merged by Alice Author" in output.get_data(as_text=True)
  59. )
  60. self.assertTrue(
  61. "Committed by Cecil Committer" in output.get_data(as_text=True)
  62. )
  63. self.assertTrue(
  64. 'title="View file as of %s"' % commit.oid.hex[0:7]
  65. in output.get_data(as_text=True)
  66. )
  67. # View first commit - with the old URL scheme
  68. output = self.app.get(
  69. "/test/%s" % commit.oid.hex, follow_redirects=True
  70. )
  71. self.assertEqual(output.status_code, 200)
  72. self.assertTrue(
  73. 'href="#commit-overview-collapse"' in output.get_data(as_text=True)
  74. )
  75. self.assertTrue(
  76. "Merged by Alice Author" in output.get_data(as_text=True)
  77. )
  78. self.assertTrue(
  79. "Committed by Cecil Committer" in output.get_data(as_text=True)
  80. )
  81. # Add some content to the git repo
  82. tests.add_content_git_repo(
  83. os.path.join(self.path, "repos", "test.git")
  84. )
  85. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  86. commit = repo.revparse_single("HEAD")
  87. # View another commit
  88. output = self.app.get(
  89. "/test/%s" % commit.oid.hex, follow_redirects=True
  90. )
  91. self.assertEqual(output.status_code, 200)
  92. self.assertTrue(
  93. 'href="#commit-overview-collapse"' in output.get_data(as_text=True)
  94. )
  95. self.assertTrue(
  96. "Authored by Alice Author" in output.get_data(as_text=True)
  97. )
  98. self.assertTrue(
  99. "Committed by Cecil Committer" in output.get_data(as_text=True)
  100. )
  101. # Add a fork of a fork
  102. item = pagure.lib.model.Project(
  103. user_id=1, # pingou
  104. name="test3",
  105. description="test project #3",
  106. is_fork=True,
  107. parent_id=1,
  108. hook_token="aaabbbkkk",
  109. )
  110. self.session.add(item)
  111. self.session.commit()
  112. forkedgit = os.path.join(
  113. self.path, "repos", "forks", "pingou", "test3.git"
  114. )
  115. tests.add_content_git_repo(forkedgit)
  116. tests.add_readme_git_repo(forkedgit)
  117. repo = pygit2.Repository(forkedgit)
  118. commit = repo.revparse_single("HEAD")
  119. # Commit does not exist in anothe repo :)
  120. output = self.app.get(
  121. "/test/%s" % commit.oid.hex, follow_redirects=True
  122. )
  123. self.assertEqual(output.status_code, 404)
  124. # View commit of fork
  125. output = self.app.get(
  126. "/fork/pingou/test3/%s" % commit.oid.hex, follow_redirects=True
  127. )
  128. self.assertEqual(output.status_code, 200)
  129. self.assertTrue(
  130. 'href="#commit-overview-collapse"' in output.get_data(as_text=True)
  131. )
  132. self.assertTrue(
  133. " Authored by Alice Author\n" in output.get_data(as_text=True)
  134. )
  135. self.assertTrue(
  136. " Committed by Cecil Committer\n" in output.get_data(as_text=True)
  137. )
  138. self.assertTrue(
  139. 'title="View file as of %s"' % commit.oid.hex[0:7]
  140. in output.get_data(as_text=True)
  141. )
  142. # View commit of fork - With the old URL scheme
  143. output = self.app.get(
  144. "/fork/pingou/test3/%s" % commit.oid.hex, follow_redirects=True
  145. )
  146. self.assertEqual(output.status_code, 200)
  147. self.assertTrue(
  148. 'href="#commit-overview-collapse"' in output.get_data(as_text=True)
  149. )
  150. self.assertTrue(
  151. "Authored by Alice Author" in output.get_data(as_text=True)
  152. )
  153. self.assertTrue(
  154. "Committed by Cecil Committer" in output.get_data(as_text=True)
  155. )
  156. # Try the old URL scheme with a short hash
  157. output = self.app.get(
  158. "/fork/pingou/test3/%s" % commit.oid.hex[:10],
  159. follow_redirects=True,
  160. )
  161. self.assertEqual(output.status_code, 404)
  162. self.assertIn(
  163. "<p>Project not found</p>", output.get_data(as_text=True)
  164. )
  165. if __name__ == "__main__":
  166. unittest.main(verbosity=2)