test_pagure_flask_ui_slash_branch_name.py 13 KB

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