test_pagure_flask_internal.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2017 - 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 os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure
  19. import pagure.lib
  20. import tests
  21. class PagureFlaskInternaltests(tests.Modeltests):
  22. """ Tests for flask Internal controller of pagure """
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskInternaltests, self).setUp()
  26. pagure.APP.config['TESTING'] = True
  27. pagure.APP.config['IP_ALLOWED_INTERNAL'] = list(set(
  28. pagure.APP.config['IP_ALLOWED_INTERNAL'] + [None]))
  29. pagure.SESSION = self.session
  30. pagure.internal.SESSION = self.session
  31. pagure.ui.repo.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.APP.config['GIT_FOLDER'] = os.path.join(
  34. self.path, 'repos')
  35. pagure.APP.config['REQUESTS_FOLDER'] = None
  36. pagure.APP.config['TICKETS_FOLDER'] = None
  37. pagure.APP.config['DOCS_FOLDER'] = None
  38. self.app = pagure.APP.test_client()
  39. @patch('pagure.lib.notify.send_email')
  40. def test_pull_request_add_comment(self, send_email):
  41. """ Test the pull_request_add_comment function. """
  42. send_email.return_value = True
  43. tests.create_projects(self.session)
  44. repo = pagure.get_authorized_project(self.session, 'test')
  45. req = pagure.lib.new_pull_request(
  46. session=self.session,
  47. repo_from=repo,
  48. branch_from='feature',
  49. repo_to=repo,
  50. branch_to='master',
  51. title='PR from the feature branch',
  52. user='pingou',
  53. requestfolder=None,
  54. )
  55. self.session.commit()
  56. self.assertEqual(req.id, 1)
  57. self.assertEqual(req.title, 'PR from the feature branch')
  58. request = repo.requests[0]
  59. self.assertEqual(len(request.comments), 0)
  60. self.assertEqual(len(request.discussion), 0)
  61. data = {
  62. 'objid': 'foo',
  63. }
  64. # Wrong http request
  65. output = self.app.post('/pv/pull-request/comment/', data=data)
  66. self.assertEqual(output.status_code, 405)
  67. # Invalid request
  68. output = self.app.put('/pv/pull-request/comment/', data=data)
  69. self.assertEqual(output.status_code, 400)
  70. data = {
  71. 'objid': 'foo',
  72. 'useremail': 'foo@pingou.com',
  73. }
  74. # Invalid objid
  75. output = self.app.put('/pv/pull-request/comment/', data=data)
  76. self.assertEqual(output.status_code, 404)
  77. data = {
  78. 'objid': request.uid,
  79. 'useremail': 'foo@pingou.com',
  80. }
  81. # Valid objid, in-complete data for a comment
  82. output = self.app.put('/pv/pull-request/comment/', data=data)
  83. self.assertEqual(output.status_code, 400)
  84. data = {
  85. 'objid': request.uid,
  86. 'useremail': 'foo@pingou.com',
  87. 'comment': 'Looks good to me!',
  88. }
  89. # Add comment
  90. output = self.app.put('/pv/pull-request/comment/', data=data)
  91. self.assertEqual(output.status_code, 200)
  92. js_data = json.loads(output.data)
  93. self.assertDictEqual(js_data, {'message': 'Comment added'})
  94. repo = pagure.get_authorized_project(self.session, 'test')
  95. request = repo.requests[0]
  96. self.assertEqual(len(request.comments), 1)
  97. self.assertEqual(len(request.discussion), 1)
  98. # Check the @localonly
  99. before = pagure.APP.config['IP_ALLOWED_INTERNAL'][:]
  100. pagure.APP.config['IP_ALLOWED_INTERNAL'] = []
  101. output = self.app.put('/pv/pull-request/comment/', data=data)
  102. self.assertEqual(output.status_code, 403)
  103. pagure.APP.config['IP_ALLOWED_INTERNAL'] = before[:]
  104. @patch('pagure.lib.notify.send_email')
  105. def test_ticket_add_comment(self, send_email):
  106. """ Test the ticket_add_comment function. """
  107. send_email.return_value = True
  108. tests.create_projects(self.session)
  109. # Create issues to play with
  110. repo = pagure.get_authorized_project(self.session, 'test')
  111. msg = pagure.lib.new_issue(
  112. session=self.session,
  113. repo=repo,
  114. title='Test issue',
  115. content='We should work on this',
  116. user='pingou',
  117. ticketfolder=None
  118. )
  119. self.session.commit()
  120. self.assertEqual(msg.title, 'Test issue')
  121. issue = repo.issues[0]
  122. self.assertEqual(len(issue.comments), 0)
  123. data = {
  124. 'objid': 'foo',
  125. }
  126. # Wrong http request
  127. output = self.app.post('/pv/ticket/comment/', data=data)
  128. self.assertEqual(output.status_code, 405)
  129. # Invalid request
  130. output = self.app.put('/pv/ticket/comment/', data=data)
  131. self.assertEqual(output.status_code, 400)
  132. data = {
  133. 'objid': 'foo',
  134. 'useremail': 'foo@pingou.com',
  135. }
  136. # Invalid objid
  137. output = self.app.put('/pv/ticket/comment/', data=data)
  138. self.assertEqual(output.status_code, 404)
  139. data = {
  140. 'objid': issue.uid,
  141. 'useremail': 'foo@pingou.com',
  142. }
  143. # Valid objid, in-complete data for a comment
  144. output = self.app.put('/pv/ticket/comment/', data=data)
  145. self.assertEqual(output.status_code, 400)
  146. data = {
  147. 'objid': issue.uid,
  148. 'useremail': 'foo@pingou.com',
  149. 'comment': 'Looks good to me!',
  150. }
  151. # Add comment
  152. output = self.app.put('/pv/ticket/comment/', data=data)
  153. self.assertEqual(output.status_code, 200)
  154. js_data = json.loads(output.data)
  155. self.assertDictEqual(js_data, {'message': 'Comment added'})
  156. repo = pagure.get_authorized_project(self.session, 'test')
  157. issue = repo.issues[0]
  158. self.assertEqual(len(issue.comments), 1)
  159. # Check the @localonly
  160. pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None)
  161. before = pagure.APP.config['IP_ALLOWED_INTERNAL'][:]
  162. pagure.APP.config['IP_ALLOWED_INTERNAL'] = []
  163. output = self.app.put('/pv/ticket/comment/', data=data)
  164. self.assertEqual(output.status_code, 403)
  165. pagure.APP.config['IP_ALLOWED_INTERNAL'] = before[:]
  166. @patch('pagure.lib.notify.send_email')
  167. def test_private_ticket_add_comment(self, send_email):
  168. """ Test the ticket_add_comment function on a private ticket. """
  169. send_email.return_value = True
  170. tests.create_projects(self.session)
  171. # Create issues to play with
  172. repo = pagure.get_authorized_project(self.session, 'test')
  173. msg = pagure.lib.new_issue(
  174. session=self.session,
  175. repo=repo,
  176. title='Test issue',
  177. content='We should work on this, really',
  178. user='pingou',
  179. private=True,
  180. ticketfolder=None
  181. )
  182. self.session.commit()
  183. self.assertEqual(msg.title, 'Test issue')
  184. issue = repo.issues[0]
  185. self.assertEqual(len(issue.comments), 0)
  186. data = {
  187. 'objid': 'foo',
  188. }
  189. # Wrong http request
  190. output = self.app.post('/pv/ticket/comment/', data=data)
  191. self.assertEqual(output.status_code, 405)
  192. # Invalid request
  193. output = self.app.put('/pv/ticket/comment/', data=data)
  194. self.assertEqual(output.status_code, 400)
  195. data = {
  196. 'objid': 'foo',
  197. 'useremail': 'foo@pingou.com',
  198. }
  199. # Invalid objid
  200. output = self.app.put('/pv/ticket/comment/', data=data)
  201. self.assertEqual(output.status_code, 404)
  202. data = {
  203. 'objid': issue.uid,
  204. 'useremail': 'foo@bar.com',
  205. }
  206. # Valid objid, un-allowed user for this (private) ticket
  207. output = self.app.put('/pv/ticket/comment/', data=data)
  208. self.assertEqual(output.status_code, 403)
  209. data = {
  210. 'objid': issue.uid,
  211. 'useremail': 'foo@pingou.com',
  212. }
  213. # Valid objid, un-allowed user for this (private) ticket
  214. output = self.app.put('/pv/ticket/comment/', data=data)
  215. self.assertEqual(output.status_code, 400)
  216. data = {
  217. 'objid': issue.uid,
  218. 'useremail': 'foo@pingou.com',
  219. 'comment': 'Looks good to me!',
  220. }
  221. # Add comment
  222. output = self.app.put('/pv/ticket/comment/', data=data)
  223. self.assertEqual(output.status_code, 200)
  224. js_data = json.loads(output.data)
  225. self.assertDictEqual(js_data, {'message': 'Comment added'})
  226. repo = pagure.get_authorized_project(self.session, 'test')
  227. issue = repo.issues[0]
  228. self.assertEqual(len(issue.comments), 1)
  229. # Check the @localonly
  230. before = pagure.APP.config['IP_ALLOWED_INTERNAL'][:]
  231. pagure.APP.config['IP_ALLOWED_INTERNAL'] = []
  232. output = self.app.put('/pv/ticket/comment/', data=data)
  233. self.assertEqual(output.status_code, 403)
  234. pagure.APP.config['IP_ALLOWED_INTERNAL'] = before[:]
  235. @patch('pagure.lib.notify.send_email')
  236. def test_private_ticket_add_comment_acl(self, send_email):
  237. """ Test the ticket_add_comment function on a private ticket. """
  238. send_email.return_value = True
  239. tests.create_projects(self.session)
  240. # Create issues to play with
  241. repo = pagure.get_authorized_project(self.session, 'test')
  242. msg = pagure.lib.new_issue(
  243. session=self.session,
  244. repo=repo,
  245. title='Test issue',
  246. content='We should work on this, really',
  247. user='pingou',
  248. private=True,
  249. ticketfolder=None
  250. )
  251. self.session.commit()
  252. self.assertEqual(msg.title, 'Test issue')
  253. repo = pagure.get_authorized_project(self.session, 'test')
  254. issue = repo.issues[0]
  255. self.assertEqual(len(issue.comments), 0)
  256. # Currently, he is just an average user,
  257. # He doesn't have any access in this repo
  258. data = {
  259. 'objid': issue.uid,
  260. 'useremail': 'foo@bar.com',
  261. 'comment': 'Looks good to me!',
  262. }
  263. # Valid objid, un-allowed user for this (private) ticket
  264. output = self.app.put('/pv/ticket/comment/', data=data)
  265. self.assertEqual(output.status_code, 403)
  266. repo = pagure.get_authorized_project(self.session, 'test')
  267. # Let's promote him to be a ticketer
  268. # He shoudn't be able to comment even then though
  269. msg = pagure.lib.add_user_to_project(
  270. self.session,
  271. project=repo,
  272. new_user='foo',
  273. user='pingou',
  274. access='ticket'
  275. )
  276. self.session.commit()
  277. self.assertEqual(msg, 'User added')
  278. repo = pagure.get_authorized_project(self.session, 'test')
  279. self.assertEqual(
  280. sorted([u.username for u in repo.users]), ['foo'])
  281. self.assertEqual(
  282. sorted([u.username for u in repo.committers]), [])
  283. self.assertEqual(
  284. sorted([u.username for u in repo.admins]), [])
  285. output = self.app.put('/pv/ticket/comment/', data=data)
  286. self.assertEqual(output.status_code, 403)
  287. repo = pagure.get_authorized_project(self.session, 'test')
  288. # Let's promote him to be a committer
  289. # He should be able to comment
  290. msg = pagure.lib.add_user_to_project(
  291. self.session,
  292. project=repo,
  293. new_user='foo',
  294. user='pingou',
  295. access='commit'
  296. )
  297. self.session.commit()
  298. self.assertEqual(msg, 'User access updated')
  299. repo = pagure.get_authorized_project(self.session, 'test')
  300. self.assertEqual(
  301. sorted([u.username for u in repo.users]), ['foo'])
  302. self.assertEqual(
  303. sorted([u.username for u in repo.committers]), ['foo'])
  304. self.assertEqual(
  305. sorted([u.username for u in repo.admins]), [])
  306. # Add comment
  307. output = self.app.put('/pv/ticket/comment/', data=data)
  308. self.assertEqual(output.status_code, 200)
  309. js_data = json.loads(output.data)
  310. self.assertDictEqual(js_data, {'message': 'Comment added'})
  311. repo = pagure.get_authorized_project(self.session, 'test')
  312. issue = repo.issues[0]
  313. self.assertEqual(len(issue.comments), 1)
  314. # Let's promote him to be a admin
  315. # He should be able to comment
  316. msg = pagure.lib.add_user_to_project(
  317. self.session,
  318. project=repo,
  319. new_user='foo',
  320. user='pingou',
  321. access='admin'
  322. )
  323. self.session.commit()
  324. self.assertEqual(msg, 'User access updated')
  325. repo = pagure.get_authorized_project(self.session, 'test')
  326. self.assertEqual(
  327. sorted([u.username for u in repo.users]), ['foo'])
  328. self.assertEqual(
  329. sorted([u.username for u in repo.committers]), ['foo'])
  330. self.assertEqual(
  331. sorted([u.username for u in repo.admins]), ['foo'])
  332. # Add comment
  333. output = self.app.put('/pv/ticket/comment/', data=data)
  334. self.assertEqual(output.status_code, 200)
  335. js_data = json.loads(output.data)
  336. self.assertDictEqual(js_data, {'message': 'Comment added'})
  337. repo = pagure.get_authorized_project(self.session, 'test')
  338. issue = repo.issues[0]
  339. self.assertEqual(len(issue.comments), 2)
  340. # Check the @localonly
  341. before = pagure.APP.config['IP_ALLOWED_INTERNAL'][:]
  342. pagure.APP.config['IP_ALLOWED_INTERNAL'] = []
  343. output = self.app.put('/pv/ticket/comment/', data=data)
  344. self.assertEqual(output.status_code, 403)
  345. pagure.APP.config['IP_ALLOWED_INTERNAL'] = before[:]
  346. @patch('pagure.lib.notify.send_email')
  347. def test_mergeable_request_pull_FF(self, send_email):
  348. """ Test the mergeable_request_pull endpoint with a fast-forward
  349. merge.
  350. """
  351. send_email.return_value = True
  352. # Create a git repo to play with
  353. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  354. self.assertFalse(os.path.exists(gitrepo))
  355. os.makedirs(gitrepo)
  356. repo = pygit2.init_repository(gitrepo)
  357. # Create a file in that git repo
  358. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  359. stream.write('foo\n bar')
  360. repo.index.add('sources')
  361. repo.index.write()
  362. # Commits the files added
  363. tree = repo.index.write_tree()
  364. author = pygit2.Signature(
  365. 'Alice Author', 'alice@authors.tld')
  366. committer = pygit2.Signature(
  367. 'Cecil Committer', 'cecil@committers.tld')
  368. repo.create_commit(
  369. 'refs/heads/master', # the name of the reference to update
  370. author,
  371. committer,
  372. 'Add sources file for testing',
  373. # binary string representing the tree object ID
  374. tree,
  375. # list of binary strings representing parents of the new commit
  376. []
  377. )
  378. first_commit = repo.revparse_single('HEAD')
  379. # Edit the sources file again
  380. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  381. stream.write('foo\n bar\nbaz\n boose')
  382. repo.index.add('sources')
  383. repo.index.write()
  384. # Commits the files added
  385. tree = repo.index.write_tree()
  386. author = pygit2.Signature(
  387. 'Alice Author', 'alice@authors.tld')
  388. committer = pygit2.Signature(
  389. 'Cecil Committer', 'cecil@committers.tld')
  390. repo.create_commit(
  391. 'refs/heads/feature', # the name of the reference to update
  392. author,
  393. committer,
  394. 'Add baz and boose to the sources\n\n There are more objects to '
  395. 'consider',
  396. # binary string representing the tree object ID
  397. tree,
  398. # list of binary strings representing parents of the new commit
  399. [first_commit.oid.hex]
  400. )
  401. second_commit = repo.revparse_single('HEAD')
  402. # Create a PR for these changes
  403. tests.create_projects(self.session)
  404. project = pagure.get_authorized_project(self.session, 'test')
  405. req = pagure.lib.new_pull_request(
  406. session=self.session,
  407. repo_from=project,
  408. branch_from='feature',
  409. repo_to=project,
  410. branch_to='master',
  411. title='PR from the feature branch',
  412. user='pingou',
  413. requestfolder=None,
  414. )
  415. self.session.commit()
  416. self.assertEqual(req.id, 1)
  417. self.assertEqual(req.title, 'PR from the feature branch')
  418. # Check if the PR can be merged
  419. data = {
  420. 'objid': 'blah',
  421. }
  422. # Missing CSRF
  423. output = self.app.post('/pv/pull-request/merge', data=data)
  424. self.assertEqual(output.status_code, 400)
  425. user = tests.FakeUser()
  426. user.username = 'pingou'
  427. with tests.user_set(pagure.APP, user):
  428. output = self.app.get('/test/adduser')
  429. csrf_token = output.data.split(
  430. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  431. # Missing request identifier
  432. data = {
  433. 'csrf_token': csrf_token,
  434. }
  435. output = self.app.post('/pv/pull-request/merge', data=data)
  436. self.assertEqual(output.status_code, 404)
  437. # With all the desired information
  438. project = pagure.get_authorized_project(self.session, 'test')
  439. data = {
  440. 'csrf_token': csrf_token,
  441. 'requestid': project.requests[0].uid,
  442. }
  443. output = self.app.post('/pv/pull-request/merge', data=data)
  444. self.assertEqual(output.status_code, 200)
  445. exp = {
  446. "code": "FFORWARD",
  447. "message": "The pull-request can be merged and fast-forwarded",
  448. "short_code": "Ok"
  449. }
  450. js_data = json.loads(output.data)
  451. self.assertDictEqual(js_data, exp)
  452. @patch('pagure.lib.notify.send_email')
  453. def test_mergeable_request_pull_no_change(self, send_email):
  454. """ Test the mergeable_request_pull endpoint when there are no
  455. changes to merge.
  456. """
  457. send_email.return_value = True
  458. # Create a git repo to play with
  459. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  460. self.assertFalse(os.path.exists(gitrepo))
  461. os.makedirs(gitrepo)
  462. repo = pygit2.init_repository(gitrepo)
  463. # Create a file in that git repo
  464. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  465. stream.write('foo\n bar')
  466. repo.index.add('sources')
  467. repo.index.write()
  468. # Commits the files added
  469. tree = repo.index.write_tree()
  470. author = pygit2.Signature(
  471. 'Alice Author', 'alice@authors.tld')
  472. committer = pygit2.Signature(
  473. 'Cecil Committer', 'cecil@committers.tld')
  474. repo.create_commit(
  475. 'refs/heads/master', # the name of the reference to update
  476. author,
  477. committer,
  478. 'Add sources file for testing',
  479. # binary string representing the tree object ID
  480. tree,
  481. # list of binary strings representing parents of the new commit
  482. []
  483. )
  484. first_commit = repo.revparse_single('HEAD')
  485. # Edit the sources file again
  486. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  487. stream.write('foo\n bar\nbaz\n boose')
  488. repo.index.add('sources')
  489. repo.index.write()
  490. # Commits the files added
  491. tree = repo.index.write_tree()
  492. author = pygit2.Signature(
  493. 'Alice Author', 'alice@authors.tld')
  494. committer = pygit2.Signature(
  495. 'Cecil Committer', 'cecil@committers.tld')
  496. repo.create_commit(
  497. 'refs/heads/master', # the name of the reference to update
  498. author,
  499. committer,
  500. 'Add baz and boose to the sources\n\n There are more objects to '
  501. 'consider',
  502. # binary string representing the tree object ID
  503. tree,
  504. # list of binary strings representing parents of the new commit
  505. [first_commit.oid.hex]
  506. )
  507. second_commit = repo.revparse_single('HEAD')
  508. # Create a PR for these changes
  509. tests.create_projects(self.session)
  510. project = pagure.get_authorized_project(self.session, 'test')
  511. req = pagure.lib.new_pull_request(
  512. session=self.session,
  513. repo_from=project,
  514. branch_from='master',
  515. repo_to=project,
  516. branch_to='master',
  517. title='PR from the feature branch',
  518. user='pingou',
  519. requestfolder=None,
  520. )
  521. self.session.commit()
  522. self.assertEqual(req.id, 1)
  523. self.assertEqual(req.title, 'PR from the feature branch')
  524. # Check if the PR can be merged
  525. data = {
  526. 'objid': 'blah',
  527. }
  528. # Missing CSRF
  529. output = self.app.post('/pv/pull-request/merge', data=data)
  530. self.assertEqual(output.status_code, 400)
  531. user = tests.FakeUser()
  532. user.username = 'pingou'
  533. with tests.user_set(pagure.APP, user):
  534. output = self.app.get('/test/adduser')
  535. csrf_token = output.data.split(
  536. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  537. # Missing request identifier
  538. data = {
  539. 'csrf_token': csrf_token,
  540. }
  541. output = self.app.post('/pv/pull-request/merge', data=data)
  542. self.assertEqual(output.status_code, 404)
  543. # With all the desired information
  544. project = pagure.get_authorized_project(self.session, 'test')
  545. data = {
  546. 'csrf_token': csrf_token,
  547. 'requestid': project.requests[0].uid,
  548. }
  549. output = self.app.post('/pv/pull-request/merge', data=data)
  550. self.assertEqual(output.status_code, 200)
  551. exp = {
  552. "code": "NO_CHANGE",
  553. "message": "Nothing to change, git is up to date",
  554. "short_code": "No changes"
  555. }
  556. js_data = json.loads(output.data)
  557. self.assertDictEqual(js_data, exp)
  558. @patch('pagure.lib.notify.send_email')
  559. def test_mergeable_request_pull_merge(self, send_email):
  560. """ Test the mergeable_request_pull endpoint when the changes can
  561. be merged with a merge commit.
  562. """
  563. send_email.return_value = True
  564. # Create a git repo to play with
  565. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  566. self.assertFalse(os.path.exists(gitrepo))
  567. os.makedirs(gitrepo)
  568. repo = pygit2.init_repository(gitrepo)
  569. # Create a file in that git repo
  570. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  571. stream.write('foo\n bar')
  572. repo.index.add('sources')
  573. repo.index.write()
  574. # Commits the files added
  575. tree = repo.index.write_tree()
  576. author = pygit2.Signature(
  577. 'Alice Author', 'alice@authors.tld')
  578. committer = pygit2.Signature(
  579. 'Cecil Committer', 'cecil@committers.tld')
  580. repo.create_commit(
  581. 'refs/heads/master', # the name of the reference to update
  582. author,
  583. committer,
  584. 'Add sources file for testing',
  585. # binary string representing the tree object ID
  586. tree,
  587. # list of binary strings representing parents of the new commit
  588. []
  589. )
  590. first_commit = repo.revparse_single('HEAD')
  591. # Edit the sources file again
  592. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  593. stream.write('foo\n bar\nbaz\n boose')
  594. repo.index.add('sources')
  595. repo.index.write()
  596. # Commits the files added
  597. tree = repo.index.write_tree()
  598. author = pygit2.Signature(
  599. 'Alice Author', 'alice@authors.tld')
  600. committer = pygit2.Signature(
  601. 'Cecil Committer', 'cecil@committers.tld')
  602. repo.create_commit(
  603. 'refs/heads/feature', # the name of the reference to update
  604. author,
  605. committer,
  606. 'Add baz and boose to the sources\n\n There are more objects to '
  607. 'consider',
  608. # binary string representing the tree object ID
  609. tree,
  610. # list of binary strings representing parents of the new commit
  611. [first_commit.oid.hex]
  612. )
  613. # Create another file in the master branch
  614. with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
  615. stream.write('*~')
  616. repo.index.add('.gitignore')
  617. repo.index.write()
  618. # Commits the files added
  619. tree = repo.index.write_tree()
  620. author = pygit2.Signature(
  621. 'Alice Author', 'alice@authors.tld')
  622. committer = pygit2.Signature(
  623. 'Cecil Committer', 'cecil@committers.tld')
  624. repo.create_commit(
  625. 'refs/heads/master', # the name of the reference to update
  626. author,
  627. committer,
  628. 'Add .gitignore file for testing',
  629. # binary string representing the tree object ID
  630. tree,
  631. # list of binary strings representing parents of the new commit
  632. [first_commit.oid.hex]
  633. )
  634. # Create a PR for these changes
  635. tests.create_projects(self.session)
  636. project = pagure.get_authorized_project(self.session, 'test')
  637. req = pagure.lib.new_pull_request(
  638. session=self.session,
  639. repo_from=project,
  640. branch_from='feature',
  641. repo_to=project,
  642. branch_to='master',
  643. title='PR from the feature branch',
  644. user='pingou',
  645. requestfolder=None,
  646. )
  647. self.session.commit()
  648. self.assertEqual(req.id, 1)
  649. self.assertEqual(req.title, 'PR from the feature branch')
  650. # Check if the PR can be merged
  651. data = {}
  652. # Missing CSRF
  653. output = self.app.post('/pv/pull-request/merge', data=data)
  654. self.assertEqual(output.status_code, 400)
  655. user = tests.FakeUser()
  656. user.username = 'pingou'
  657. with tests.user_set(pagure.APP, user):
  658. output = self.app.get('/test/adduser')
  659. csrf_token = output.data.split(
  660. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  661. # Missing request identifier
  662. data = {
  663. 'csrf_token': csrf_token,
  664. }
  665. output = self.app.post('/pv/pull-request/merge', data=data)
  666. self.assertEqual(output.status_code, 404)
  667. # With all the desired information
  668. project = pagure.get_authorized_project(self.session, 'test')
  669. data = {
  670. 'csrf_token': csrf_token,
  671. 'requestid': project.requests[0].uid,
  672. }
  673. output = self.app.post('/pv/pull-request/merge', data=data)
  674. self.assertEqual(output.status_code, 200)
  675. exp = {
  676. "code": "MERGE",
  677. "message": "The pull-request can be merged with a merge commit",
  678. "short_code": "With merge"
  679. }
  680. js_data = json.loads(output.data)
  681. self.assertDictEqual(js_data, exp)
  682. @patch('pagure.lib.notify.send_email')
  683. def test_mergeable_request_pull_conflicts(self, send_email):
  684. """ Test the mergeable_request_pull endpoint when the changes cannot
  685. be merged due to conflicts.
  686. """
  687. send_email.return_value = True
  688. # Create a git repo to play with
  689. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  690. self.assertFalse(os.path.exists(gitrepo))
  691. os.makedirs(gitrepo)
  692. repo = pygit2.init_repository(gitrepo)
  693. # Create a file in that git repo
  694. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  695. stream.write('foo\n bar')
  696. repo.index.add('sources')
  697. repo.index.write()
  698. # Commits the files added
  699. tree = repo.index.write_tree()
  700. author = pygit2.Signature(
  701. 'Alice Author', 'alice@authors.tld')
  702. committer = pygit2.Signature(
  703. 'Cecil Committer', 'cecil@committers.tld')
  704. repo.create_commit(
  705. 'refs/heads/master', # the name of the reference to update
  706. author,
  707. committer,
  708. 'Add sources file for testing',
  709. # binary string representing the tree object ID
  710. tree,
  711. # list of binary strings representing parents of the new commit
  712. []
  713. )
  714. first_commit = repo.revparse_single('HEAD')
  715. # Edit the sources file again
  716. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  717. stream.write('foo\n bar\nbaz\n boose')
  718. repo.index.add('sources')
  719. repo.index.write()
  720. # Commits the files added
  721. tree = repo.index.write_tree()
  722. author = pygit2.Signature(
  723. 'Alice Author', 'alice@authors.tld')
  724. committer = pygit2.Signature(
  725. 'Cecil Committer', 'cecil@committers.tld')
  726. repo.create_commit(
  727. 'refs/heads/feature', # the name of the reference to update
  728. author,
  729. committer,
  730. 'Add baz and boose to the sources\n\n There are more objects to '
  731. 'consider',
  732. # binary string representing the tree object ID
  733. tree,
  734. # list of binary strings representing parents of the new commit
  735. [first_commit.oid.hex]
  736. )
  737. # Create another file in the master branch
  738. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  739. stream.write('foo\n bar\nbaz\n')
  740. repo.index.add('sources')
  741. repo.index.write()
  742. # Commits the files added
  743. tree = repo.index.write_tree()
  744. author = pygit2.Signature(
  745. 'Alice Author', 'alice@authors.tld')
  746. committer = pygit2.Signature(
  747. 'Cecil Committer', 'cecil@committers.tld')
  748. repo.create_commit(
  749. 'refs/heads/master', # the name of the reference to update
  750. author,
  751. committer,
  752. 'Add .gitignore file for testing',
  753. # binary string representing the tree object ID
  754. tree,
  755. # list of binary strings representing parents of the new commit
  756. [first_commit.oid.hex]
  757. )
  758. # Create a PR for these changes
  759. tests.create_projects(self.session)
  760. project = pagure.get_authorized_project(self.session, 'test')
  761. req = pagure.lib.new_pull_request(
  762. session=self.session,
  763. repo_from=project,
  764. branch_from='feature',
  765. repo_to=project,
  766. branch_to='master',
  767. title='PR from the feature branch',
  768. user='pingou',
  769. requestfolder=None,
  770. )
  771. self.session.commit()
  772. self.assertEqual(req.id, 1)
  773. self.assertEqual(req.title, 'PR from the feature branch')
  774. # Check if the PR can be merged
  775. data = {}
  776. # Missing CSRF
  777. output = self.app.post('/pv/pull-request/merge', data=data)
  778. self.assertEqual(output.status_code, 400)
  779. user = tests.FakeUser()
  780. user.username = 'pingou'
  781. with tests.user_set(pagure.APP, user):
  782. output = self.app.get('/test/adduser')
  783. csrf_token = output.data.split(
  784. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  785. # Missing request identifier
  786. data = {
  787. 'csrf_token': csrf_token,
  788. }
  789. output = self.app.post('/pv/pull-request/merge', data=data)
  790. self.assertEqual(output.status_code, 404)
  791. # With all the desired information
  792. project = pagure.get_authorized_project(self.session, 'test')
  793. data = {
  794. 'csrf_token': csrf_token,
  795. 'requestid': project.requests[0].uid,
  796. }
  797. output = self.app.post('/pv/pull-request/merge', data=data)
  798. self.assertEqual(output.status_code, 200)
  799. exp = {
  800. "code": "CONFLICTS",
  801. "message": "The pull-request cannot be merged due to conflicts",
  802. "short_code": "Conflicts"
  803. }
  804. js_data = json.loads(output.data)
  805. self.assertDictEqual(js_data, exp)
  806. def test_get_branches_of_commit(self):
  807. ''' Test the get_branches_of_commit from the internal API. '''
  808. tests.create_projects(self.session)
  809. tests.create_projects_git(os.path.join(self.path, 'repos'))
  810. user = tests.FakeUser()
  811. user.username = 'pingou'
  812. with tests.user_set(pagure.APP, user):
  813. output = self.app.get('/test/adduser')
  814. self.assertEqual(output.status_code, 200)
  815. csrf_token = output.data.split(
  816. b'name="csrf_token" type="hidden" value="')[1].split(b'">')[0]
  817. # No CSRF token
  818. data = {
  819. 'repo': 'fakerepo',
  820. 'commit_id': 'foo',
  821. }
  822. output = self.app.post('/pv/branches/commit/', data=data)
  823. self.assertEqual(output.status_code, 400)
  824. js_data = json.loads(output.data.decode('utf-8'))
  825. self.assertDictEqual(
  826. js_data,
  827. {u'code': u'ERROR', u'message': u'Invalid input submitted'}
  828. )
  829. # Invalid repo
  830. data = {
  831. 'repo': 'fakerepo',
  832. 'commit_id': 'foo',
  833. 'csrf_token': csrf_token,
  834. }
  835. output = self.app.post('/pv/branches/commit/', data=data)
  836. self.assertEqual(output.status_code, 404)
  837. js_data = json.loads(output.data.decode('utf-8'))
  838. self.assertDictEqual(
  839. js_data,
  840. {
  841. u'code': u'ERROR',
  842. u'message': u'No repo found with the information provided'
  843. }
  844. )
  845. # Rigth repo, no commit
  846. data = {
  847. 'repo': 'test',
  848. 'csrf_token': csrf_token,
  849. }
  850. output = self.app.post('/pv/branches/commit/', data=data)
  851. self.assertEqual(output.status_code, 400)
  852. js_data = json.loads(output.data.decode('utf-8'))
  853. self.assertDictEqual(
  854. js_data,
  855. {u'code': u'ERROR', u'message': u'No commit id submitted'}
  856. )
  857. # Request is fine, but git repo doesn't exist
  858. item = pagure.lib.model.Project(
  859. user_id=1, # pingou
  860. name='test20',
  861. description='test project #20',
  862. hook_token='aaabbbhhh',
  863. )
  864. self.session.add(item)
  865. self.session.commit()
  866. data = {
  867. 'repo': 'test20',
  868. 'commit_id': 'foo',
  869. 'csrf_token': csrf_token,
  870. }
  871. output = self.app.post('/pv/branches/commit/', data=data)
  872. self.assertEqual(output.status_code, 404)
  873. js_data = json.loads(output.data.decode('utf-8'))
  874. self.assertDictEqual(
  875. js_data,
  876. {
  877. u'code': u'ERROR',
  878. u'message': u'No git repo found with the information provided'
  879. }
  880. )
  881. # Create a git repo to play with
  882. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  883. self.assertTrue(os.path.exists(gitrepo))
  884. repo = pygit2.Repository(gitrepo)
  885. # Create a file in that git repo
  886. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  887. stream.write('foo\n bar')
  888. repo.index.add('sources')
  889. repo.index.write()
  890. # Commits the files added
  891. tree = repo.index.write_tree()
  892. author = pygit2.Signature(
  893. 'Alice Author', 'alice@authors.tld')
  894. committer = pygit2.Signature(
  895. 'Cecil Committer', 'cecil@committers.tld')
  896. repo.create_commit(
  897. 'refs/heads/master', # the name of the reference to update
  898. author,
  899. committer,
  900. 'Add sources file for testing',
  901. # binary string representing the tree object ID
  902. tree,
  903. # list of binary strings representing parents of the new commit
  904. []
  905. )
  906. first_commit = repo.revparse_single('HEAD')
  907. # Edit the sources file again
  908. with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
  909. stream.write('foo\n bar\nbaz\n boose')
  910. repo.index.add('sources')
  911. repo.index.write()
  912. # Commits the files added
  913. tree = repo.index.write_tree()
  914. author = pygit2.Signature(
  915. 'Alice Author', 'alice@authors.tld')
  916. committer = pygit2.Signature(
  917. 'Cecil Committer', 'cecil@committers.tld')
  918. repo.create_commit(
  919. 'refs/heads/feature', # the name of the reference to update
  920. author,
  921. committer,
  922. 'Add baz and boose to the sources\n\n There are more objects to '
  923. 'consider',
  924. # binary string representing the tree object ID
  925. tree,
  926. # list of binary strings representing parents of the new commit
  927. [first_commit.oid.hex]
  928. )
  929. # Create another file in the master branch
  930. with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
  931. stream.write('*~')
  932. repo.index.add('.gitignore')
  933. repo.index.write()
  934. # Commits the files added
  935. tree = repo.index.write_tree()
  936. author = pygit2.Signature(
  937. 'Alice Author', 'alice@authors.tld')
  938. committer = pygit2.Signature(
  939. 'Cecil Committer', 'cecil@committers.tld')
  940. commit_hash = repo.create_commit(
  941. 'refs/heads/feature_branch', # the name of the reference to update
  942. author,
  943. committer,
  944. 'Add .gitignore file for testing',
  945. # binary string representing the tree object ID
  946. tree,
  947. # list of binary strings representing parents of the new commit
  948. [first_commit.oid.hex]
  949. )
  950. # All good but the commit id
  951. data = {
  952. 'repo': 'test',
  953. 'commit_id': 'foo',
  954. 'csrf_token': csrf_token,
  955. }
  956. output = self.app.post('/pv/branches/commit/', data=data)
  957. self.assertEqual(output.status_code, 404)
  958. js_data = json.loads(output.data.decode('utf-8'))
  959. self.assertDictEqual(
  960. js_data,
  961. {
  962. u'code': u'ERROR',
  963. u'message': 'This commit could not be found in this repo'
  964. }
  965. )
  966. # All good
  967. data = {
  968. 'repo': 'test',
  969. 'commit_id': commit_hash,
  970. 'csrf_token': csrf_token,
  971. }
  972. output = self.app.post('/pv/branches/commit/', data=data)
  973. self.assertEqual(output.status_code, 200)
  974. js_data = json.loads(output.data.decode('utf-8'))
  975. self.assertDictEqual(
  976. js_data,
  977. {
  978. u'code': u'OK',
  979. u'branches': ['feature_branch'],
  980. }
  981. )
  982. if __name__ == '__main__':
  983. unittest.main(verbosity=2)