test_pagure_flask_docs.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-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 json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import mock
  15. import pygit2
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure
  20. import pagure.docs_server
  21. import pagure.lib
  22. import tests
  23. from pagure.lib.repo import PagureRepo
  24. class PagureFlaskDocstests(tests.Modeltests):
  25. """ Tests for flask docs of pagure """
  26. def setUp(self):
  27. """ Set up the environnment, ran before every tests. """
  28. super(PagureFlaskDocstests, self).setUp()
  29. pagure.docs_server.APP.config['TESTING'] = True
  30. pagure.docs_server.SESSION = self.session
  31. pagure.APP.config['TESTING'] = True
  32. pagure.SESSION = self.session
  33. pagure.ui.SESSION = self.session
  34. pagure.ui.app.SESSION = self.session
  35. pagure.ui.repo.SESSION = self.session
  36. pagure.docs_server.APP.config['GIT_FOLDER'] = os.path.join(
  37. self.path, 'repos')
  38. pagure.docs_server.APP.config['TICKETS_FOLDER'] = os.path.join(
  39. self.path, 'tickets')
  40. pagure.docs_server.APP.config['DOCS_FOLDER'] = os.path.join(
  41. self.path, 'docs')
  42. self.app = pagure.docs_server.APP.test_client()
  43. def _set_up_doc(self):
  44. # forked doc repo
  45. docrepo = os.path.join(self.path, 'docs', 'test', 'test.git')
  46. repo = pygit2.init_repository(docrepo)
  47. # Create files in that git repo
  48. with open(os.path.join(docrepo, 'sources'), 'w') as stream:
  49. stream.write('foo\n bar')
  50. repo.index.add('sources')
  51. repo.index.write()
  52. folderpart = os.path.join(docrepo, 'folder1', 'folder2')
  53. os.makedirs(folderpart)
  54. with open(os.path.join(folderpart, 'test_file'), 'w') as stream:
  55. stream.write('row1\nrow2\nrow3')
  56. repo.index.add(os.path.join('folder1', 'folder2', 'test_file'))
  57. repo.index.write()
  58. # Commits the files added
  59. tree = repo.index.write_tree()
  60. author = pygit2.Signature(
  61. 'Alice Author', 'alice@authors.tld')
  62. committer = pygit2.Signature(
  63. 'Cecil Committer', 'cecil@committers.tld')
  64. repo.create_commit(
  65. 'refs/heads/master', # the name of the reference to update
  66. author,
  67. committer,
  68. 'Add test files and folder',
  69. # binary string representing the tree object ID
  70. tree,
  71. # list of binary strings representing parents of the new commit
  72. []
  73. )
  74. # Push the changes to the bare repo
  75. remote = repo.create_remote(
  76. 'origin', os.path.join(self.path, 'docs', 'test.git'))
  77. PagureRepo.push(remote, 'refs/heads/master:refs/heads/master')
  78. # Turn on the docs project since it's off by default
  79. repo = pagure.get_authorized_project(self.session, 'test')
  80. repo.settings = {'project_documentation': True}
  81. self.session.add(repo)
  82. self.session.commit()
  83. def test_view_docs_no_project(self):
  84. """ Test the view_docs endpoint with no project. """
  85. output = self.app.get('/foo/docs')
  86. self.assertEqual(output.status_code, 404)
  87. def test_view_docs_project_no_git(self):
  88. """ Test the view_docs endpoint with a project that has no
  89. corresponding git repo.
  90. """
  91. tests.create_projects(self.session)
  92. # Turn on the docs project since it's off by default
  93. repo = pagure.get_authorized_project(self.session, 'test')
  94. repo.settings = {'project_documentation': True}
  95. self.session.add(repo)
  96. self.session.commit()
  97. output = self.app.get('/test/docs', follow_redirects=True)
  98. self.assertEqual(output.status_code, 404)
  99. self.assertTrue(
  100. '<p>Documentation not found</p>' in output.data)
  101. output = self.app.get('/test', follow_redirects=True)
  102. self.assertEqual(output.status_code, 404)
  103. self.assertTrue(
  104. '<p>Documentation not found</p>' in output.data)
  105. def test_view_docs_project_no_docs(self):
  106. """ Test the view_docs endpoint with a project that disabled the
  107. docs.
  108. """
  109. tests.create_projects(self.session)
  110. repo = pagure.get_authorized_project(self.session, 'test')
  111. tests.create_projects_git(os.path.join(self.path, 'docs'))
  112. output = self.app.get('/test/docs')
  113. self.assertEqual(output.status_code, 404)
  114. repo.settings = {'project_documentation': False}
  115. self.session.add(repo)
  116. self.session.commit()
  117. output = self.app.get('/test/docs', follow_redirects=True)
  118. self.assertEqual(output.status_code, 404)
  119. def test_view_docs_empty_repo(self):
  120. """ Test the view_docs endpoint when the git repo is empty. """
  121. tests.create_projects(self.session)
  122. repo = pygit2.init_repository(
  123. os.path.join(self.path, 'docs', 'test.git'), bare=True)
  124. # Turn on the docs project since it's off by default
  125. repo = pagure.get_authorized_project(self.session, 'test')
  126. repo.settings = {'project_documentation': True}
  127. self.session.add(repo)
  128. self.session.commit()
  129. output = self.app.get('/test/docs')
  130. self.assertEqual(output.status_code, 404)
  131. self.assertIn(
  132. '<p>No content found is the repository, you may want to read '
  133. 'the <a href="https://docs.pagure.org/pagure/usage/'
  134. 'using_doc.html">Using the doc repository of your project</a> '
  135. 'documentation</p>', output.data)
  136. def test_view_docs(self):
  137. """ Test the view_docs endpoint. """
  138. tests.create_projects(self.session)
  139. repo = pygit2.init_repository(
  140. os.path.join(self.path, 'docs', 'test.git'), bare=True)
  141. output = self.app.get('/test/docs')
  142. self.assertEqual(output.status_code, 404)
  143. self._set_up_doc()
  144. # Now check the UI
  145. output = self.app.get('/test/docs')
  146. self.assertEqual(output.status_code, 404)
  147. output = self.app.get('/test/sources')
  148. self.assertEqual(output.status_code, 200)
  149. self.assertEqual('<pre>foo\n bar</pre>', output.data)
  150. output = self.app.get('/test/folder1/folder2')
  151. self.assertEqual(output.status_code, 200)
  152. self.assertTrue(
  153. '<li><ul><a href="test_file">test_file</a></ul></li>'
  154. in output.data)
  155. output = self.app.get('/test/folder1/folder2/test_file')
  156. self.assertEqual(output.status_code, 200)
  157. self.assertEqual('<pre>row1\nrow2\nrow3</pre>', output.data)
  158. output = self.app.get('/test/folder1')
  159. self.assertEqual(output.status_code, 200)
  160. self.assertTrue(
  161. '<li><ul><a href="folder2/">folder2/</a></ul></li>'
  162. in output.data)
  163. output = self.app.get('/test/folder1/foo')
  164. self.assertEqual(output.status_code, 404)
  165. output = self.app.get('/test/folder1/foo/folder2')
  166. self.assertEqual(output.status_code, 404)
  167. @mock.patch(
  168. 'pagure.lib.encoding_utils.decode',
  169. mock.MagicMock(side_effect=pagure.exceptions.PagureEncodingException))
  170. def test_view_docs_encoding_error(self):
  171. """ Test viewing a file of which we cannot find the encoding. """
  172. tests.create_projects(self.session)
  173. repo = pygit2.init_repository(
  174. os.path.join(self.path, 'docs', 'test.git'), bare=True)
  175. output = self.app.get('/test/docs')
  176. self.assertEqual(output.status_code, 404)
  177. self._set_up_doc()
  178. output = self.app.get('/test/sources')
  179. self.assertEqual(output.status_code, 200)
  180. self.assertEqual('foo\n bar', output.data)
  181. output = self.app.get('/test/folder1')
  182. self.assertEqual(output.status_code, 200)
  183. self.assertTrue(
  184. '<li><ul><a href="folder2/">folder2/</a></ul></li>'
  185. in output.data)
  186. @mock.patch(
  187. 'pagure.lib.encoding_utils.decode',
  188. mock.MagicMock(side_effect=IOError))
  189. def test_view_docs_unknown_error(self):
  190. """ Test viewing a file of which we cannot find the encoding. """
  191. tests.create_projects(self.session)
  192. repo = pygit2.init_repository(
  193. os.path.join(self.path, 'docs', 'test.git'), bare=True)
  194. output = self.app.get('/test/docs')
  195. self.assertEqual(output.status_code, 404)
  196. self._set_up_doc()
  197. output = self.app.get('/test/sources')
  198. self.assertEqual(output.status_code, 500)
  199. output = self.app.get('/test/folder1')
  200. self.assertEqual(output.status_code, 200)
  201. if __name__ == '__main__':
  202. unittest.main(verbosity=2)