test_pagure_flask_docs.py 8.7 KB

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