12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847 |
- # -*- coding: utf-8 -*-
- """
- (c) 2015 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- __requires__ = ['SQLAlchemy >= 0.8']
- import pkg_resources
- import json
- import unittest
- import shutil
- import sys
- import tempfile
- import os
- import pygit2
- from mock import patch
- sys.path.insert(0, os.path.join(os.path.dirname(
- os.path.abspath(__file__)), '..'))
- import pagure.lib
- import tests
- from pagure.lib.repo import PagureRepo
- def _get_commits(output):
- ''' Returns the commits message in the output. All commits must have
- been made by `Alice Author` or `PY C` to be found.
- '''
- commits = []
- save = False
- cnt = 0
- for row in output.split('\n'):
- if row.strip() in ['Alice Author', 'Alice Äuthòr', 'PY C']:
- save = True
- if save:
- cnt += 1
- if cnt == 7:
- commits.append(row.strip())
- save = False
- cnt = 0
- return commits
- class PagureFlaskForktests(tests.Modeltests):
- """ Tests for flask fork controller of pagure """
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskForktests, self).setUp()
- pagure.APP.config['TESTING'] = True
- pagure.SESSION = self.session
- pagure.lib.SESSION = self.session
- pagure.ui.SESSION = self.session
- pagure.ui.app.SESSION = self.session
- pagure.ui.filters.SESSION = self.session
- pagure.ui.fork.SESSION = self.session
- pagure.ui.repo.SESSION = self.session
- pagure.ui.issues.SESSION = self.session
- pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
- pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
- tests.HERE, 'tickets')
- pagure.APP.config['DOCS_FOLDER'] = os.path.join(
- tests.HERE, 'docs')
- pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
- tests.HERE, 'requests')
- self.app = pagure.APP.test_client()
- def set_up_git_repo(
- self, new_project=None, branch_from='feature', mtype='FF'):
- """ Set up the git repo and create the corresponding PullRequest
- object.
- """
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- repo = pygit2.init_repository(gitrepo, bare=True)
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- repopath = os.path.join(newpath, 'test')
- clone_repo = pygit2.clone_repository(gitrepo, repopath)
- # Create a file in that git repo
- with open(os.path.join(repopath, 'sources'), 'w') as stream:
- stream.write('foo\n bar')
- clone_repo.index.add('sources')
- clone_repo.index.write()
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature(
- 'Alice Author', 'alice@authors.tld')
- committer = pygit2.Signature(
- 'Cecil Committer', 'cecil@committers.tld')
- clone_repo.create_commit(
- 'refs/heads/master', # the name of the reference to update
- author,
- committer,
- 'Add sources file for testing',
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- []
- )
- refname = 'refs/heads/master:refs/heads/master'
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- first_commit = repo.revparse_single('HEAD')
- if mtype == 'merge':
- with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
- stream.write('*~')
- clone_repo.index.add('.gitignore')
- clone_repo.index.write()
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature(
- 'Alice Äuthòr', 'alice@äuthòrs.tld')
- committer = pygit2.Signature(
- 'Cecil Cõmmîttër', 'cecil@cõmmîttërs.tld')
- clone_repo.create_commit(
- 'refs/heads/master',
- author,
- committer,
- 'Add .gitignore file for testing',
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [first_commit.oid.hex]
- )
- refname = 'refs/heads/master:refs/heads/master'
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- if mtype == 'conflicts':
- with open(os.path.join(repopath, 'sources'), 'w') as stream:
- stream.write('foo\n bar\nbaz')
- clone_repo.index.add('sources')
- clone_repo.index.write()
- # Commits the files added
- tree = clone_repo.index.write_tree()
- author = pygit2.Signature(
- 'Alice Author', 'alice@authors.tld')
- committer = pygit2.Signature(
- 'Cecil Committer', 'cecil@committers.tld')
- clone_repo.create_commit(
- 'refs/heads/master',
- author,
- committer,
- 'Add sources conflicting',
- # binary string representing the tree object ID
- tree,
- # list of binary strings representing parents of the new commit
- [first_commit.oid.hex]
- )
- refname = 'refs/heads/master:refs/heads/master'
- ori_remote = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Set the second repo
- new_gitrepo = repopath
- if new_project:
- # Create a new git repo to play with
- new_gitrepo = os.path.join(newpath, new_project.fullname)
- if not os.path.exists(new_gitrepo):
- os.makedirs(new_gitrepo)
- new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
- repo = pygit2.Repository(new_gitrepo)
- if mtype != 'nochanges':
- # Edit the sources file again
- with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream:
- stream.write('foo\n bar\nbaz\n boose')
- repo.index.add('sources')
- repo.index.write()
- # Commits the files added
- tree = repo.index.write_tree()
- author = pygit2.Signature(
- 'Alice Author', 'alice@authors.tld')
- committer = pygit2.Signature(
- 'Cecil Committer', 'cecil@committers.tld')
- repo.create_commit(
- 'refs/heads/%s' % branch_from,
- author,
- committer,
- 'A commit on branch %s' % branch_from,
- tree,
- [first_commit.oid.hex]
- )
- refname = 'refs/heads/%s' % (branch_from)
- ori_remote = repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these changes
- project = pagure.lib.get_project(self.session, 'test')
- req = pagure.lib.new_pull_request(
- session=self.session,
- repo_from=project,
- branch_from=branch_from,
- repo_to=project,
- branch_to='master',
- title='PR from the %s branch' % branch_from,
- user='pingou',
- requestfolder=None,
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, 'PR from the %s branch' % branch_from)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull(self, send_email):
- """ Test the request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- # Non-existant project
- output = self.app.get('/foobar/pull-request/1')
- self.assertEqual(output.status_code, 404)
- # Project has no PR
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 404)
- self.set_up_git_repo(new_project=None, branch_from='feature')
- project = pagure.lib.get_project(self.session, 'test')
- self.assertEqual(len(project.requests), 1)
- # View the pull-request
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n</h3>', output.data)
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output.data)
- @patch('pagure.lib.notify.send_email')
- def test_merge_request_pull_FF(self, send_email):
- """ Test the merge_request_pull endpoint with a FF PR. """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- # No CSRF
- output = self.app.post(
- '/test/pull-request/1/merge', data={}, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n</h3>', output.data)
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output.data)
- # Wrong project
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/foobar/pull-request/100/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Wrong project
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 403)
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # Wrong request id
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/test/pull-request/100/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Pull-request disabled
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Project w pull-request but only assignee can merge
- settings['pull_requests'] = True
- settings['Only_assignee_can_merge_pull-request'] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- self.assertIn(
- '</button>\n This request must be '
- 'assigned to be merged', output.data)
- # PR assigned but not to this user
- repo = pagure.lib.get_project(self.session, 'test')
- req = repo.requests[0]
- req.assignee_id = 2
- self.session.add(req)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- self.assertIn(
- '</button>\n Only the assignee can '
- 'merge this review', output.data)
- # Project w/ minimal PR score
- settings['Only_assignee_can_merge_pull-request'] = False
- settings['Minimum_score_to_merge_pull-request'] = 2
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- self.assertIn(
- '</button>\n This request does not '
- 'have the minimum review score necessary to be merged',
- output.data)
- # Merge
- settings['Minimum_score_to_merge_pull-request'] = -1
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Changes merged!',
- output.data)
- self.assertIn(
- 'A commit on branch feature', output.data)
- self.assertNotIn(
- 'Merge #1 `PR from the feature branch`', output.data)
- # Ensure we have the new commit
- commits = _get_commits(output.data)
- self.assertEqual(
- commits,
- [
- 'A commit on branch feature',
- 'Add sources file for testing'
- ]
- )
- # Check if the closing notification was added
- output = self.app.get('/test/pull-request/1')
- self.assertIn(
- '<small><p>Pull-Request has been merged by pingou</p></small>',
- output.data)
- @patch('pagure.lib.notify.send_email')
- def test_merge_request_pull_merge(self, send_email):
- """ Test the merge_request_pull endpoint with a merge PR. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(
- new_project=None, branch_from='feature', mtype='merge')
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- }
- # Merge
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Changes merged!',
- output.data)
- # Check if the closing notification was added
- output = self.app.get('/test/pull-request/1')
- self.assertIn(
- '<small><p>Pull-Request has been merged by pingou</p></small>',
- output.data)
- @patch('pagure.lib.notify.send_email')
- def test_merge_request_pull_conflicts(self, send_email):
- """ Test the merge_request_pull endpoint with a conflicting PR. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(
- new_project=None, branch_from='feature', mtype='conflicts')
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- }
- # Merge conflicts
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- self.assertIn(
- '</button>\n Merge conflicts!',
- output.data)
- @patch('pagure.lib.notify.send_email')
- def test_merge_request_pull_nochange(self, send_email):
- """ Test the merge_request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(
- new_project=None, branch_from='master', mtype='nochanges')
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- }
- # Nothing to merge
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' <span class="label label-success">Merged</span>',
- output.data)
- self.assertIn(
- '</button>\n Nothing to do, changes '
- 'were already merged', output.data)
- # Check if the closing notification was added
- output = self.app.get('/test/pull-request/1')
- self.assertIn(
- '<small><p>Pull-Request has been merged by pingou</p></small>',
- output.data)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_close(self, send_email):
- """ Test the request_pull endpoint with a closed PR. """
- send_email.return_value = True
- self.test_merge_request_pull_FF()
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' <span class="label label-success">', output.data)
- self.assertIn('<div>Merged by\n', output.data)
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output.data)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_disabled(self, send_email):
- """ Test the request_pull endpoint with PR disabled. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(new_project=None, branch_from='feature')
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_empty_repo(self, send_email):
- """ Test the request_pull endpoint against an empty repo. """
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- gitrepo = os.path.join(tests.HERE, 'repos', 'forks', 'foo', 'test.git')
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Edit the sources file again
- with open(os.path.join(newpath, 'sources'), 'w') as stream:
- stream.write('foo\n bar\nbaz\n boose')
- new_repo.index.add('sources')
- new_repo.index.write()
- # Commits the files added
- tree = new_repo.index.write_tree()
- author = pygit2.Signature(
- 'Alice Author', 'alice@authors.tld')
- committer = pygit2.Signature(
- 'Cecil Committer', 'cecil@committers.tld')
- new_repo.create_commit(
- 'refs/heads/feature',
- author,
- committer,
- 'A commit on branch feature',
- tree,
- []
- )
- refname = 'refs/heads/feature:refs/heads/feature'
- ori_remote = new_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these changes
- project = pagure.lib.get_project(self.session, 'test')
- req = pagure.lib.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from='feature',
- repo_to=project,
- branch_to='master',
- title='PR from the feature branch',
- user='pingou',
- requestfolder=None,
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, 'PR from the feature branch')
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n</h3>', output.data)
- self.assertTrue(
- output.data.count('<span class="commitdate" title='), 1)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_empty_fork(self, send_email):
- """ Test the request_pull endpoint from an empty fork. """
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- gitrepo = os.path.join(
- tests.HERE, 'repos', 'forks', 'foo', 'test.git')
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Create a PR for these "changes" (there are none, both repos are
- # empty)
- project = pagure.lib.get_project(self.session, 'test')
- req = pagure.lib.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from='feature',
- repo_to=project,
- branch_to='master',
- title='PR from the feature branch',
- user='pingou',
- requestfolder=None,
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, 'PR from the feature branch')
- output = self.app.get('/test/pull-request/1', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Fork is empty, there are no '
- 'commits to request pulling', output.data)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_request_pulls(self, send_email):
- """ Test the request_pulls endpoint. """
- send_email.return_value = True
- # No such project
- output = self.app.get('/test/pull-requests')
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'repos'), bare=True)
- output = self.app.get('/test/pull-requests')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Pull Requests <span class="label label-default">0</span>',
- output.data)
- # Open is primary
- self.assertIn(
- '<a class="btn btn-primary btn-sm" '
- 'href="/test/pull-requests">Open</a>', output.data)
- self.assertIn(
- '<a class="btn btn-secondary btn-sm" '
- 'href="/test/pull-requests?status=0">Closed</a>', output.data)
- self.set_up_git_repo(new_project=None, branch_from='feature')
- output = self.app.get('/test/pull-requests')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Pull Requests <span class="label label-default">1</span>',
- output.data)
- # Open is primary
- self.assertIn(
- '<a class="btn btn-primary btn-sm" '
- 'href="/test/pull-requests">Open</a>', output.data)
- self.assertIn(
- '<a class="btn btn-secondary btn-sm" '
- 'href="/test/pull-requests?status=0">Closed</a>', output.data)
- output = self.app.get('/test/pull-requests?status=Closed')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Closed Pull Requests <span class="label label-default">0</span>',
- output.data)
- # Close is primary
- self.assertIn(
- '<a class="btn btn-secondary btn-sm" '
- 'href="/test/pull-requests">Open</a>', output.data)
- self.assertIn(
- '<a class="btn btn-primary btn-sm" '
- 'href="/test/pull-requests?status=0">Closed</a>', output.data)
- output = self.app.get('/test/pull-requests?status=0')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Closed/Merged Pull Requests <span class="label label-default">0</span>',
- output.data)
- # Close is primary
- self.assertIn(
- '<a class="btn btn-secondary btn-sm" '
- 'href="/test/pull-requests">Open</a>', output.data)
- self.assertIn(
- '<a class="btn btn-primary btn-sm" '
- 'href="/test/pull-requests?status=0">Closed</a>', output.data)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get('/test/pull-requests')
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_patch(self, send_email):
- """ Test the request_pull_patch endpoint. """
- send_email.return_value = True
- output = self.app.get('/test/pull-request/1.patch')
- self.assertEqual(output.status_code, 404)
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(
- new_project=None, branch_from='feature', mtype='merge')
- output = self.app.get('/test/pull-request/100.patch')
- self.assertEqual(output.status_code, 404)
- output = self.app.get('/test/pull-request/1.patch')
- self.assertEqual(output.status_code, 200)
- npatch = []
- for row in output.data.split('\n'):
- if row.startswith('Date:'):
- continue
- if row.startswith('From '):
- row = row.split(' ', 2)[2]
- npatch.append(row)
- exp = """Mon Sep 17 00:00:00 2001
- From: Alice Author <alice@authors.tld>
- Subject: A commit on branch feature
- ---
- diff --git a/.gitignore b/.gitignore
- new file mode 100644
- index 0000000..e4e5f6c
- --- /dev/null
- +++ b/.gitignore
- @@ -0,0 +1 @@
- +*~
- \ No newline at end of file
- diff --git a/sources b/sources
- index 9f44358..2a552bb 100644
- --- a/sources
- +++ b/sources
- @@ -1,2 +1,4 @@
- foo
- - bar
- \ No newline at end of file
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- patch = '\n'.join(npatch)
- #print patch
- self.assertEqual(patch, exp)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.get('/test/pull-request/1.patch')
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_patch_close(self, send_email):
- """ Test the request_pull_patch endpoint with a closed PR. """
- send_email.return_value = True
- self.test_merge_request_pull_FF()
- output = self.app.get('/test/pull-request/1.patch')
- self.assertEqual(output.status_code, 200)
- npatch = []
- for row in output.data.split('\n'):
- if row.startswith('Date:'):
- continue
- if row.startswith('From '):
- row = row.split(' ', 2)[2]
- npatch.append(row)
- exp = """Mon Sep 17 00:00:00 2001
- From: Alice Author <alice@authors.tld>
- Subject: A commit on branch feature
- ---
- diff --git a/sources b/sources
- index 9f44358..2a552bb 100644
- --- a/sources
- +++ b/sources
- @@ -1,2 +1,4 @@
- foo
- - bar
- \ No newline at end of file
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- patch = '\n'.join(npatch)
- #print patch
- self.assertEqual(patch, exp)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_patch_empty_repo(self, send_email):
- """ Test the request_pull_patch endpoint against an empty repo. """
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- gitrepo = os.path.join(
- tests.HERE, 'repos', 'forks', 'foo', 'test.git')
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Edit the sources file again
- with open(os.path.join(newpath, 'sources'), 'w') as stream:
- stream.write('foo\n bar\nbaz\n boose')
- new_repo.index.add('sources')
- new_repo.index.write()
- # Commits the files added
- tree = new_repo.index.write_tree()
- author = pygit2.Signature(
- 'Alice Author', 'alice@authors.tld')
- committer = pygit2.Signature(
- 'Cecil Committer', 'cecil@committers.tld')
- new_repo.create_commit(
- 'refs/heads/feature',
- author,
- committer,
- 'A commit on branch feature',
- tree,
- []
- )
- refname = 'refs/heads/feature:refs/heads/feature'
- ori_remote = new_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these "changes" (there are none, both repos are
- # empty)
- project = pagure.lib.get_project(self.session, 'test')
- req = pagure.lib.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from='feature',
- repo_to=project,
- branch_to='master',
- title='PR from the feature branch',
- user='pingou',
- requestfolder=None,
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, 'PR from the feature branch')
- output = self.app.get(
- '/test/pull-request/1.patch', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- npatch = []
- for row in output.data.split('\n'):
- if row.startswith('Date:'):
- continue
- if row.startswith('From '):
- row = row.split(' ', 2)[2]
- npatch.append(row)
- exp = """Mon Sep 17 00:00:00 2001
- From: Alice Author <alice@authors.tld>
- Subject: A commit on branch feature
- ---
- diff --git a/sources b/sources
- new file mode 100644
- index 0000000..2a552bb
- --- /dev/null
- +++ b/sources
- @@ -0,0 +1,4 @@
- +foo
- + bar
- +baz
- + boose
- \ No newline at end of file
- """
- patch = '\n'.join(npatch)
- #print patch
- self.assertEqual(patch, exp)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_request_pull_patch_empty_fork(self, send_email):
- """ Test the request_pull_patch endpoint from an empty fork. """
- send_email.return_value = True
- tests.create_projects(self.session)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'repos', 'forks', 'foo'), bare=True)
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- self.assertFalse(os.path.exists(gitrepo))
- os.makedirs(gitrepo)
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- gitrepo = os.path.join(
- tests.HERE, 'repos', 'forks', 'foo', 'test.git')
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- # Create a PR for these "changes" (there are none, both repos are
- # empty)
- project = pagure.lib.get_project(self.session, 'test')
- req = pagure.lib.new_pull_request(
- session=self.session,
- repo_from=item,
- branch_from='feature',
- repo_to=project,
- branch_to='master',
- title='PR from the feature branch',
- user='pingou',
- requestfolder=None,
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, 'PR from the feature branch')
- output = self.app.get('/test/pull-request/1.patch', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Fork is empty, there are no '
- 'commits to request pulling', output.data)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_cancel_request_pull(self, send_email):
- """ Test the cancel_request_pull endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(
- new_project=None, branch_from='feature', mtype='merge')
- user = tests.FakeUser()
- with tests.user_set(pagure.APP, user):
- output = self.app.post('/test/pull-request/cancel/1')
- self.assertEqual(output.status_code, 302)
- output = self.app.post(
- '/test/pull-request/cancel/1', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Invalid input submitted',
- output.data)
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- }
- # Invalid project
- output = self.app.post(
- '/foo/pull-request/cancel/1', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Invalid PR id
- output = self.app.post(
- '/test/pull-request/cancel/100', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Invalid user for this project
- output = self.app.post(
- '/test/pull-request/cancel/1', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 403)
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/cancel/1', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/cancel/1', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Request pull canceled!',
- output.data)
- @patch('pagure.lib.notify.send_email')
- def test_set_assignee_requests(self, send_email):
- """ Test the set_assignee_requests endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- self.set_up_git_repo(new_project=None, branch_from='feature')
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # No such project
- output = self.app.post('/foo/pull-request/1/assign')
- self.assertEqual(output.status_code, 404)
- output = self.app.post('/test/pull-request/100/assign')
- self.assertEqual(output.status_code, 404)
- # Invalid input
- output = self.app.post(
- '/test/pull-request/1/assign', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n', output.data)
- self.assertNotIn(
- '</button>\n Request assigned',
- output.data)
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'user': 'pingou',
- }
- # No CSRF
- output = self.app.post(
- '/test/pull-request/1/assign', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n', output.data)
- self.assertNotIn(
- '</button>\n Request assigned',
- output.data)
- # Invalid assignee
- data = {
- 'csrf_token': csrf_token,
- 'user': 'bar',
- }
- output = self.app.post(
- '/test/pull-request/1/assign', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n', output.data)
- self.assertIn(
- '</button>\n No user "bar" found',
- output.data)
- # Assign the PR
- data = {
- 'csrf_token': csrf_token,
- 'user': 'pingou',
- }
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- output = self.app.post(
- '/test/pull-request/1/assign', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 403)
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.post(
- '/test/pull-request/1/assign', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n', output.data)
- self.assertIn(
- '</button>\n Request assigned',
- output.data)
- # Pull-Request closed
- repo = pagure.lib.get_project(self.session, 'test')
- req = repo.requests[0]
- req.status = 'Closed'
- req.closed_by_in = 1
- self.session.add(req)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/assign', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 403)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/assign', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_fork_project(self, send_email):
- """ Test the fork_project endpoint. """
- send_email.return_value = True
- tests.create_projects(self.session)
- for folder in ['docs', 'tickets', 'requests', 'repos']:
- tests.create_projects_git(
- os.path.join(tests.HERE, folder), bare=True)
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.post('/do_fork/test')
- self.assertEqual(output.status_code, 400)
- output = self.app.get('/new/')
- self.assertEqual(output.status_code, 200)
- self.assertIn('<strong>Create new Project</strong>', output.data)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/do_fork/foo', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- output = self.app.post('/do_fork/test')
- self.assertEqual(output.status_code, 400)
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/do_fork/test', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '</button>\n Repo "test" '
- 'cloned to "foo/test"', output.data)
- @patch('pagure.lib.notify.send_email')
- def test_new_request_pull(self, send_email):
- """ Test the new_request_pull endpoint. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- repo = pagure.lib.get_project(self.session, 'test')
- fork = pagure.lib.get_project(self.session, 'test', user='foo')
- self.set_up_git_repo(
- new_project=fork, branch_from='feature', mtype='FF')
- user = tests.FakeUser()
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/foo/diff/master..feature')
- self.assertEqual(output.status_code, 404)
- output = self.app.get('/test/diff/master..foo')
- self.assertEqual(output.status_code, 400)
- output = self.app.get('/test/diff/foo..master')
- self.assertEqual(output.status_code, 400)
- output = self.app.get('/test/diff/feature..master')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Diff from master to feature - test - Pagure</title>',
- output.data)
- self.assertIn(
- '<p class="error"> No commits found </p>', output.data)
- output = self.app.get('/test/diff/master..feature')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Diff from feature to master - test - Pagure</title>',
- output.data)
- self.assertNotIn(
- '<input type="submit" class="submit positive button" '
- 'value="Create">', output.data)
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/test/diff/master..feature')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title> Create new Pull Request for master - test - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<input type="submit" class="btn btn-primary" value="Create">',
- output.data)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- # Case 1 - Add an initial comment
- data = {
- 'csrf_token': csrf_token,
- 'title': 'foo bar PR',
- 'initial_comment': 'Test Initial Comment',
- }
- output = self.app.post(
- '/test/diff/master..feature', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#2: foo bar PR - test\n - Pagure</title>',
- output.data)
- self.assertIn('<p>Test Initial Comment</p>', output.data)
- # Case 2 - Add an empty initial comment
- data = {
- 'csrf_token': csrf_token,
- 'title': 'foo bar PR',
- 'initial_comment': '',
- }
- output = self.app.post(
- '/test/diff/master..feature', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#3: foo bar PR - test\n - Pagure</title>',
- output.data)
- self.assertNotIn('<div id="comment-', output.data)
- @patch('pagure.lib.notify.send_email')
- def test_new_request_pull_empty_repo(self, send_email):
- """ Test the new_request_pull endpoint against an empty repo. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- repo = pagure.lib.get_project(self.session, 'test')
- fork = pagure.lib.get_project(self.session, 'test', user='foo')
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- gitrepo = os.path.join(tests.HERE, 'repos', 'forks', 'foo', 'test.git')
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- user = tests.FakeUser()
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- output = self.app.get(
- '/fork/foo/test/diff/master..feature',
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Fork is empty, there are '
- 'no commits to request pulling', output.data)
- output = self.app.get('/test/new_issue')
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- 'title': 'foo bar PR',
- }
- output = self.app.post(
- '/test/diff/master..feature', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Fork is empty, there are '
- 'no commits to request pulling', output.data)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_new_request_pull_empty_fork(self, send_email):
- """ Test the new_request_pull endpoint against an empty repo. """
- send_email.return_value = True
- self.test_fork_project()
- tests.create_projects_git(
- os.path.join(tests.HERE, 'requests'), bare=True)
- repo = pagure.lib.get_project(self.session, 'test')
- fork = pagure.lib.get_project(self.session, 'test', user='foo')
- # Create a git repo to play with
- gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
- repo = pygit2.init_repository(gitrepo, bare=True)
- # Create a fork of this repo
- newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
- gitrepo = os.path.join(
- tests.HERE, 'repos', 'forks', 'foo', 'test.git')
- new_repo = pygit2.clone_repository(gitrepo, newpath)
- user = tests.FakeUser()
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- output = self.app.get(
- '/fork/foo/test/diff/master..master', follow_redirects=True)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Fork is empty, there are '
- 'no commits to request pulling', output.data)
- shutil.rmtree(newpath)
- @patch('pagure.lib.notify.send_email')
- def test_pull_request_add_comment(self, send_email):
- """ Test the pull_request_add_comment endpoint. """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- output = self.app.post('/foo/pull-request/1/comment')
- self.assertEqual(output.status_code, 404)
- output = self.app.post('/test/pull-request/100/comment')
- self.assertEqual(output.status_code, 404)
- output = self.app.post('/test/pull-request/1/comment')
- self.assertEqual(output.status_code, 200)
- self.assertTrue(
- output.data.startswith('\n<section class="add_comment">'))
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- 'comment': 'This look alright but we can do better',
- }
- output = self.app.post(
- '/test/pull-request/1/comment', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Comment added',
- output.data)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/comment', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_pull_request_drop_comment(self, send_email):
- """ Test the pull_request_drop_comment endpoint. """
- send_email.return_value = True
- self.test_pull_request_add_comment()
- # Project w/ pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- user = tests.FakeUser()
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- output = self.app.post('/foo/pull-request/1/comment/drop')
- self.assertEqual(output.status_code, 404)
- output = self.app.post('/test/pull-request/100/comment/drop')
- self.assertEqual(output.status_code, 404)
- output = self.app.post(
- '/test/pull-request/1/comment/drop', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n</h3>', output.data)
- #self.assertIn('href="#comment-1">¶</a>', output.data)
- self.assertIn(
- '<p>This look alright but we can do better</p>',
- output.data)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- # Invalid comment id
- data = {
- 'csrf_token': csrf_token,
- 'drop_comment': '10',
- }
- output = self.app.post(
- '/test/pull-request/1/comment/drop', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- data['drop_comment'] = '1'
- output = self.app.post(
- '/test/pull-request/1/comment/drop', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 403)
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # Drop comment
- output = self.app.post(
- '/test/pull-request/1/comment/drop', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- self.assertIn(
- '</button>\n Comment removed',
- output.data)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/comment/drop', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_pull_request_edit_comment(self, send_email):
- """ Test the pull request edit comment endpoint """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # Repo 'foo' does not exist so it is verifying that condition
- output = self.app.post('/foo/pull-request/1/comment/1/edit')
- self.assertEqual(output.status_code, 404)
- # Here no comment is present in the PR so its verifying that condition
- output = self.app.post('/test/pull-request/100/comment/100/edit')
- self.assertEqual(output.status_code, 404)
- output = self.app.post('/test/pull-request/1/comment')
- self.assertEqual(output.status_code, 200)
- # Creating comment to play with
- self.assertTrue(
- output.data.startswith('\n<section class="add_comment">'))
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- 'comment': 'This look alright but we can do better',
- }
- output = self.app.post(
- '/test/pull-request/1/comment', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- self.assertIn(
- '</button>\n Comment added',
- output.data)
- # Check if the comment is there
- self.assertIn(
- '<p>This look alright but we can do better</p>', output.data)
- output = self.app.get('/test/pull-request/1/comment/1/edit')
- self.assertEqual(output.status_code, 200)
- self.assertIn('<section class="edit_comment">', output.data)
- # Checking if the comment is there in the update page
- self.assertIn(
- 'This look alright but we can do better</textarea>', output.data)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- data = {
- 'csrf_token': csrf_token,
- 'update_comment': 'This look alright but we can do better than this.',
- }
- output = self.app.post(
- '/test/pull-request/1/comment/1/edit', data=data,
- follow_redirects=True)
- # Checking if the comment is updated in the main page
- self.assertIn(
- '<p>This look alright but we can do better than this.</p>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n <span class="pull-xs-right">',
- output.data)
- # Checking if Edited by User is there or not
- self.assertIn(
- '<small class="text-muted">Edited just now by pingou </small>',
- output.data)
- self.assertIn(
- '</button>\n Comment updated', output.data)
- # Project w/o pull-request
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['pull_requests'] = False
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- output = self.app.post(
- '/test/pull-request/1/comment/edit/1', data=data,
- follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- @patch('pagure.lib.notify.send_email')
- def test_merge_request_pull_FF_w_merge_commit(self, send_email):
- """ Test the merge_request_pull endpoint with a FF PR but with a
- merge commit.
- """
- send_email.return_value = True
- self.test_request_pull()
- user = tests.FakeUser()
- with tests.user_set(pagure.APP, user):
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- csrf_token = output.data.split(
- 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
- # No CSRF
- output = self.app.post(
- '/test/pull-request/1/merge', data={}, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>PR#1: PR from the feature branch - test\n - '
- 'Pagure</title>', output.data)
- self.assertIn(
- '<h3><span class="label label-default">PR#1</span>\n'
- ' PR from the feature branch\n</h3>', output.data)
- self.assertIn(
- 'title="View file as of 2a552b">sources</a>', output.data)
- # Wrong project
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/foobar/pull-request/100/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Wrong project
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 403)
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # Wrong request id
- data = {
- 'csrf_token': csrf_token,
- }
- output = self.app.post(
- '/test/pull-request/100/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- # Project requiring a merge commit
- repo = pagure.lib.get_project(self.session, 'test')
- settings = repo.settings
- settings['always_merge'] = True
- repo.settings = settings
- self.session.add(repo)
- self.session.commit()
- # Merge
- output = self.app.post(
- '/test/pull-request/1/merge', data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<title>Overview - test - Pagure</title>', output.data)
- self.assertIn(
- '</button>\n Changes merged!',
- output.data)
- self.assertIn(
- 'Merge #1 `PR from the feature branch`', output.data)
- self.assertIn(
- 'A commit on branch feature', output.data)
- # Ensure we have the merge commit
- commits = _get_commits(output.data)
- self.assertEqual(commits, [
- 'Merge #1 `PR from the feature branch`',
- 'Add sources file for testing',
- 'A commit on branch feature',
- ])
- # Check if the closing notification was added
- output = self.app.get('/test/pull-request/1')
- self.assertIn(
- '<small><p>Pull-Request has been merged by pingou</p></small>',
- output.data)
- if __name__ == '__main__':
- SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests)
- unittest.TextTestRunner(verbosity=2).run(SUITE)
|