test_pagure_flask_docs.py 8.8 KB

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