test_pagure_flask_docs.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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 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
  20. import tests
  21. from pagure.lib.repo import PagureRepo
  22. class PagureFlaskDocstests(tests.Modeltests):
  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.APP.config['TESTING'] = True
  30. pagure.SESSION = self.session
  31. pagure.ui.SESSION = self.session
  32. pagure.ui.app.SESSION = self.session
  33. pagure.ui.repo.SESSION = self.session
  34. pagure.docs_server.APP.config['GIT_FOLDER'] = tests.HERE
  35. pagure.docs_server.APP.config['FORK_FOLDER'] = os.path.join(
  36. tests.HERE, 'forks')
  37. pagure.docs_server.APP.config['TICKETS_FOLDER'] = os.path.join(
  38. tests.HERE, 'tickets')
  39. pagure.docs_server.APP.config['DOCS_FOLDER'] = os.path.join(
  40. tests.HERE, 'docs')
  41. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  42. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  43. tests.HERE, 'forks')
  44. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  45. tests.HERE, 'tickets')
  46. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  47. tests.HERE, 'docs')
  48. self.app = pagure.docs_server.APP.test_client()
  49. def test_view_docs_no_project(self):
  50. """ Test the view_docs endpoint with no project. """
  51. output = self.app.get('/foo/docs')
  52. self.assertEqual(output.status_code, 404)
  53. def test_view_docs_project_no_git(self):
  54. """ Test the view_docs endpoint with a project that has no
  55. corresponding git repo.
  56. """
  57. tests.create_projects(self.session)
  58. # Turn on the docs project since it's off by default
  59. repo = pagure.lib.get_project(self.session, 'test')
  60. repo.settings = {'project_documentation': True}
  61. self.session.add(repo)
  62. self.session.commit()
  63. output = self.app.get('/test/docs', follow_redirects=True)
  64. self.assertEqual(output.status_code, 404)
  65. self.assertTrue(
  66. '<p>Documentation not found</p>' in output.data)
  67. output = self.app.get('/test', follow_redirects=True)
  68. self.assertEqual(output.status_code, 404)
  69. self.assertTrue(
  70. '<p>Documentation not found</p>' in output.data)
  71. def test_view_docs_project_no_docs(self):
  72. """ Test the view_docs endpoint with a project that disabled the
  73. docs.
  74. """
  75. tests.create_projects(self.session)
  76. repo = pagure.lib.get_project(self.session, 'test')
  77. tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
  78. output = self.app.get('/test/docs')
  79. self.assertEqual(output.status_code, 404)
  80. repo.settings = {'project_documentation': False}
  81. self.session.add(repo)
  82. self.session.commit()
  83. output = self.app.get('/test/docs', follow_redirects=True)
  84. self.assertEqual(output.status_code, 404)
  85. def test_view_docs(self):
  86. """ Test the view_docs endpoint. """
  87. tests.create_projects(self.session)
  88. repo = pygit2.init_repository(
  89. os.path.join(tests.HERE, 'docs', 'test.git'), bare=True)
  90. output = self.app.get('/test/docs')
  91. self.assertEqual(output.status_code, 404)
  92. # forked doc repo
  93. docrepo = os.path.join(tests.HERE, 'docs', 'test', 'test.git')
  94. repo = pygit2.init_repository(docrepo)
  95. # Create files in that git repo
  96. with open(os.path.join(docrepo, 'sources'), 'w') as stream:
  97. stream.write('foo\n bar')
  98. repo.index.add('sources')
  99. repo.index.write()
  100. folderpart = os.path.join(docrepo, 'folder1', 'folder2')
  101. os.makedirs(folderpart)
  102. with open(os.path.join(folderpart, 'test_file'), 'w') as stream:
  103. stream.write('row1\nrow2\nrow3')
  104. repo.index.add(os.path.join('folder1', 'folder2', 'test_file'))
  105. repo.index.write()
  106. # Commits the files added
  107. tree = repo.index.write_tree()
  108. author = pygit2.Signature(
  109. 'Alice Author', 'alice@authors.tld')
  110. committer = pygit2.Signature(
  111. 'Cecil Committer', 'cecil@committers.tld')
  112. repo.create_commit(
  113. 'refs/heads/master', # the name of the reference to update
  114. author,
  115. committer,
  116. 'Add test files and folder',
  117. # binary string representing the tree object ID
  118. tree,
  119. # list of binary strings representing parents of the new commit
  120. []
  121. )
  122. # Push the changes to the bare repo
  123. remote = repo.create_remote(
  124. 'origin', os.path.join(tests.HERE, 'docs', 'test.git'))
  125. PagureRepo.push(remote, 'refs/heads/master:refs/heads/master')
  126. # Turn on the docs project since it's off by default
  127. repo = pagure.lib.get_project(self.session, 'test')
  128. repo.settings = {'project_documentation': True}
  129. self.session.add(repo)
  130. self.session.commit()
  131. # Now check the UI
  132. output = self.app.get('/test/docs')
  133. self.assertEqual(output.status_code, 404)
  134. output = self.app.get('/test/sources')
  135. self.assertEqual(output.status_code, 200)
  136. self.assertEqual('<pre>foo\n bar</pre>', output.data)
  137. output = self.app.get('/test/folder1/folder2')
  138. self.assertEqual(output.status_code, 200)
  139. self.assertTrue(
  140. '<li><ul><a href="test_file">test_file</a></ul></li>'
  141. in output.data)
  142. output = self.app.get('/test/folder1/folder2/test_file')
  143. self.assertEqual(output.status_code, 200)
  144. self.assertEqual('<pre>row1\nrow2\nrow3</pre>', output.data)
  145. output = self.app.get('/test/folder1')
  146. self.assertEqual(output.status_code, 200)
  147. self.assertTrue(
  148. '<li><ul><a href="folder2">folder2/</a></ul></li>'
  149. in output.data)
  150. output = self.app.get('/test/folder1/foo')
  151. self.assertEqual(output.status_code, 404)
  152. output = self.app.get('/test/folder1/foo/folder2')
  153. self.assertEqual(output.status_code, 404)
  154. if __name__ == '__main__':
  155. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskDocstests)
  156. unittest.TextTestRunner(verbosity=2).run(SUITE)