test_pagure_flask_ui_clone.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Patrick Uiterwijk <puiterwijk@redhat.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import shutil
  11. import sys
  12. import tempfile
  13. import os
  14. import six
  15. import json
  16. import pygit2
  17. from mock import patch, MagicMock
  18. sys.path.insert(
  19. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  20. )
  21. import pagure.lib.query
  22. import tests
  23. class PagureFlaskAppClonetests(tests.Modeltests):
  24. """ Tests for the clone bridging. """
  25. def setUp(self):
  26. super(PagureFlaskAppClonetests, self).setUp()
  27. tests.create_projects(self.session)
  28. tests.create_tokens(self.session)
  29. tests.create_tokens_acl(self.session)
  30. self.create_project_full("clonetest", {"create_readme": "y"})
  31. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": False})
  32. def test_http_clone_disabled(self):
  33. """ Test that the HTTP clone endpoint gets correctly closed. """
  34. output = self.app.get(
  35. "/clonetest.git/info/refs?service=git-upload-pack"
  36. )
  37. self.assertEqual(output.status_code, 403)
  38. self.assertIn("not allowed", output.get_data(as_text=True))
  39. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  40. def test_http_clone_invalid_service(self):
  41. """ Test that the HTTP endpoint refuses invalid services. """
  42. output = self.app.get("/clonetest.git/info/refs?service=myservice")
  43. self.assertEqual(output.status_code, 400)
  44. self.assertIn("Unknown service", output.get_data(as_text=True))
  45. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  46. def test_http_clone_invalid_project(self):
  47. """ Test that the HTTP endpoint refuses invalid projects. """
  48. output = self.app.get(
  49. "/nosuchrepo.git/info/refs?service=git-upload-pack"
  50. )
  51. self.assertEqual(output.status_code, 404)
  52. self.assertIn("Project not found", output.get_data(as_text=True))
  53. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  54. def test_http_clone_dumb(self):
  55. """ Test that the HTTP endpoint refuses dumb service request. """
  56. output = self.app.get("/clonetest.git/info/refs")
  57. self.assertEqual(output.status_code, 400)
  58. self.assertIn("Please switch", output.get_data(as_text=True))
  59. @patch.dict(
  60. "pagure.config.config",
  61. {
  62. "ALLOW_HTTP_PULL_PUSH": True,
  63. "ALLOW_HTTP_PUSH": False,
  64. "HTTP_REPO_ACCESS_GITOLITE": None,
  65. },
  66. )
  67. def test_http_push_disabled(self):
  68. """ Test that the HTTP push gets refused. """
  69. output = self.app.get(
  70. "/clonetest.git/info/refs?service=git-receive-pack"
  71. )
  72. self.assertEqual(output.status_code, 403)
  73. self.assertIn("pushing disabled", output.get_data(as_text=True))
  74. output = self.app.post("/clonetest.git/git-receive-pack")
  75. self.assertEqual(output.status_code, 403)
  76. self.assertIn("pushing disabled", output.get_data(as_text=True))
  77. @patch.dict(
  78. "pagure.config.config",
  79. {
  80. "ALLOW_HTTP_PULL_PUSH": True,
  81. "ALLOW_HTTP_PUSH": True,
  82. "HTTP_REPO_ACCESS_GITOLITE": None,
  83. },
  84. )
  85. def test_http_push_unauthed(self):
  86. """ Test that the HTTP push gets refused unauthed. """
  87. output = self.app.get(
  88. "/clonetest.git/info/refs?service=git-receive-pack"
  89. )
  90. self.assertEqual(output.status_code, 403)
  91. self.assertIn("Unauthenticated push", output.get_data(as_text=True))
  92. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  93. def test_http_clone_private_project_unauthed(self):
  94. """ Test that the HTTP endpoint enforced project.private. """
  95. project = pagure.lib.query._get_project(self.session, "clonetest")
  96. project.private = True
  97. self.session.add(project)
  98. self.session.commit()
  99. output = self.app.get(
  100. "/clonetest.git/info/refs?service=git-upload-pack"
  101. )
  102. self.assertEqual(output.status_code, 404)
  103. self.assertIn("Project not found", output.get_data(as_text=True))
  104. @patch.dict(
  105. "pagure.config.config",
  106. {
  107. "ALLOW_HTTP_PULL_PUSH": True,
  108. "ALLOW_HTTP_PUSH": False,
  109. "HTTP_REPO_ACCESS_GITOLITE": None,
  110. },
  111. )
  112. def test_http_clone(self):
  113. """ Test that HTTP cloning gives reasonable output. """
  114. # Unfortunately, actually testing a git clone would need the app to
  115. # run on a TCP port, which the test environment doesn't do.
  116. output = self.app.get(
  117. "/clonetest.git/info/refs?service=git-upload-pack"
  118. )
  119. self.assertEqual(output.status_code, 200)
  120. output_text = output.get_data(as_text=True)
  121. self.assertIn("# service=git-upload-pack", output_text)
  122. self.assertIn(" refs/heads/master\n0000", output_text)
  123. output = self.app.post(
  124. "/clonetest.git/git-upload-pack",
  125. headers={"Content-Type": "application/x-git-upload-pack-request"},
  126. )
  127. # Git 2.17 returns 415, older return 200
  128. # Either means we didn't fully crash when returning the response
  129. self.assertIn(output.status_code, (200, 415))
  130. @patch.dict(
  131. "pagure.config.config",
  132. {
  133. "ALLOW_HTTP_PULL_PUSH": True,
  134. "ALLOW_HTTP_PUSH": False,
  135. "HTTP_REPO_ACCESS_GITOLITE": None,
  136. },
  137. )
  138. def test_http_clone_private(self):
  139. """ Test that HTTP cloning gives reasonable output with project.private. """
  140. # Unfortunately, actually testing a git clone would need the app to
  141. # run on a TCP port, which the test environment doesn't do.
  142. project = pagure.lib.query._get_project(self.session, "clonetest")
  143. project.private = True
  144. self.session.add(project)
  145. self.session.commit()
  146. output = self.app.get(
  147. "/clonetest.git/info/refs?service=git-upload-pack"
  148. )
  149. self.assertEqual(output.status_code, 404)
  150. self.assertIn("Project not found", output.get_data(as_text=True))
  151. output = self.app.get(
  152. "/clonetest.git/info/refs?service=git-upload-pack",
  153. environ_overrides={"REMOTE_USER": "pingou"},
  154. )
  155. self.assertEqual(output.status_code, 200)
  156. output_text = output.get_data(as_text=True)
  157. self.assertIn("# service=git-upload-pack", output_text)
  158. self.assertIn(" refs/heads/master\n0000", output_text)
  159. @patch.dict(
  160. "pagure.config.config",
  161. {
  162. "ALLOW_HTTP_PULL_PUSH": True,
  163. "ALLOW_HTTP_PUSH": True,
  164. "HTTP_REPO_ACCESS_GITOLITE": None,
  165. },
  166. )
  167. def test_http_push(self):
  168. """ Test that the HTTP push gets accepted. """
  169. output = self.app.get(
  170. "/clonetest.git/info/refs?service=git-receive-pack",
  171. environ_overrides={"REMOTE_USER": "pingou"},
  172. )
  173. self.assertEqual(output.status_code, 200)
  174. output_text = output.get_data(as_text=True)
  175. self.assertIn("# service=git-receive-pack", output_text)
  176. self.assertIn(" refs/heads/master\x00", output_text)