test_pagure_flask_ui_clone.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 base64
  9. import datetime
  10. import unittest
  11. import shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import six
  16. import json
  17. import pygit2
  18. from mock import patch, MagicMock
  19. sys.path.insert(
  20. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  21. )
  22. import pagure.lib.query
  23. import tests
  24. class PagureFlaskAppClonetests(tests.Modeltests):
  25. """Tests for the clone bridging."""
  26. def setUp(self):
  27. super(PagureFlaskAppClonetests, self).setUp()
  28. tests.create_projects(self.session)
  29. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  30. tests.add_content_git_repo(
  31. os.path.join(self.path, "repos", "test.git")
  32. )
  33. tests.create_tokens(self.session)
  34. tests.create_tokens_acl(self.session)
  35. self.create_project_full("clonetest", {"create_readme": "y"})
  36. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": False})
  37. def test_http_clone_disabled(self):
  38. """Test that the HTTP clone endpoint gets correctly closed."""
  39. output = self.app.get(
  40. "/clonetest.git/info/refs?service=git-upload-pack"
  41. )
  42. self.assertEqual(output.status_code, 403)
  43. self.assertIn("not allowed", output.get_data(as_text=True))
  44. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  45. def test_http_clone_invalid_service(self):
  46. """Test that the HTTP endpoint refuses invalid services."""
  47. output = self.app.get("/clonetest.git/info/refs?service=myservice")
  48. self.assertEqual(output.status_code, 400)
  49. self.assertIn("Unknown service", output.get_data(as_text=True))
  50. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  51. def test_http_clone_invalid_project(self):
  52. """Test that the HTTP endpoint refuses invalid projects."""
  53. output = self.app.get(
  54. "/nosuchrepo.git/info/refs?service=git-upload-pack"
  55. )
  56. self.assertEqual(output.status_code, 404)
  57. self.assertIn("Project not found", output.get_data(as_text=True))
  58. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  59. def test_http_clone_dumb(self):
  60. """Test that the HTTP endpoint refuses dumb service request."""
  61. output = self.app.get("/clonetest.git/info/refs")
  62. self.assertEqual(output.status_code, 400)
  63. self.assertIn("Please switch", output.get_data(as_text=True))
  64. @patch.dict(
  65. "pagure.config.config",
  66. {
  67. "ALLOW_HTTP_PULL_PUSH": True,
  68. "ALLOW_HTTP_PUSH": False,
  69. },
  70. )
  71. def test_http_push_disabled(self):
  72. """Test that the HTTP push gets refused."""
  73. output = self.app.get(
  74. "/clonetest.git/info/refs?service=git-receive-pack"
  75. )
  76. self.assertEqual(output.status_code, 403)
  77. self.assertIn("pushing disabled", output.get_data(as_text=True))
  78. output = self.app.post("/clonetest.git/git-receive-pack")
  79. self.assertEqual(output.status_code, 403)
  80. self.assertIn("pushing disabled", output.get_data(as_text=True))
  81. @patch.dict(
  82. "pagure.config.config",
  83. {
  84. "ALLOW_HTTP_PULL_PUSH": True,
  85. "ALLOW_HTTP_PUSH": True,
  86. },
  87. )
  88. def test_http_push_unauthed(self):
  89. """Test that the HTTP push gets refused unauthed."""
  90. output = self.app.get(
  91. "/clonetest.git/info/refs?service=git-receive-pack"
  92. )
  93. self.assertEqual(output.status_code, 401)
  94. self.assertIn("Authorization Required", output.get_data(as_text=True))
  95. @patch.dict("pagure.config.config", {"ALLOW_HTTP_PULL_PUSH": True})
  96. def test_http_clone_private_project_unauthed(self):
  97. """Test that the HTTP endpoint enforced project.private."""
  98. project = pagure.lib.query._get_project(self.session, "clonetest")
  99. project.private = True
  100. self.session.add(project)
  101. self.session.commit()
  102. output = self.app.get(
  103. "/clonetest.git/info/refs?service=git-upload-pack"
  104. )
  105. self.assertEqual(output.status_code, 404)
  106. self.assertIn("Project not found", output.get_data(as_text=True))
  107. @patch.dict(
  108. "pagure.config.config",
  109. {
  110. "ALLOW_HTTP_PULL_PUSH": True,
  111. "ALLOW_HTTP_PUSH": False,
  112. },
  113. )
  114. def test_http_clone(self):
  115. """Test that HTTP cloning gives reasonable output."""
  116. # Unfortunately, actually testing a git clone would need the app to
  117. # run on a TCP port, which the test environment doesn't do.
  118. output = self.app.get(
  119. "/clonetest.git/info/refs?service=git-upload-pack"
  120. )
  121. self.assertEqual(output.status_code, 200)
  122. output_text = output.get_data(as_text=True)
  123. self.assertIn("# service=git-upload-pack", output_text)
  124. self.assertIn(" refs/heads/master\n0000", output_text)
  125. output = self.app.post(
  126. "/clonetest.git/git-upload-pack",
  127. headers={"Content-Type": "application/x-git-upload-pack-request"},
  128. )
  129. # Git 2.17 returns 415, older return 200
  130. # Either means we didn't fully crash when returning the response
  131. self.assertIn(output.status_code, (200, 415))
  132. @patch.dict(
  133. "pagure.config.config",
  134. {
  135. "ALLOW_HTTP_PULL_PUSH": True,
  136. "ALLOW_HTTP_PUSH": False,
  137. },
  138. )
  139. def test_http_clone_private(self):
  140. """Test that HTTP cloning gives reasonable output with project.private."""
  141. # Unfortunately, actually testing a git clone would need the app to
  142. # run on a TCP port, which the test environment doesn't do.
  143. project = pagure.lib.query._get_project(self.session, "clonetest")
  144. project.private = True
  145. self.session.add(project)
  146. self.session.commit()
  147. output = self.app.get(
  148. "/clonetest.git/info/refs?service=git-upload-pack"
  149. )
  150. self.assertEqual(output.status_code, 404)
  151. self.assertIn("Project not found", output.get_data(as_text=True))
  152. output = self.app.get(
  153. "/clonetest.git/info/refs?service=git-upload-pack",
  154. environ_overrides={"REMOTE_USER": "pingou"},
  155. )
  156. self.assertEqual(output.status_code, 200)
  157. output_text = output.get_data(as_text=True)
  158. self.assertIn("# service=git-upload-pack", output_text)
  159. self.assertIn(" refs/heads/master\n0000", output_text)
  160. @patch.dict(
  161. "pagure.config.config",
  162. {
  163. "ALLOW_HTTP_PULL_PUSH": True,
  164. "ALLOW_HTTP_PUSH": True,
  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)
  177. @patch.dict(
  178. "pagure.config.config",
  179. {
  180. "ALLOW_HTTP_PULL_PUSH": True,
  181. "ALLOW_HTTP_PUSH": True,
  182. },
  183. )
  184. def test_http_push_api_token(self):
  185. """Test that the HTTP push gets accepted."""
  186. headers = {
  187. "Authorization": b"Basic %s"
  188. % base64.b64encode(b"pingou:aaabbbcccddd")
  189. }
  190. output = self.app.get(
  191. "/test.git/info/refs?service=git-receive-pack",
  192. headers=headers,
  193. )
  194. self.assertEqual(output.status_code, 200)
  195. output_text = output.get_data(as_text=True)
  196. self.assertIn("# service=git-receive-pack", output_text)
  197. self.assertIn(" refs/heads/master\x00", output_text)
  198. @patch.dict(
  199. "pagure.config.config",
  200. {
  201. "ALLOW_HTTP_PULL_PUSH": True,
  202. "ALLOW_HTTP_PUSH": True,
  203. },
  204. )
  205. def test_http_push_projectless_api_token(self):
  206. """Test that the HTTP push gets accepted."""
  207. tests.create_tokens(self.session, project_id=None, suffix="2")
  208. tests.create_tokens_acl(
  209. self.session, token_id="aaabbbcccddd2", acl_name="commit"
  210. )
  211. headers = {
  212. "Authorization": b"Basic %s"
  213. % base64.b64encode(b"pingou:aaabbbcccddd2")
  214. }
  215. output = self.app.get(
  216. "/clonetest.git/info/refs?service=git-receive-pack",
  217. headers=headers,
  218. )
  219. self.assertEqual(output.status_code, 200)
  220. output_text = output.get_data(as_text=True)
  221. self.assertIn("# service=git-receive-pack", output_text)
  222. self.assertIn(" refs/heads/master\x00", output_text)
  223. @patch.dict(
  224. "pagure.config.config",
  225. {
  226. "ALLOW_HTTP_PULL_PUSH": True,
  227. "ALLOW_HTTP_PUSH": True,
  228. },
  229. )
  230. def test_http_push__invalid_project_for_api_token(self):
  231. """Test that the HTTP push gets accepted."""
  232. headers = {
  233. "Authorization": b"Basic %s"
  234. % base64.b64encode(b"pingou:aaabbbcccddd")
  235. }
  236. output = self.app.get(
  237. "/clonetest.git/info/refs?service=git-receive-pack",
  238. headers=headers,
  239. )
  240. self.assertEqual(output.status_code, 401)
  241. self.assertIn("Authorization Required", output.get_data(as_text=True))
  242. @patch.dict(
  243. "pagure.config.config",
  244. {
  245. "ALLOW_HTTP_PULL_PUSH": True,
  246. "ALLOW_HTTP_PUSH": True,
  247. },
  248. )
  249. def test_http_push_api_token_invalid_user(self):
  250. """Test that the HTTP push gets accepted."""
  251. headers = {
  252. "Authorization": b"Basic %s"
  253. % base64.b64encode(b"invalid:aaabbbcccddd")
  254. }
  255. output = self.app.get(
  256. "/clonetest.git/info/refs?service=git-receive-pack",
  257. headers=headers,
  258. )
  259. self.assertEqual(output.status_code, 401)
  260. self.assertIn("Authorization Required", output.get_data(as_text=True))
  261. @patch.dict(
  262. "pagure.config.config",
  263. {
  264. "ALLOW_HTTP_PULL_PUSH": True,
  265. "ALLOW_HTTP_PUSH": True,
  266. },
  267. )
  268. def test_http_push_invalid_api_token(self):
  269. """Test that the HTTP push gets accepted."""
  270. headers = {
  271. "Authorization": b"Basic %s"
  272. % base64.b64encode(b"pingou:invalid_token")
  273. }
  274. output = self.app.get(
  275. "/clonetest.git/info/refs?service=git-receive-pack",
  276. headers=headers,
  277. )
  278. self.assertEqual(output.status_code, 401)
  279. self.assertIn("Authorization Required", output.get_data(as_text=True))
  280. @patch.dict(
  281. "pagure.config.config",
  282. {
  283. "ALLOW_HTTP_PULL_PUSH": True,
  284. "ALLOW_HTTP_PUSH": True,
  285. },
  286. )
  287. def test_http_push_invalid_acl_on_token(self):
  288. """Test that the HTTP push gets accepted."""
  289. tests.create_tokens(self.session, suffix="2")
  290. tests.create_tokens_acl(
  291. self.session, token_id="aaabbbcccddd2", acl_name="commit_flag"
  292. )
  293. headers = {
  294. "Authorization": b"Basic %s"
  295. % base64.b64encode(b"pingou:aaabbbcccddd2")
  296. }
  297. output = self.app.get(
  298. "/test.git/info/refs?service=git-receive-pack",
  299. headers=headers,
  300. )
  301. self.assertEqual(output.status_code, 401)
  302. self.assertIn("Authorization Required", output.get_data(as_text=True))
  303. @patch.dict(
  304. "pagure.config.config",
  305. {
  306. "ALLOW_HTTP_PULL_PUSH": True,
  307. "ALLOW_HTTP_PUSH": True,
  308. "PAGURE_AUTH": "local",
  309. },
  310. )
  311. def test_http_push_local_auth(self):
  312. """Test that the HTTP push gets accepted."""
  313. headers = {
  314. "Authorization": b"Basic %s" % base64.b64encode(b"pingou:foo")
  315. }
  316. output = self.app.get(
  317. "/clonetest.git/info/refs?service=git-receive-pack",
  318. headers=headers,
  319. )
  320. self.assertEqual(output.status_code, 200)
  321. output_text = output.get_data(as_text=True)
  322. self.assertIn("# service=git-receive-pack", output_text)
  323. self.assertIn(" refs/heads/master\x00", output_text)
  324. @patch.dict(
  325. "pagure.config.config",
  326. {
  327. "ALLOW_HTTP_PULL_PUSH": True,
  328. "ALLOW_HTTP_PUSH": True,
  329. "PAGURE_AUTH": "local",
  330. },
  331. )
  332. def test_http_push_local_auth_invalid_username(self):
  333. """Test that the HTTP push gets accepted."""
  334. headers = {
  335. "Authorization": b"Basic %s" % base64.b64encode(b"invalid:foo")
  336. }
  337. output = self.app.get(
  338. "/clonetest.git/info/refs?service=git-receive-pack",
  339. headers=headers,
  340. )
  341. self.assertEqual(output.status_code, 401)
  342. self.assertIn("Authorization Required", output.get_data(as_text=True))