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
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import unittest
  10. import sys
  11. import os
  12. import pygit2
  13. from mock import patch
  14. import pagure.lib
  15. import tests
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  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. @patch.dict('pagure.config.config', {'OLD_VIEW_COMMIT_ENABLED': True})
  27. def test_view_commit_old_with_bogus_url(self):
  28. """ Test the view_commit_old endpoint. """
  29. tests.create_projects(self.session)
  30. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  31. # Add a README to the git repo - First commit
  32. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  33. pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  34. # View first commit
  35. output = self.app.get('/apple-touch-icon-152x152-precomposed.png')
  36. self.assertEqual(output.status_code, 404)
  37. @patch.dict('pagure.config.config', {'OLD_VIEW_COMMIT_ENABLED': True})
  38. def test_view_commit_old(self):
  39. """ Test the view_commit_old endpoint. """
  40. tests.create_projects(self.session)
  41. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  42. # Add a README to the git repo - First commit
  43. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  44. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  45. commit = repo.revparse_single('HEAD')
  46. # View first commit
  47. output = self.app.get('/test/%s' % commit.oid.hex)
  48. self.assertEqual(output.status_code, 302)
  49. output = self.app.get(
  50. '/test/%s' % commit.oid.hex, follow_redirects=True)
  51. self.assertEqual(output.status_code, 200)
  52. self.assertTrue(
  53. 'href="#commit-overview-collapse"'
  54. in output.get_data(as_text=True))
  55. self.assertTrue('Merged by Alice Author' in output.get_data(as_text=True))
  56. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  57. self.assertTrue('title="View file as of %s"' % commit.oid.hex[0:6]
  58. in output.get_data(as_text=True))
  59. # View first commit - with the old URL scheme
  60. output = self.app.get(
  61. '/test/%s' % commit.oid.hex, follow_redirects=True)
  62. self.assertEqual(output.status_code, 200)
  63. self.assertTrue(
  64. 'href="#commit-overview-collapse"'
  65. in output.get_data(as_text=True))
  66. self.assertTrue('Merged by Alice Author' in output.get_data(as_text=True))
  67. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  68. # Add some content to the git repo
  69. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  70. 'test.git'))
  71. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  72. commit = repo.revparse_single('HEAD')
  73. # View another commit
  74. output = self.app.get(
  75. '/test/%s' % commit.oid.hex, follow_redirects=True)
  76. self.assertEqual(output.status_code, 200)
  77. self.assertTrue(
  78. 'href="#commit-overview-collapse"'
  79. in output.get_data(as_text=True))
  80. self.assertTrue('Authored by Alice Author' in output.get_data(as_text=True))
  81. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  82. # Add a fork of a fork
  83. item = pagure.lib.model.Project(
  84. user_id=1, # pingou
  85. name='test3',
  86. description='test project #3',
  87. is_fork=True,
  88. parent_id=1,
  89. hook_token='aaabbbkkk',
  90. )
  91. self.session.add(item)
  92. self.session.commit()
  93. forkedgit = os.path.join(
  94. self.path, 'repos', 'forks', 'pingou', 'test3.git')
  95. tests.add_content_git_repo(forkedgit)
  96. tests.add_readme_git_repo(forkedgit)
  97. repo = pygit2.Repository(forkedgit)
  98. commit = repo.revparse_single('HEAD')
  99. # Commit does not exist in anothe repo :)
  100. output = self.app.get(
  101. '/test/%s' % commit.oid.hex, follow_redirects=True)
  102. self.assertEqual(output.status_code, 404)
  103. # View commit of fork
  104. output = self.app.get(
  105. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  106. self.assertEqual(output.status_code, 200)
  107. self.assertTrue(
  108. 'href="#commit-overview-collapse"'
  109. in output.get_data(as_text=True))
  110. self.assertTrue(' Authored by Alice Author\n' in output.get_data(as_text=True))
  111. self.assertTrue(' Committed by Cecil Committer\n' in output.get_data(as_text=True))
  112. self.assertTrue('title="View file as of %s"' % commit.oid.hex[0:6]
  113. in output.get_data(as_text=True))
  114. # View commit of fork - With the old URL scheme
  115. output = self.app.get(
  116. '/fork/pingou/test3/%s' % commit.oid.hex, follow_redirects=True)
  117. self.assertEqual(output.status_code, 200)
  118. self.assertTrue(
  119. 'href="#commit-overview-collapse"'
  120. in output.get_data(as_text=True))
  121. self.assertTrue('Authored by Alice Author' in output.get_data(as_text=True))
  122. self.assertTrue('Committed by Cecil Committer' in output.get_data(as_text=True))
  123. # Try the old URL scheme with a short hash
  124. output = self.app.get(
  125. '/fork/pingou/test3/%s' % commit.oid.hex[:10],
  126. follow_redirects=True)
  127. self.assertEqual(output.status_code, 404)
  128. self.assertIn('<p>Project not found</p>', output.get_data(as_text=True))
  129. if __name__ == '__main__':
  130. unittest.main(verbosity=2)