test_pagure_flask_ui_fork.py 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  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(tests.HERE, 'repos')
  54. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  55. tests.HERE, 'tickets')
  56. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  57. tests.HERE, 'docs')
  58. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  59. tests.HERE, '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(tests.HERE, '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(tests.HERE, '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(tests.HERE, '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(tests.HERE, '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(tests.HERE, '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(tests.HERE, '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. parent_id=1,
  496. )
  497. self.session.add(item)
  498. self.session.commit()
  499. tests.create_projects_git(
  500. os.path.join(tests.HERE, 'requests'), bare=True)
  501. tests.create_projects_git(
  502. os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
  503. # Create a git repo to play with
  504. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  505. self.assertFalse(os.path.exists(gitrepo))
  506. os.makedirs(gitrepo)
  507. repo = pygit2.init_repository(gitrepo, bare=True)
  508. # Create a fork of this repo
  509. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  510. gitrepo = os.path.join(tests.HERE, 'repos', 'forks', 'foo', 'test.git')
  511. new_repo = pygit2.clone_repository(gitrepo, newpath)
  512. # Edit the sources file again
  513. with open(os.path.join(newpath, 'sources'), 'w') as stream:
  514. stream.write('foo\n bar\nbaz\n boose')
  515. new_repo.index.add('sources')
  516. new_repo.index.write()
  517. # Commits the files added
  518. tree = new_repo.index.write_tree()
  519. author = pygit2.Signature(
  520. 'Alice Author', 'alice@authors.tld')
  521. committer = pygit2.Signature(
  522. 'Cecil Committer', 'cecil@committers.tld')
  523. new_repo.create_commit(
  524. 'refs/heads/feature',
  525. author,
  526. committer,
  527. 'A commit on branch feature',
  528. tree,
  529. []
  530. )
  531. refname = 'refs/heads/feature:refs/heads/feature'
  532. ori_remote = new_repo.remotes[0]
  533. PagureRepo.push(ori_remote, refname)
  534. # Create a PR for these changes
  535. project = pagure.lib.get_project(self.session, 'test')
  536. req = pagure.lib.new_pull_request(
  537. session=self.session,
  538. repo_from=item,
  539. branch_from='feature',
  540. repo_to=project,
  541. branch_to='master',
  542. title='PR from the feature branch',
  543. user='pingou',
  544. requestfolder=None,
  545. )
  546. self.session.commit()
  547. self.assertEqual(req.id, 1)
  548. self.assertEqual(req.title, 'PR from the feature branch')
  549. output = self.app.get('/test/pull-request/1')
  550. self.assertEqual(output.status_code, 200)
  551. self.assertIn(
  552. '<h3><span class="label label-default">PR#1</span>\n'
  553. ' PR from the feature branch\n</h3>', output.data)
  554. self.assertTrue(
  555. output.data.count('<span class="commitdate" title='), 1)
  556. shutil.rmtree(newpath)
  557. @patch('pagure.lib.notify.send_email')
  558. def test_request_pull_empty_fork(self, send_email):
  559. """ Test the request_pull endpoint from an empty fork. """
  560. send_email.return_value = True
  561. tests.create_projects(self.session)
  562. item = pagure.lib.model.Project(
  563. user_id=2, # foo
  564. name='test',
  565. description='test project #1',
  566. hook_token='aaabbb',
  567. parent_id=1,
  568. )
  569. self.session.add(item)
  570. self.session.commit()
  571. tests.create_projects_git(
  572. os.path.join(tests.HERE, 'requests'), bare=True)
  573. tests.create_projects_git(
  574. os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
  575. # Create a git repo to play with
  576. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  577. self.assertFalse(os.path.exists(gitrepo))
  578. os.makedirs(gitrepo)
  579. repo = pygit2.init_repository(gitrepo, bare=True)
  580. # Create a fork of this repo
  581. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  582. gitrepo = os.path.join(
  583. tests.HERE, 'repos', 'forks', 'foo', 'test.git')
  584. new_repo = pygit2.clone_repository(gitrepo, newpath)
  585. # Create a PR for these "changes" (there are none, both repos are
  586. # empty)
  587. project = pagure.lib.get_project(self.session, 'test')
  588. req = pagure.lib.new_pull_request(
  589. session=self.session,
  590. repo_from=item,
  591. branch_from='feature',
  592. repo_to=project,
  593. branch_to='master',
  594. title='PR from the feature branch',
  595. user='pingou',
  596. requestfolder=None,
  597. )
  598. self.session.commit()
  599. self.assertEqual(req.id, 1)
  600. self.assertEqual(req.title, 'PR from the feature branch')
  601. output = self.app.get('/test/pull-request/1', follow_redirects=True)
  602. self.assertEqual(output.status_code, 200)
  603. self.assertIn(
  604. '<title>Overview - test - Pagure</title>', output.data)
  605. self.assertIn(
  606. '</button>\n Fork is empty, there are no '
  607. 'commits to request pulling', output.data)
  608. shutil.rmtree(newpath)
  609. @patch('pagure.lib.notify.send_email')
  610. def test_request_pulls(self, send_email):
  611. """ Test the request_pulls endpoint. """
  612. send_email.return_value = True
  613. # No such project
  614. output = self.app.get('/test/pull-requests')
  615. self.assertEqual(output.status_code, 404)
  616. tests.create_projects(self.session)
  617. tests.create_projects_git(
  618. os.path.join(tests.HERE, 'repos'), bare=True)
  619. output = self.app.get('/test/pull-requests')
  620. self.assertEqual(output.status_code, 200)
  621. self.assertIn(
  622. 'Pull Requests <span class="label label-default">0</span>',
  623. output.data)
  624. # Open is primary
  625. self.assertIn(
  626. '<a class="btn btn-primary btn-sm" '
  627. 'href="/test/pull-requests">Open</a>', output.data)
  628. self.assertIn(
  629. '<a class="btn btn-secondary btn-sm" '
  630. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  631. self.set_up_git_repo(new_project=None, branch_from='feature')
  632. output = self.app.get('/test/pull-requests')
  633. self.assertEqual(output.status_code, 200)
  634. self.assertIn(
  635. 'Pull Requests <span class="label label-default">1</span>',
  636. output.data)
  637. # Open is primary
  638. self.assertIn(
  639. '<a class="btn btn-primary btn-sm" '
  640. 'href="/test/pull-requests">Open</a>', output.data)
  641. self.assertIn(
  642. '<a class="btn btn-secondary btn-sm" '
  643. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  644. output = self.app.get('/test/pull-requests?status=Closed')
  645. self.assertEqual(output.status_code, 200)
  646. self.assertIn(
  647. 'Closed Pull Requests <span class="label label-default">0</span>',
  648. output.data)
  649. # Close is primary
  650. self.assertIn(
  651. '<a class="btn btn-secondary btn-sm" '
  652. 'href="/test/pull-requests">Open</a>', output.data)
  653. self.assertIn(
  654. '<a class="btn btn-primary btn-sm" '
  655. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  656. output = self.app.get('/test/pull-requests?status=0')
  657. self.assertEqual(output.status_code, 200)
  658. self.assertIn(
  659. 'Closed/Merged Pull Requests <span class="label label-default">0</span>',
  660. output.data)
  661. # Close is primary
  662. self.assertIn(
  663. '<a class="btn btn-secondary btn-sm" '
  664. 'href="/test/pull-requests">Open</a>', output.data)
  665. self.assertIn(
  666. '<a class="btn btn-primary btn-sm" '
  667. 'href="/test/pull-requests?status=0">Closed</a>', output.data)
  668. # Project w/o pull-request
  669. repo = pagure.lib.get_project(self.session, 'test')
  670. settings = repo.settings
  671. settings['pull_requests'] = False
  672. repo.settings = settings
  673. self.session.add(repo)
  674. self.session.commit()
  675. output = self.app.get('/test/pull-requests')
  676. self.assertEqual(output.status_code, 404)
  677. @patch('pagure.lib.notify.send_email')
  678. def test_request_pull_patch(self, send_email):
  679. """ Test the request_pull_patch endpoint. """
  680. send_email.return_value = True
  681. output = self.app.get('/test/pull-request/1.patch')
  682. self.assertEqual(output.status_code, 404)
  683. tests.create_projects(self.session)
  684. tests.create_projects_git(
  685. os.path.join(tests.HERE, 'requests'), bare=True)
  686. self.set_up_git_repo(
  687. new_project=None, branch_from='feature', mtype='merge')
  688. output = self.app.get('/test/pull-request/100.patch')
  689. self.assertEqual(output.status_code, 404)
  690. output = self.app.get('/test/pull-request/1.patch')
  691. self.assertEqual(output.status_code, 200)
  692. npatch = []
  693. for row in output.data.split('\n'):
  694. if row.startswith('Date:'):
  695. continue
  696. if row.startswith('From '):
  697. row = row.split(' ', 2)[2]
  698. npatch.append(row)
  699. exp = """Mon Sep 17 00:00:00 2001
  700. From: Alice Author <alice@authors.tld>
  701. Subject: A commit on branch feature
  702. ---
  703. diff --git a/.gitignore b/.gitignore
  704. new file mode 100644
  705. index 0000000..e4e5f6c
  706. --- /dev/null
  707. +++ b/.gitignore
  708. @@ -0,0 +1 @@
  709. +*~
  710. \ No newline at end of file
  711. diff --git a/sources b/sources
  712. index 9f44358..2a552bb 100644
  713. --- a/sources
  714. +++ b/sources
  715. @@ -1,2 +1,4 @@
  716. foo
  717. - bar
  718. \ No newline at end of file
  719. + bar
  720. +baz
  721. + boose
  722. \ No newline at end of file
  723. """
  724. patch = '\n'.join(npatch)
  725. #print patch
  726. self.assertEqual(patch, exp)
  727. # Project w/o pull-request
  728. repo = pagure.lib.get_project(self.session, 'test')
  729. settings = repo.settings
  730. settings['pull_requests'] = False
  731. repo.settings = settings
  732. self.session.add(repo)
  733. self.session.commit()
  734. output = self.app.get('/test/pull-request/1.patch')
  735. self.assertEqual(output.status_code, 404)
  736. @patch('pagure.lib.notify.send_email')
  737. def test_request_pull_patch_close(self, send_email):
  738. """ Test the request_pull_patch endpoint with a closed PR. """
  739. send_email.return_value = True
  740. self.test_merge_request_pull_FF()
  741. output = self.app.get('/test/pull-request/1.patch')
  742. self.assertEqual(output.status_code, 200)
  743. npatch = []
  744. for row in output.data.split('\n'):
  745. if row.startswith('Date:'):
  746. continue
  747. if row.startswith('From '):
  748. row = row.split(' ', 2)[2]
  749. npatch.append(row)
  750. exp = """Mon Sep 17 00:00:00 2001
  751. From: Alice Author <alice@authors.tld>
  752. Subject: A commit on branch feature
  753. ---
  754. diff --git a/sources b/sources
  755. index 9f44358..2a552bb 100644
  756. --- a/sources
  757. +++ b/sources
  758. @@ -1,2 +1,4 @@
  759. foo
  760. - bar
  761. \ No newline at end of file
  762. + bar
  763. +baz
  764. + boose
  765. \ No newline at end of file
  766. """
  767. patch = '\n'.join(npatch)
  768. #print patch
  769. self.assertEqual(patch, exp)
  770. @patch('pagure.lib.notify.send_email')
  771. def test_request_pull_patch_empty_repo(self, send_email):
  772. """ Test the request_pull_patch endpoint against an empty repo. """
  773. send_email.return_value = True
  774. tests.create_projects(self.session)
  775. item = pagure.lib.model.Project(
  776. user_id=2, # foo
  777. name='test',
  778. description='test project #1',
  779. hook_token='aaabbb',
  780. parent_id=1,
  781. )
  782. self.session.add(item)
  783. self.session.commit()
  784. tests.create_projects_git(
  785. os.path.join(tests.HERE, 'requests'), bare=True)
  786. tests.create_projects_git(
  787. os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
  788. # Create a git repo to play with
  789. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  790. self.assertFalse(os.path.exists(gitrepo))
  791. os.makedirs(gitrepo)
  792. repo = pygit2.init_repository(gitrepo, bare=True)
  793. # Create a fork of this repo
  794. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  795. gitrepo = os.path.join(
  796. tests.HERE, 'repos', 'forks', 'foo', 'test.git')
  797. new_repo = pygit2.clone_repository(gitrepo, newpath)
  798. # Edit the sources file again
  799. with open(os.path.join(newpath, 'sources'), 'w') as stream:
  800. stream.write('foo\n bar\nbaz\n boose')
  801. new_repo.index.add('sources')
  802. new_repo.index.write()
  803. # Commits the files added
  804. tree = new_repo.index.write_tree()
  805. author = pygit2.Signature(
  806. 'Alice Author', 'alice@authors.tld')
  807. committer = pygit2.Signature(
  808. 'Cecil Committer', 'cecil@committers.tld')
  809. new_repo.create_commit(
  810. 'refs/heads/feature',
  811. author,
  812. committer,
  813. 'A commit on branch feature',
  814. tree,
  815. []
  816. )
  817. refname = 'refs/heads/feature:refs/heads/feature'
  818. ori_remote = new_repo.remotes[0]
  819. PagureRepo.push(ori_remote, refname)
  820. # Create a PR for these "changes" (there are none, both repos are
  821. # empty)
  822. project = pagure.lib.get_project(self.session, 'test')
  823. req = pagure.lib.new_pull_request(
  824. session=self.session,
  825. repo_from=item,
  826. branch_from='feature',
  827. repo_to=project,
  828. branch_to='master',
  829. title='PR from the feature branch',
  830. user='pingou',
  831. requestfolder=None,
  832. )
  833. self.session.commit()
  834. self.assertEqual(req.id, 1)
  835. self.assertEqual(req.title, 'PR from the feature branch')
  836. output = self.app.get(
  837. '/test/pull-request/1.patch', follow_redirects=True)
  838. self.assertEqual(output.status_code, 200)
  839. npatch = []
  840. for row in output.data.split('\n'):
  841. if row.startswith('Date:'):
  842. continue
  843. if row.startswith('From '):
  844. row = row.split(' ', 2)[2]
  845. npatch.append(row)
  846. exp = """Mon Sep 17 00:00:00 2001
  847. From: Alice Author <alice@authors.tld>
  848. Subject: A commit on branch feature
  849. ---
  850. diff --git a/sources b/sources
  851. new file mode 100644
  852. index 0000000..2a552bb
  853. --- /dev/null
  854. +++ b/sources
  855. @@ -0,0 +1,4 @@
  856. +foo
  857. + bar
  858. +baz
  859. + boose
  860. \ No newline at end of file
  861. """
  862. patch = '\n'.join(npatch)
  863. #print patch
  864. self.assertEqual(patch, exp)
  865. shutil.rmtree(newpath)
  866. @patch('pagure.lib.notify.send_email')
  867. def test_request_pull_patch_empty_fork(self, send_email):
  868. """ Test the request_pull_patch endpoint from an empty fork. """
  869. send_email.return_value = True
  870. tests.create_projects(self.session)
  871. item = pagure.lib.model.Project(
  872. user_id=2, # foo
  873. name='test',
  874. description='test project #1',
  875. hook_token='aaabbb',
  876. parent_id=1,
  877. )
  878. self.session.add(item)
  879. self.session.commit()
  880. tests.create_projects_git(
  881. os.path.join(tests.HERE, 'requests'), bare=True)
  882. tests.create_projects_git(
  883. os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
  884. # Create a git repo to play with
  885. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  886. self.assertFalse(os.path.exists(gitrepo))
  887. os.makedirs(gitrepo)
  888. repo = pygit2.init_repository(gitrepo, bare=True)
  889. # Create a fork of this repo
  890. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  891. gitrepo = os.path.join(
  892. tests.HERE, 'repos', 'forks', 'foo', 'test.git')
  893. new_repo = pygit2.clone_repository(gitrepo, newpath)
  894. # Create a PR for these "changes" (there are none, both repos are
  895. # empty)
  896. project = pagure.lib.get_project(self.session, 'test')
  897. req = pagure.lib.new_pull_request(
  898. session=self.session,
  899. repo_from=item,
  900. branch_from='feature',
  901. repo_to=project,
  902. branch_to='master',
  903. title='PR from the feature branch',
  904. user='pingou',
  905. requestfolder=None,
  906. )
  907. self.session.commit()
  908. self.assertEqual(req.id, 1)
  909. self.assertEqual(req.title, 'PR from the feature branch')
  910. output = self.app.get('/test/pull-request/1.patch', follow_redirects=True)
  911. self.assertEqual(output.status_code, 200)
  912. self.assertIn(
  913. '<title>Overview - test - Pagure</title>', output.data)
  914. self.assertIn(
  915. '</button>\n Fork is empty, there are no '
  916. 'commits to request pulling', output.data)
  917. shutil.rmtree(newpath)
  918. @patch('pagure.lib.notify.send_email')
  919. def test_cancel_request_pull(self, send_email):
  920. """ Test the cancel_request_pull endpoint. """
  921. send_email.return_value = True
  922. tests.create_projects(self.session)
  923. tests.create_projects_git(
  924. os.path.join(tests.HERE, 'requests'), bare=True)
  925. self.set_up_git_repo(
  926. new_project=None, branch_from='feature', mtype='merge')
  927. user = tests.FakeUser()
  928. with tests.user_set(pagure.APP, user):
  929. output = self.app.post('/test/pull-request/cancel/1')
  930. self.assertEqual(output.status_code, 302)
  931. output = self.app.post(
  932. '/test/pull-request/cancel/1', follow_redirects=True)
  933. self.assertEqual(output.status_code, 200)
  934. self.assertIn(
  935. '<title>Overview - test - Pagure</title>', output.data)
  936. self.assertIn(
  937. '</button>\n Invalid input submitted',
  938. output.data)
  939. output = self.app.get('/test/pull-request/1')
  940. self.assertEqual(output.status_code, 200)
  941. csrf_token = output.data.split(
  942. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  943. data = {
  944. 'csrf_token': csrf_token,
  945. }
  946. # Invalid project
  947. output = self.app.post(
  948. '/foo/pull-request/cancel/1', data=data, follow_redirects=True)
  949. self.assertEqual(output.status_code, 404)
  950. # Invalid PR id
  951. output = self.app.post(
  952. '/test/pull-request/cancel/100', data=data, follow_redirects=True)
  953. self.assertEqual(output.status_code, 404)
  954. # Invalid user for this project
  955. output = self.app.post(
  956. '/test/pull-request/cancel/1', data=data, follow_redirects=True)
  957. self.assertEqual(output.status_code, 403)
  958. user.username = 'pingou'
  959. with tests.user_set(pagure.APP, user):
  960. # Project w/o pull-request
  961. repo = pagure.lib.get_project(self.session, 'test')
  962. settings = repo.settings
  963. settings['pull_requests'] = False
  964. repo.settings = settings
  965. self.session.add(repo)
  966. self.session.commit()
  967. output = self.app.post(
  968. '/test/pull-request/cancel/1', data=data, follow_redirects=True)
  969. self.assertEqual(output.status_code, 404)
  970. # Project w/o pull-request
  971. repo = pagure.lib.get_project(self.session, 'test')
  972. settings = repo.settings
  973. settings['pull_requests'] = True
  974. repo.settings = settings
  975. self.session.add(repo)
  976. self.session.commit()
  977. output = self.app.post(
  978. '/test/pull-request/cancel/1', data=data, follow_redirects=True)
  979. self.assertEqual(output.status_code, 200)
  980. self.assertIn(
  981. '<title>Overview - test - Pagure</title>', output.data)
  982. self.assertIn(
  983. '</button>\n Request pull canceled!',
  984. output.data)
  985. @patch('pagure.lib.notify.send_email')
  986. def test_set_assignee_requests(self, send_email):
  987. """ Test the set_assignee_requests endpoint. """
  988. send_email.return_value = True
  989. tests.create_projects(self.session)
  990. tests.create_projects_git(
  991. os.path.join(tests.HERE, 'requests'), bare=True)
  992. self.set_up_git_repo(new_project=None, branch_from='feature')
  993. user = tests.FakeUser()
  994. user.username = 'pingou'
  995. with tests.user_set(pagure.APP, user):
  996. # No such project
  997. output = self.app.post('/foo/pull-request/1/assign')
  998. self.assertEqual(output.status_code, 404)
  999. output = self.app.post('/test/pull-request/100/assign')
  1000. self.assertEqual(output.status_code, 404)
  1001. # Invalid input
  1002. output = self.app.post(
  1003. '/test/pull-request/1/assign', follow_redirects=True)
  1004. self.assertEqual(output.status_code, 200)
  1005. self.assertIn(
  1006. '<title>PR#1: PR from the feature branch - test\n - '
  1007. 'Pagure</title>', output.data)
  1008. self.assertIn(
  1009. '<h3><span class="label label-default">PR#1</span>\n'
  1010. ' PR from the feature branch\n', output.data)
  1011. self.assertNotIn(
  1012. '</button>\n Request assigned',
  1013. output.data)
  1014. output = self.app.get('/test/pull-request/1')
  1015. self.assertEqual(output.status_code, 200)
  1016. csrf_token = output.data.split(
  1017. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1018. data = {
  1019. 'user': 'pingou',
  1020. }
  1021. # No CSRF
  1022. output = self.app.post(
  1023. '/test/pull-request/1/assign', data=data,
  1024. follow_redirects=True)
  1025. self.assertEqual(output.status_code, 200)
  1026. self.assertIn(
  1027. '<title>PR#1: PR from the feature branch - test\n - '
  1028. 'Pagure</title>', output.data)
  1029. self.assertIn(
  1030. '<h3><span class="label label-default">PR#1</span>\n'
  1031. ' PR from the feature branch\n', output.data)
  1032. self.assertNotIn(
  1033. '</button>\n Request assigned',
  1034. output.data)
  1035. # Invalid assignee
  1036. data = {
  1037. 'csrf_token': csrf_token,
  1038. 'user': 'bar',
  1039. }
  1040. output = self.app.post(
  1041. '/test/pull-request/1/assign', data=data,
  1042. follow_redirects=True)
  1043. self.assertEqual(output.status_code, 200)
  1044. self.assertIn(
  1045. '<title>PR#1: PR from the feature branch - test\n - '
  1046. 'Pagure</title>', output.data)
  1047. self.assertIn(
  1048. '<h3><span class="label label-default">PR#1</span>\n'
  1049. ' PR from the feature branch\n', output.data)
  1050. self.assertIn(
  1051. '</button>\n No user &#34;bar&#34; found',
  1052. output.data)
  1053. # Assign the PR
  1054. data = {
  1055. 'csrf_token': csrf_token,
  1056. 'user': 'pingou',
  1057. }
  1058. user.username = 'foo'
  1059. with tests.user_set(pagure.APP, user):
  1060. output = self.app.post(
  1061. '/test/pull-request/1/assign', data=data,
  1062. follow_redirects=True)
  1063. self.assertEqual(output.status_code, 403)
  1064. user.username = 'pingou'
  1065. with tests.user_set(pagure.APP, user):
  1066. output = self.app.post(
  1067. '/test/pull-request/1/assign', data=data,
  1068. follow_redirects=True)
  1069. self.assertEqual(output.status_code, 200)
  1070. self.assertIn(
  1071. '<title>PR#1: PR from the feature branch - test\n - '
  1072. 'Pagure</title>', output.data)
  1073. self.assertIn(
  1074. '<h3><span class="label label-default">PR#1</span>\n'
  1075. ' PR from the feature branch\n', output.data)
  1076. self.assertIn(
  1077. '</button>\n Request assigned',
  1078. output.data)
  1079. # Pull-Request closed
  1080. repo = pagure.lib.get_project(self.session, 'test')
  1081. req = repo.requests[0]
  1082. req.status = 'Closed'
  1083. req.closed_by_in = 1
  1084. self.session.add(req)
  1085. self.session.commit()
  1086. output = self.app.post(
  1087. '/test/pull-request/1/assign', data=data,
  1088. follow_redirects=True)
  1089. self.assertEqual(output.status_code, 403)
  1090. # Project w/o pull-request
  1091. repo = pagure.lib.get_project(self.session, 'test')
  1092. settings = repo.settings
  1093. settings['pull_requests'] = False
  1094. repo.settings = settings
  1095. self.session.add(repo)
  1096. self.session.commit()
  1097. output = self.app.post(
  1098. '/test/pull-request/1/assign', data=data,
  1099. follow_redirects=True)
  1100. self.assertEqual(output.status_code, 404)
  1101. @patch('pagure.lib.notify.send_email')
  1102. def test_fork_project(self, send_email):
  1103. """ Test the fork_project endpoint. """
  1104. send_email.return_value = True
  1105. tests.create_projects(self.session)
  1106. for folder in ['docs', 'tickets', 'requests', 'repos']:
  1107. tests.create_projects_git(
  1108. os.path.join(tests.HERE, folder), bare=True)
  1109. user = tests.FakeUser()
  1110. user.username = 'pingou'
  1111. with tests.user_set(pagure.APP, user):
  1112. output = self.app.post('/do_fork/test')
  1113. self.assertEqual(output.status_code, 400)
  1114. output = self.app.get('/new/')
  1115. self.assertEqual(output.status_code, 200)
  1116. self.assertIn('<strong>Create new Project</strong>', output.data)
  1117. csrf_token = output.data.split(
  1118. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1119. data = {
  1120. 'csrf_token': csrf_token,
  1121. }
  1122. output = self.app.post(
  1123. '/do_fork/foo', data=data, follow_redirects=True)
  1124. self.assertEqual(output.status_code, 404)
  1125. user.username = 'foo'
  1126. with tests.user_set(pagure.APP, user):
  1127. output = self.app.post('/do_fork/test')
  1128. self.assertEqual(output.status_code, 400)
  1129. data = {
  1130. 'csrf_token': csrf_token,
  1131. }
  1132. output = self.app.post(
  1133. '/do_fork/test', data=data, follow_redirects=True)
  1134. self.assertEqual(output.status_code, 200)
  1135. self.assertIn(
  1136. '</button>\n Repo &#34;test&#34; '
  1137. 'cloned to &#34;foo/test&#34;', output.data)
  1138. @patch('pagure.lib.notify.send_email')
  1139. def test_new_request_pull(self, send_email):
  1140. """ Test the new_request_pull endpoint. """
  1141. send_email.return_value = True
  1142. self.test_fork_project()
  1143. tests.create_projects_git(
  1144. os.path.join(tests.HERE, 'requests'), bare=True)
  1145. repo = pagure.lib.get_project(self.session, 'test')
  1146. fork = pagure.lib.get_project(self.session, 'test', user='foo')
  1147. self.set_up_git_repo(
  1148. new_project=fork, branch_from='feature', mtype='FF')
  1149. user = tests.FakeUser()
  1150. user.username = 'foo'
  1151. with tests.user_set(pagure.APP, user):
  1152. output = self.app.get('/foo/diff/master..feature')
  1153. self.assertEqual(output.status_code, 404)
  1154. output = self.app.get('/test/diff/master..foo')
  1155. self.assertEqual(output.status_code, 400)
  1156. output = self.app.get('/test/diff/foo..master')
  1157. self.assertEqual(output.status_code, 400)
  1158. output = self.app.get('/test/diff/feature..master')
  1159. self.assertEqual(output.status_code, 200)
  1160. self.assertIn(
  1161. '<title>Diff from master to feature - test - Pagure</title>',
  1162. output.data)
  1163. self.assertIn(
  1164. '<p class="error"> No commits found </p>', output.data)
  1165. output = self.app.get('/test/diff/master..feature')
  1166. self.assertEqual(output.status_code, 200)
  1167. self.assertIn(
  1168. '<title>Diff from feature to master - test - Pagure</title>',
  1169. output.data)
  1170. self.assertNotIn(
  1171. '<input type="submit" class="submit positive button" '
  1172. 'value="Create">', output.data)
  1173. user.username = 'pingou'
  1174. with tests.user_set(pagure.APP, user):
  1175. output = self.app.get('/test/diff/master..feature')
  1176. self.assertEqual(output.status_code, 200)
  1177. self.assertIn(
  1178. '<title> Create new Pull Request for master - test - '
  1179. 'Pagure</title>', output.data)
  1180. self.assertIn(
  1181. '<input type="submit" class="btn btn-primary" value="Create">',
  1182. output.data)
  1183. csrf_token = output.data.split(
  1184. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1185. # Case 1 - Add an initial comment
  1186. data = {
  1187. 'csrf_token': csrf_token,
  1188. 'title': 'foo bar PR',
  1189. 'initial_comment': 'Test Initial Comment',
  1190. }
  1191. output = self.app.post(
  1192. '/test/diff/master..feature', data=data, follow_redirects=True)
  1193. self.assertEqual(output.status_code, 200)
  1194. self.assertIn(
  1195. '<title>PR#2: foo bar PR - test\n - Pagure</title>',
  1196. output.data)
  1197. self.assertIn('<p>Test Initial Comment</p>', output.data)
  1198. # Case 2 - Add an empty initial comment
  1199. data = {
  1200. 'csrf_token': csrf_token,
  1201. 'title': 'foo bar PR',
  1202. 'initial_comment': '',
  1203. }
  1204. output = self.app.post(
  1205. '/test/diff/master..feature', data=data, follow_redirects=True)
  1206. self.assertEqual(output.status_code, 200)
  1207. self.assertIn(
  1208. '<title>PR#3: foo bar PR - test\n - Pagure</title>',
  1209. output.data)
  1210. self.assertNotIn('<div id="comment-', output.data)
  1211. @patch('pagure.lib.notify.send_email')
  1212. def test_new_request_pull_empty_repo(self, send_email):
  1213. """ Test the new_request_pull endpoint against an empty repo. """
  1214. send_email.return_value = True
  1215. self.test_fork_project()
  1216. tests.create_projects_git(
  1217. os.path.join(tests.HERE, 'requests'), bare=True)
  1218. repo = pagure.lib.get_project(self.session, 'test')
  1219. fork = pagure.lib.get_project(self.session, 'test', user='foo')
  1220. # Create a git repo to play with
  1221. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  1222. repo = pygit2.init_repository(gitrepo, bare=True)
  1223. # Create a fork of this repo
  1224. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  1225. gitrepo = os.path.join(tests.HERE, 'repos', 'forks', 'foo', 'test.git')
  1226. new_repo = pygit2.clone_repository(gitrepo, newpath)
  1227. user = tests.FakeUser()
  1228. user.username = 'foo'
  1229. with tests.user_set(pagure.APP, user):
  1230. output = self.app.get(
  1231. '/fork/foo/test/diff/master..feature',
  1232. follow_redirects=True)
  1233. self.assertEqual(output.status_code, 200)
  1234. self.assertIn(
  1235. '<title>Overview - test - Pagure</title>', output.data)
  1236. self.assertIn(
  1237. '</button>\n Fork is empty, there are '
  1238. 'no commits to request pulling', output.data)
  1239. output = self.app.get('/test/new_issue')
  1240. csrf_token = output.data.split(
  1241. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1242. data = {
  1243. 'csrf_token': csrf_token,
  1244. 'title': 'foo bar PR',
  1245. }
  1246. output = self.app.post(
  1247. '/test/diff/master..feature', data=data, follow_redirects=True)
  1248. self.assertEqual(output.status_code, 200)
  1249. self.assertIn(
  1250. '<title>Overview - test - Pagure</title>', output.data)
  1251. self.assertIn(
  1252. '</button>\n Fork is empty, there are '
  1253. 'no commits to request pulling', output.data)
  1254. shutil.rmtree(newpath)
  1255. @patch('pagure.lib.notify.send_email')
  1256. def test_new_request_pull_empty_fork(self, send_email):
  1257. """ Test the new_request_pull endpoint against an empty repo. """
  1258. send_email.return_value = True
  1259. self.test_fork_project()
  1260. tests.create_projects_git(
  1261. os.path.join(tests.HERE, 'requests'), bare=True)
  1262. repo = pagure.lib.get_project(self.session, 'test')
  1263. fork = pagure.lib.get_project(self.session, 'test', user='foo')
  1264. # Create a git repo to play with
  1265. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  1266. repo = pygit2.init_repository(gitrepo, bare=True)
  1267. # Create a fork of this repo
  1268. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  1269. gitrepo = os.path.join(
  1270. tests.HERE, 'repos', 'forks', 'foo', 'test.git')
  1271. new_repo = pygit2.clone_repository(gitrepo, newpath)
  1272. user = tests.FakeUser()
  1273. user.username = 'foo'
  1274. with tests.user_set(pagure.APP, user):
  1275. output = self.app.get(
  1276. '/fork/foo/test/diff/master..master', follow_redirects=True)
  1277. self.assertIn(
  1278. '<title>Overview - test - Pagure</title>', output.data)
  1279. self.assertIn(
  1280. '</button>\n Fork is empty, there are '
  1281. 'no commits to request pulling', output.data)
  1282. shutil.rmtree(newpath)
  1283. @patch('pagure.lib.notify.send_email')
  1284. def test_pull_request_add_comment(self, send_email):
  1285. """ Test the pull_request_add_comment endpoint. """
  1286. send_email.return_value = True
  1287. self.test_request_pull()
  1288. user = tests.FakeUser()
  1289. user.username = 'pingou'
  1290. with tests.user_set(pagure.APP, user):
  1291. output = self.app.post('/foo/pull-request/1/comment')
  1292. self.assertEqual(output.status_code, 404)
  1293. output = self.app.post('/test/pull-request/100/comment')
  1294. self.assertEqual(output.status_code, 404)
  1295. output = self.app.post('/test/pull-request/1/comment')
  1296. self.assertEqual(output.status_code, 200)
  1297. self.assertTrue(
  1298. output.data.startswith('\n<section class="add_comment">'))
  1299. csrf_token = output.data.split(
  1300. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1301. data = {
  1302. 'csrf_token': csrf_token,
  1303. 'comment': 'This look alright but we can do better',
  1304. }
  1305. output = self.app.post(
  1306. '/test/pull-request/1/comment', data=data,
  1307. follow_redirects=True)
  1308. self.assertEqual(output.status_code, 200)
  1309. self.assertIn(
  1310. '<title>PR#1: PR from the feature branch - test\n - '
  1311. 'Pagure</title>', output.data)
  1312. self.assertIn(
  1313. '</button>\n Comment added',
  1314. output.data)
  1315. # Project w/o pull-request
  1316. repo = pagure.lib.get_project(self.session, 'test')
  1317. settings = repo.settings
  1318. settings['pull_requests'] = False
  1319. repo.settings = settings
  1320. self.session.add(repo)
  1321. self.session.commit()
  1322. output = self.app.post(
  1323. '/test/pull-request/1/comment', data=data,
  1324. follow_redirects=True)
  1325. self.assertEqual(output.status_code, 404)
  1326. @patch('pagure.lib.notify.send_email')
  1327. def test_pull_request_drop_comment(self, send_email):
  1328. """ Test the pull_request_drop_comment endpoint. """
  1329. send_email.return_value = True
  1330. self.test_pull_request_add_comment()
  1331. # Project w/ pull-request
  1332. repo = pagure.lib.get_project(self.session, 'test')
  1333. settings = repo.settings
  1334. settings['pull_requests'] = True
  1335. repo.settings = settings
  1336. self.session.add(repo)
  1337. self.session.commit()
  1338. user = tests.FakeUser()
  1339. user.username = 'foo'
  1340. with tests.user_set(pagure.APP, user):
  1341. output = self.app.post('/foo/pull-request/1/comment/drop')
  1342. self.assertEqual(output.status_code, 404)
  1343. output = self.app.post('/test/pull-request/100/comment/drop')
  1344. self.assertEqual(output.status_code, 404)
  1345. output = self.app.post(
  1346. '/test/pull-request/1/comment/drop', follow_redirects=True)
  1347. self.assertEqual(output.status_code, 200)
  1348. self.assertIn(
  1349. '<h3><span class="label label-default">PR#1</span>\n'
  1350. ' PR from the feature branch\n</h3>', output.data)
  1351. #self.assertIn('href="#comment-1">¶</a>', output.data)
  1352. self.assertIn(
  1353. '<p>This look alright but we can do better</p>',
  1354. output.data)
  1355. csrf_token = output.data.split(
  1356. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1357. # Invalid comment id
  1358. data = {
  1359. 'csrf_token': csrf_token,
  1360. 'drop_comment': '10',
  1361. }
  1362. output = self.app.post(
  1363. '/test/pull-request/1/comment/drop', data=data,
  1364. follow_redirects=True)
  1365. self.assertEqual(output.status_code, 404)
  1366. data['drop_comment'] = '1'
  1367. output = self.app.post(
  1368. '/test/pull-request/1/comment/drop', data=data,
  1369. follow_redirects=True)
  1370. self.assertEqual(output.status_code, 403)
  1371. user.username = 'pingou'
  1372. with tests.user_set(pagure.APP, user):
  1373. # Drop comment
  1374. output = self.app.post(
  1375. '/test/pull-request/1/comment/drop', data=data,
  1376. follow_redirects=True)
  1377. self.assertEqual(output.status_code, 200)
  1378. self.assertIn(
  1379. '<h3><span class="label label-default">PR#1</span>\n'
  1380. ' PR from the feature branch\n <span class="pull-xs-right">',
  1381. output.data)
  1382. self.assertIn(
  1383. '</button>\n Comment removed',
  1384. output.data)
  1385. # Project w/o pull-request
  1386. repo = pagure.lib.get_project(self.session, 'test')
  1387. settings = repo.settings
  1388. settings['pull_requests'] = False
  1389. repo.settings = settings
  1390. self.session.add(repo)
  1391. self.session.commit()
  1392. output = self.app.post(
  1393. '/test/pull-request/1/comment/drop', data=data,
  1394. follow_redirects=True)
  1395. self.assertEqual(output.status_code, 404)
  1396. @patch('pagure.lib.notify.send_email')
  1397. def test_pull_request_edit_comment(self, send_email):
  1398. """ Test the pull request edit comment endpoint """
  1399. send_email.return_value = True
  1400. self.test_request_pull()
  1401. user = tests.FakeUser()
  1402. user.username = 'pingou'
  1403. with tests.user_set(pagure.APP, user):
  1404. # Repo 'foo' does not exist so it is verifying that condition
  1405. output = self.app.post('/foo/pull-request/1/comment/1/edit')
  1406. self.assertEqual(output.status_code, 404)
  1407. # Here no comment is present in the PR so its verifying that condition
  1408. output = self.app.post('/test/pull-request/100/comment/100/edit')
  1409. self.assertEqual(output.status_code, 404)
  1410. output = self.app.post('/test/pull-request/1/comment')
  1411. self.assertEqual(output.status_code, 200)
  1412. # Creating comment to play with
  1413. self.assertTrue(
  1414. output.data.startswith('\n<section class="add_comment">'))
  1415. csrf_token = output.data.split(
  1416. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1417. data = {
  1418. 'csrf_token': csrf_token,
  1419. 'comment': 'This look alright but we can do better',
  1420. }
  1421. output = self.app.post(
  1422. '/test/pull-request/1/comment', data=data,
  1423. follow_redirects=True)
  1424. self.assertEqual(output.status_code, 200)
  1425. self.assertIn(
  1426. '<h3><span class="label label-default">PR#1</span>\n'
  1427. ' PR from the feature branch\n <span class="pull-xs-right">',
  1428. output.data)
  1429. self.assertIn(
  1430. '</button>\n Comment added',
  1431. output.data)
  1432. # Check if the comment is there
  1433. self.assertIn(
  1434. '<p>This look alright but we can do better</p>', output.data)
  1435. output = self.app.get('/test/pull-request/1/comment/1/edit')
  1436. self.assertEqual(output.status_code, 200)
  1437. self.assertIn('<section class="edit_comment">', output.data)
  1438. # Checking if the comment is there in the update page
  1439. self.assertIn(
  1440. 'This look alright but we can do better</textarea>', output.data)
  1441. csrf_token = output.data.split(
  1442. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1443. data = {
  1444. 'csrf_token': csrf_token,
  1445. 'update_comment': 'This look alright but we can do better than this.',
  1446. }
  1447. output = self.app.post(
  1448. '/test/pull-request/1/comment/1/edit', data=data,
  1449. follow_redirects=True)
  1450. # Checking if the comment is updated in the main page
  1451. self.assertIn(
  1452. '<p>This look alright but we can do better than this.</p>', output.data)
  1453. self.assertIn(
  1454. '<h3><span class="label label-default">PR#1</span>\n'
  1455. ' PR from the feature branch\n <span class="pull-xs-right">',
  1456. output.data)
  1457. # Checking if Edited by User is there or not
  1458. self.assertTrue(
  1459. '<small class="text-muted">Edited just now by pingou </small>'
  1460. in output.data
  1461. or
  1462. '<small class="text-muted">Edited seconds ago by pingou </small>'
  1463. in output.data)
  1464. self.assertIn(
  1465. '</button>\n Comment updated', output.data)
  1466. # Project w/o pull-request
  1467. repo = pagure.lib.get_project(self.session, 'test')
  1468. settings = repo.settings
  1469. settings['pull_requests'] = False
  1470. repo.settings = settings
  1471. self.session.add(repo)
  1472. self.session.commit()
  1473. output = self.app.post(
  1474. '/test/pull-request/1/comment/edit/1', data=data,
  1475. follow_redirects=True)
  1476. self.assertEqual(output.status_code, 404)
  1477. @patch('pagure.lib.notify.send_email')
  1478. def test_merge_request_pull_FF_w_merge_commit(self, send_email):
  1479. """ Test the merge_request_pull endpoint with a FF PR but with a
  1480. merge commit.
  1481. """
  1482. send_email.return_value = True
  1483. self.test_request_pull()
  1484. user = tests.FakeUser()
  1485. with tests.user_set(pagure.APP, user):
  1486. output = self.app.get('/test/pull-request/1')
  1487. self.assertEqual(output.status_code, 200)
  1488. csrf_token = output.data.split(
  1489. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1490. # No CSRF
  1491. output = self.app.post(
  1492. '/test/pull-request/1/merge', data={}, follow_redirects=True)
  1493. self.assertEqual(output.status_code, 200)
  1494. self.assertIn(
  1495. '<title>PR#1: PR from the feature branch - test\n - '
  1496. 'Pagure</title>', output.data)
  1497. self.assertIn(
  1498. '<h3><span class="label label-default">PR#1</span>\n'
  1499. ' PR from the feature branch\n</h3>', output.data)
  1500. self.assertIn(
  1501. 'title="View file as of 2a552b">sources</a>', output.data)
  1502. # Wrong project
  1503. data = {
  1504. 'csrf_token': csrf_token,
  1505. }
  1506. output = self.app.post(
  1507. '/foobar/pull-request/100/merge', data=data, follow_redirects=True)
  1508. self.assertEqual(output.status_code, 404)
  1509. # Wrong project
  1510. data = {
  1511. 'csrf_token': csrf_token,
  1512. }
  1513. output = self.app.post(
  1514. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  1515. self.assertEqual(output.status_code, 403)
  1516. user.username = 'pingou'
  1517. with tests.user_set(pagure.APP, user):
  1518. # Wrong request id
  1519. data = {
  1520. 'csrf_token': csrf_token,
  1521. }
  1522. output = self.app.post(
  1523. '/test/pull-request/100/merge', data=data, follow_redirects=True)
  1524. self.assertEqual(output.status_code, 404)
  1525. # Project requiring a merge commit
  1526. repo = pagure.lib.get_project(self.session, 'test')
  1527. settings = repo.settings
  1528. settings['always_merge'] = True
  1529. repo.settings = settings
  1530. self.session.add(repo)
  1531. self.session.commit()
  1532. # Merge
  1533. output = self.app.post(
  1534. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  1535. self.assertEqual(output.status_code, 200)
  1536. self.assertIn(
  1537. '<title>Overview - test - Pagure</title>', output.data)
  1538. self.assertIn(
  1539. '</button>\n Changes merged!',
  1540. output.data)
  1541. self.assertIn(
  1542. 'Merge #1 `PR from the feature branch`', output.data)
  1543. self.assertIn(
  1544. 'A commit on branch feature', output.data)
  1545. # Ensure we have the merge commit
  1546. commits = _get_commits(output.data)
  1547. self.assertEqual(commits, [
  1548. 'Merge #1 `PR from the feature branch`',
  1549. 'Add sources file for testing',
  1550. 'A commit on branch feature',
  1551. ])
  1552. # Check if the closing notification was added
  1553. output = self.app.get('/test/pull-request/1')
  1554. self.assertIn(
  1555. '<small><p>Pull-Request has been merged by pingou</p></small>',
  1556. output.data)
  1557. @patch('pagure.lib.notify.send_email')
  1558. def test_fork_edit_file(self, send_email):
  1559. """ Test the fork_edit file endpoint. """
  1560. send_email.return_value = True
  1561. # User not logged in
  1562. output = self.app.post('fork_edit/test/edit/master/f/sources')
  1563. self.assertEqual(output.status_code, 302)
  1564. tests.create_projects(self.session)
  1565. for folder in ['docs', 'tickets', 'requests', 'repos']:
  1566. tests.create_projects_git(
  1567. os.path.join(tests.HERE, folder), bare=True)
  1568. user = tests.FakeUser()
  1569. user.username = 'pingou'
  1570. with tests.user_set(pagure.APP, user):
  1571. # Invalid request
  1572. output = self.app.post('fork_edit/test/edit/master/f/source')
  1573. self.assertEqual(output.status_code, 400)
  1574. output = self.app.get('/new/')
  1575. self.assertEqual(output.status_code, 200)
  1576. self.assertIn('<strong>Create new Project</strong>', output.data)
  1577. csrf_token = output.data.split(
  1578. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1579. data = {
  1580. 'csrf_token': csrf_token,
  1581. }
  1582. # No files can be found since they are not added
  1583. output = self.app.post('fork_edit/test/edit/master/f/sources',
  1584. data=data, follow_redirects=True)
  1585. self.assertEqual(output.status_code, 404)
  1586. user = tests.FakeUser()
  1587. user.username = 'foo'
  1588. with tests.user_set(pagure.APP, user):
  1589. data = {
  1590. 'csrf_token': csrf_token,
  1591. }
  1592. # Invalid request
  1593. output = self.app.post('fork_edit/test/edit/master/f/sources',
  1594. follow_redirects=True)
  1595. self.assertEqual(output.status_code, 400)
  1596. # Add content to the repo
  1597. tests.add_content_git_repo(os.path.join(
  1598. pagure.APP.config['GIT_FOLDER'], 'test.git'))
  1599. tests.add_readme_git_repo(os.path.join(
  1600. pagure.APP.config['GIT_FOLDER'], 'test.git'))
  1601. tests.add_binary_git_repo(
  1602. os.path.join(
  1603. pagure.APP.config['GIT_FOLDER'], 'test.git'), 'test.jpg')
  1604. # Check if button exists
  1605. output = self.app.get('/test/blob/master/f/sources')
  1606. self.assertEqual(output.status_code, 200)
  1607. self.assertIn(
  1608. 'Fork and Edit\n </button>\n',
  1609. output.data)
  1610. # Check fork-edit doesn't show for binary files
  1611. output = self.app.get('/test/blob/master/f/test.jpg')
  1612. self.assertEqual(output.status_code, 200)
  1613. self.assertNotIn(
  1614. 'Fork and Edit\n </button>\n',
  1615. output.data)
  1616. # Check for edit panel
  1617. output = self.app.post('fork_edit/test/edit/master/f/sources',
  1618. data=data, follow_redirects=True)
  1619. self.assertEqual(output.status_code, 200)
  1620. self.assertIn(
  1621. '<li><a href="/fork/foo/test/tree/master">'
  1622. '<span class="oi" data-glyph="random"></span>&nbsp; master</a>'
  1623. '</li><li class="active"><span class="oi" data-glyph="file">'
  1624. '</span>&nbsp; sources</li>',
  1625. output.data)
  1626. self.assertIn(
  1627. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  1628. output.data)
  1629. self.assertIn(
  1630. '</button>\n Repo &#34;test&#34; '
  1631. 'cloned to &#34;foo/test&#34;', output.data)
  1632. # View what's supposed to be an image
  1633. output = self.app.post('fork_edit/test/edit/master/f/test.jpg',
  1634. data=data, follow_redirects=True)
  1635. self.assertEqual(output.status_code, 400)
  1636. self.assertIn('<p>Cannot edit binary files</p>', output.data)
  1637. # Check fork-edit shows when user is not logged in
  1638. output = self.app.get('/test/blob/master/f/sources')
  1639. self.assertEqual(output.status_code, 200)
  1640. self.assertIn(
  1641. 'Fork and Edit\n </button>\n',
  1642. output.data)
  1643. # Check if fork-edit shows for different user
  1644. user.username = 'pingou'
  1645. with tests.user_set(pagure.APP, user):
  1646. # Check if button exists
  1647. output = self.app.get('/test/blob/master/f/sources')
  1648. self.assertEqual(output.status_code, 200)
  1649. self.assertIn(
  1650. 'Fork and Edit\n </button>\n',
  1651. output.data)
  1652. # Check fork-edit doesn't show for binary
  1653. output = self.app.get('/test/blob/master/f/test.jpg')
  1654. self.assertEqual(output.status_code, 200)
  1655. self.assertNotIn(
  1656. 'Fork and Edit\n </button>\n',
  1657. output.data)
  1658. if __name__ == '__main__':
  1659. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests)
  1660. unittest.TextTestRunner(verbosity=2).run(SUITE)