test_pagure_flask_ui_slash_branch_name.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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 tempfile
  13. import os
  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.lib.query
  20. import tests
  21. from pagure.lib.repo import PagureRepo
  22. class PagureFlaskSlashInBranchtests(tests.SimplePagureTest):
  23. """Tests for flask application when the branch name contains a '/'."""
  24. def set_up_git_repo(self):
  25. """Set up the git repo to play with."""
  26. # Create a git repo to play with
  27. gitrepo = os.path.join(self.path, "repos", "test.git")
  28. repo = pygit2.init_repository(gitrepo, bare=True)
  29. newpath = tempfile.mkdtemp(prefix="pagure-other-test")
  30. repopath = os.path.join(newpath, "test")
  31. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  32. # Create a file in that git repo
  33. with open(os.path.join(repopath, "sources"), "w") as stream:
  34. stream.write("foo\n bar")
  35. clone_repo.index.add("sources")
  36. clone_repo.index.write()
  37. # Commits the files added
  38. tree = clone_repo.index.write_tree()
  39. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  40. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  41. clone_repo.create_commit(
  42. "refs/heads/master", # the name of the reference to update
  43. author,
  44. committer,
  45. "Add sources file for testing",
  46. # binary string representing the tree object ID
  47. tree,
  48. # list of binary strings representing parents of the new commit
  49. [],
  50. )
  51. refname = "refs/heads/master"
  52. ori_remote = clone_repo.remotes[0]
  53. PagureRepo.push(ori_remote, refname)
  54. master_branch = clone_repo.lookup_branch("master")
  55. first_commit = master_branch.peel().hex
  56. # Second commit
  57. with open(os.path.join(repopath, ".gitignore"), "w") as stream:
  58. stream.write("*~")
  59. clone_repo.index.add(".gitignore")
  60. clone_repo.index.write()
  61. tree = clone_repo.index.write_tree()
  62. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  63. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  64. clone_repo.create_commit(
  65. "refs/heads/maxamilion/feature",
  66. author,
  67. committer,
  68. "Add .gitignore file for testing",
  69. # binary string representing the tree object ID
  70. tree,
  71. # list of binary strings representing parents of the new commit
  72. [first_commit],
  73. )
  74. refname = "refs/heads/maxamilion/feature"
  75. ori_remote = clone_repo.remotes[0]
  76. PagureRepo.push(ori_remote, refname)
  77. shutil.rmtree(newpath)
  78. @patch("pagure.lib.notify.send_email")
  79. def test_view_repo(self, send_email):
  80. """Test the view_repo endpoint when the git repo has no master
  81. branch.
  82. """
  83. send_email.return_value = True
  84. tests.create_projects(self.session)
  85. # Non-existant git repo
  86. output = self.app.get("/test")
  87. self.assertEqual(output.status_code, 404)
  88. self.set_up_git_repo()
  89. # With git repo
  90. output = self.app.get("/test")
  91. self.assertEqual(output.status_code, 200)
  92. output_text = output.get_data(as_text=True)
  93. self.assertIn(
  94. '<input class="form-control bg-white select-on-focus" type="text" '
  95. 'value="git://localhost.localdomain/test.git" readonly>',
  96. output_text,
  97. )
  98. '''
  99. @patch('pagure.lib.notify.send_email')
  100. def test_view_repo_branch(self, send_email):
  101. """ Test the view_repo_branch endpoint when the git repo has no
  102. master branch.
  103. """
  104. send_email.return_value = True
  105. tests.create_projects(self.session)
  106. # Non-existant git repo
  107. output = self.app.get('/test/branch/master')
  108. self.assertEqual(output.status_code, 404)
  109. self.set_up_git_repo()
  110. # With git repo
  111. output = self.app.get('/test/branch/maxamilion/feature')
  112. self.assertEqual(output.status_code, 200)
  113. output_text = output.get_data(as_text=True)
  114. self.assertIn(
  115. '<input class="form-control bg-white select-on-focus" type="text" '
  116. 'value="git://localhost.localdomain/test.git" readonly>', output_text)
  117. '''
  118. @patch("pagure.lib.notify.send_email")
  119. def test_view_commits(self, send_email):
  120. """Test the view_commits endpoint when the git repo has no
  121. master branch.
  122. """
  123. send_email.return_value = True
  124. tests.create_projects(self.session)
  125. # Non-existant git repo
  126. output = self.app.get("/test/commits")
  127. self.assertEqual(output.status_code, 404)
  128. self.set_up_git_repo()
  129. # With git repo
  130. output = self.app.get("/test/commits")
  131. self.assertEqual(output.status_code, 200)
  132. self.assertEqual(
  133. output.get_data(as_text=True).count('<span class="commitdate"'), 1
  134. )
  135. output = self.app.get("/test/commits/maxamilion/feature")
  136. self.assertEqual(output.status_code, 200)
  137. output_text = output.get_data(as_text=True)
  138. self.assertIn("<title>Commits - test - Pagure</title>", output_text)
  139. self.assertIn("Add sources file for testing", output_text)
  140. self.assertIn("Add .gitignore file for testing", output_text)
  141. self.assertEqual(output_text.count('<span class="commitdate"'), 3)
  142. @patch("pagure.lib.notify.send_email")
  143. def test_view_file(self, send_email):
  144. """Test the view_file endpoint when the git repo has no
  145. master branch.
  146. """
  147. send_email.return_value = True
  148. tests.create_projects(self.session)
  149. # Non-existant git repo
  150. output = self.app.get("/test/blob/master/f/sources")
  151. self.assertEqual(output.status_code, 404)
  152. self.set_up_git_repo()
  153. # With git repo
  154. output = self.app.get("/test/blob/master/f/sources")
  155. self.assertEqual(output.status_code, 200)
  156. self.assertIn(
  157. """<ol class="breadcrumb p-0 bg-transparent mb-0">
  158. <li class="breadcrumb-item">
  159. <a href="/test/tree/master">
  160. <span class="fa fa-random">
  161. </span>&nbsp; master
  162. </a>
  163. </li>
  164. <li class="active breadcrumb-item">
  165. <span class="fa fa-file" data-glyph="">
  166. </span>&nbsp; sources
  167. </li>
  168. </ol>""",
  169. output.get_data(as_text=True),
  170. )
  171. output = self.app.get("/test/blob/master/f/.gitignore")
  172. self.assertEqual(output.status_code, 404)
  173. output = self.app.get("/test/blob/maxamilion/feature/f/.gitignore")
  174. self.assertEqual(output.status_code, 200)
  175. output_text = output.get_data(as_text=True)
  176. self.assertIn(
  177. """<ol class="breadcrumb p-0 bg-transparent mb-0">
  178. <li class="breadcrumb-item">
  179. <a href="/test/tree/maxamilion/feature">
  180. <span class="fa fa-random">
  181. </span>&nbsp; maxamilion/feature
  182. </a>
  183. </li>
  184. <li class="active breadcrumb-item">
  185. <span class="fa fa-file" data-glyph="">
  186. </span>&nbsp; .gitignore
  187. </li>
  188. </ol>""",
  189. output_text,
  190. )
  191. self.assertIn(
  192. '<pre class="syntaxhighlightblock">'
  193. '<code class="lang-plaintext">*~</code></pre>',
  194. output_text,
  195. )
  196. @patch("pagure.lib.notify.send_email")
  197. def test_view_raw_file(self, send_email):
  198. """Test the view_raw_file endpoint when the git repo has no
  199. master branch.
  200. """
  201. send_email.return_value = True
  202. tests.create_projects(self.session)
  203. # Non-existant git repo
  204. output = self.app.get("/test/raw/master")
  205. self.assertEqual(output.status_code, 404)
  206. output = self.app.get("/test/raw/master/f/sources")
  207. self.assertEqual(output.status_code, 404)
  208. self.set_up_git_repo()
  209. # With git repo
  210. output = self.app.get("/test/raw/master")
  211. self.assertEqual(output.status_code, 200)
  212. output_text = output.get_data(as_text=True)
  213. self.assertIn("diff --git a/sources b/sources", output_text)
  214. self.assertIn("+foo\n+ bar", output_text)
  215. output = self.app.get("/test/raw/master/f/sources")
  216. self.assertEqual(output.status_code, 200)
  217. self.assertEqual(output.get_data(as_text=True), "foo\n bar")
  218. output = self.app.get("/test/raw/maxamilion/feature")
  219. self.assertEqual(output.status_code, 200)
  220. output_text = output.get_data(as_text=True)
  221. self.assertIn("diff --git a/.gitignore b/.gitignore", output_text)
  222. self.assertIn("+*~", output_text)
  223. output = self.app.get("/test/raw/maxamilion/feature/f/sources")
  224. self.assertEqual(output.status_code, 200)
  225. self.assertEqual("foo\n bar", output.get_data(as_text=True))
  226. @patch("pagure.lib.notify.send_email")
  227. def test_view_tree(self, send_email):
  228. """Test the view_tree endpoint when the git repo has no
  229. master branch.
  230. """
  231. send_email.return_value = True
  232. tests.create_projects(self.session)
  233. # Non-existant git repo
  234. output = self.app.get("/test/tree/")
  235. self.assertEqual(output.status_code, 404)
  236. output = self.app.get("/test/tree/master")
  237. self.assertEqual(output.status_code, 404)
  238. self.set_up_git_repo()
  239. # With git repo
  240. output = self.app.get("/test/tree/master")
  241. self.assertEqual(output.status_code, 200)
  242. output_text = output.get_data(as_text=True)
  243. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  244. output = self.app.get("/test/tree/master/sources")
  245. self.assertEqual(output.status_code, 200)
  246. output_text = output.get_data(as_text=True)
  247. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  248. output = self.app.get("/test/tree/feature")
  249. self.assertEqual(output.status_code, 200)
  250. output_text = output.get_data(as_text=True)
  251. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  252. output = self.app.get("/test/tree/maxamilion/feature")
  253. self.assertEqual(output.status_code, 200)
  254. output_text = output.get_data(as_text=True)
  255. self.assertIn(
  256. '<a href="/test/blob/maxamilion/feature/f/sources">', output_text
  257. )
  258. # Wrong identifier, back onto master
  259. output = self.app.get("/test/tree/maxamilion/feature/f/.gitignore")
  260. self.assertEqual(output.status_code, 200)
  261. output_text = output.get_data(as_text=True)
  262. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  263. @patch("pagure.lib.notify.send_email")
  264. def test_new_request_pull(self, send_email):
  265. """Test the new_request_pull endpoint when the git repo has no
  266. master branch.
  267. """
  268. send_email.return_value = True
  269. tests.create_projects(self.session)
  270. # Non-existant git repo
  271. output = self.app.get("/test/diff/master..maxamilion/feature")
  272. # (used to be 302 but seeing a diff is allowed even logged out)
  273. self.assertEqual(output.status_code, 404)
  274. user = tests.FakeUser()
  275. with tests.user_set(self.app.application, user):
  276. output = self.app.get("/test/diff/master..maxamilion/feature")
  277. self.assertEqual(output.status_code, 404)
  278. self.set_up_git_repo()
  279. output = self.app.get("/test/diff/master..maxamilion/feature")
  280. # (used to be 302 but seeing a diff is allowed even logged out)
  281. self.assertEqual(output.status_code, 200)
  282. output_text = output.get_data(as_text=True)
  283. self.assertEqual(output_text.count('<span class="commitdate"'), 1)
  284. self.assertIn(
  285. '<span class="font-weight-bold btn btn-sm btn-success disabled opacity-100">'
  286. "+1</span>\n",
  287. output_text,
  288. )
  289. self.assertIn(
  290. '<div class="btn btn-outline-success disabled opacity-100 border-0 font-weight-bold">\n'
  291. " file added\n",
  292. output_text,
  293. )
  294. user = tests.FakeUser()
  295. with tests.user_set(self.app.application, user):
  296. output = self.app.get("/test/diff/master..maxamilion/feature")
  297. self.assertEqual(output.status_code, 200)
  298. output_text = output.get_data(as_text=True)
  299. self.assertEqual(output_text.count('<span class="commitdate"'), 1)
  300. self.assertIn(
  301. '<span class="font-weight-bold btn btn-sm btn-success disabled opacity-100">'
  302. "+1</span>\n",
  303. output_text,
  304. )
  305. self.assertIn(
  306. '<div class="btn btn-outline-success disabled opacity-100 border-0 font-weight-bold">\n'
  307. " file added\n",
  308. output_text,
  309. )
  310. if __name__ == "__main__":
  311. unittest.main(verbosity=2)