test_pagure_flask_ui_old_commit.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 pkg_resources
  9. import datetime
  10. import json
  11. import unittest
  12. import shutil
  13. import sys
  14. import tempfile
  15. import os
  16. import pygit2
  17. from mock import patch
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. from pagure.lib.repo import PagureRepo
  23. class PagureFlaskRepoOldUrltests(tests.Modeltests):
  24. """ Tests for flask app controller of pagure """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskRepoOldUrltests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.ui.SESSION = self.session
  31. pagure.ui.app.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.ui.repo.SESSION = self.session
  34. pagure.APP.config['OLD_VIEW_COMMIT_ENABLED'] = True
  35. pagure.APP.config['EMAIL_SEND'] = False
  36. pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join(
  37. self.path, 'releases')
  38. self.app = pagure.APP.test_client()
  39. def tearDown(self):
  40. """ Tear down the environnment, after every tests. """
  41. super(PagureFlaskRepoOldUrltests, self).tearDown()
  42. pagure.APP.config['EMAIL_SEND'] = False
  43. pagure.APP.config['OLD_VIEW_COMMIT_ENABLED'] = False
  44. def test_view_commit_old(self):
  45. """ Test the view_commit_old endpoint. """
  46. tests.create_projects(self.session)
  47. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  48. # Add a README to the git repo - First commit
  49. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  50. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  51. commit = repo.revparse_single('HEAD')
  52. # View first commit
  53. output = self.app.get('/test/%s' % commit.oid.hex)
  54. self.assertEqual(output.status_code, 302)
  55. output = self.app.get(
  56. '/test/%s' % commit.oid.hex, follow_redirects=True)
  57. self.assertEqual(output.status_code, 200)
  58. self.assertTrue(
  59. '<div class="list-group" id="diff_list" style="display:none;">'
  60. in output.data)
  61. self.assertTrue(' Merged by Alice Author\n' in output.data)
  62. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  63. self.assertTrue(
  64. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px">' +
  65. ' 2 </span><span style="color: #00A000; background-color: ' +
  66. '#ddffdd">+ Pagure</span>' in output.data)
  67. self.assertTrue(
  68. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px">' +
  69. ' 3 </span><span style="color: #00A000; background-color: ' +
  70. '#ddffdd">+ ======</span>' in output.data)
  71. self.app = pagure.APP.test_client()
  72. # View first commit - with the old URL scheme
  73. output = self.app.get(
  74. '/test/%s' % commit.oid.hex, follow_redirects=True)
  75. self.assertEqual(output.status_code, 200)
  76. self.assertTrue(
  77. '<div class="list-group" id="diff_list" style="display:none;">'
  78. in output.data)
  79. self.assertTrue(' Merged by Alice Author\n' in output.data)
  80. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  81. self.assertTrue(
  82. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px"> 2' +
  83. ' </span><span style="color: #00A000; background-color: #ddffdd">' +
  84. '+ Pagure</span>' in output.data)
  85. self.assertTrue(
  86. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px">' +
  87. ' 3 </span><span style="color: #00A000; background-color: ' +
  88. '#ddffdd">+ ======</span>' in output.data)
  89. # Add some content to the git repo
  90. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  91. 'test.git'))
  92. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  93. commit = repo.revparse_single('HEAD')
  94. # View another commit
  95. output = self.app.get(
  96. '/test/%s' % commit.oid.hex, follow_redirects=True)
  97. self.assertEqual(output.status_code, 200)
  98. self.assertTrue(
  99. '<div class="list-group" id="diff_list" style="display:none;">'
  100. in output.data)
  101. self.assertTrue(' Authored by Alice Author\n' in output.data)
  102. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  103. self.assertTrue(
  104. # new version of pygments
  105. '<div class="highlight" style="background: #f8f8f8"><pre style' +
  106. '="line-height: 125%"><span></span><span style="background-color' +
  107. ': #f0f0f0; padding: 0 5px 0 5px">1 </span><span style="color: ' +
  108. '#800080; font-weight: bold">@@ -0,0 +1,3 @@</span>' in
  109. output.data
  110. or
  111. # old version of pygments
  112. '<div class="highlight" style="background: #f8f8f8">' +
  113. '<pre style="line-height: 125%">' +
  114. '<span style="color: #800080; font-weight: bold">' +
  115. '@@ -0,0 +1,3 @@</span>' in output.data)
  116. # Add a fork of a fork
  117. item = pagure.lib.model.Project(
  118. user_id=1, # pingou
  119. name='test3',
  120. description='test project #3',
  121. is_fork=True,
  122. parent_id=1,
  123. hook_token='aaabbbkkk',
  124. )
  125. self.session.add(item)
  126. self.session.commit()
  127. forkedgit = os.path.join(
  128. self.path, 'repos', 'forks', 'pingou', 'test3.git')
  129. tests.add_content_git_repo(forkedgit)
  130. tests.add_readme_git_repo(forkedgit)
  131. repo = pygit2.Repository(forkedgit)
  132. commit = repo.revparse_single('HEAD')
  133. # Commit does not exist in anothe repo :)
  134. output = self.app.get(
  135. '/test/%s' % commit.oid.hex, follow_redirects=True)
  136. self.assertEqual(output.status_code, 404)
  137. # View commit of fork
  138. output = self.app.get(
  139. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  140. self.assertEqual(output.status_code, 200)
  141. self.assertTrue(
  142. '<div class="list-group" id="diff_list" style="display:none;">'
  143. in output.data)
  144. self.assertTrue(' Authored by Alice Author\n' in output.data)
  145. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  146. self.assertTrue(
  147. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px"> 2' +
  148. ' </span><span style="color: #00A000; background-color: #ddffdd">' +
  149. '+ Pagure</span>' in output.data)
  150. self.assertTrue(
  151. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px"> 3' +
  152. ' </span><span style="color: #00A000; background-color: #ddffdd">' +
  153. '+ ======</span>' 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)