test_pagure_flask_ui_old_commit.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.query
  16. import tests
  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. 'href="#commit-overview-collapse"'
  53. in output.get_data(as_text=True))
  54. self.assertTrue('Merged by Alice Author' in output.get_data(as_text=True))
  55. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  56. self.assertTrue('title="View file as of %s"' % commit.oid.hex[0:6]
  57. in output.get_data(as_text=True))
  58. # View first commit - with the old URL scheme
  59. output = self.app.get(
  60. '/test/%s' % commit.oid.hex, follow_redirects=True)
  61. self.assertEqual(output.status_code, 200)
  62. self.assertTrue(
  63. 'href="#commit-overview-collapse"'
  64. in output.get_data(as_text=True))
  65. self.assertTrue('Merged by Alice Author' in output.get_data(as_text=True))
  66. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  67. # Add some content to the git repo
  68. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  69. 'test.git'))
  70. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  71. commit = repo.revparse_single('HEAD')
  72. # View another commit
  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. 'href="#commit-overview-collapse"'
  78. in output.get_data(as_text=True))
  79. self.assertTrue('Authored by Alice Author' in output.get_data(as_text=True))
  80. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  81. # Add a fork of a fork
  82. item = pagure.lib.model.Project(
  83. user_id=1, # pingou
  84. name='test3',
  85. description='test project #3',
  86. is_fork=True,
  87. parent_id=1,
  88. hook_token='aaabbbkkk',
  89. )
  90. self.session.add(item)
  91. self.session.commit()
  92. forkedgit = os.path.join(
  93. self.path, 'repos', 'forks', 'pingou', 'test3.git')
  94. tests.add_content_git_repo(forkedgit)
  95. tests.add_readme_git_repo(forkedgit)
  96. repo = pygit2.Repository(forkedgit)
  97. commit = repo.revparse_single('HEAD')
  98. # Commit does not exist in anothe repo :)
  99. output = self.app.get(
  100. '/test/%s' % commit.oid.hex, follow_redirects=True)
  101. self.assertEqual(output.status_code, 404)
  102. # View commit of fork
  103. output = self.app.get(
  104. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  105. self.assertEqual(output.status_code, 200)
  106. self.assertTrue(
  107. 'href="#commit-overview-collapse"'
  108. in output.get_data(as_text=True))
  109. self.assertTrue(' Authored by Alice Author\n' in output.get_data(as_text=True))
  110. self.assertTrue(' Committed by Cecil Committer\n' in output.get_data(as_text=True))
  111. self.assertTrue('title="View file as of %s"' % commit.oid.hex[0:6]
  112. in output.get_data(as_text=True))
  113. # View commit of fork - With the old URL scheme
  114. output = self.app.get(
  115. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  116. self.assertEqual(output.status_code, 200)
  117. self.assertTrue(
  118. 'href="#commit-overview-collapse"'
  119. in output.get_data(as_text=True))
  120. self.assertTrue('Authored by Alice Author' in output.get_data(as_text=True))
  121. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  122. # Try the old URL scheme with a short hash
  123. output = self.app.get(
  124. '/fork/pingou/test3/%s' % commit.oid.hex[:10],
  125. follow_redirects=True)
  126. self.assertEqual(output.status_code, 404)
  127. self.assertIn('<p>Project not found</p>', output.get_data(as_text=True))
  128. if __name__ == '__main__':
  129. unittest.main(verbosity=2)