test_pagure_flask_ui_old_commit.py 8.6 KB

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