test_pagure_flask_docs.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  19. import pagure.docs_server
  20. import pagure.lib.query
  21. import tests
  22. from pagure.lib.repo import PagureRepo
  23. class PagureFlaskDocstests(tests.SimplePagureTest):
  24. """ Tests for flask docs of pagure """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskDocstests, self).setUp()
  28. pagure.docs_server.APP.config["TESTING"] = True
  29. pagure.docs_server.SESSION = self.session
  30. pagure.docs_server.APP.config["GIT_FOLDER"] = os.path.join(
  31. self.path, "repos"
  32. )
  33. pagure.docs_server.APP.config["TICKETS_FOLDER"] = os.path.join(
  34. self.path, "tickets"
  35. )
  36. pagure.docs_server.APP.config["DOCS_FOLDER"] = os.path.join(
  37. self.path, "repos", "docs"
  38. )
  39. self.app = pagure.docs_server.APP.test_client()
  40. def _set_up_doc(self):
  41. # forked doc repo
  42. docrepo = os.path.join(self.path, "repos", "docs", "test", "test.git")
  43. repo = pygit2.init_repository(docrepo)
  44. # Create files in that git repo
  45. with open(os.path.join(docrepo, "sources"), "w") as stream:
  46. stream.write("foo\n bar")
  47. repo.index.add("sources")
  48. repo.index.write()
  49. folderpart = os.path.join(docrepo, "folder1", "folder2")
  50. os.makedirs(folderpart)
  51. with open(os.path.join(folderpart, "test_file"), "w") as stream:
  52. stream.write("row1\nrow2\nrow3")
  53. repo.index.add(os.path.join("folder1", "folder2", "test_file"))
  54. repo.index.write()
  55. # Commits the files added
  56. tree = repo.index.write_tree()
  57. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  58. committer = pygit2.Signature("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. )
  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.query.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.query.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. )
  98. output = self.app.get("/test", follow_redirects=True)
  99. self.assertEqual(output.status_code, 404)
  100. self.assertTrue(
  101. "<p>Documentation not found</p>" in output.get_data(as_text=True)
  102. )
  103. def test_view_docs_project_no_docs(self):
  104. """ Test the view_docs endpoint with a project that disabled the
  105. docs.
  106. """
  107. tests.create_projects(self.session)
  108. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  109. tests.create_projects_git(os.path.join(self.path, "repos", "docs"))
  110. output = self.app.get("/test/docs")
  111. self.assertEqual(output.status_code, 404)
  112. repo.settings = {"project_documentation": False}
  113. self.session.add(repo)
  114. self.session.commit()
  115. output = self.app.get("/test/docs", follow_redirects=True)
  116. self.assertEqual(output.status_code, 404)
  117. def test_view_docs_empty_repo(self):
  118. """ Test the view_docs endpoint when the git repo is empty. """
  119. tests.create_projects(self.session)
  120. repo = pygit2.init_repository(
  121. os.path.join(self.path, "repos", "docs", "test.git"), bare=True
  122. )
  123. # Turn on the docs project since it's off by default
  124. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  125. repo.settings = {"project_documentation": True}
  126. self.session.add(repo)
  127. self.session.commit()
  128. output = self.app.get("/test/docs")
  129. self.assertEqual(output.status_code, 404)
  130. self.assertIn(
  131. "<p>No content found in the repository, you may want to read "
  132. 'the <a href="https://docs.pagure.org/pagure/usage/'
  133. 'using_doc.html">Using the doc repository of your project</a> '
  134. "documentation.</p>",
  135. output.get_data(as_text=True),
  136. )
  137. def test_view_docs(self):
  138. """ Test the view_docs endpoint. """
  139. tests.create_projects(self.session)
  140. repo = pygit2.init_repository(
  141. os.path.join(self.path, "repos", "docs", "test.git"), bare=True
  142. )
  143. output = self.app.get("/test/docs")
  144. self.assertEqual(output.status_code, 404)
  145. self._set_up_doc()
  146. # Now check the UI
  147. output = self.app.get("/test/docs")
  148. self.assertEqual(output.status_code, 404)
  149. output = self.app.get("/test/sources")
  150. self.assertEqual(output.status_code, 200)
  151. self.assertEqual("<pre>foo\n bar</pre>", output.get_data(as_text=True))
  152. output = self.app.get("/test/folder1/folder2")
  153. self.assertEqual(output.status_code, 200)
  154. self.assertTrue(
  155. '<li><ul><a href="test_file">test_file</a></ul></li>'
  156. in output.get_data(as_text=True)
  157. )
  158. output = self.app.get("/test/folder1/folder2/test_file")
  159. self.assertEqual(output.status_code, 200)
  160. self.assertEqual(
  161. "<pre>row1\nrow2\nrow3</pre>", output.get_data(as_text=True)
  162. )
  163. output = self.app.get("/test/folder1")
  164. self.assertEqual(output.status_code, 200)
  165. self.assertTrue(
  166. '<li><ul><a href="folder2/">folder2/</a></ul></li>'
  167. in output.get_data(as_text=True)
  168. )
  169. output = self.app.get("/test/folder1/foo")
  170. self.assertEqual(output.status_code, 404)
  171. output = self.app.get("/test/folder1/foo/folder2")
  172. self.assertEqual(output.status_code, 404)
  173. @mock.patch(
  174. "pagure.lib.encoding_utils.decode",
  175. mock.MagicMock(side_effect=pagure.exceptions.PagureEncodingException),
  176. )
  177. def test_view_docs_encoding_error(self):
  178. """ Test viewing a file of which we cannot find the encoding. """
  179. tests.create_projects(self.session)
  180. repo = pygit2.init_repository(
  181. os.path.join(self.path, "repos", "docs", "test.git"), bare=True
  182. )
  183. output = self.app.get("/test/docs")
  184. self.assertEqual(output.status_code, 404)
  185. self._set_up_doc()
  186. output = self.app.get("/test/sources")
  187. self.assertEqual(output.status_code, 200)
  188. self.assertEqual("foo\n bar", output.get_data(as_text=True))
  189. output = self.app.get("/test/folder1")
  190. self.assertEqual(output.status_code, 200)
  191. self.assertTrue(
  192. '<li><ul><a href="folder2/">folder2/</a></ul></li>'
  193. in output.get_data(as_text=True)
  194. )
  195. @mock.patch(
  196. "pagure.lib.encoding_utils.decode", mock.MagicMock(side_effect=IOError)
  197. )
  198. def test_view_docs_unknown_error(self):
  199. """ Test viewing a file of which we cannot find the encoding. """
  200. tests.create_projects(self.session)
  201. repo = pygit2.init_repository(
  202. os.path.join(self.path, "repos", "docs", "test.git"), bare=True
  203. )
  204. output = self.app.get("/test/docs")
  205. self.assertEqual(output.status_code, 404)
  206. self._set_up_doc()
  207. output = self.app.get("/test/sources")
  208. self.assertEqual(output.status_code, 500)
  209. output = self.app.get("/test/folder1")
  210. self.assertEqual(output.status_code, 200)
  211. if __name__ == "__main__":
  212. unittest.main(verbosity=2)