test_pagure_flask_ui_fork.py 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import pygit2
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib
  20. import tests
  21. from pagure.lib.repo import PagureRepo
  22. def _get_commits(output):
  23. ''' Returns the commits message in the output. All commits must have
  24. been made by `Alice Author` or `PY C` to be found.
  25. '''
  26. commits = []
  27. save = False
  28. cnt = 0
  29. for row in output.split('\n'):
  30. if row.strip() in ['Alice Author', 'Alice Äuthòr', 'PY C']:
  31. save = True
  32. if save:
  33. cnt += 1
  34. if cnt == 7:
  35. commits.append(row.strip())
  36. save = False
  37. cnt = 0
  38. return commits
  39. class PagureFlaskForktests(tests.Modeltests):
  40. """ Tests for flask fork controller of pagure """
  41. def setUp(self):
  42. """ Set up the environnment, ran before every tests. """
  43. super(PagureFlaskForktests, self).setUp()
  44. pagure.APP.config['TESTING'] = True
  45. pagure.SESSION = self.session
  46. pagure.lib.SESSION = self.session
  47. pagure.ui.SESSION = self.session
  48. pagure.ui.app.SESSION = self.session
  49. pagure.ui.filters.SESSION = self.session
  50. pagure.ui.fork.SESSION = self.session
  51. pagure.ui.repo.SESSION = self.session
  52. pagure.ui.issues.SESSION = self.session
  53. pagure.APP.config['GIT_FOLDER'] = os.path.join(self.path, 'repos')
  54. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  55. self.path, 'tickets')
  56. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  57. self.path, 'docs')
  58. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  59. self.path, 'requests')
  60. self.app = pagure.APP.test_client()
  61. def set_up_git_repo(
  62. self, new_project=None, branch_from='feature', mtype='FF'):
  63. """ Set up the git repo and create the corresponding PullRequest
  64. object.
  65. """
  66. # Create a git repo to play with
  67. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  68. repo = pygit2.init_repository(gitrepo, bare=True)
  69. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  70. repopath = os.path.join(newpath, 'test')
  71. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  72. # Create a file in that git repo
  73. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  74. stream.write('foo\n bar')
  75. clone_repo.index.add('sources')
  76. clone_repo.index.write()
  77. # Commits the files added
  78. tree = clone_repo.index.write_tree()
  79. author = pygit2.Signature(
  80. 'Alice Author', 'alice@authors.tld')
  81. committer = pygit2.Signature(
  82. 'Cecil Committer', 'cecil@committers.tld')
  83. clone_repo.create_commit(
  84. 'refs/heads/master', # the name of the reference to update
  85. author,
  86. committer,
  87. 'Add sources file for testing',
  88. # binary string representing the tree object ID
  89. tree,
  90. # list of binary strings representing parents of the new commit
  91. []
  92. )
  93. refname = 'refs/heads/master:refs/heads/master'
  94. ori_remote = clone_repo.remotes[0]
  95. PagureRepo.push(ori_remote, refname)
  96. first_commit = repo.revparse_single('HEAD')
  97. if mtype == 'merge':
  98. with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
  99. stream.write('*~')
  100. clone_repo.index.add('.gitignore')
  101. clone_repo.index.write()
  102. # Commits the files added
  103. tree = clone_repo.index.write_tree()
  104. author = pygit2.Signature(
  105. 'Alice Äuthòr', 'alice@äuthòrs.tld')
  106. committer = pygit2.Signature(
  107. 'Cecil Cõmmîttër', 'cecil@cõmmîttërs.tld')
  108. clone_repo.create_commit(
  109. 'refs/heads/master',
  110. author,
  111. committer,
  112. 'Add .gitignore file for testing',
  113. # binary string representing the tree object ID
  114. tree,
  115. # list of binary strings representing parents of the new commit
  116. [first_commit.oid.hex]
  117. )
  118. refname = 'refs/heads/master:refs/heads/master'
  119. ori_remote = clone_repo.remotes[0]
  120. PagureRepo.push(ori_remote, refname)
  121. if mtype == 'conflicts':
  122. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  123. stream.write('foo\n bar\nbaz')
  124. clone_repo.index.add('sources')
  125. clone_repo.index.write()
  126. # Commits the files added
  127. tree = clone_repo.index.write_tree()
  128. author = pygit2.Signature(
  129. 'Alice Author', 'alice@authors.tld')
  130. committer = pygit2.Signature(
  131. 'Cecil Committer', 'cecil@committers.tld')
  132. clone_repo.create_commit(
  133. 'refs/heads/master',
  134. author,
  135. committer,
  136. 'Add sources conflicting',
  137. # binary string representing the tree object ID
  138. tree,
  139. # list of binary strings representing parents of the new commit
  140. [first_commit.oid.hex]
  141. )
  142. refname = 'refs/heads/master:refs/heads/master'
  143. ori_remote = clone_repo.remotes[0]
  144. PagureRepo.push(ori_remote, refname)
  145. # Set the second repo
  146. new_gitrepo = repopath
  147. if new_project:
  148. # Create a new git repo to play with
  149. new_gitrepo = os.path.join(newpath, new_project.fullname)
  150. if not os.path.exists(new_gitrepo):
  151. os.makedirs(new_gitrepo)
  152. new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
  153. repo = pygit2.Repository(new_gitrepo)
  154. if mtype != 'nochanges':
  155. # Edit the sources file again
  156. with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream:
  157. stream.write('foo\n bar\nbaz\n boose')
  158. repo.index.add('sources')
  159. repo.index.write()
  160. # Commits the files added
  161. tree = repo.index.write_tree()
  162. author = pygit2.Signature(
  163. 'Alice Author', 'alice@authors.tld')
  164. committer = pygit2.Signature(
  165. 'Cecil Committer', 'cecil@committers.tld')
  166. repo.create_commit(
  167. 'refs/heads/%s' % branch_from,
  168. author,
  169. committer,
  170. 'A commit on branch %s' % branch_from,
  171. tree,
  172. [first_commit.oid.hex]
  173. )
  174. refname = 'refs/heads/%s' % (branch_from)
  175. ori_remote = repo.remotes[0]
  176. PagureRepo.push(ori_remote, refname)
  177. # Create a PR for these changes
  178. project = pagure.lib.get_project(self.session, 'test')
  179. req = pagure.lib.new_pull_request(
  180. session=self.session,
  181. repo_from=project,
  182. branch_from=branch_from,
  183. repo_to=project,
  184. branch_to='master',
  185. title='PR from the %s branch' % branch_from,
  186. user='pingou',
  187. requestfolder=None,
  188. )
  189. self.session.commit()
  190. self.assertEqual(req.id, 1)
  191. self.assertEqual(req.title, 'PR from the %s branch' % branch_from)
  192. shutil.rmtree(newpath)
  193. @patch('pagure.lib.notify.send_email')
  194. def test_request_pull(self, send_email):
  195. """ Test the request_pull endpoint. """
  196. send_email.return_value = True
  197. tests.create_projects(self.session)
  198. tests.create_projects_git(
  199. os.path.join(self.path, 'requests'), bare=True)
  200. # Non-existant project
  201. output = self.app.get('/foobar/pull-request/1')
  202. self.assertEqual(output.status_code, 404)
  203. # Project has no PR
  204. output = self.app.get('/test/pull-request/1')
  205. self.assertEqual(output.status_code, 404)
  206. self.set_up_git_repo(new_project=None, branch_from='feature')
  207. project = pagure.lib.get_project(self.session, 'test')
  208. self.assertEqual(len(project.requests), 1)
  209. # View the pull-request
  210. output = self.app.get('/test/pull-request/1')
  211. self.assertEqual(output.status_code, 200)
  212. self.assertIn(
  213. '<h3><span class="label label-default">PR#1</span>\n'
  214. ' PR from the feature branch\n</h3>', output.data)
  215. self.assertIn(
  216. 'title="View file as of 2a552b">sources</a>', output.data)
  217. @patch('pagure.lib.notify.send_email')
  218. def test_merge_request_pull_FF(self, send_email):
  219. """ Test the merge_request_pull endpoint with a FF PR. """
  220. send_email.return_value = True
  221. self.test_request_pull()
  222. user = tests.FakeUser()
  223. with tests.user_set(pagure.APP, user):
  224. output = self.app.get('/test/pull-request/1')
  225. self.assertEqual(output.status_code, 200)
  226. csrf_token = output.data.split(
  227. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  228. # No CSRF
  229. output = self.app.post(
  230. '/test/pull-request/1/merge', data={}, follow_redirects=True)
  231. self.assertEqual(output.status_code, 200)
  232. self.assertIn(
  233. '<title>PR#1: PR from the feature branch - test\n - '
  234. 'Pagure</title>', output.data)
  235. self.assertIn(
  236. '<h3><span class="label label-default">PR#1</span>\n'
  237. ' PR from the feature branch\n</h3>', output.data)
  238. self.assertIn(
  239. 'title="View file as of 2a552b">sources</a>', output.data)
  240. # Wrong project
  241. data = {
  242. 'csrf_token': csrf_token,
  243. }
  244. output = self.app.post(
  245. '/foobar/pull-request/100/merge', data=data, follow_redirects=True)
  246. self.assertEqual(output.status_code, 404)
  247. # Wrong project
  248. data = {
  249. 'csrf_token': csrf_token,
  250. }
  251. output = self.app.post(
  252. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  253. self.assertEqual(output.status_code, 403)
  254. user.username = 'pingou'
  255. with tests.user_set(pagure.APP, user):
  256. # Wrong request id
  257. data = {
  258. 'csrf_token': csrf_token,
  259. }
  260. output = self.app.post(
  261. '/test/pull-request/100/merge', data=data, follow_redirects=True)
  262. self.assertEqual(output.status_code, 404)
  263. # Project w/o pull-request
  264. repo = pagure.lib.get_project(self.session, 'test')
  265. settings = repo.settings
  266. settings['pull_requests'] = False
  267. repo.settings = settings
  268. self.session.add(repo)
  269. self.session.commit()
  270. # Pull-request disabled
  271. output = self.app.post(
  272. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  273. self.assertEqual(output.status_code, 404)
  274. # Project w pull-request but only assignee can merge
  275. settings['pull_requests'] = True
  276. settings['Only_assignee_can_merge_pull-request'] = True
  277. repo.settings = settings
  278. self.session.add(repo)
  279. self.session.commit()
  280. output = self.app.post(
  281. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  282. self.assertEqual(output.status_code, 200)
  283. self.assertIn(
  284. '<title>PR#1: PR from the feature branch - test\n - '
  285. 'Pagure</title>', output.data)
  286. self.assertIn(
  287. '<h3><span class="label label-default">PR#1</span>\n'
  288. ' PR from the feature branch\n <span class="pull-xs-right">',
  289. output.data)
  290. self.assertIn(
  291. '</button>\n This request must be '
  292. 'assigned to be merged', output.data)
  293. # PR assigned but not to this user
  294. repo = pagure.lib.get_project(self.session, 'test')
  295. req = repo.requests[0]
  296. req.assignee_id = 2
  297. self.session.add(req)
  298. self.session.commit()
  299. output = self.app.post(
  300. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  301. self.assertEqual(output.status_code, 200)
  302. self.assertIn(
  303. '<h3><span class="label label-default">PR#1</span>\n'
  304. ' PR from the feature branch\n <span class="pull-xs-right">',
  305. output.data)
  306. self.assertIn(
  307. '</button>\n Only the assignee can '
  308. 'merge this review', output.data)
  309. # Project w/ minimal PR score
  310. settings['Only_assignee_can_merge_pull-request'] = False
  311. settings['Minimum_score_to_merge_pull-request'] = 2
  312. repo.settings = settings
  313. self.session.add(repo)
  314. self.session.commit()
  315. output = self.app.post(
  316. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  317. self.assertEqual(output.status_code, 200)
  318. self.assertIn(
  319. '<h3><span class="label label-default">PR#1</span>\n'
  320. ' PR from the feature branch\n <span class="pull-xs-right">',
  321. output.data)
  322. self.assertIn(
  323. '</button>\n This request does not '
  324. 'have the minimum review score necessary to be merged',
  325. output.data)
  326. # Merge
  327. settings['Minimum_score_to_merge_pull-request'] = -1
  328. repo.settings = settings
  329. self.session.add(repo)
  330. self.session.commit()
  331. output = self.app.post(
  332. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  333. self.assertEqual(output.status_code, 200)
  334. self.assertIn(
  335. '<title>Overview - test - Pagure</title>', output.data)
  336. self.assertIn(
  337. '</button>\n Changes merged!',
  338. output.data)
  339. self.assertIn(
  340. 'A commit on branch feature', output.data)
  341. self.assertNotIn(
  342. 'Merge #1 `PR from the feature branch`', output.data)
  343. # Ensure we have the new commit
  344. commits = _get_commits(output.data)
  345. self.assertEqual(
  346. commits,
  347. [
  348. 'A commit on branch feature',
  349. 'Add sources file for testing'
  350. ]
  351. )
  352. # Check if the closing notification was added
  353. output = self.app.get('/test/pull-request/1')
  354. self.assertIn(
  355. '<small><p>Pull-Request has been merged by pingou</p></small>',
  356. output.data)
  357. @patch('pagure.lib.notify.send_email')
  358. def test_merge_request_pull_merge(self, send_email):
  359. """ Test the merge_request_pull endpoint with a merge PR. """
  360. send_email.return_value = True
  361. tests.create_projects(self.session)
  362. tests.create_projects_git(
  363. os.path.join(self.path, 'requests'), bare=True)
  364. self.set_up_git_repo(
  365. new_project=None, branch_from='feature', mtype='merge')
  366. user = tests.FakeUser()
  367. user.username = 'pingou'
  368. with tests.user_set(pagure.APP, user):
  369. output = self.app.get('/test/pull-request/1')
  370. self.assertEqual(output.status_code, 200)
  371. csrf_token = output.data.split(
  372. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  373. data = {
  374. 'csrf_token': csrf_token,
  375. }
  376. # Merge
  377. output = self.app.post(
  378. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  379. self.assertEqual(output.status_code, 200)
  380. self.assertIn(
  381. '<title>Overview - test - Pagure</title>', output.data)
  382. self.assertIn(
  383. '</button>\n Changes merged!',
  384. output.data)
  385. # Check if the closing notification was added
  386. output = self.app.get('/test/pull-request/1')
  387. self.assertIn(
  388. '<small><p>Pull-Request has been merged by pingou</p></small>',
  389. output.data)
  390. @patch('pagure.lib.notify.send_email')
  391. def test_merge_request_pull_conflicts(self, send_email):
  392. """ Test the merge_request_pull endpoint with a conflicting PR. """
  393. send_email.return_value = True
  394. tests.create_projects(self.session)
  395. tests.create_projects_git(
  396. os.path.join(self.path, 'requests'), bare=True)
  397. self.set_up_git_repo(
  398. new_project=None, branch_from='feature', mtype='conflicts')
  399. user = tests.FakeUser()
  400. user.username = 'pingou'
  401. with tests.user_set(pagure.APP, user):
  402. output = self.app.get('/test/pull-request/1')
  403. self.assertEqual(output.status_code, 200)
  404. csrf_token = output.data.split(
  405. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  406. data = {
  407. 'csrf_token': csrf_token,
  408. }
  409. # Merge conflicts
  410. output = self.app.post(
  411. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  412. self.assertEqual(output.status_code, 200)
  413. self.assertIn(
  414. '<h3><span class="label label-default">PR#1</span>\n'
  415. ' PR from the feature branch\n <span class="pull-xs-right">',
  416. output.data)
  417. self.assertIn(
  418. '</button>\n Merge conflicts!',
  419. output.data)
  420. @patch('pagure.lib.notify.send_email')
  421. def test_merge_request_pull_nochange(self, send_email):
  422. """ Test the merge_request_pull endpoint. """
  423. send_email.return_value = True
  424. tests.create_projects(self.session)
  425. tests.create_projects_git(
  426. os.path.join(self.path, 'requests'), bare=True)
  427. self.set_up_git_repo(
  428. new_project=None, branch_from='master', mtype='nochanges')
  429. user = tests.FakeUser()
  430. user.username = 'pingou'
  431. with tests.user_set(pagure.APP, user):
  432. output = self.app.get('/test/pull-request/1')
  433. self.assertEqual(output.status_code, 200)
  434. csrf_token = output.data.split(
  435. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  436. data = {
  437. 'csrf_token': csrf_token,
  438. }
  439. # Nothing to merge
  440. output = self.app.post(
  441. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  442. self.assertEqual(output.status_code, 200)
  443. self.assertIn(
  444. '<h3><span class="label label-default">PR#1</span>\n'
  445. ' <span class="label label-success">Merged</span>',
  446. output.data)
  447. self.assertIn(
  448. '</button>\n Nothing to do, changes '
  449. 'were already merged', output.data)
  450. # Check if the closing notification was added
  451. output = self.app.get('/test/pull-request/1')
  452. self.assertIn(
  453. '<small><p>Pull-Request has been merged by pingou</p></small>',
  454. output.data)
  455. @patch('pagure.lib.notify.send_email')
  456. def test_request_pull_close(self, send_email):
  457. """ Test the request_pull endpoint with a closed PR. """
  458. send_email.return_value = True
  459. self.test_merge_request_pull_FF()
  460. output = self.app.get('/test/pull-request/1')
  461. self.assertEqual(output.status_code, 200)
  462. self.assertIn(
  463. '<h3><span class="label label-default">PR#1</span>\n'
  464. ' <span class="label label-success">', output.data)
  465. self.assertIn('<div>Merged by\n', output.data)
  466. self.assertIn(
  467. 'title="View file as of 2a552b">sources</a>', output.data)
  468. @patch('pagure.lib.notify.send_email')
  469. def test_request_pull_disabled(self, send_email):
  470. """ Test the request_pull endpoint with PR disabled. """
  471. send_email.return_value = True
  472. tests.create_projects(self.session)
  473. tests.create_projects_git(
  474. os.path.join(self.path, 'requests'), bare=True)
  475. self.set_up_git_repo(new_project=None, branch_from='feature')
  476. # Project w/o pull-request
  477. repo = pagure.lib.get_project(self.session, 'test')
  478. settings = repo.settings
  479. settings['pull_requests'] = False
  480. repo.settings = settings
  481. self.session.add(repo)
  482. self.session.commit()
  483. output = self.app.get('/test/pull-request/1')
  484. self.assertEqual(output.status_code, 404)
  485. @patch('pagure.lib.notify.send_email')
  486. def test_request_pull_empty_repo(self, send_email):
  487. """ Test the request_pull endpoint against an empty repo. """
  488. send_email.return_value = True
  489. tests.create_projects(self.session)
  490. item = pagure.lib.model.Project(
  491. user_id=2, # foo
  492. name='test',
  493. description='test project #1',
  494. hook_token='aaabbb',
  495. is_fork=True,
  496. parent_id=1,
  497. )
  498. self.session.add(item)
  499. self.session.commit()
  500. tests.create_projects_git(
  501. os.path.join(self.path, 'requests'), bare=True)
  502. tests.create_projects_git(
  503. os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
  504. # Create a git repo to play with
  505. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  506. self.assertFalse(os.path.exists(gitrepo))
  507. os.makedirs(gitrepo)
  508. repo = pygit2.init_repository(gitrepo, bare=True)
  509. # Create a fork of this repo
  510. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  511. gitrepo = os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git')
  512. new_repo = pygit2.clone_repository(gitrepo, newpath)
  513. # Edit the sources file again
  514. with open(os.path.join(newpath, 'sources'), 'w') as stream:
  515. stream.write('foo\n bar\nbaz\n boose')
  516. new_repo.index.add('sources')
  517. new_repo.index.write()
  518. # Commits the files added
  519. tree = new_repo.index.write_tree()
  520. author = pygit2.Signature(
  521. 'Alice Author', 'alice@authors.tld')
  522. committer = pygit2.Signature(
  523. 'Cecil Committer', 'cecil@committers.tld')
  524. new_repo.create_commit(
  525. 'refs/heads/feature',
  526. author,
  527. committer,
  528. 'A commit on branch feature',
  529. tree,
  530. []
  531. )
  532. refname = 'refs/heads/feature:refs/heads/feature'
  533. ori_remote = new_repo.remotes[0]
  534. PagureRepo.push(ori_remote, refname)
  535. # Create a PR for these changes
  536. project = pagure.lib.get_project(self.session, 'test')
  537. req = pagure.lib.new_pull_request(
  538. session=self.session,
  539. repo_from=item,
  540. branch_from='feature',
  541. repo_to=project,
  542. branch_to='master',
  543. title='PR from the feature branch',
  544. user='pingou',
  545. requestfolder=None,
  546. )
  547. self.session.commit()
  548. self.assertEqual(req.id, 1)
  549. self.assertEqual(req.title, 'PR from the feature branch')
  550. output = self.app.get('/test/pull-request/1')
  551. self.assertEqual(output.status_code, 200)
  552. self.assertIn(
  553. '<h3><span class="label label-default">PR#1</span>\n'
  554. ' PR from the feature branch\n</h3>', output.data)
  555. self.assertTrue(
  556. output.data.count('<span class="commitdate" title='), 1)
  557. shutil.rmtree(newpath)
  558. @patch('pagure.lib.notify.send_email')
  559. def test_request_pull_empty_fork(self, send_email):
  560. """ Test the request_pull endpoint from an empty fork. """
  561. send_email.return_value = True
  562. tests.create_projects(self.session)
  563. item = pagure.lib.model.Project(
  564. user_id=2, # foo
  565. name='test',
  566. description='test project #1',
  567. hook_token='aaabbb',
  568. is_fork=True,
  569. parent_id=1,
  570. )
  571. self.session.add(item)
  572. self.session.commit()
  573. tests.create_projects_git(
  574. os.path.join(self.path, 'requests'), bare=True)
  575. tests.create_projects_git(
  576. os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
  577. # Create a git repo to play with
  578. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  579. self.assertFalse(os.path.exists(gitrepo))
  580. os.makedirs(gitrepo)
  581. repo = pygit2.init_repository(gitrepo, bare=True)
  582. # Create a fork of this repo
  583. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  584. gitrepo = os.path.join(
  585. self.path, 'repos', 'forks', 'foo', 'test.git')
  586. new_repo = pygit2.clone_repository(gitrepo, newpath)
  587. # Create a PR for these "changes" (there are none, both repos are
  588. # empty)
  589. project = pagure.lib.get_project(self.session, 'test')
  590. req = pagure.lib.new_pull_request(
  591. session=self.session,
  592. repo_from=item,
  593. branch_from='feature',
  594. repo_to=project,
  595. branch_to='master',
  596. title='PR from the feature branch',
  597. user='pingou',
  598. requestfolder=None,
  599. )
  600. self.session.commit()
  601. self.assertEqual(req.id, 1)
  602. self.assertEqual(req.title, 'PR from the feature branch')
  603. output = self.app.get('/test/pull-request/1', follow_redirects=True)
  604. self.assertEqual(output.status_code, 200)
  605. self.assertIn(
  606. '<title>Overview - test - Pagure</title>', output.data)
  607. self.assertIn(
  608. '</button>\n Fork is empty, there are no '
  609. 'commits to request pulling', output.data)
  610. shutil.rmtree(newpath)
  611. @patch('pagure.lib.notify.send_email')
  612. def test_request_pulls(self, send_email):
  613. """ Test the request_pulls endpoint. """
  614. send_email.return_value = True
  615. # No such project
  616. output = self.app.get('/test/pull-requests')
  617. self.assertEqual(output.status_code, 404)
  618. tests.create_projects(self.session)
  619. tests.create_projects_git(
  620. os.path.join(self.path, 'repos'), bare=True)
  621. output = self.app.get('/test/pull-requests')
  622. self.assertEqual(output.status_code, 200)
  623. self.assertIn(
  624. '<h2 class="p-b-1">\n 0 Pull Requests (of 0)\n </h2>',
  625. output.data)
  626. # Open is primary
  627. self.assertIn(
  628. '<a class="btn btn-primary btn-sm" '
  629. 'href="/test/pull-requests">Open</a>', output.data)
  630. self.assertIn(
  631. '<a class="btn btn-secondary btn-sm" '
  632. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  633. self.set_up_git_repo(new_project=None, branch_from='feature')
  634. output = self.app.get('/test/pull-requests')
  635. self.assertEqual(output.status_code, 200)
  636. self.assertIn(
  637. '<h2 class="p-b-1">\n 1 Pull Requests (of 1)\n </h2>',
  638. output.data)
  639. # Open is primary
  640. self.assertIn(
  641. '<a class="btn btn-primary btn-sm" '
  642. 'href="/test/pull-requests">Open</a>', output.data)
  643. self.assertIn(
  644. '<a class="btn btn-secondary btn-sm" '
  645. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  646. output = self.app.get('/test/pull-requests?status=Closed')
  647. self.assertEqual(output.status_code, 200)
  648. self.assertIn(
  649. '<h2 class="p-b-1">\n 0 Closed Pull Requests (of 0)\n </h2>',
  650. output.data)
  651. # Close is primary
  652. self.assertIn(
  653. '<a class="btn btn-secondary btn-sm" '
  654. 'href="/test/pull-requests">Open</a>', output.data)
  655. self.assertIn(
  656. '<a class="btn btn-primary btn-sm" '
  657. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  658. output = self.app.get('/test/pull-requests?status=0')
  659. self.assertEqual(output.status_code, 200)
  660. self.assertIn(
  661. '<h2 class="p-b-1">\n 0 Closed/Merged Pull Requests (of 0)\n </h2>',
  662. output.data)
  663. # Close is primary
  664. self.assertIn(
  665. '<a class="btn btn-secondary btn-sm" '
  666. 'href="/test/pull-requests">Open</a>', output.data)
  667. self.assertIn(
  668. '<a class="btn btn-primary btn-sm" '
  669. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  670. # Project w/o pull-request
  671. repo = pagure.lib.get_project(self.session, 'test')
  672. settings = repo.settings
  673. settings['pull_requests'] = False
  674. repo.settings = settings
  675. self.session.add(repo)
  676. self.session.commit()
  677. output = self.app.get('/test/pull-requests')
  678. self.assertEqual(output.status_code, 404)
  679. @patch('pagure.lib.notify.send_email')
  680. def test_request_pull_patch(self, send_email):
  681. """ Test the request_pull_patch endpoint. """
  682. send_email.return_value = True
  683. output = self.app.get('/test/pull-request/1.patch')
  684. self.assertEqual(output.status_code, 404)
  685. tests.create_projects(self.session)
  686. tests.create_projects_git(
  687. os.path.join(self.path, 'requests'), bare=True)
  688. self.set_up_git_repo(
  689. new_project=None, branch_from='feature', mtype='merge')
  690. output = self.app.get('/test/pull-request/100.patch')
  691. self.assertEqual(output.status_code, 404)
  692. output = self.app.get('/test/pull-request/1.patch')
  693. self.assertEqual(output.status_code, 200)
  694. npatch = []
  695. for row in output.data.split('\n'):
  696. if row.startswith('Date:'):
  697. continue
  698. if row.startswith('From '):
  699. row = row.split(' ', 2)[2]
  700. npatch.append(row)
  701. exp = """Mon Sep 17 00:00:00 2001
  702. From: Alice Author <alice@authors.tld>
  703. Subject: A commit on branch feature
  704. ---
  705. diff --git a/.gitignore b/.gitignore
  706. new file mode 100644
  707. index 0000000..e4e5f6c
  708. --- /dev/null
  709. +++ b/.gitignore
  710. @@ -0,0 +1 @@
  711. +*~
  712. \ No newline at end of file
  713. diff --git a/sources b/sources
  714. index 9f44358..2a552bb 100644
  715. --- a/sources
  716. +++ b/sources
  717. @@ -1,2 +1,4 @@
  718. foo
  719. - bar
  720. \ No newline at end of file
  721. + bar
  722. +baz
  723. + boose
  724. \ No newline at end of file
  725. """
  726. patch = '\n'.join(npatch)
  727. #print patch
  728. self.assertEqual(patch, exp)
  729. # Project w/o pull-request
  730. repo = pagure.lib.get_project(self.session, 'test')
  731. settings = repo.settings
  732. settings['pull_requests'] = False
  733. repo.settings = settings
  734. self.session.add(repo)
  735. self.session.commit()
  736. output = self.app.get('/test/pull-request/1.patch')
  737. self.assertEqual(output.status_code, 404)
  738. @patch('pagure.lib.notify.send_email')
  739. def test_request_pull_patch_close(self, send_email):
  740. """ Test the request_pull_patch endpoint with a closed PR. """
  741. send_email.return_value = True
  742. self.test_merge_request_pull_FF()
  743. output = self.app.get('/test/pull-request/1.patch')
  744. self.assertEqual(output.status_code, 200)
  745. npatch = []
  746. for row in output.data.split('\n'):
  747. if row.startswith('Date:'):
  748. continue
  749. if row.startswith('From '):
  750. row = row.split(' ', 2)[2]
  751. npatch.append(row)
  752. exp = """Mon Sep 17 00:00:00 2001
  753. From: Alice Author <alice@authors.tld>
  754. Subject: A commit on branch feature
  755. ---
  756. diff --git a/sources b/sources
  757. index 9f44358..2a552bb 100644
  758. --- a/sources
  759. +++ b/sources
  760. @@ -1,2 +1,4 @@
  761. foo
  762. - bar
  763. \ No newline at end of file
  764. + bar
  765. +baz
  766. + boose
  767. \ No newline at end of file
  768. """
  769. patch = '\n'.join(npatch)
  770. #print patch
  771. self.assertEqual(patch, exp)
  772. @patch('pagure.lib.notify.send_email')
  773. def test_request_pull_patch_empty_repo(self, send_email):
  774. """ Test the request_pull_patch endpoint against an empty repo. """
  775. send_email.return_value = True
  776. tests.create_projects(self.session)
  777. item = pagure.lib.model.Project(
  778. user_id=2, # foo
  779. name='test',
  780. description='test project #1',
  781. hook_token='aaabbb',
  782. is_fork=True,
  783. parent_id=1,
  784. )
  785. self.session.add(item)
  786. self.session.commit()
  787. tests.create_projects_git(
  788. os.path.join(self.path, 'requests'), bare=True)
  789. tests.create_projects_git(
  790. os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
  791. # Create a git repo to play with
  792. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  793. self.assertFalse(os.path.exists(gitrepo))
  794. os.makedirs(gitrepo)
  795. repo = pygit2.init_repository(gitrepo, bare=True)
  796. # Create a fork of this repo
  797. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  798. gitrepo = os.path.join(
  799. self.path, 'repos', 'forks', 'foo', 'test.git')
  800. new_repo = pygit2.clone_repository(gitrepo, newpath)
  801. # Edit the sources file again
  802. with open(os.path.join(newpath, 'sources'), 'w') as stream:
  803. stream.write('foo\n bar\nbaz\n boose')
  804. new_repo.index.add('sources')
  805. new_repo.index.write()
  806. # Commits the files added
  807. tree = new_repo.index.write_tree()
  808. author = pygit2.Signature(
  809. 'Alice Author', 'alice@authors.tld')
  810. committer = pygit2.Signature(
  811. 'Cecil Committer', 'cecil@committers.tld')
  812. new_repo.create_commit(
  813. 'refs/heads/feature',
  814. author,
  815. committer,
  816. 'A commit on branch feature',
  817. tree,
  818. []
  819. )
  820. refname = 'refs/heads/feature:refs/heads/feature'
  821. ori_remote = new_repo.remotes[0]
  822. PagureRepo.push(ori_remote, refname)
  823. # Create a PR for these "changes" (there are none, both repos are
  824. # empty)
  825. project = pagure.lib.get_project(self.session, 'test')
  826. req = pagure.lib.new_pull_request(
  827. session=self.session,
  828. repo_from=item,
  829. branch_from='feature',
  830. repo_to=project,
  831. branch_to='master',
  832. title='PR from the feature branch',
  833. user='pingou',
  834. requestfolder=None,
  835. )
  836. self.session.commit()
  837. self.assertEqual(req.id, 1)
  838. self.assertEqual(req.title, 'PR from the feature branch')
  839. output = self.app.get(
  840. '/test/pull-request/1.patch', follow_redirects=True)
  841. self.assertEqual(output.status_code, 200)
  842. npatch = []
  843. for row in output.data.split('\n'):
  844. if row.startswith('Date:'):
  845. continue
  846. if row.startswith('From '):
  847. row = row.split(' ', 2)[2]
  848. npatch.append(row)
  849. exp = """Mon Sep 17 00:00:00 2001
  850. From: Alice Author <alice@authors.tld>
  851. Subject: A commit on branch feature
  852. ---
  853. diff --git a/sources b/sources
  854. new file mode 100644
  855. index 0000000..2a552bb
  856. --- /dev/null
  857. +++ b/sources
  858. @@ -0,0 +1,4 @@
  859. +foo
  860. + bar
  861. +baz
  862. + boose
  863. \ No newline at end of file
  864. """
  865. patch = '\n'.join(npatch)
  866. #print patch
  867. self.assertEqual(patch, exp)
  868. shutil.rmtree(newpath)
  869. @patch('pagure.lib.notify.send_email')
  870. def test_request_pull_patch_empty_fork(self, send_email):
  871. """ Test the request_pull_patch endpoint from an empty fork. """
  872. send_email.return_value = True
  873. tests.create_projects(self.session)
  874. item = pagure.lib.model.Project(
  875. user_id=2, # foo
  876. name='test',
  877. description='test project #1',
  878. hook_token='aaabbb',
  879. is_fork=True,
  880. parent_id=1,
  881. )
  882. self.session.add(item)
  883. self.session.commit()
  884. tests.create_projects_git(
  885. os.path.join(self.path, 'requests'), bare=True)
  886. tests.create_projects_git(
  887. os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
  888. # Create a git repo to play with
  889. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  890. self.assertFalse(os.path.exists(gitrepo))
  891. os.makedirs(gitrepo)
  892. repo = pygit2.init_repository(gitrepo, bare=True)
  893. # Create a fork of this repo
  894. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  895. gitrepo = os.path.join(
  896. self.path, 'repos', 'forks', 'foo', 'test.git')
  897. new_repo = pygit2.clone_repository(gitrepo, newpath)
  898. # Create a PR for these "changes" (there are none, both repos are
  899. # empty)
  900. project = pagure.lib.get_project(self.session, 'test')
  901. req = pagure.lib.new_pull_request(
  902. session=self.session,
  903. repo_from=item,
  904. branch_from='feature',
  905. repo_to=project,
  906. branch_to='master',
  907. title='PR from the feature branch',
  908. user='pingou',
  909. requestfolder=None,
  910. )
  911. self.session.commit()
  912. self.assertEqual(req.id, 1)
  913. self.assertEqual(req.title, 'PR from the feature branch')
  914. output = self.app.get('/test/pull-request/1.patch', follow_redirects=True)
  915. self.assertEqual(output.status_code, 200)
  916. self.assertIn(
  917. '<title>Overview - test - Pagure</title>', output.data)
  918. self.assertIn(
  919. '</button>\n Fork is empty, there are no '
  920. 'commits to request pulling', output.data)
  921. shutil.rmtree(newpath)
  922. @patch('pagure.lib.notify.send_email')
  923. def test_cancel_request_pull(self, send_email):
  924. """ Test the cancel_request_pull endpoint. """
  925. send_email.return_value = True
  926. tests.create_projects(self.session)
  927. tests.create_projects_git(
  928. os.path.join(self.path, 'requests'), bare=True)
  929. self.set_up_git_repo(
  930. new_project=None, branch_from='feature', mtype='merge')
  931. user = tests.FakeUser()
  932. with tests.user_set(pagure.APP, user):
  933. output = self.app.post('/test/pull-request/cancel/1')
  934. self.assertEqual(output.status_code, 302)
  935. output = self.app.post(
  936. '/test/pull-request/cancel/1', follow_redirects=True)
  937. self.assertEqual(output.status_code, 200)
  938. self.assertIn(
  939. '<title>Overview - test - Pagure</title>', output.data)
  940. self.assertIn(
  941. '</button>\n Invalid input submitted',
  942. output.data)
  943. output = self.app.get('/test/pull-request/1')
  944. self.assertEqual(output.status_code, 200)
  945. csrf_token = output.data.split(
  946. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  947. data = {
  948. 'csrf_token': csrf_token,
  949. }
  950. # Invalid project
  951. output = self.app.post(
  952. '/foo/pull-request/cancel/1', data=data, follow_redirects=True)
  953. self.assertEqual(output.status_code, 404)
  954. # Invalid PR id
  955. output = self.app.post(
  956. '/test/pull-request/cancel/100', data=data, follow_redirects=True)
  957. self.assertEqual(output.status_code, 404)
  958. # Invalid user for this project
  959. output = self.app.post(
  960. '/test/pull-request/cancel/1', data=data, follow_redirects=True)
  961. self.assertEqual(output.status_code, 403)
  962. user.username = 'pingou'
  963. with tests.user_set(pagure.APP, user):
  964. # Project w/o pull-request
  965. repo = pagure.lib.get_project(self.session, 'test')
  966. settings = repo.settings
  967. settings['pull_requests'] = False
  968. repo.settings = settings
  969. self.session.add(repo)
  970. self.session.commit()
  971. output = self.app.post(
  972. '/test/pull-request/cancel/1', data=data, follow_redirects=True)
  973. self.assertEqual(output.status_code, 404)
  974. # Project w/o pull-request
  975. repo = pagure.lib.get_project(self.session, 'test')
  976. settings = repo.settings
  977. settings['pull_requests'] = True
  978. repo.settings = settings
  979. self.session.add(repo)
  980. self.session.commit()
  981. output = self.app.post(
  982. '/test/pull-request/cancel/1', data=data, follow_redirects=True)
  983. self.assertEqual(output.status_code, 200)
  984. self.assertIn(
  985. '<title>Overview - test - Pagure</title>', output.data)
  986. self.assertIn(
  987. '</button>\n Request pull canceled!',
  988. output.data)
  989. @patch('pagure.lib.notify.send_email')
  990. def test_set_assignee_requests(self, send_email):
  991. """ Test the set_assignee_requests endpoint. """
  992. send_email.return_value = True
  993. tests.create_projects(self.session)
  994. tests.create_projects_git(
  995. os.path.join(self.path, 'requests'), bare=True)
  996. self.set_up_git_repo(new_project=None, branch_from='feature')
  997. user = tests.FakeUser()
  998. user.username = 'pingou'
  999. with tests.user_set(pagure.APP, user):
  1000. # No such project
  1001. output = self.app.post('/foo/pull-request/1/assign')
  1002. self.assertEqual(output.status_code, 404)
  1003. output = self.app.post('/test/pull-request/100/assign')
  1004. self.assertEqual(output.status_code, 404)
  1005. # Invalid input
  1006. output = self.app.post(
  1007. '/test/pull-request/1/assign', follow_redirects=True)
  1008. self.assertEqual(output.status_code, 200)
  1009. self.assertIn(
  1010. '<title>PR#1: PR from the feature branch - test\n - '
  1011. 'Pagure</title>', output.data)
  1012. self.assertIn(
  1013. '<h3><span class="label label-default">PR#1</span>\n'
  1014. ' PR from the feature branch\n', output.data)
  1015. self.assertNotIn(
  1016. '</button>\n Request assigned',
  1017. output.data)
  1018. output = self.app.get('/test/pull-request/1')
  1019. self.assertEqual(output.status_code, 200)
  1020. csrf_token = output.data.split(
  1021. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1022. data = {
  1023. 'user': 'pingou',
  1024. }
  1025. # No CSRF
  1026. output = self.app.post(
  1027. '/test/pull-request/1/assign', data=data,
  1028. follow_redirects=True)
  1029. self.assertEqual(output.status_code, 200)
  1030. self.assertIn(
  1031. '<title>PR#1: PR from the feature branch - test\n - '
  1032. 'Pagure</title>', output.data)
  1033. self.assertIn(
  1034. '<h3><span class="label label-default">PR#1</span>\n'
  1035. ' PR from the feature branch\n', output.data)
  1036. self.assertNotIn(
  1037. '</button>\n Request assigned',
  1038. output.data)
  1039. # Invalid assignee
  1040. data = {
  1041. 'csrf_token': csrf_token,
  1042. 'user': 'bar',
  1043. }
  1044. output = self.app.post(
  1045. '/test/pull-request/1/assign', data=data,
  1046. follow_redirects=True)
  1047. self.assertEqual(output.status_code, 200)
  1048. self.assertIn(
  1049. '<title>PR#1: PR from the feature branch - test\n - '
  1050. 'Pagure</title>', output.data)
  1051. self.assertIn(
  1052. '<h3><span class="label label-default">PR#1</span>\n'
  1053. ' PR from the feature branch\n', output.data)
  1054. self.assertIn(
  1055. '</button>\n No user &#34;bar&#34; found',
  1056. output.data)
  1057. # Assign the PR
  1058. data = {
  1059. 'csrf_token': csrf_token,
  1060. 'user': 'pingou',
  1061. }
  1062. user.username = 'foo'
  1063. with tests.user_set(pagure.APP, user):
  1064. output = self.app.post(
  1065. '/test/pull-request/1/assign', data=data,
  1066. follow_redirects=True)
  1067. self.assertEqual(output.status_code, 403)
  1068. user.username = 'pingou'
  1069. with tests.user_set(pagure.APP, user):
  1070. output = self.app.post(
  1071. '/test/pull-request/1/assign', data=data,
  1072. follow_redirects=True)
  1073. self.assertEqual(output.status_code, 200)
  1074. self.assertIn(
  1075. '<title>PR#1: PR from the feature branch - test\n - '
  1076. 'Pagure</title>', output.data)
  1077. self.assertIn(
  1078. '<h3><span class="label label-default">PR#1</span>\n'
  1079. ' PR from the feature branch\n', output.data)
  1080. self.assertIn(
  1081. '</button>\n Request assigned',
  1082. output.data)
  1083. # Pull-Request closed
  1084. repo = pagure.lib.get_project(self.session, 'test')
  1085. req = repo.requests[0]
  1086. req.status = 'Closed'
  1087. req.closed_by_in = 1
  1088. self.session.add(req)
  1089. self.session.commit()
  1090. output = self.app.post(
  1091. '/test/pull-request/1/assign', data=data,
  1092. follow_redirects=True)
  1093. self.assertEqual(output.status_code, 403)
  1094. # Project w/o pull-request
  1095. repo = pagure.lib.get_project(self.session, 'test')
  1096. settings = repo.settings
  1097. settings['pull_requests'] = False
  1098. repo.settings = settings
  1099. self.session.add(repo)
  1100. self.session.commit()
  1101. output = self.app.post(
  1102. '/test/pull-request/1/assign', data=data,
  1103. follow_redirects=True)
  1104. self.assertEqual(output.status_code, 404)
  1105. @patch('pagure.lib.notify.send_email')
  1106. def test_fork_project(self, send_email):
  1107. """ Test the fork_project endpoint. """
  1108. send_email.return_value = True
  1109. tests.create_projects(self.session)
  1110. for folder in ['docs', 'tickets', 'requests', 'repos']:
  1111. tests.create_projects_git(
  1112. os.path.join(self.path, folder), bare=True)
  1113. user = tests.FakeUser()
  1114. user.username = 'pingou'
  1115. with tests.user_set(pagure.APP, user):
  1116. output = self.app.post('/do_fork/test')
  1117. self.assertEqual(output.status_code, 400)
  1118. output = self.app.get('/new/')
  1119. self.assertEqual(output.status_code, 200)
  1120. self.assertIn('<strong>Create new Project</strong>', output.data)
  1121. csrf_token = output.data.split(
  1122. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1123. data = {
  1124. 'csrf_token': csrf_token,
  1125. }
  1126. output = self.app.post(
  1127. '/do_fork/foo', data=data, follow_redirects=True)
  1128. self.assertEqual(output.status_code, 404)
  1129. user.username = 'foo'
  1130. with tests.user_set(pagure.APP, user):
  1131. output = self.app.post('/do_fork/test')
  1132. self.assertEqual(output.status_code, 400)
  1133. data = {
  1134. 'csrf_token': csrf_token,
  1135. }
  1136. output = self.app.post(
  1137. '/do_fork/test', data=data, follow_redirects=True)
  1138. self.assertEqual(output.status_code, 200)
  1139. self.assertIn(
  1140. '</button>\n Repo &#34;test&#34; '
  1141. 'cloned to &#34;foo/test&#34;', output.data)
  1142. @patch('pagure.lib.notify.send_email')
  1143. def test_new_request_pull(self, send_email):
  1144. """ Test the new_request_pull endpoint. """
  1145. send_email.return_value = True
  1146. self.test_fork_project()
  1147. tests.create_projects_git(
  1148. os.path.join(self.path, 'requests'), bare=True)
  1149. repo = pagure.lib.get_project(self.session, 'test')
  1150. fork = pagure.lib.get_project(self.session, 'test', user='foo')
  1151. self.set_up_git_repo(
  1152. new_project=fork, branch_from='feature', mtype='FF')
  1153. user = tests.FakeUser()
  1154. user.username = 'foo'
  1155. with tests.user_set(pagure.APP, user):
  1156. output = self.app.get('/foo/diff/master..feature')
  1157. self.assertEqual(output.status_code, 404)
  1158. output = self.app.get('/test/diff/master..foo')
  1159. self.assertEqual(output.status_code, 400)
  1160. output = self.app.get('/test/diff/foo..master')
  1161. self.assertEqual(output.status_code, 400)
  1162. output = self.app.get('/test/diff/feature..master')
  1163. self.assertEqual(output.status_code, 200)
  1164. self.assertIn(
  1165. '<title>Diff from master to feature - test\n - '
  1166. 'Pagure</title>', output.data)
  1167. self.assertIn(
  1168. '<p class="error"> No commits found </p>', output.data)
  1169. output = self.app.get('/test/diff/master..feature')
  1170. self.assertEqual(output.status_code, 200)
  1171. self.assertIn(
  1172. '<title>Diff from feature to master - test\n - '
  1173. 'Pagure</title>', output.data)
  1174. self.assertNotIn(
  1175. '<input type="submit" class="submit positive button" '
  1176. 'value="Create">', output.data)
  1177. user.username = 'pingou'
  1178. with tests.user_set(pagure.APP, user):
  1179. output = self.app.get('/test/diff/master..feature')
  1180. self.assertEqual(output.status_code, 200)
  1181. self.assertIn(
  1182. '<title>Create new Pull Request for master - test\n - '
  1183. 'Pagure</title>', output.data)
  1184. self.assertIn(
  1185. '<input type="submit" class="btn btn-primary" value="Create">',
  1186. output.data)
  1187. csrf_token = output.data.split(
  1188. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1189. # Case 1 - Add an initial comment
  1190. data = {
  1191. 'csrf_token': csrf_token,
  1192. 'title': 'foo bar PR',
  1193. 'initial_comment': 'Test Initial Comment',
  1194. }
  1195. output = self.app.post(
  1196. '/test/diff/master..feature', data=data, follow_redirects=True)
  1197. self.assertEqual(output.status_code, 200)
  1198. self.assertIn(
  1199. '<title>PR#2: foo bar PR - test\n - Pagure</title>',
  1200. output.data)
  1201. self.assertIn('<p>Test Initial Comment</p>', output.data)
  1202. # Case 2 - Add an empty initial comment
  1203. data = {
  1204. 'csrf_token': csrf_token,
  1205. 'title': 'foo bar PR',
  1206. 'initial_comment': '',
  1207. }
  1208. output = self.app.post(
  1209. '/test/diff/master..feature', data=data, follow_redirects=True)
  1210. self.assertEqual(output.status_code, 200)
  1211. self.assertIn(
  1212. '<title>PR#3: foo bar PR - test\n - Pagure</title>',
  1213. output.data)
  1214. self.assertNotIn('<div id="comment-', output.data)
  1215. @patch('pagure.lib.notify.send_email')
  1216. def test_new_request_pull_empty_repo(self, send_email):
  1217. """ Test the new_request_pull endpoint against an empty repo. """
  1218. send_email.return_value = True
  1219. self.test_fork_project()
  1220. tests.create_projects_git(
  1221. os.path.join(self.path, 'requests'), bare=True)
  1222. repo = pagure.lib.get_project(self.session, 'test')
  1223. fork = pagure.lib.get_project(self.session, 'test', user='foo')
  1224. # Create a git repo to play with
  1225. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  1226. repo = pygit2.init_repository(gitrepo, bare=True)
  1227. # Create a fork of this repo
  1228. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  1229. gitrepo = os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git')
  1230. new_repo = pygit2.clone_repository(gitrepo, newpath)
  1231. user = tests.FakeUser()
  1232. user.username = 'foo'
  1233. with tests.user_set(pagure.APP, user):
  1234. output = self.app.get(
  1235. '/fork/foo/test/diff/master..feature',
  1236. follow_redirects=True)
  1237. self.assertEqual(output.status_code, 200)
  1238. self.assertIn(
  1239. '<title>Overview - test - Pagure</title>', output.data)
  1240. self.assertIn(
  1241. '</button>\n Fork is empty, there are '
  1242. 'no commits to request pulling', output.data)
  1243. output = self.app.get('/test/new_issue')
  1244. csrf_token = output.data.split(
  1245. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1246. data = {
  1247. 'csrf_token': csrf_token,
  1248. 'title': 'foo bar PR',
  1249. }
  1250. output = self.app.post(
  1251. '/test/diff/master..feature', data=data, follow_redirects=True)
  1252. self.assertEqual(output.status_code, 200)
  1253. self.assertIn(
  1254. '<title>Overview - test - Pagure</title>', output.data)
  1255. self.assertIn(
  1256. '</button>\n Fork is empty, there are '
  1257. 'no commits to request pulling', output.data)
  1258. shutil.rmtree(newpath)
  1259. @patch('pagure.lib.notify.send_email')
  1260. def test_new_request_pull_empty_fork(self, send_email):
  1261. """ Test the new_request_pull endpoint against an empty repo. """
  1262. send_email.return_value = True
  1263. self.test_fork_project()
  1264. tests.create_projects_git(
  1265. os.path.join(self.path, 'requests'), bare=True)
  1266. repo = pagure.lib.get_project(self.session, 'test')
  1267. fork = pagure.lib.get_project(self.session, 'test', user='foo')
  1268. # Create a git repo to play with
  1269. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  1270. repo = pygit2.init_repository(gitrepo, bare=True)
  1271. # Create a fork of this repo
  1272. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  1273. gitrepo = os.path.join(
  1274. self.path, 'repos', 'forks', 'foo', 'test.git')
  1275. new_repo = pygit2.clone_repository(gitrepo, newpath)
  1276. user = tests.FakeUser()
  1277. user.username = 'foo'
  1278. with tests.user_set(pagure.APP, user):
  1279. output = self.app.get(
  1280. '/fork/foo/test/diff/master..master', follow_redirects=True)
  1281. self.assertIn(
  1282. '<title>Overview - test - Pagure</title>', output.data)
  1283. self.assertIn(
  1284. '</button>\n Fork is empty, there are '
  1285. 'no commits to request pulling', output.data)
  1286. shutil.rmtree(newpath)
  1287. @patch('pagure.lib.notify.send_email')
  1288. def test_pull_request_add_comment(self, send_email):
  1289. """ Test the pull_request_add_comment endpoint. """
  1290. send_email.return_value = True
  1291. self.test_request_pull()
  1292. user = tests.FakeUser()
  1293. user.username = 'pingou'
  1294. with tests.user_set(pagure.APP, user):
  1295. output = self.app.post('/foo/pull-request/1/comment')
  1296. self.assertEqual(output.status_code, 404)
  1297. output = self.app.post('/test/pull-request/100/comment')
  1298. self.assertEqual(output.status_code, 404)
  1299. output = self.app.post('/test/pull-request/1/comment')
  1300. self.assertEqual(output.status_code, 200)
  1301. self.assertTrue(
  1302. output.data.startswith('\n<section class="add_comment">'))
  1303. csrf_token = output.data.split(
  1304. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1305. data = {
  1306. 'csrf_token': csrf_token,
  1307. 'comment': 'This look alright but we can do better',
  1308. }
  1309. output = self.app.post(
  1310. '/test/pull-request/1/comment', data=data,
  1311. follow_redirects=True)
  1312. self.assertEqual(output.status_code, 200)
  1313. self.assertIn(
  1314. '<title>PR#1: PR from the feature branch - test\n - '
  1315. 'Pagure</title>', output.data)
  1316. self.assertIn(
  1317. '</button>\n Comment added',
  1318. output.data)
  1319. # Project w/o pull-request
  1320. repo = pagure.lib.get_project(self.session, 'test')
  1321. settings = repo.settings
  1322. settings['pull_requests'] = False
  1323. repo.settings = settings
  1324. self.session.add(repo)
  1325. self.session.commit()
  1326. output = self.app.post(
  1327. '/test/pull-request/1/comment', data=data,
  1328. follow_redirects=True)
  1329. self.assertEqual(output.status_code, 404)
  1330. @patch('pagure.lib.notify.send_email')
  1331. def test_pull_request_drop_comment(self, send_email):
  1332. """ Test the pull_request_drop_comment endpoint. """
  1333. send_email.return_value = True
  1334. self.test_pull_request_add_comment()
  1335. # Project w/ pull-request
  1336. repo = pagure.lib.get_project(self.session, 'test')
  1337. settings = repo.settings
  1338. settings['pull_requests'] = True
  1339. repo.settings = settings
  1340. self.session.add(repo)
  1341. self.session.commit()
  1342. user = tests.FakeUser()
  1343. user.username = 'foo'
  1344. with tests.user_set(pagure.APP, user):
  1345. output = self.app.post('/foo/pull-request/1/comment/drop')
  1346. self.assertEqual(output.status_code, 404)
  1347. output = self.app.post('/test/pull-request/100/comment/drop')
  1348. self.assertEqual(output.status_code, 404)
  1349. output = self.app.post(
  1350. '/test/pull-request/1/comment/drop', follow_redirects=True)
  1351. self.assertEqual(output.status_code, 200)
  1352. self.assertIn(
  1353. '<h3><span class="label label-default">PR#1</span>\n'
  1354. ' PR from the feature branch\n</h3>', output.data)
  1355. #self.assertIn('href="#comment-1">¶</a>', output.data)
  1356. self.assertIn(
  1357. '<p>This look alright but we can do better</p>',
  1358. output.data)
  1359. csrf_token = output.data.split(
  1360. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1361. # Invalid comment id
  1362. data = {
  1363. 'csrf_token': csrf_token,
  1364. 'drop_comment': '10',
  1365. }
  1366. output = self.app.post(
  1367. '/test/pull-request/1/comment/drop', data=data,
  1368. follow_redirects=True)
  1369. self.assertEqual(output.status_code, 404)
  1370. data['drop_comment'] = '1'
  1371. output = self.app.post(
  1372. '/test/pull-request/1/comment/drop', data=data,
  1373. follow_redirects=True)
  1374. self.assertEqual(output.status_code, 403)
  1375. user.username = 'pingou'
  1376. with tests.user_set(pagure.APP, user):
  1377. # Drop comment
  1378. output = self.app.post(
  1379. '/test/pull-request/1/comment/drop', data=data,
  1380. follow_redirects=True)
  1381. self.assertEqual(output.status_code, 200)
  1382. self.assertIn(
  1383. '<h3><span class="label label-default">PR#1</span>\n'
  1384. ' PR from the feature branch\n <span class="pull-xs-right">',
  1385. output.data)
  1386. self.assertIn(
  1387. '</button>\n Comment removed',
  1388. output.data)
  1389. # Project w/o pull-request
  1390. repo = pagure.lib.get_project(self.session, 'test')
  1391. settings = repo.settings
  1392. settings['pull_requests'] = False
  1393. repo.settings = settings
  1394. self.session.add(repo)
  1395. self.session.commit()
  1396. output = self.app.post(
  1397. '/test/pull-request/1/comment/drop', data=data,
  1398. follow_redirects=True)
  1399. self.assertEqual(output.status_code, 404)
  1400. @patch('pagure.lib.notify.send_email')
  1401. def test_pull_request_edit_comment(self, send_email):
  1402. """ Test the pull request edit comment endpoint """
  1403. send_email.return_value = True
  1404. self.test_request_pull()
  1405. user = tests.FakeUser()
  1406. user.username = 'pingou'
  1407. with tests.user_set(pagure.APP, user):
  1408. # Repo 'foo' does not exist so it is verifying that condition
  1409. output = self.app.post('/foo/pull-request/1/comment/1/edit')
  1410. self.assertEqual(output.status_code, 404)
  1411. # Here no comment is present in the PR so its verifying that condition
  1412. output = self.app.post('/test/pull-request/100/comment/100/edit')
  1413. self.assertEqual(output.status_code, 404)
  1414. output = self.app.post('/test/pull-request/1/comment')
  1415. self.assertEqual(output.status_code, 200)
  1416. # Creating comment to play with
  1417. self.assertTrue(
  1418. output.data.startswith('\n<section class="add_comment">'))
  1419. csrf_token = output.data.split(
  1420. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1421. data = {
  1422. 'csrf_token': csrf_token,
  1423. 'comment': 'This look alright but we can do better',
  1424. }
  1425. output = self.app.post(
  1426. '/test/pull-request/1/comment', data=data,
  1427. follow_redirects=True)
  1428. self.assertEqual(output.status_code, 200)
  1429. self.assertIn(
  1430. '<h3><span class="label label-default">PR#1</span>\n'
  1431. ' PR from the feature branch\n <span class="pull-xs-right">',
  1432. output.data)
  1433. self.assertIn(
  1434. '</button>\n Comment added',
  1435. output.data)
  1436. # Check if the comment is there
  1437. self.assertIn(
  1438. '<p>This look alright but we can do better</p>', output.data)
  1439. output = self.app.get('/test/pull-request/1/comment/1/edit')
  1440. self.assertEqual(output.status_code, 200)
  1441. self.assertIn('<section class="edit_comment">', output.data)
  1442. # Checking if the comment is there in the update page
  1443. self.assertIn(
  1444. 'This look alright but we can do better</textarea>', output.data)
  1445. csrf_token = output.data.split(
  1446. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1447. data = {
  1448. 'csrf_token': csrf_token,
  1449. 'update_comment': 'This look alright but we can do better than this.',
  1450. }
  1451. output = self.app.post(
  1452. '/test/pull-request/1/comment/1/edit', data=data,
  1453. follow_redirects=True)
  1454. # Checking if the comment is updated in the main page
  1455. self.assertIn(
  1456. '<p>This look alright but we can do better than this.</p>', output.data)
  1457. self.assertIn(
  1458. '<h3><span class="label label-default">PR#1</span>\n'
  1459. ' PR from the feature branch\n <span class="pull-xs-right">',
  1460. output.data)
  1461. # Checking if Edited by User is there or not
  1462. self.assertTrue(
  1463. '<small class="text-muted">Edited just now by pingou </small>'
  1464. in output.data
  1465. or
  1466. '<small class="text-muted">Edited seconds ago by pingou </small>'
  1467. in output.data)
  1468. self.assertIn(
  1469. '</button>\n Comment updated', output.data)
  1470. # Project w/o pull-request
  1471. repo = pagure.lib.get_project(self.session, 'test')
  1472. settings = repo.settings
  1473. settings['pull_requests'] = False
  1474. repo.settings = settings
  1475. self.session.add(repo)
  1476. self.session.commit()
  1477. output = self.app.post(
  1478. '/test/pull-request/1/comment/edit/1', data=data,
  1479. follow_redirects=True)
  1480. self.assertEqual(output.status_code, 404)
  1481. @patch('pagure.lib.notify.send_email')
  1482. def test_merge_request_pull_FF_w_merge_commit(self, send_email):
  1483. """ Test the merge_request_pull endpoint with a FF PR but with a
  1484. merge commit.
  1485. """
  1486. send_email.return_value = True
  1487. self.test_request_pull()
  1488. user = tests.FakeUser()
  1489. with tests.user_set(pagure.APP, user):
  1490. output = self.app.get('/test/pull-request/1')
  1491. self.assertEqual(output.status_code, 200)
  1492. csrf_token = output.data.split(
  1493. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1494. # No CSRF
  1495. output = self.app.post(
  1496. '/test/pull-request/1/merge', data={}, follow_redirects=True)
  1497. self.assertEqual(output.status_code, 200)
  1498. self.assertIn(
  1499. '<title>PR#1: PR from the feature branch - test\n - '
  1500. 'Pagure</title>', output.data)
  1501. self.assertIn(
  1502. '<h3><span class="label label-default">PR#1</span>\n'
  1503. ' PR from the feature branch\n</h3>', output.data)
  1504. self.assertIn(
  1505. 'title="View file as of 2a552b">sources</a>', output.data)
  1506. # Wrong project
  1507. data = {
  1508. 'csrf_token': csrf_token,
  1509. }
  1510. output = self.app.post(
  1511. '/foobar/pull-request/100/merge', data=data, follow_redirects=True)
  1512. self.assertEqual(output.status_code, 404)
  1513. # Wrong project
  1514. data = {
  1515. 'csrf_token': csrf_token,
  1516. }
  1517. output = self.app.post(
  1518. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  1519. self.assertEqual(output.status_code, 403)
  1520. user.username = 'pingou'
  1521. with tests.user_set(pagure.APP, user):
  1522. # Wrong request id
  1523. data = {
  1524. 'csrf_token': csrf_token,
  1525. }
  1526. output = self.app.post(
  1527. '/test/pull-request/100/merge', data=data, follow_redirects=True)
  1528. self.assertEqual(output.status_code, 404)
  1529. # Project requiring a merge commit
  1530. repo = pagure.lib.get_project(self.session, 'test')
  1531. settings = repo.settings
  1532. settings['always_merge'] = True
  1533. repo.settings = settings
  1534. self.session.add(repo)
  1535. self.session.commit()
  1536. # Merge
  1537. output = self.app.post(
  1538. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  1539. self.assertEqual(output.status_code, 200)
  1540. self.assertIn(
  1541. '<title>Overview - test - Pagure</title>', output.data)
  1542. self.assertIn(
  1543. '</button>\n Changes merged!',
  1544. output.data)
  1545. self.assertIn(
  1546. 'Merge #1 `PR from the feature branch`', output.data)
  1547. self.assertIn(
  1548. 'A commit on branch feature', output.data)
  1549. # Ensure we have the merge commit
  1550. commits = _get_commits(output.data)
  1551. self.assertEqual(commits, [
  1552. 'Merge #1 `PR from the feature branch`',
  1553. 'Add sources file for testing',
  1554. 'A commit on branch feature',
  1555. ])
  1556. # Check if the closing notification was added
  1557. output = self.app.get('/test/pull-request/1')
  1558. self.assertIn(
  1559. '<small><p>Pull-Request has been merged by pingou</p></small>',
  1560. output.data)
  1561. @patch('pagure.lib.notify.send_email')
  1562. def test_fork_edit_file(self, send_email):
  1563. """ Test the fork_edit file endpoint. """
  1564. send_email.return_value = True
  1565. # Git repo not found
  1566. output = self.app.post('fork_edit/test/edit/master/f/sources')
  1567. self.assertEqual(output.status_code, 404)
  1568. tests.create_projects(self.session)
  1569. for folder in ['docs', 'tickets', 'requests', 'repos']:
  1570. tests.create_projects_git(
  1571. os.path.join(self.path, folder), bare=True)
  1572. # User not logged in
  1573. output = self.app.post('fork_edit/test/edit/master/f/sources')
  1574. self.assertEqual(output.status_code, 302)
  1575. user = tests.FakeUser()
  1576. user.username = 'pingou'
  1577. with tests.user_set(pagure.APP, user):
  1578. # Invalid request
  1579. output = self.app.post('fork_edit/test/edit/master/f/source')
  1580. self.assertEqual(output.status_code, 400)
  1581. output = self.app.get('/new/')
  1582. self.assertEqual(output.status_code, 200)
  1583. self.assertIn('<strong>Create new Project</strong>', output.data)
  1584. csrf_token = output.data.split(
  1585. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1586. data = {
  1587. 'csrf_token': csrf_token,
  1588. }
  1589. # No files can be found since they are not added
  1590. output = self.app.post('fork_edit/test/edit/master/f/sources',
  1591. data=data, follow_redirects=True)
  1592. self.assertEqual(output.status_code, 404)
  1593. user = tests.FakeUser()
  1594. user.username = 'foo'
  1595. with tests.user_set(pagure.APP, user):
  1596. data = {
  1597. 'csrf_token': csrf_token,
  1598. }
  1599. # Invalid request
  1600. output = self.app.post('fork_edit/test/edit/master/f/sources',
  1601. follow_redirects=True)
  1602. self.assertEqual(output.status_code, 400)
  1603. # Add content to the repo
  1604. tests.add_content_git_repo(os.path.join(
  1605. pagure.APP.config['GIT_FOLDER'], 'test.git'))
  1606. tests.add_readme_git_repo(os.path.join(
  1607. pagure.APP.config['GIT_FOLDER'], 'test.git'))
  1608. tests.add_binary_git_repo(
  1609. os.path.join(
  1610. pagure.APP.config['GIT_FOLDER'], 'test.git'), 'test.jpg')
  1611. # Check if button exists
  1612. output = self.app.get('/test/blob/master/f/sources')
  1613. self.assertEqual(output.status_code, 200)
  1614. self.assertIn(
  1615. 'Fork and Edit\n </button>\n',
  1616. output.data)
  1617. # Check fork-edit doesn't show for binary files
  1618. output = self.app.get('/test/blob/master/f/test.jpg')
  1619. self.assertEqual(output.status_code, 200)
  1620. self.assertNotIn(
  1621. 'Fork and Edit\n </button>\n',
  1622. output.data)
  1623. # Check for edit panel
  1624. output = self.app.post('fork_edit/test/edit/master/f/sources',
  1625. data=data, follow_redirects=True)
  1626. self.assertEqual(output.status_code, 200)
  1627. self.assertIn(
  1628. '<li><a href="/fork/foo/test/tree/master">'
  1629. '<span class="oi" data-glyph="random"></span>&nbsp; master</a>'
  1630. '</li><li class="active"><span class="oi" data-glyph="file">'
  1631. '</span>&nbsp; sources</li>',
  1632. output.data)
  1633. self.assertIn(
  1634. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  1635. output.data)
  1636. self.assertIn(
  1637. '</button>\n Repo &#34;test&#34; '
  1638. 'cloned to &#34;foo/test&#34;', output.data)
  1639. # View what's supposed to be an image
  1640. output = self.app.post('fork_edit/test/edit/master/f/test.jpg',
  1641. data=data, follow_redirects=True)
  1642. self.assertEqual(output.status_code, 400)
  1643. self.assertIn('<p>Cannot edit binary files</p>', output.data)
  1644. # Check fork-edit shows when user is not logged in
  1645. output = self.app.get('/test/blob/master/f/sources')
  1646. self.assertEqual(output.status_code, 200)
  1647. self.assertIn(
  1648. 'Fork and Edit\n </button>\n',
  1649. output.data)
  1650. # Check if fork-edit shows for different user
  1651. user.username = 'pingou'
  1652. with tests.user_set(pagure.APP, user):
  1653. # Check if button exists
  1654. output = self.app.get('/test/blob/master/f/sources')
  1655. self.assertEqual(output.status_code, 200)
  1656. self.assertIn(
  1657. 'Fork and Edit\n </button>\n',
  1658. output.data)
  1659. # Check fork-edit doesn't show for binary
  1660. output = self.app.get('/test/blob/master/f/test.jpg')
  1661. self.assertEqual(output.status_code, 200)
  1662. self.assertNotIn(
  1663. 'Fork and Edit\n </button>\n',
  1664. output.data)
  1665. @patch('pagure.lib.notify.send_email')
  1666. def test_fork_without_main_repo(self, send_email):
  1667. """ Test the fork without the main repo. """
  1668. send_email.return_value = True
  1669. tests.create_projects(self.session)
  1670. # Create a fork with no parent i.e parent_id = None
  1671. item = pagure.lib.model.Project(
  1672. user_id=2, # foo
  1673. name='test',
  1674. description='test project #1',
  1675. hook_token='aaabbb',
  1676. is_fork=True,
  1677. parent_id=None,
  1678. )
  1679. self.session.add(item)
  1680. self.session.commit()
  1681. # Get fork project
  1682. project = pagure.lib.get_project(self.session, 'test', 'foo')
  1683. # Pull-requests and issue-trackers are off for forks
  1684. # lib function is not used here so mannually turning them off
  1685. project_settings = project.settings
  1686. project_settings['pull_requests'] = False
  1687. project_settings['issue_tracker'] = False
  1688. project.settings = project_settings
  1689. self.session.add(project)
  1690. self.session.commit()
  1691. tests.create_projects_git(
  1692. os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
  1693. # Create a git repo to play with
  1694. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  1695. self.assertFalse(os.path.exists(gitrepo))
  1696. os.makedirs(gitrepo)
  1697. repo = pygit2.init_repository(gitrepo, bare=True)
  1698. # Create a fork of this repo
  1699. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  1700. gitrepo = os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git')
  1701. new_repo = pygit2.clone_repository(gitrepo, newpath)
  1702. tests.add_content_git_repo(gitrepo)
  1703. # UI test for deleted main
  1704. output = self.app.get('/fork/foo/test')
  1705. self.assertEqual(output.status_code, 200)
  1706. self.assertIn('Fork from a deleted repository\n', output.data)
  1707. # Testing commit endpoint
  1708. output = self.app.get('/fork/foo/test/commits/master')
  1709. self.assertEqual(output.status_code, 200)
  1710. self.assertIn('Commits <span class="label label-default"> 2</span>\n </h3>\n', output.data)
  1711. # Test pull-request endpoint
  1712. output = self.app.get('/fork/foo/test/pull-requests')
  1713. self.assertEqual(output.status_code, 404)
  1714. # Test issue-tracker endpoint
  1715. output = self.app.get('/fork/foo/test/issues')
  1716. self.assertEqual(output.status_code, 404)
  1717. shutil.rmtree(newpath)
  1718. if __name__ == '__main__':
  1719. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests)
  1720. unittest.TextTestRunner(verbosity=2).run(SUITE)