test_pagure_flask_docs.py 8.8 KB

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