123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974 |
- # -*- 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.assertTrue(
- '<small class="text-muted">Edited just now by pingou </small>'
- in output.data
- or
- '<small class="text-muted">Edited seconds ago by pingou </small>'
- in 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)
- @patch('pagure.lib.notify.send_email')
- def test_fork_edit_file(self, send_email):
- """ Test the fork_edit file endpoint. """
- send_email.return_value = True
- # User not logged in
- output = self.app.post('fork_edit/test/edit/master/f/sources')
- self.assertEqual(output.status_code, 302)
- 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):
- # Invalid request
- output = self.app.post('fork_edit/test/edit/master/f/source')
- 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,
- }
- # No files can be found since they are not added
- output = self.app.post('fork_edit/test/edit/master/f/sources',
- data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 404)
- user = tests.FakeUser()
- user.username = 'foo'
- with tests.user_set(pagure.APP, user):
- data = {
- 'csrf_token': csrf_token,
- }
- # Invalid request
- output = self.app.post('fork_edit/test/edit/master/f/sources',
- follow_redirects=True)
- self.assertEqual(output.status_code, 400)
- # Add content to the repo
- tests.add_content_git_repo(os.path.join(
- pagure.APP.config['GIT_FOLDER'], 'test.git'))
- tests.add_readme_git_repo(os.path.join(
- pagure.APP.config['GIT_FOLDER'], 'test.git'))
- tests.add_binary_git_repo(
- os.path.join(
- pagure.APP.config['GIT_FOLDER'], 'test.git'), 'test.jpg')
- # Check if button exists
- output = self.app.get('/test/blob/master/f/sources')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Fork and Edit\n </button>\n',
- output.data)
- # Check fork-edit doesn't show for binary files
- output = self.app.get('/test/blob/master/f/test.jpg')
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- 'Fork and Edit\n </button>\n',
- output.data)
- # Check for edit panel
- output = self.app.post('fork_edit/test/edit/master/f/sources',
- data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '<li><a href="/fork/foo/test/tree/master">'
- '<span class="oi" data-glyph="random"></span> master</a>'
- '</li><li class="active"><span class="oi" data-glyph="file">'
- '</span> sources</li>',
- output.data)
- self.assertIn(
- '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
- output.data)
- self.assertIn(
- '</button>\n Repo "test" '
- 'cloned to "foo/test"', output.data)
- # View what's supposed to be an image
- output = self.app.post('fork_edit/test/edit/master/f/test.jpg',
- data=data, follow_redirects=True)
- self.assertEqual(output.status_code, 400)
- self.assertIn('<p>Cannot edit binary files</p>', output.data)
- # Check fork-edit shows when user is not logged in
- output = self.app.get('/test/blob/master/f/sources')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Fork and Edit\n </button>\n',
- output.data)
- # Check if fork-edit shows for different user
- user.username = 'pingou'
- with tests.user_set(pagure.APP, user):
- # Check if button exists
- output = self.app.get('/test/blob/master/f/sources')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- 'Fork and Edit\n </button>\n',
- output.data)
- # Check fork-edit doesn't show for binary
- output = self.app.get('/test/blob/master/f/test.jpg')
- self.assertEqual(output.status_code, 200)
- self.assertNotIn(
- 'Fork and Edit\n </button>\n',
- output.data)
- if __name__ == '__main__':
- SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests)
- unittest.TextTestRunner(verbosity=2).run(SUITE)
|