1
0

test_pagure_flask_ui_clone.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib.query
  21. import tests
  22. class PagureFlaskAppClonetests(tests.Modeltests):
  23. """ Tests for the clone bridging. """
  24. def setUp(self):
  25. super(PagureFlaskAppClonetests, self).setUp()
  26. tests.create_projects(self.session)
  27. tests.create_tokens(self.session)
  28. tests.create_tokens_acl(self.session)
  29. self.create_project_full('clonetest', {"create_readme": "y"})
  30. @patch.dict('pagure.config.config', {'ALLOW_HTTP_PULL_PUSH': False})
  31. def test_http_clone_disabled(self):
  32. """ Test that the HTTP clone endpoint gets correctly closed. """
  33. output = self.app.get('/clonetest.git/info/refs?service=git-upload-pack')
  34. self.assertEqual(output.status_code, 403)
  35. self.assertIn('not allowed', output.get_data(as_text=True))
  36. @patch.dict('pagure.config.config', {'ALLOW_HTTP_PULL_PUSH': True})
  37. def test_http_clone_invalid_service(self):
  38. """ Test that the HTTP endpoint refuses invalid services. """
  39. output = self.app.get('/clonetest.git/info/refs?service=myservice')
  40. self.assertEqual(output.status_code, 400)
  41. self.assertIn('Unknown service', output.get_data(as_text=True))
  42. @patch.dict('pagure.config.config', {'ALLOW_HTTP_PULL_PUSH': True})
  43. def test_http_clone_invalid_project(self):
  44. """ Test that the HTTP endpoint refuses invalid projects. """
  45. output = self.app.get('/nosuchrepo.git/info/refs?service=git-upload-pack')
  46. self.assertEqual(output.status_code, 404)
  47. self.assertIn('Project not found', output.get_data(as_text=True))
  48. @patch.dict('pagure.config.config', {'ALLOW_HTTP_PULL_PUSH': True})
  49. def test_http_clone_dumb(self):
  50. """ Test that the HTTP endpoint refuses dumb service request. """
  51. output = self.app.get('/clonetest.git/info/refs')
  52. self.assertEqual(output.status_code, 400)
  53. self.assertIn('Please switch', output.get_data(as_text=True))
  54. @patch.dict('pagure.config.config', {
  55. 'ALLOW_HTTP_PULL_PUSH': True,
  56. 'ALLOW_HTTP_PUSH': False,
  57. 'HTTP_REPO_ACCESS_GITOLITE': None,
  58. })
  59. def test_http_push_disabled(self):
  60. """ Test that the HTTP push gets refused. """
  61. output = self.app.get('/clonetest.git/info/refs?service=git-receive-pack')
  62. self.assertEqual(output.status_code, 403)
  63. self.assertIn('pushing disabled', output.get_data(as_text=True))
  64. output = self.app.post('/clonetest.git/git-receive-pack')
  65. self.assertEqual(output.status_code, 403)
  66. self.assertIn('pushing disabled', output.get_data(as_text=True))
  67. @patch.dict('pagure.config.config', {
  68. 'ALLOW_HTTP_PULL_PUSH': True,
  69. 'ALLOW_HTTP_PUSH': True,
  70. 'HTTP_REPO_ACCESS_GITOLITE': None,
  71. })
  72. def test_http_push_unauthed(self):
  73. """ Test that the HTTP push gets refused unauthed. """
  74. output = self.app.get('/clonetest.git/info/refs?service=git-receive-pack')
  75. self.assertEqual(output.status_code, 403)
  76. self.assertIn('Unauthenticated push', output.get_data(as_text=True))
  77. @patch.dict('pagure.config.config', {'ALLOW_HTTP_PULL_PUSH': True})
  78. def test_http_clone_private_project_unauthed(self):
  79. """ Test that the HTTP endpoint enforced project.private. """
  80. project = pagure.lib.query._get_project(self.session, 'clonetest')
  81. project.private = True
  82. self.session.add(project)
  83. self.session.commit()
  84. output = self.app.get('/clonetest.git/info/refs?service=git-upload-pack')
  85. self.assertEqual(output.status_code, 404)
  86. self.assertIn('Project not found', output.get_data(as_text=True))
  87. @patch.dict('pagure.config.config', {
  88. 'ALLOW_HTTP_PULL_PUSH': True,
  89. 'ALLOW_HTTP_PUSH': False,
  90. 'HTTP_REPO_ACCESS_GITOLITE': None,
  91. })
  92. def test_http_clone(self):
  93. """ Test that HTTP cloning gives reasonable output. """
  94. # Unfortunately, actually testing a git clone would need the app to
  95. # run on a TCP port, which the test environment doesn't do.
  96. output = self.app.get('/clonetest.git/info/refs?service=git-upload-pack')
  97. self.assertEqual(output.status_code, 200)
  98. output_text = output.get_data(as_text=True)
  99. self.assertIn("# service=git-upload-pack", output_text)
  100. self.assertIn(" refs/heads/master\n0000", output_text)
  101. output = self.app.post(
  102. '/clonetest.git/git-upload-pack',
  103. headers={'Content-Type': 'application/x-git-upload-pack-request'},
  104. )
  105. # Git 2.17 returns 415, older return 200
  106. # Either means we didn't fully crash when returning the response
  107. self.assertIn(output.status_code, (200, 415))
  108. @patch.dict('pagure.config.config', {
  109. 'ALLOW_HTTP_PULL_PUSH': True,
  110. 'ALLOW_HTTP_PUSH': False,
  111. 'HTTP_REPO_ACCESS_GITOLITE': None,
  112. })
  113. def test_http_clone_private(self):
  114. """ Test that HTTP cloning gives reasonable output with project.private. """
  115. # Unfortunately, actually testing a git clone would need the app to
  116. # run on a TCP port, which the test environment doesn't do.
  117. project = pagure.lib.query._get_project(self.session, 'clonetest')
  118. project.private = True
  119. self.session.add(project)
  120. self.session.commit()
  121. output = self.app.get('/clonetest.git/info/refs?service=git-upload-pack')
  122. self.assertEqual(output.status_code, 404)
  123. self.assertIn('Project not found', output.get_data(as_text=True))
  124. output = self.app.get(
  125. '/clonetest.git/info/refs?service=git-upload-pack',
  126. environ_overrides={'REMOTE_USER': 'pingou'},
  127. )
  128. self.assertEqual(output.status_code, 200)
  129. output_text = output.get_data(as_text=True)
  130. self.assertIn("# service=git-upload-pack", output_text)
  131. self.assertIn(" refs/heads/master\n0000", output_text)
  132. @patch.dict('pagure.config.config', {
  133. 'ALLOW_HTTP_PULL_PUSH': True,
  134. 'ALLOW_HTTP_PUSH': True,
  135. 'HTTP_REPO_ACCESS_GITOLITE': None,
  136. })
  137. def test_http_push(self):
  138. """ Test that the HTTP push gets accepted. """
  139. output = self.app.get(
  140. '/clonetest.git/info/refs?service=git-receive-pack',
  141. environ_overrides={'REMOTE_USER': 'pingou'},
  142. )
  143. self.assertEqual(output.status_code, 200)
  144. output_text = output.get_data(as_text=True)
  145. self.assertIn("# service=git-receive-pack", output_text)
  146. self.assertIn(" refs/heads/master\x00", output_text)