123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- # -*- coding: utf-8 -*-
- """
- (c) 2015-2018 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals
- __requires__ = ['SQLAlchemy >= 0.8']
- import pkg_resources
- import json
- import unittest
- import shutil
- import sys
- import tempfile
- import time
- import os
- import pygit2
- from mock import patch, MagicMock
- sys.path.insert(0, os.path.join(os.path.dirname(
- os.path.abspath(__file__)), '..'))
- import pagure.lib.query
- import tests
- from pagure.lib.repo import PagureRepo
- class BasePrNoSourcestests(tests.Modeltests):
- """ Tests PR in pagure when the source is gone """
- maxDiff = None
- @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
- @patch('pagure.lib.notify.fedmsg_publish', MagicMock(return_value=True))
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(BasePrNoSourcestests, self).setUp()
- tests.create_projects(self.session)
- tests.create_projects_git(
- os.path.join(self.path, 'repos'), bare=True)
- # Create foo's fork of pingou's test project
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- # Create the fork's git repo
- repo_path = os.path.join(self.path, 'repos', item.path)
- pygit2.init_repository(repo_path, bare=True)
- project = pagure.lib.query.get_authorized_project(self.session, 'test')
- fork = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- print('fork', fork.fullname)
- self.set_up_git_repo(repo=project, fork=fork)
- # Ensure things got setup straight
- project = pagure.lib.query.get_authorized_project(self.session, 'test')
- self.assertEqual(len(project.requests), 1)
- # wait for the worker to process the task
- path = os.path.join(
- self.path, 'repos', 'test.git',
- 'refs', 'pull', '1', 'head')
- self.assertTrue(os.path.exists(path))
- def set_up_git_repo(self, repo, fork, branch_from='feature'):
- """ Set up the git repo and create the corresponding PullRequest
- object.
- """
- # Clone the main repo
- gitrepo = os.path.join(self.path, 'repos', repo.path)
- 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 = clone_repo.revparse_single('HEAD')
- # Set the second repo
- repopath = os.path.join(self.path, 'repos', fork.path)
- new_gitrepo = os.path.join(newpath, 'fork_test')
- clone_repo = pygit2.clone_repository(repopath, new_gitrepo)
- # Add the main project as remote repo
- upstream_path = os.path.join(self.path, 'repos', repo.path)
- remote = clone_repo.create_remote('upstream', upstream_path)
- remote.fetch()
- # Edit the sources file again
- with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream:
- stream.write('foo\n bar\nbaz\n boose')
- 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/%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 = clone_repo.remotes[0]
- PagureRepo.push(ori_remote, refname)
- # Create a PR for these changes
- project = pagure.lib.query.get_authorized_project(self.session, 'test')
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=fork,
- branch_from=branch_from,
- repo_to=project,
- branch_to='master',
- title='PR from the %s branch' % branch_from,
- user='pingou',
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, 'PR from the %s branch' % branch_from)
- shutil.rmtree(newpath)
- class PagureFlaskPrNoSourcestests(BasePrNoSourcestests):
- """ Tests PR in pagure when the source is gone """
- maxDiff = None
- @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
- @patch('pagure.lib.notify.fedmsg_publish', MagicMock(return_value=True))
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskPrNoSourcestests, self).setUp()
- def test_request_pull_reference(self):
- """ Test if there is a reference created for a new PR. """
- project = pagure.lib.query.get_authorized_project(self.session, 'test')
- self.assertEqual(len(project.requests), 1)
- gitrepo = os.path.join(self.path, 'repos', 'test.git')
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- ['refs/heads/master', 'refs/pull/1/head']
- )
- def test_request_pull_fork_reference(self):
- """ Test if there the references created on the fork. """
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- self.assertEqual(len(project.requests), 0)
- gitrepo = os.path.join(self.path, 'repos', project.path)
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- ['refs/heads/feature']
- )
- def test_accessing_pr_fork_deleted(self):
- """ Test accessing the PR if the fork has been deleted. """
- # Delete fork on disk
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- repo_path = os.path.join(self.path, 'repos', project.path)
- self.assertTrue(os.path.exists(repo_path))
- shutil.rmtree(repo_path)
- self.assertFalse(os.path.exists(repo_path))
- # Delete fork in the DB
- self.session.delete(project)
- self.session.commit()
- # View the pull-request
- output2 = self.app.get('/test/pull-request/1')
- self.assertEqual(output2.status_code, 200)
- output_text = output2.get_data(as_text=True)
- self.assertIn('''<span>Files Changed </span>
- <span class="badge badge-secondary badge-pill">
- 1
- </span>''', output_text)
- def test_accessing_pr_patch_fork_deleted(self):
- """ Test accessing the PR's patch if the fork has been deleted. """
- # Delete fork on disk
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- repo_path = os.path.join(self.path, 'repos', project.path)
- self.assertTrue(os.path.exists(repo_path))
- shutil.rmtree(repo_path)
- self.assertFalse(os.path.exists(repo_path))
- # Delete fork in the DB
- self.session.delete(project)
- self.session.commit()
- # View the pull-request
- output = self.app.get('/test/pull-request/1.patch')
- self.assertEqual(output.status_code, 200)
- self.assertIn(
- '--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@',
- output.get_data(as_text=True))
- def test_accessing_pr_branch_deleted(self):
- """ Test accessing the PR if branch it originates from has been
- deleted. """
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- # Check the branches before
- gitrepo = os.path.join(self.path, 'repos', project.path)
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- ['refs/heads/feature']
- )
- # Delete branch of the fork
- user = tests.FakeUser(username='foo')
- with tests.user_set(self.app.application, user):
- output = self.app.post(
- '/fork/foo/test/b/feature/delete', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- # Check the branches after
- gitrepo = os.path.join(self.path, 'repos', project.path)
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- []
- )
- # View the pull-request
- output2 = self.app.get('/test/pull-request/1')
- self.assertEqual(output2.status_code, 200)
- output_text = output2.get_data(as_text=True)
- self.assertIn('''<span>Files Changed </span>
- <span class="badge badge-secondary badge-pill">
- 1
- </span>''', output_text)
- def test_accessing_pr_patch_branch_deleted(self):
- """ Test accessing the PR's patch if branch it originates from has
- been deleted. """
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- # Check the branches before
- gitrepo = os.path.join(self.path, 'repos', project.path)
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- ['refs/heads/feature']
- )
- # Delete branch of the fork
- user = tests.FakeUser(username='foo')
- with tests.user_set(self.app.application, user):
- output = self.app.post(
- '/fork/foo/test/b/feature/delete', follow_redirects=True)
- self.assertEqual(output.status_code, 200)
- # Check the branches after
- gitrepo = os.path.join(self.path, 'repos', project.path)
- repo = pygit2.Repository(gitrepo)
- self.assertEqual(
- list(repo.listall_references()),
- []
- )
- # View the pull-request
- output = self.app.get('/test/pull-request/1.patch')
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn(
- '--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@',
- output_text)
- def test_accessing_pr_patch_fork_deleted_recreated(self):
- """ Test accessing the PR's patch if the fork has been deleted
- and re-created. """
- # Delete fork on disk
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- repo_path = os.path.join(self.path, 'repos', project.path)
- self.assertTrue(os.path.exists(repo_path))
- shutil.rmtree(repo_path)
- self.assertFalse(os.path.exists(repo_path))
- # Delete fork in the DB
- self.session.delete(project)
- self.session.commit()
- # Re-create foo's fork of pingou's test project
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- # Create the fork's git repo
- repo_path = os.path.join(self.path, 'repos', item.path)
- pygit2.init_repository(repo_path, bare=True)
- # View the pull-request
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn('''<span>Commits </span>
- <span class="badge badge-secondary badge-pill">
- 1
- </span>''', output_text)
- self.assertIn('''<span>Files Changed </span>
- <span class="badge badge-secondary badge-pill">
- 1
- </span>''', output_text)
- class PagureFlaskClosedPrNoSourcestests(BasePrNoSourcestests):
- """ Tests viewing closed PR in pagure when the source is gone """
- maxDiff = None
- @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
- @patch('pagure.lib.notify.fedmsg_publish', MagicMock(return_value=True))
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskClosedPrNoSourcestests, self).setUp()
- # Ensure things got setup straight
- project = pagure.lib.query.get_authorized_project(self.session, 'test')
- self.assertEqual(len(project.requests), 1)
- request = project.requests[0]
- self.assertEqual(request.status, "Open")
- request.status = "Closed"
- self.session.add(request)
- self.session.commit()
- def test_accessing_pr_patch_fork_deleted_recreated(self):
- """ Test accessing the PR's patch if the fork has been deleted
- and re-created. """
- # Delete fork on disk
- project = pagure.lib.query.get_authorized_project(
- self.session, 'test', user='foo')
- repo_path = os.path.join(self.path, 'repos', project.path)
- self.assertTrue(os.path.exists(repo_path))
- shutil.rmtree(repo_path)
- self.assertFalse(os.path.exists(repo_path))
- # Delete fork in the DB
- self.session.delete(project)
- self.session.commit()
- # Re-create foo's fork of pingou's test project
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name='test',
- description='test project #1',
- hook_token='aaabbb',
- is_fork=True,
- parent_id=1,
- )
- self.session.add(item)
- self.session.commit()
- # Create the fork's git repo
- fork_path = os.path.join(self.path, 'repos', item.path)
- pygit2.clone_repository(repo_path, fork_path, bare=True)
- # View the pull-request
- output = self.app.get('/test/pull-request/1')
- self.assertEqual(output.status_code, 200)
- output_text = output.get_data(as_text=True)
- self.assertIn('''<span>Commits </span>
- <span class="badge badge-secondary badge-pill">
- 1
- </span>''', output_text)
- self.assertIn('''<span>Files Changed </span>
- <span class="badge badge-secondary badge-pill">
- 1
- </span>''', output_text)
- if __name__ == '__main__':
- unittest.main(verbosity=2)
|