test_zzz_pagure_flask_ui_old_commit.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - 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. HERE = os.path.join(os.path.dirname(os.path.abspath(__file__)))
  21. CONFIG = os.path.join(HERE, 'test_config')
  22. os.environ['PAGURE_CONFIG'] = CONFIG
  23. import pagure.lib
  24. import tests
  25. from pagure.lib.repo import PagureRepo
  26. class PagureFlaskRepoOldUrltests(tests.Modeltests):
  27. """ Tests for flask app controller of pagure """
  28. def setUp(self):
  29. """ Set up the environnment, ran before every tests. """
  30. super(PagureFlaskRepoOldUrltests, self).setUp()
  31. # We need to reload pagure as otherwise the configuration file will
  32. # not be taken into account
  33. pagure.APP.view_functions = {}
  34. os.environ['PAGURE_CONFIG'] = CONFIG
  35. reload(pagure)
  36. reload(pagure.lib)
  37. reload(pagure.lib.model)
  38. reload(pagure.ui.admin)
  39. reload(pagure.ui.app)
  40. reload(pagure.ui.groups)
  41. reload(pagure.ui.repo)
  42. reload(pagure.ui.filters)
  43. reload(pagure.ui.issues)
  44. reload(pagure.ui.fork)
  45. pagure.APP.config['TESTING'] = True
  46. pagure.SESSION = self.session
  47. pagure.ui.SESSION = self.session
  48. pagure.ui.app.SESSION = self.session
  49. pagure.ui.filters.SESSION = self.session
  50. pagure.ui.repo.SESSION = self.session
  51. pagure.APP.config['OLD_VIEW_COMMIT_ENABLED'] = True
  52. pagure.APP.config['EMAIL_SEND'] = False
  53. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  54. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  55. tests.HERE, 'forks')
  56. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  57. tests.HERE, 'requests')
  58. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  59. tests.HERE, 'tickets')
  60. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  61. tests.HERE, 'docs')
  62. pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join(
  63. tests.HERE, 'releases')
  64. self.app = pagure.APP.test_client()
  65. def tearDown(self):
  66. """ Tear down the environnment, after every tests. """
  67. super(PagureFlaskRepoOldUrltests, self).tearDown()
  68. if 'PAGURE_CONFIG' in os.environ:
  69. del os.environ['PAGURE_CONFIG']
  70. # We need to reload pagure as otherwise the configuration file will
  71. # remain set for the other tests
  72. pagure.APP.view_functions = {}
  73. reload(pagure)
  74. reload(pagure.lib)
  75. reload(pagure.lib.model)
  76. reload(pagure.hooks)
  77. reload(pagure.hooks.mail)
  78. reload(pagure.hooks.irc)
  79. reload(pagure.hooks.fedmsg)
  80. reload(pagure.hooks.pagure_force_commit)
  81. reload(pagure.hooks.pagure_hook)
  82. reload(pagure.hooks.pagure_request_hook)
  83. reload(pagure.hooks.pagure_ticket_hook)
  84. reload(pagure.hooks.rtd)
  85. reload(pagure.ui.admin)
  86. reload(pagure.ui.app)
  87. reload(pagure.ui.groups)
  88. reload(pagure.ui.repo)
  89. reload(pagure.ui.filters)
  90. reload(pagure.ui.plugins)
  91. reload(pagure.ui.issues)
  92. reload(pagure.ui.fork)
  93. pagure.APP.config['EMAIL_SEND'] = False
  94. pagure.LOG.handlers = []
  95. def test_view_commit_old(self):
  96. """ Test the view_commit_old endpoint. """
  97. tests.create_projects(self.session)
  98. tests.create_projects_git(tests.HERE, bare=True)
  99. # Add a README to the git repo - First commit
  100. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  101. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  102. commit = repo.revparse_single('HEAD')
  103. # View first commit
  104. output = self.app.get('/test/%s' % commit.oid.hex)
  105. self.assertEqual(output.status_code, 302)
  106. output = self.app.get(
  107. '/test/%s' % commit.oid.hex, follow_redirects=True)
  108. self.assertEqual(output.status_code, 200)
  109. self.assertTrue(
  110. '<div class="list-group" id="diff_list" style="display:none;">'
  111. in output.data)
  112. self.assertTrue('</a> Authored by Alice Author' in output.data)
  113. self.assertTrue('Committed by Cecil Committer' in output.data)
  114. self.assertTrue(
  115. '<span style="color: #00A000">+ Pagure</span>' in output.data)
  116. self.assertTrue(
  117. '<span style="color: #00A000">+ ======</span>' in output.data)
  118. self.app = pagure.APP.test_client()
  119. # View first commit - with the old URL scheme
  120. output = self.app.get(
  121. '/test/%s' % commit.oid.hex, follow_redirects=True)
  122. self.assertEqual(output.status_code, 200)
  123. self.assertTrue(
  124. '<div class="list-group" id="diff_list" style="display:none;">'
  125. in output.data)
  126. self.assertTrue('</a> Authored by Alice Author' in output.data)
  127. self.assertTrue('Committed by Cecil Committer' in output.data)
  128. self.assertTrue(
  129. '<span style="color: #00A000">+ Pagure</span>' in output.data)
  130. self.assertTrue(
  131. '<span style="color: #00A000">+ ======</span>' in output.data)
  132. # Add some content to the git repo
  133. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  134. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  135. commit = repo.revparse_single('HEAD')
  136. # View another commit
  137. output = self.app.get(
  138. '/test/%s' % commit.oid.hex, follow_redirects=True)
  139. self.assertEqual(output.status_code, 200)
  140. self.assertTrue(
  141. '<div class="list-group" id="diff_list" style="display:none;">'
  142. in output.data)
  143. self.assertTrue('</a> Authored by Alice Author' in output.data)
  144. self.assertTrue('Committed by Cecil Committer' in output.data)
  145. self.assertTrue(
  146. # new version of pygments
  147. '<div class="highlight" style="background: #f8f8f8">'
  148. '<pre style="line-height: 125%">'
  149. '<span></span>'
  150. '<span style="color: #800080; font-weight: bold">'
  151. '@@ -0,0 +1,3 @@</span>' in output.data
  152. or
  153. # old version of pygments
  154. '<div class="highlight" style="background: #f8f8f8">'
  155. '<pre style="line-height: 125%">'
  156. '<span style="color: #800080; font-weight: bold">'
  157. '@@ -0,0 +1,3 @@</span>' in output.data)
  158. # Add a fork of a fork
  159. item = pagure.lib.model.Project(
  160. user_id=1, # pingou
  161. name='test3',
  162. description='test project #3',
  163. parent_id=1,
  164. hook_token='aaabbbkkk',
  165. )
  166. self.session.add(item)
  167. self.session.commit()
  168. forkedgit = os.path.join(
  169. tests.HERE, 'forks', 'pingou', 'test3.git')
  170. tests.add_content_git_repo(forkedgit)
  171. tests.add_readme_git_repo(forkedgit)
  172. repo = pygit2.Repository(forkedgit)
  173. commit = repo.revparse_single('HEAD')
  174. # Commit does not exist in anothe repo :)
  175. output = self.app.get(
  176. '/test/%s' % commit.oid.hex, follow_redirects=True)
  177. self.assertEqual(output.status_code, 404)
  178. # View commit of fork
  179. output = self.app.get(
  180. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  181. self.assertEqual(output.status_code, 200)
  182. self.assertTrue(
  183. '<div class="list-group" id="diff_list" style="display:none;">'
  184. in output.data)
  185. self.assertTrue('</a> Authored by Alice Author' in output.data)
  186. self.assertTrue('Committed by Cecil Committer' in output.data)
  187. self.assertTrue(
  188. '<span style="color: #00A000">+ Pagure</span>' in output.data)
  189. self.assertTrue(
  190. '<span style="color: #00A000">+ ======</span>' in output.data)
  191. # View commit of fork - With the old URL scheme
  192. output = self.app.get(
  193. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  194. self.assertEqual(output.status_code, 200)
  195. self.assertTrue(
  196. '<div class="list-group" id="diff_list" style="display:none;">'
  197. in output.data)
  198. self.assertTrue('</a> Authored by Alice Author' in output.data)
  199. self.assertTrue('Committed by Cecil Committer' in output.data)
  200. self.assertTrue(
  201. '<span style="color: #00A000">+ Pagure</span>' in output.data)
  202. self.assertTrue(
  203. '<span style="color: #00A000">+ ======</span>' in output.data)
  204. # Try the old URL scheme with a short hash
  205. output = self.app.get(
  206. '/fork/pingou/test3/%s' % commit.oid.hex[:10],
  207. follow_redirects=True)
  208. self.assertEqual(output.status_code, 404)
  209. self.assertIn('<p>Project not found</p>', output.data)
  210. if __name__ == '__main__':
  211. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskRepoOldUrltests)
  212. unittest.TextTestRunner(verbosity=2).run(SUITE)