Blame tests/test_pagure_private_repo.py

farhaanbukhsh 1df65f
# -*- coding: utf-8 -*-
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
__requires__ = ['SQLAlchemy >= 0.8']
farhaanbukhsh 1df65f
import pkg_resources
farhaanbukhsh 1df65f
farhaanbukhsh 60d445
import datetime
farhaanbukhsh 1df65f
import unittest
farhaanbukhsh 1df65f
import shutil
farhaanbukhsh 1df65f
import sys
farhaanbukhsh c4f329
import tempfile
farhaanbukhsh 1df65f
import os
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
import json
farhaanbukhsh c4f329
import pygit2
farhaanbukhsh 1df65f
from mock import patch
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
sys.path.insert(0, os.path.join(os.path.dirname(
farhaanbukhsh 1df65f
    os.path.abspath(__file__)), '..'))
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
import pagure.lib
farhaanbukhsh 1df65f
import tests
farhaanbukhsh c4f329
from pagure.lib.repo import PagureRepo
farhaanbukhsh 1df65f
farhaanbukhsh 753ee7
farhaanbukhsh 1df65f
class PagurePrivateRepotest(tests.Modeltests):
farhaanbukhsh 1df65f
    """ Tests for private repo in pagure """
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
    def setUp(self):
farhaanbukhsh 1df65f
        """ Set up the environnment, ran before every tests. """
farhaanbukhsh 1df65f
        super(PagurePrivateRepotest, self).setUp()
farhaanbukhsh 1df65f
farhaanbukhsh 4858f8
        pagure.APP.config['TESTING'] = True
farhaanbukhsh 4858f8
        pagure.SESSION = self.session
farhaanbukhsh 4858f8
        pagure.lib.SESSION = self.session
farhaanbukhsh 4858f8
        pagure.ui.SESSION = self.session
farhaanbukhsh 1df65f
        pagure.ui.app.SESSION = self.session
farhaanbukhsh 1df65f
        pagure.ui.filters.SESSION = self.session
farhaanbukhsh 4858f8
        pagure.ui.fork.SESSION = self.session
farhaanbukhsh 1df65f
        pagure.ui.repo.SESSION = self.session
farhaanbukhsh 4858f8
        pagure.ui.issues.SESSION = self.session
farhaanbukhsh 60d445
        pagure.api.SESSION = self.session
farhaanbukhsh 60d445
        pagure.api.project.SESSION = self.session
farhaanbukhsh 44a97a
        pagure.api.fork.SESSION = self.session
farhaanbukhsh 1df65f
farhaanbukhsh 4858f8
        pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
farhaanbukhsh 1df65f
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
farhaanbukhsh 1df65f
            tests.HERE, 'forks')
farhaanbukhsh 1df65f
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
farhaanbukhsh 1df65f
            tests.HERE, 'tickets')
farhaanbukhsh 1df65f
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
farhaanbukhsh 1df65f
            tests.HERE, 'docs')
farhaanbukhsh 1df65f
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
farhaanbukhsh 1df65f
            tests.HERE, 'requests')
farhaanbukhsh 1df65f
        self.app = pagure.APP.test_client()
farhaanbukhsh 1df65f
farhaanbukhsh c4f329
    def set_up_git_repo(
farhaanbukhsh c4f329
            self, new_project=None, branch_from='feature', mtype='FF'):
farhaanbukhsh c4f329
        """ Set up the git repo and create the corresponding PullRequest
farhaanbukhsh c4f329
        object.
farhaanbukhsh c4f329
        """
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Create a git repo to play with
farhaanbukhsh 4ea512
        gitrepo = os.path.join(tests.HERE, 'repos', 'pmc.git')
farhaanbukhsh c4f329
        repo = pygit2.init_repository(gitrepo, bare=True)
farhaanbukhsh c4f329
farhaanbukhsh 4ea512
        newpath = tempfile.mkdtemp(prefix='pagure-private-test')
farhaanbukhsh 4858f8
        repopath = os.path.join(newpath, 'test')
farhaanbukhsh c4f329
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Create a file in that git repo
farhaanbukhsh c4f329
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
farhaanbukhsh c4f329
            stream.write('foo\n bar')
farhaanbukhsh c4f329
        clone_repo.index.add('sources')
farhaanbukhsh c4f329
        clone_repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Commits the files added
farhaanbukhsh c4f329
        tree = clone_repo.index.write_tree()
farhaanbukhsh c4f329
        author = pygit2.Signature(
farhaanbukhsh c4f329
            'Alice Author', 'alice@authors.tld')
farhaanbukhsh c4f329
        committer = pygit2.Signature(
farhaanbukhsh c4f329
            'Cecil Committer', 'cecil@committers.tld')
farhaanbukhsh c4f329
        clone_repo.create_commit(
farhaanbukhsh c4f329
            'refs/heads/master',  # the name of the reference to update
farhaanbukhsh c4f329
            author,
farhaanbukhsh c4f329
            committer,
farhaanbukhsh c4f329
            'Add sources file for testing',
farhaanbukhsh c4f329
            # binary string representing the tree object ID
farhaanbukhsh c4f329
            tree,
farhaanbukhsh c4f329
            # list of binary strings representing parents of the new commit
farhaanbukhsh c4f329
            []
farhaanbukhsh c4f329
        )
farhaanbukhsh c4f329
        refname = 'refs/heads/master:refs/heads/master'
farhaanbukhsh c4f329
        ori_remote = clone_repo.remotes[0]
farhaanbukhsh c4f329
        PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        first_commit = repo.revparse_single('HEAD')
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        if mtype == 'merge':
farhaanbukhsh c4f329
            with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
farhaanbukhsh c4f329
                stream.write('*~')
farhaanbukhsh c4f329
            clone_repo.index.add('.gitignore')
farhaanbukhsh c4f329
            clone_repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            # Commits the files added
farhaanbukhsh c4f329
            tree = clone_repo.index.write_tree()
farhaanbukhsh c4f329
            author = pygit2.Signature(
farhaanbukhsh c4f329
                'Alice Äuthòr', 'alice@äuthòrs.tld')
farhaanbukhsh c4f329
            committer = pygit2.Signature(
farhaanbukhsh c4f329
                'Cecil Cõmmîttër', 'cecil@cõmmîttërs.tld')
farhaanbukhsh c4f329
            clone_repo.create_commit(
farhaanbukhsh c4f329
                'refs/heads/master',
farhaanbukhsh c4f329
                author,
farhaanbukhsh c4f329
                committer,
farhaanbukhsh c4f329
                'Add .gitignore file for testing',
farhaanbukhsh c4f329
                # binary string representing the tree object ID
farhaanbukhsh c4f329
                tree,
farhaanbukhsh c4f329
                # list of binary strings representing parents of the new commit
farhaanbukhsh c4f329
                [first_commit.oid.hex]
farhaanbukhsh c4f329
            )
farhaanbukhsh c4f329
            refname = 'refs/heads/master:refs/heads/master'
farhaanbukhsh c4f329
            ori_remote = clone_repo.remotes[0]
farhaanbukhsh c4f329
            PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        if mtype == 'conflicts':
farhaanbukhsh c4f329
            with open(os.path.join(repopath, 'sources'), 'w') as stream:
farhaanbukhsh c4f329
                stream.write('foo\n bar\nbaz')
farhaanbukhsh c4f329
            clone_repo.index.add('sources')
farhaanbukhsh c4f329
            clone_repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            # Commits the files added
farhaanbukhsh c4f329
            tree = clone_repo.index.write_tree()
farhaanbukhsh c4f329
            author = pygit2.Signature(
farhaanbukhsh c4f329
                'Alice Author', 'alice@authors.tld')
farhaanbukhsh c4f329
            committer = pygit2.Signature(
farhaanbukhsh c4f329
                'Cecil Committer', 'cecil@committers.tld')
farhaanbukhsh c4f329
            clone_repo.create_commit(
farhaanbukhsh c4f329
                'refs/heads/master',
farhaanbukhsh c4f329
                author,
farhaanbukhsh c4f329
                committer,
farhaanbukhsh c4f329
                'Add sources conflicting',
farhaanbukhsh c4f329
                # binary string representing the tree object ID
farhaanbukhsh c4f329
                tree,
farhaanbukhsh c4f329
                # list of binary strings representing parents of the new commit
farhaanbukhsh c4f329
                [first_commit.oid.hex]
farhaanbukhsh c4f329
            )
farhaanbukhsh c4f329
            refname = 'refs/heads/master:refs/heads/master'
farhaanbukhsh c4f329
            ori_remote = clone_repo.remotes[0]
farhaanbukhsh c4f329
            PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Set the second repo
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        new_gitrepo = repopath
farhaanbukhsh c4f329
        if new_project:
farhaanbukhsh c4f329
            # Create a new git repo to play with
farhaanbukhsh c4f329
            new_gitrepo = os.path.join(newpath, new_project.fullname)
farhaanbukhsh c4f329
            if not os.path.exists(new_gitrepo):
farhaanbukhsh c4f329
                os.makedirs(new_gitrepo)
farhaanbukhsh c4f329
                new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        repo = pygit2.Repository(new_gitrepo)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        if mtype != 'nochanges':
farhaanbukhsh c4f329
            # Edit the sources file again
farhaanbukhsh c4f329
            with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream:
farhaanbukhsh c4f329
                stream.write('foo\n bar\nbaz\n boose')
farhaanbukhsh c4f329
            repo.index.add('sources')
farhaanbukhsh c4f329
            repo.index.write()
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            # Commits the files added
farhaanbukhsh c4f329
            tree = repo.index.write_tree()
farhaanbukhsh c4f329
            author = pygit2.Signature(
farhaanbukhsh c4f329
                'Alice Author', 'alice@authors.tld')
farhaanbukhsh c4f329
            committer = pygit2.Signature(
farhaanbukhsh c4f329
                'Cecil Committer', 'cecil@committers.tld')
farhaanbukhsh c4f329
            repo.create_commit(
farhaanbukhsh c4f329
                'refs/heads/%s' % branch_from,
farhaanbukhsh c4f329
                author,
farhaanbukhsh c4f329
                committer,
farhaanbukhsh c4f329
                'A commit on branch %s' % branch_from,
farhaanbukhsh c4f329
                tree,
farhaanbukhsh c4f329
                [first_commit.oid.hex]
farhaanbukhsh c4f329
            )
farhaanbukhsh c4f329
            refname = 'refs/heads/%s' % (branch_from)
farhaanbukhsh c4f329
            ori_remote = repo.remotes[0]
farhaanbukhsh c4f329
            PagureRepo.push(ori_remote, refname)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Create a PR for these changes
farhaanbukhsh c4f329
        project = pagure.lib.get_project(self.session, 'pmc')
farhaanbukhsh c4f329
        req = pagure.lib.new_pull_request(
farhaanbukhsh c4f329
            session=self.session,
farhaanbukhsh c4f329
            repo_from=project,
farhaanbukhsh c4f329
            branch_from=branch_from,
farhaanbukhsh c4f329
            repo_to=project,
farhaanbukhsh c4f329
            branch_to='master',
farhaanbukhsh c4f329
            title='PR from the %s branch' % branch_from,
farhaanbukhsh c4f329
            user='pingou',
farhaanbukhsh c4f329
            requestfolder=None,
farhaanbukhsh c4f329
        )
farhaanbukhsh c4f329
        self.session.commit()
farhaanbukhsh c4f329
        self.assertEqual(req.id, 1)
farhaanbukhsh c4f329
        self.assertEqual(req.title, 'PR from the %s branch' % branch_from)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        shutil.rmtree(newpath)
farhaanbukhsh c4f329
farhaanbukhsh 1df65f
    def test_index(self):
farhaanbukhsh 1df65f
        """ Test the index endpoint. """
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        output = self.app.get('/')
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
farhaanbukhsh 1df65f
        self.assertIn(
farhaanbukhsh 1df65f
            '

All Projects '

farhaanbukhsh 1df65f
            '0', output.data)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a private project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
farhaanbukhsh 1df65f
            name='test3',
farhaanbukhsh 1df65f
            description='test project description',
farhaanbukhsh 1df65f
            hook_token='aaabbbeee',
farhaanbukhsh 1df65f
            private=True,
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a public project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
farhaanbukhsh 1df65f
            name='test4',
farhaanbukhsh 1df65f
            description='test project description',
farhaanbukhsh 1df65f
            hook_token='aaabbbeeeccceee',
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
        self.session.commit()
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        output = self.app.get('/?page=abc')
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
farhaanbukhsh 1df65f
        self.assertIn(
farhaanbukhsh 1df65f
            '

All Projects '

farhaanbukhsh 44a97a
            '1', output.data)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        user = tests.FakeUser(username='foo')
farhaanbukhsh 1df65f
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 3604c1
            output = self.app.get('/')
farhaanbukhsh 1df65f
            self.assertIn(
Pierre-Yves Chibon 3604c1
                'My Projects 2',
farhaanbukhsh 1df65f
                output.data)
farhaanbukhsh 1df65f
            self.assertIn(
farhaanbukhsh 1df65f
                'Forks 0',
farhaanbukhsh 1df65f
                output.data)
farhaanbukhsh 1df65f
            self.assertEqual(
farhaanbukhsh 1df65f
                output.data.count('

No group found

'), 1)
farhaanbukhsh 1df65f
            self.assertEqual(
farhaanbukhsh 1df65f
                output.data.count('
'), 3)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
    def test_view_user(self):
farhaanbukhsh 1df65f
        """ Test the view_user endpoint. """
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        output = self.app.get('/user/foo?repopage=abc&forkpage=def')
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
farhaanbukhsh 1df65f
        self.assertIn(
farhaanbukhsh 1df65f
            'Projects 0',
farhaanbukhsh 1df65f
            output.data)
farhaanbukhsh 1df65f
        self.assertIn(
farhaanbukhsh 1df65f
            'Forks 0',
farhaanbukhsh 1df65f
            output.data)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a private project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
farhaanbukhsh 1df65f
            name='test3',
farhaanbukhsh 1df65f
            description='test project description',
farhaanbukhsh 1df65f
            hook_token='aaabbbeee',
farhaanbukhsh 1df65f
            private=True,
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        # Add a public project
farhaanbukhsh 1df65f
        item = pagure.lib.model.Project(
farhaanbukhsh 1df65f
            user_id=2,  # foo
farhaanbukhsh 1df65f
            name='test4',
farhaanbukhsh 1df65f
            description='test project description',
farhaanbukhsh 1df65f
            hook_token='aaabbbeeeccceee',
farhaanbukhsh 1df65f
        )
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.session.add(item)
farhaanbukhsh 1df65f
        self.session.commit()
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        self.gitrepos = tests.create_projects_git(
farhaanbukhsh 1df65f
            pagure.APP.config['GIT_FOLDER'])
farhaanbukhsh 1df65f
Pierre-Yves Chibon 3604c1
        output = self.app.get('/user/foo')
farhaanbukhsh 1df65f
        self.assertEqual(output.status_code, 200)
farhaanbukhsh 1df65f
        self.assertIn(
farhaanbukhsh 1df65f
            'Projects 1',
farhaanbukhsh 1df65f
            output.data)
farhaanbukhsh 1df65f
        self.assertIn(
farhaanbukhsh 1df65f
            'Forks 0', output.data)
farhaanbukhsh 1df65f
farhaanbukhsh 1df65f
        user = tests.FakeUser(username='foo')
farhaanbukhsh 1df65f
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 3604c1
            output = self.app.get('/user/foo')
farhaanbukhsh 1df65f
            self.assertIn(
farhaanbukhsh 1df65f
                'Projects 2',
farhaanbukhsh 1df65f
                output.data)
farhaanbukhsh 1df65f
            self.assertIn(
farhaanbukhsh 1df65f
                'Forks 0',
farhaanbukhsh 1df65f
                output.data)
farhaanbukhsh 1df65f
            self.assertEqual(
farhaanbukhsh 1df65f
                output.data.count('

No group found

'), 1)
farhaanbukhsh 1df65f
            self.assertEqual(
farhaanbukhsh 1df65f
                output.data.count('
'), 3)
farhaanbukhsh 1df65f
farhaanbukhsh b909de
        user.username = 'pingou'
farhaanbukhsh 1df65f
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 3604c1
            output = self.app.get('/user/foo')
farhaanbukhsh 1df65f
            self.assertIn(
farhaanbukhsh 1df65f
                'Projects 1',
farhaanbukhsh 1df65f
                output.data)
farhaanbukhsh 1df65f
            self.assertIn(
farhaanbukhsh 1df65f
                'Forks 0',
farhaanbukhsh 1df65f
                output.data)
farhaanbukhsh 1df65f
            self.assertEqual(
farhaanbukhsh 1df65f
                output.data.count('

No group found

'), 1)
farhaanbukhsh 1df65f
            self.assertEqual(
farhaanbukhsh 1df65f
                output.data.count('
'), 3)
farhaanbukhsh 1df65f
farhaanbukhsh d59922
        # Check pingou has 0 projects
farhaanbukhsh d59922
        user.username = 'pingou'
farhaanbukhsh d59922
        with tests.user_set(pagure.APP, user):
farhaanbukhsh d59922
            output = self.app.get('/')
farhaanbukhsh d59922
            self.assertIn(
farhaanbukhsh d59922
                'My Projects 0',
farhaanbukhsh d59922
                output.data)
farhaanbukhsh d59922
            self.assertIn(
farhaanbukhsh d59922
                'Forks 0',
farhaanbukhsh d59922
                output.data)
farhaanbukhsh d59922
            self.assertEqual(
farhaanbukhsh d59922
                output.data.count('

No group found

'), 1)
farhaanbukhsh d59922
            self.assertEqual(
farhaanbukhsh d59922
                output.data.count('
'), 3)
farhaanbukhsh d59922
farhaanbukhsh b6711e
        repo = pagure.lib.get_project(self.session, 'test3')
farhaanbukhsh b909de
farhaanbukhsh b909de
        msg = pagure.lib.add_user_to_project(
farhaanbukhsh b6711e
            session=self.session,
farhaanbukhsh b6711e
            project=repo,
farhaanbukhsh b6711e
            new_user='pingou',
farhaanbukhsh b6711e
            user='foo',
farhaanbukhsh b6711e
        )
farhaanbukhsh b909de
        self.assertEqual(msg, 'User added')
farhaanbukhsh b6711e
farhaanbukhsh b6711e
        # New user added to private projects
farhaanbukhsh b909de
        user.username = 'pingou'
farhaanbukhsh b6711e
        with tests.user_set(pagure.APP, user):
farhaanbukhsh b6711e
            output = self.app.get('/')
farhaanbukhsh b6711e
            self.assertIn(
farhaanbukhsh b6711e
                'My Projects 1',
farhaanbukhsh b6711e
                output.data)
farhaanbukhsh b6711e
            self.assertIn(
farhaanbukhsh b6711e
                'Forks 0',
farhaanbukhsh b6711e
                output.data)
farhaanbukhsh b6711e
            self.assertEqual(
farhaanbukhsh b6711e
                output.data.count('

No group found

'), 1)
farhaanbukhsh b6711e
            self.assertEqual(
farhaanbukhsh b6711e
                output.data.count('
'), 3)
farhaanbukhsh b6711e
farhaanbukhsh c4f329
    @patch('pagure.ui.repo.admin_session_timedout')
farhaanbukhsh c4f329
    def test_private_settings_ui(self, ast):
farhaanbukhsh c4f329
        """ Test UI for private repo"""
farhaanbukhsh c4f329
farhaanbukhsh b909de
        # Add private repo
farhaanbukhsh 0163a1
        item = pagure.lib.model.Project(
farhaanbukhsh 0163a1
            user_id=1,  # pingou
farhaanbukhsh 0163a1
            name='test4',
farhaanbukhsh 0163a1
            description='test project description',
farhaanbukhsh 0163a1
            hook_token='aaabbbeeeceee',
farhaanbukhsh 0163a1
            private=True,
farhaanbukhsh 0163a1
        )
farhaanbukhsh 0163a1
        self.session.add(item)
farhaanbukhsh 0163a1
        self.session.commit()
farhaanbukhsh 0163a1
farhaanbukhsh 0163a1
        # Add a git repo
farhaanbukhsh b909de
        repo_path = os.path.join(
farhaanbukhsh b909de
            pagure.APP.config.get('GIT_FOLDER'), 'test4.git')
farhaanbukhsh 0163a1
        if not os.path.exists(repo_path):
farhaanbukhsh 0163a1
            os.makedirs(repo_path)
farhaanbukhsh 0163a1
        pygit2.init_repository(repo_path)
farhaanbukhsh 0163a1
farhaanbukhsh c4f329
        user = tests.FakeUser(username='pingou')
farhaanbukhsh c4f329
        with tests.user_set(pagure.APP, user):
farhaanbukhsh c4f329
            tests.create_projects(self.session)
farhaanbukhsh 4858f8
            tests.create_projects_git(pagure.APP.config.get('GIT_FOLDER'))
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            ast.return_value = False
farhaanbukhsh c4f329
            output = self.app.post('/test/settings')
farhaanbukhsh c4f329
farhaanbukhsh 0163a1
            # Check for a public repo
farhaanbukhsh c4f329
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
            self.assertIn(
farhaanbukhsh c4f329
                '
farhaanbukhsh c4f329
farhaanbukhsh 0163a1
            ast.return_value = False
farhaanbukhsh 0163a1
            output = self.app.post('/test4/settings')
farhaanbukhsh 0163a1
farhaanbukhsh 0163a1
            # Check for private repo
farhaanbukhsh 0163a1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 0163a1
            self.assertIn(
farhaanbukhsh 0163a1
                '<input checked="" name="private" type="checkbox" value="private">', output.data)
farhaanbukhsh 0163a1
farhaanbukhsh fc046c
            # Check the new project form has 'private' checkbox
farhaanbukhsh fc046c
            output = self.app.get('/new')
farhaanbukhsh fc046c
            self.assertEqual(output.status_code, 200)
farhaanbukhsh fc046c
            self.assertIn(
farhaanbukhsh fc046c
                '<input id="private" name="private" type="checkbox" value="y">', output.data)
farhaanbukhsh 0163a1
farhaanbukhsh 4ea512
    @patch('pagure.lib.notify.send_email')
farhaanbukhsh 4ea512
    def test_private_pr(self, send_email):
farhaanbukhsh c4f329
        """Test pull request made to the private repo"""
farhaanbukhsh c4f329
farhaanbukhsh 4ea512
        send_email.return_value = True
farhaanbukhsh c4f329
        # Add a private project
farhaanbukhsh c4f329
        item = pagure.lib.model.Project(
farhaanbukhsh c4f329
            user_id=1,  # pingou
farhaanbukhsh c4f329
            name='pmc',
farhaanbukhsh c4f329
            description='test project description',
farhaanbukhsh c4f329
            hook_token='aaabbbeeeceee',
farhaanbukhsh c4f329
            private=True,
farhaanbukhsh c4f329
        )
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        self.session.add(item)
farhaanbukhsh c4f329
        self.session.commit()
farhaanbukhsh c4f329
farhaanbukhsh f889f7
        repo = pagure.lib.get_project(self.session, 'pmc')
farhaanbukhsh f889f7
farhaanbukhsh f889f7
        msg = pagure.lib.add_user_to_project(
farhaanbukhsh f889f7
            session=self.session,
farhaanbukhsh f889f7
            project=repo,
farhaanbukhsh f889f7
            new_user='foo',
farhaanbukhsh f889f7
            user='pingou',
farhaanbukhsh f889f7
        )
farhaanbukhsh f889f7
        self.session.commit()
farhaanbukhsh f889f7
        self.assertEqual(msg, 'User added')
farhaanbukhsh f889f7
farhaanbukhsh c4f329
        # Create all the git repos
farhaanbukhsh c4f329
        tests.create_projects_git(
farhaanbukhsh c4f329
            os.path.join(tests.HERE, 'requests'), bare=True)
farhaanbukhsh c4f329
farhaanbukhsh 1432c1
        # Add a git repo
farhaanbukhsh b909de
        repo_path = os.path.join(
farhaanbukhsh b909de
            pagure.APP.config.get('REQUESTS_FOLDER'), 'pmc.git')
farhaanbukhsh 1432c1
        if not os.path.exists(repo_path):
farhaanbukhsh 1432c1
            os.makedirs(repo_path)
farhaanbukhsh 1432c1
        pygit2.init_repository(repo_path, bare=True)
farhaanbukhsh 1432c1
farhaanbukhsh c4f329
        # Check repo was created
farhaanbukhsh c4f329
        user = tests.FakeUser(username='pingou')
farhaanbukhsh c4f329
        with tests.user_set(pagure.APP, user):
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            output = self.app.get('/user/pingou/')
farhaanbukhsh c4f329
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
            self.assertIn(
farhaanbukhsh c4f329
                '
\n Projects
farhaanbukhsh c4f329
                'class="label label-default">1', output.data)
farhaanbukhsh c4f329
            self.assertIn(
farhaanbukhsh c4f329
                'Forks 0',
farhaanbukhsh c4f329
                output.data)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            self.set_up_git_repo(new_project=None, branch_from='feature')
farhaanbukhsh c4f329
            project = pagure.lib.get_project(self.session, 'pmc')
farhaanbukhsh c4f329
            self.assertEqual(len(project.requests), 1)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
            output = self.app.get('/pmc/pull-request/1')
farhaanbukhsh c4f329
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        # Check repo was created
farhaanbukhsh c4f329
        user = tests.FakeUser()
farhaanbukhsh c4f329
        with tests.user_set(pagure.APP, user):
farhaanbukhsh c4f329
            output = self.app.get('/pmc/pull-requests')
farhaanbukhsh e9261e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh c4f329
farhaanbukhsh c4f329
        user = tests.FakeUser(username='pingou')
farhaanbukhsh c4f329
        with tests.user_set(pagure.APP, user):
farhaanbukhsh c4f329
            output = self.app.get('/pmc/pull-requests')
farhaanbukhsh 1432c1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh c4f329
farhaanbukhsh f889f7
        user = tests.FakeUser(username='foo')
farhaanbukhsh f889f7
        with tests.user_set(pagure.APP, user):
farhaanbukhsh f889f7
            output = self.app.get('/pmc/pull-requests')
farhaanbukhsh f889f7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh f889f7
farhaanbukhsh 420a97
    @patch('pagure.lib.git.update_git')
farhaanbukhsh 420a97
    @patch('pagure.lib.notify.send_email')
farhaanbukhsh 420a97
    def test_private_repo_issues_ui(self, p_send_email, p_ugt):
farhaanbukhsh 420a97
        """ Test issues made to private repo"""
farhaanbukhsh 420a97
        p_send_email.return_value = True
farhaanbukhsh 420a97
        p_ugt.return_value = True
farhaanbukhsh 420a97
farhaanbukhsh 420a97
        # Add private repo
farhaanbukhsh 420a97
        item = pagure.lib.model.Project(
farhaanbukhsh 420a97
            user_id=1,  # pingou
farhaanbukhsh 420a97
            name='test4',
farhaanbukhsh 420a97
            description='test project description',
farhaanbukhsh 420a97
            hook_token='aaabbbeeeceee',
farhaanbukhsh 420a97
            private=True,
farhaanbukhsh 420a97
        )
farhaanbukhsh 420a97
        self.session.add(item)
farhaanbukhsh 420a97
        self.session.commit()
farhaanbukhsh 420a97
farhaanbukhsh d59922
        for repo in ['GIT_FOLDER', 'TICKETS_FOLDER']:
farhaanbukhsh d59922
            # Add a git repo
farhaanbukhsh d59922
            repo_path = os.path.join(
farhaanbukhsh d59922
                pagure.APP.config.get(repo), 'test4.git')
farhaanbukhsh d59922
            if not os.path.exists(repo_path):
farhaanbukhsh d59922
                os.makedirs(repo_path)
farhaanbukhsh d59922
            pygit2.init_repository(repo_path)
farhaanbukhsh 420a97
farhaanbukhsh 44a97a
        # Check if the private repo issues are publicly not accesible
farhaanbukhsh 420a97
        output = self.app.get('/test4/issues')
farhaanbukhsh e9261e
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 420a97
farhaanbukhsh 420a97
        # Create issues to play with
farhaanbukhsh 420a97
        repo = pagure.lib.get_project(self.session, 'test4')
farhaanbukhsh 420a97
        msg = pagure.lib.new_issue(
farhaanbukhsh 420a97
            session=self.session,
farhaanbukhsh 420a97
            repo=repo,
farhaanbukhsh 420a97
            title='Test issue',
farhaanbukhsh 420a97
            content='We should work on this',
farhaanbukhsh 420a97
            user='pingou',
farhaanbukhsh 420a97
            ticketfolder=None
farhaanbukhsh 420a97
        )
farhaanbukhsh 420a97
        self.session.commit()
farhaanbukhsh 420a97
        self.assertEqual(msg.title, 'Test issue')
farhaanbukhsh 420a97
farhaanbukhsh 420a97
        user = tests.FakeUser()
farhaanbukhsh 420a97
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Whole list
farhaanbukhsh 420a97
            output = self.app.get('/test4/issues')
farhaanbukhsh e9261e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Check single issue
farhaanbukhsh 420a97
            output = self.app.get('/test4/issue/1')
farhaanbukhsh e9261e
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 420a97
farhaanbukhsh 44a97a
        user = tests.FakeUser()
farhaanbukhsh 44a97a
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
            # Whole list
farhaanbukhsh 44a97a
            output = self.app.get('/test4/issues')
farhaanbukhsh 44a97a
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 44a97a
farhaanbukhsh 420a97
        user = tests.FakeUser(username='pingou')
farhaanbukhsh 420a97
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Whole list
farhaanbukhsh 420a97
            output = self.app.get('/test4/issues')
farhaanbukhsh 420a97
            self.assertEqual(output.status_code, 200)
farhaanbukhsh d59922
            self.assertIn(
farhaanbukhsh d59922
                '<title>Issues - test4 - Pagure</title>', output.data)
farhaanbukhsh 420a97
            self.assertTrue(
farhaanbukhsh d59922
                '

\n 1 Open Issues' in output.data)

farhaanbukhsh 420a97
farhaanbukhsh 420a97
            # Check single issue
farhaanbukhsh 420a97
            output = self.app.get('/test4/issue/1')
farhaanbukhsh 420a97
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 420a97
farhaanbukhsh f889f7
        repo = pagure.lib.get_project(self.session, 'test4')
farhaanbukhsh f889f7
farhaanbukhsh f889f7
        msg = pagure.lib.add_user_to_project(
farhaanbukhsh f889f7
            session=self.session,
farhaanbukhsh f889f7
            project=repo,
farhaanbukhsh f889f7
            new_user='foo',
farhaanbukhsh f889f7
            user='pingou',
farhaanbukhsh f889f7
        )
farhaanbukhsh f889f7
        self.session.commit()
farhaanbukhsh f889f7
        self.assertEqual(msg, 'User added')
farhaanbukhsh f889f7
farhaanbukhsh d59922
        user.username = 'foo'
farhaanbukhsh f889f7
        with tests.user_set(pagure.APP, user):
farhaanbukhsh f889f7
farhaanbukhsh f889f7
            # Whole list
farhaanbukhsh f889f7
            output = self.app.get('/test4/issues')
farhaanbukhsh f889f7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh d59922
            self.assertIn(
farhaanbukhsh d59922
                '<title>Issues - test4 - Pagure</title>', output.data)
farhaanbukhsh f889f7
            self.assertTrue(
farhaanbukhsh d59922
                '

\n 1 Open Issues' in output.data)

farhaanbukhsh f889f7
farhaanbukhsh f889f7
            # Check single issue
farhaanbukhsh f889f7
            output = self.app.get('/test4/issue/1')
farhaanbukhsh f889f7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh f889f7
farhaanbukhsh 7ca1b1
    # API checks
farhaanbukhsh 7ca1b1
    def test_api_private_repo_projects(self):
farhaanbukhsh 7ca1b1
        """ Test api points for private repo for projects"""
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Add private repo
farhaanbukhsh 60d445
        item = pagure.lib.model.Project(
farhaanbukhsh 60d445
            user_id=1,  # pingou
farhaanbukhsh 60d445
            name='test4',
farhaanbukhsh 60d445
            description='test project description',
farhaanbukhsh 60d445
            hook_token='aaabbbeeeceee',
farhaanbukhsh 60d445
            private=True,
farhaanbukhsh 60d445
        )
farhaanbukhsh 60d445
        self.session.add(item)
farhaanbukhsh 60d445
        self.session.commit()
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Create a git repo to play with
farhaanbukhsh 60d445
        gitrepo = os.path.join(tests.HERE, 'repos', 'test4.git')
farhaanbukhsh 60d445
        repo = pygit2.init_repository(gitrepo, bare=True)
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
farhaanbukhsh 60d445
        repopath = os.path.join(newpath, 'test4')
farhaanbukhsh 60d445
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Create a file in that git repo
farhaanbukhsh 60d445
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
farhaanbukhsh 60d445
            stream.write('foo\n bar')
farhaanbukhsh 60d445
        clone_repo.index.add('sources')
farhaanbukhsh 60d445
        clone_repo.index.write()
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Commits the files added
farhaanbukhsh 60d445
        tree = clone_repo.index.write_tree()
farhaanbukhsh 60d445
        author = pygit2.Signature(
farhaanbukhsh 60d445
            'Alice Author', 'alice@authors.tld')
farhaanbukhsh 60d445
        committer = pygit2.Signature(
farhaanbukhsh 60d445
            'Cecil Committer', 'cecil@committers.tld')
farhaanbukhsh 60d445
        clone_repo.create_commit(
farhaanbukhsh 60d445
            'refs/heads/master',  # the name of the reference to update
farhaanbukhsh 60d445
            author,
farhaanbukhsh 60d445
            committer,
farhaanbukhsh 60d445
            'Add sources file for testing',
farhaanbukhsh 60d445
            # binary string representing the tree object ID
farhaanbukhsh 60d445
            tree,
farhaanbukhsh 60d445
            # list of binary strings representing parents of the new commit
farhaanbukhsh 60d445
            []
farhaanbukhsh 60d445
        )
farhaanbukhsh 60d445
        refname = 'refs/heads/master:refs/heads/master'
farhaanbukhsh 60d445
        ori_remote = clone_repo.remotes[0]
farhaanbukhsh 60d445
        PagureRepo.push(ori_remote, refname)
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Tag our first commit
farhaanbukhsh 60d445
        first_commit = repo.revparse_single('HEAD')
farhaanbukhsh 60d445
        tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
farhaanbukhsh 60d445
        repo.create_tag(
farhaanbukhsh 60d445
            "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
farhaanbukhsh 60d445
            "Release 0.0.1")
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        # Create a token for foo for this project
farhaanbukhsh 60d445
        item = pagure.lib.model.Token(
farhaanbukhsh 60d445
            id='foobar_token',
farhaanbukhsh 60d445
            user_id=1,
farhaanbukhsh 60d445
            project_id=1,
farhaanbukhsh 60d445
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
farhaanbukhsh 60d445
                days=30)
farhaanbukhsh 60d445
        )
farhaanbukhsh 60d445
        self.session.add(item)
farhaanbukhsh 60d445
        self.session.commit()
farhaanbukhsh 60d445
        item = pagure.lib.model.TokenAcl(
farhaanbukhsh 60d445
            token_id='foobar_token',
farhaanbukhsh d59922
            acl_id=1,
farhaanbukhsh 60d445
        )
farhaanbukhsh 60d445
        self.session.add(item)
farhaanbukhsh 60d445
        self.session.commit()
farhaanbukhsh 60d445
farhaanbukhsh 753ee7
        # Check if the admin requests
farhaanbukhsh 753ee7
        user = tests.FakeUser(username='pingou')
farhaanbukhsh 753ee7
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 753ee7
            # Check tags
farhaanbukhsh 753ee7
            output = self.app.get('/api/0/test4/git/tags')
farhaanbukhsh 753ee7
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 753ee7
            data = json.loads(output.data)
farhaanbukhsh 753ee7
            self.assertDictEqual(
farhaanbukhsh 753ee7
                data,
farhaanbukhsh 753ee7
                {'tags': ['0.0.1'], 'total_tags': 1}
farhaanbukhsh 753ee7
            )
farhaanbukhsh 753ee7
farhaanbukhsh 753ee7
        output = self.app.get('/api/0/test4/git/tags')
farhaanbukhsh 753ee7
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 60d445
farhaanbukhsh 753ee7
        # Chekc if user is not admin
farhaanbukhsh 753ee7
        user = tests.FakeUser()
farhaanbukhsh 753ee7
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 753ee7
            output = self.app.get('/api/0/test4/git/tags')
farhaanbukhsh 753ee7
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 60d445
farhaanbukhsh 60d445
        shutil.rmtree(newpath)
Pierre-Yves Chibon 3604c1
farhaanbukhsh 7ca1b1
        # Check before adding
farhaanbukhsh 7ca1b1
        repo = pagure.lib.get_project(self.session, 'test4')
farhaanbukhsh 7ca1b1
        self.assertEqual(repo.tags, [])
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Adding a tag
farhaanbukhsh 7ca1b1
        output = pagure.lib.update_tags(
farhaanbukhsh 7ca1b1
            self.session, repo, 'infra', 'pingou',
farhaanbukhsh 7ca1b1
            ticketfolder=None)
farhaanbukhsh 7ca1b1
        self.assertEqual(output, ['Tag added: infra'])
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Check after adding
farhaanbukhsh 7ca1b1
        repo = pagure.lib.get_project(self.session, 'test4')
farhaanbukhsh 7ca1b1
        self.assertEqual(len(repo.tags), 1)
farhaanbukhsh 7ca1b1
        self.assertEqual(repo.tags_text, ['infra'])
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Check the API
farhaanbukhsh 7ca1b1
        output = self.app.get('/api/0/projects?tags=inf')
farhaanbukhsh 7ca1b1
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 7ca1b1
        data = json.loads(output.data)
farhaanbukhsh 7ca1b1
        self.assertDictEqual(
farhaanbukhsh 7ca1b1
            data,
farhaanbukhsh 7ca1b1
            {'error_code': 'ENOPROJECTS', 'error': 'No projects found'}
farhaanbukhsh 7ca1b1
        )
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        # Request by not a loggged in user
farhaanbukhsh 7ca1b1
        output = self.app.get('/api/0/projects?tags=infra')
farhaanbukhsh 7ca1b1
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        user = tests.FakeUser()
farhaanbukhsh 7ca1b1
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 7ca1b1
            # Request by a non authorized user
farhaanbukhsh 7ca1b1
            output = self.app.get('/api/0/projects?tags=infra')
farhaanbukhsh 7ca1b1
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
        user.username = 'pingou'
farhaanbukhsh 7ca1b1
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 7ca1b1
            # Private repo username is compulsion to pass
farhaanbukhsh 7ca1b1
            output = self.app.get('/api/0/projects?tags=infra')
farhaanbukhsh 7ca1b1
            self.assertEqual(output.status_code, 404)
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
            output = self.app.get('/api/0/projects?username=pingou')
farhaanbukhsh 7ca1b1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 7ca1b1
            data = json.loads(output.data)
farhaanbukhsh 7ca1b1
            data['projects'][0]['date_created'] = "1436527638"
farhaanbukhsh 7ca1b1
            self.assertDictEqual(
farhaanbukhsh 7ca1b1
                data,
farhaanbukhsh 7ca1b1
                {
farhaanbukhsh 7ca1b1
                    "total_projects": 1,
farhaanbukhsh 7ca1b1
                    "projects": [
farhaanbukhsh 7ca1b1
                        {
farhaanbukhsh 7ca1b1
                            "date_created": "1436527638",
farhaanbukhsh 7ca1b1
                            "description": "test project description",
farhaanbukhsh 7ca1b1
                            "id": 1,
farhaanbukhsh 7ca1b1
                            "name": "test4",
farhaanbukhsh 7ca1b1
                            "parent": None,
farhaanbukhsh 7ca1b1
                            "priorities": {},
farhaanbukhsh 7ca1b1
                            "tags": ["infra"],
farhaanbukhsh 7ca1b1
                            "user": {
farhaanbukhsh 7ca1b1
                                "fullname": "PY C",
farhaanbukhsh 7ca1b1
                                "name": "pingou"
farhaanbukhsh 7ca1b1
                            }
farhaanbukhsh 7ca1b1
                        },
farhaanbukhsh 7ca1b1
                    ]
farhaanbukhsh 7ca1b1
                }
farhaanbukhsh 7ca1b1
            )
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
            output = self.app.get('/api/0/projects?username=pingou&tags=infra')
farhaanbukhsh 7ca1b1
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 7ca1b1
            data = json.loads(output.data)
farhaanbukhsh 7ca1b1
            data['projects'][0]['date_created'] = "1436527638"
farhaanbukhsh 7ca1b1
            self.assertDictEqual(
farhaanbukhsh 7ca1b1
                data,
farhaanbukhsh 7ca1b1
                {
farhaanbukhsh 7ca1b1
                    "total_projects": 1,
farhaanbukhsh 7ca1b1
                    "projects": [
farhaanbukhsh 7ca1b1
                        {
farhaanbukhsh 7ca1b1
                            "date_created": "1436527638",
farhaanbukhsh 7ca1b1
                            "description": "test project description",
farhaanbukhsh 7ca1b1
                            "id": 1,
farhaanbukhsh 7ca1b1
                            "name": "test4",
farhaanbukhsh 7ca1b1
                            "parent": None,
farhaanbukhsh 7ca1b1
                            "priorities": {},
farhaanbukhsh 7ca1b1
                            "tags": ["infra"],
farhaanbukhsh 7ca1b1
                            "user": {
farhaanbukhsh 7ca1b1
                                "fullname": "PY C",
farhaanbukhsh 7ca1b1
                                "name": "pingou"
farhaanbukhsh 7ca1b1
                            }
farhaanbukhsh 7ca1b1
                        }
farhaanbukhsh 7ca1b1
                    ]
farhaanbukhsh 7ca1b1
                }
farhaanbukhsh 7ca1b1
            )
farhaanbukhsh 7ca1b1
farhaanbukhsh 7ca1b1
farhaanbukhsh 44a97a
    # Api pull-request views
farhaanbukhsh 44a97a
    @patch('pagure.lib.notify.send_email')
farhaanbukhsh 44a97a
    def test_api_private_repo_fork(self,send_email):
farhaanbukhsh 44a97a
        """ Test api endpoints in api/fork"""
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
        send_email.return_value = True
farhaanbukhsh 44a97a
        # Add private repo
farhaanbukhsh 44a97a
        item = pagure.lib.model.Project(
farhaanbukhsh 44a97a
            user_id=1,  # pingou
farhaanbukhsh 44a97a
            name='test4',
farhaanbukhsh 44a97a
            description='test project description',
farhaanbukhsh 44a97a
            hook_token='aaabbbeeeceee',
farhaanbukhsh 44a97a
            private=True,
farhaanbukhsh 44a97a
        )
farhaanbukhsh 44a97a
        self.session.add(item)
farhaanbukhsh 44a97a
        self.session.commit()
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
        # Create a token for pingou for this project
farhaanbukhsh 44a97a
        item = pagure.lib.model.Token(
farhaanbukhsh 44a97a
            id='foobar_token',
farhaanbukhsh 44a97a
            user_id=1,
farhaanbukhsh 44a97a
            project_id=1,
farhaanbukhsh 44a97a
            expiration=datetime.datetime.utcnow() + datetime.timedelta(
farhaanbukhsh 44a97a
                days=30)
farhaanbukhsh 44a97a
        )
farhaanbukhsh 44a97a
        self.session.add(item)
farhaanbukhsh 44a97a
        self.session.commit()
farhaanbukhsh 44a97a
        item = pagure.lib.model.TokenAcl(
farhaanbukhsh 44a97a
            token_id='foobar_token',
farhaanbukhsh 44a97a
            acl_id=1,
farhaanbukhsh 44a97a
        )
farhaanbukhsh 44a97a
        self.session.add(item)
farhaanbukhsh 44a97a
        self.session.commit()
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
        # Create a pull-request
farhaanbukhsh 44a97a
        repo = pagure.lib.get_project(self.session, 'test4')
farhaanbukhsh 44a97a
        forked_repo = pagure.lib.get_project(self.session, 'test4')
farhaanbukhsh 44a97a
        req = pagure.lib.new_pull_request(
farhaanbukhsh 44a97a
            session=self.session,
farhaanbukhsh 44a97a
            repo_from=forked_repo,
farhaanbukhsh 44a97a
            branch_from='master',
farhaanbukhsh 44a97a
            repo_to=repo,
farhaanbukhsh 44a97a
            branch_to='master',
farhaanbukhsh 44a97a
            title='test pull-request',
farhaanbukhsh 44a97a
            user='pingou',
farhaanbukhsh 44a97a
            requestfolder=None,
farhaanbukhsh 44a97a
        )
farhaanbukhsh 44a97a
        self.session.commit()
farhaanbukhsh 44a97a
        self.assertEqual(req.id, 1)
farhaanbukhsh 44a97a
        self.assertEqual(req.title, 'test pull-request')
farhaanbukhsh 44a97a
        output = self.app.get('/api/0/test4/pull-requests')
farhaanbukhsh 44a97a
        self.assertEqual(output.status_code, 404)
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
        user = tests.FakeUser(username='pingou')
farhaanbukhsh 44a97a
        with tests.user_set(pagure.APP, user):
farhaanbukhsh 44a97a
            # List pull-requests
farhaanbukhsh 44a97a
            output = self.app.get('/api/0/test4/pull-requests')
farhaanbukhsh 44a97a
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 44a97a
            data = json.loads(output.data)
farhaanbukhsh 44a97a
            data['requests'][0]['date_created'] = '1431414800'
farhaanbukhsh 44a97a
            data['requests'][0]['updated_on'] = '1431414800'
farhaanbukhsh 44a97a
            data['requests'][0]['project']['date_created'] = '1431414800'
farhaanbukhsh 44a97a
            data['requests'][0]['repo_from']['date_created'] = '1431414800'
farhaanbukhsh 44a97a
            data['requests'][0]['uid'] = '1431414800'
farhaanbukhsh 44a97a
            self.assertDictEqual(
farhaanbukhsh 44a97a
                data,
farhaanbukhsh 44a97a
                {
farhaanbukhsh 44a97a
                    'args': {
farhaanbukhsh 44a97a
                             'assignee': None,
farhaanbukhsh 44a97a
                             'author': None,
farhaanbukhsh 44a97a
                             'status': True
farhaanbukhsh 44a97a
                        },
farhaanbukhsh 44a97a
                         'requests': [{
farhaanbukhsh 44a97a
                             'assignee': None,
farhaanbukhsh 44a97a
                             'branch':  'master',
farhaanbukhsh 44a97a
                             'branch_from':  'master',
farhaanbukhsh 44a97a
                             'closed_at': None,
farhaanbukhsh 44a97a
                             'closed_by': None,
farhaanbukhsh 44a97a
                             'comments': [],
farhaanbukhsh 44a97a
                             'commit_start': None,
farhaanbukhsh 44a97a
                             'commit_stop': None,
farhaanbukhsh 44a97a
                             'date_created': '1431414800',
farhaanbukhsh 44a97a
                             'id': 1,
farhaanbukhsh 44a97a
                             'initial_comment': None,
farhaanbukhsh 44a97a
                             'project': {
farhaanbukhsh 44a97a
                                 'date_created': '1431414800',
farhaanbukhsh 44a97a
                                 'description':  'test project description',
farhaanbukhsh 44a97a
                                 'id': 1,
farhaanbukhsh 44a97a
                                 'name':  'test4',
farhaanbukhsh 44a97a
                                 'parent': None,
farhaanbukhsh 44a97a
                                 'priorities': {},
farhaanbukhsh 44a97a
                                 'tags': [],
farhaanbukhsh 44a97a
                                 'user': {
farhaanbukhsh 44a97a
                                     'fullname':  'PY C',
farhaanbukhsh 44a97a
                                     'name':  'pingou'
farhaanbukhsh 44a97a
                                }
farhaanbukhsh 44a97a
                            },
farhaanbukhsh 44a97a
                             'remote_git': None,
farhaanbukhsh 44a97a
                             'repo_from': {
farhaanbukhsh 44a97a
                                 'date_created': '1431414800',
farhaanbukhsh 44a97a
                                 'description':  'test project description',
farhaanbukhsh 44a97a
                                 'id': 1,
farhaanbukhsh 44a97a
                                 'name':  'test4',
farhaanbukhsh 44a97a
                                 'parent': None,
farhaanbukhsh 44a97a
                                 'priorities': {},
farhaanbukhsh 44a97a
                                 'tags': [],
farhaanbukhsh 44a97a
                                 'user': {
farhaanbukhsh 44a97a
                                     'fullname':  'PY C',
farhaanbukhsh 44a97a
                                     'name':  'pingou'
farhaanbukhsh 44a97a
                                }
farhaanbukhsh 44a97a
                            },
farhaanbukhsh 44a97a
                             'status':  'Open',
farhaanbukhsh 44a97a
                             'title':  'test pull-request',
farhaanbukhsh 44a97a
                             'uid': '1431414800',
farhaanbukhsh 44a97a
                             'updated_on': '1431414800',
farhaanbukhsh 44a97a
                             'user': {
farhaanbukhsh 44a97a
                                 'fullname':  'PY C',
farhaanbukhsh 44a97a
                                 'name':  'pingou'
farhaanbukhsh 44a97a
                            }
farhaanbukhsh 44a97a
                        }],
farhaanbukhsh 44a97a
                         'total_requests': 1
farhaanbukhsh 44a97a
                }
farhaanbukhsh 44a97a
            )
farhaanbukhsh 44a97a
            headers = {'Authorization': 'token foobar_token'}
farhaanbukhsh 44a97a
farhaanbukhsh 44a97a
            # Access Pull-Request authenticated
farhaanbukhsh 44a97a
            output = self.app.get('/api/0/test4/pull-requests', headers=headers)
farhaanbukhsh 44a97a
            self.assertEqual(output.status_code, 200)
farhaanbukhsh 44a97a
            data2 = json.loads(output.data)
farhaanbukhsh 44a97a
            data2['requests'][0]['date_created'] = '1431414800'
farhaanbukhsh 44a97a
            data2['requests'][0]['updated_on'] = '1431414800'
farhaanbukhsh 44a97a
            data2['requests'][0]['project']['date_created'] = '1431414800'
farhaanbukhsh 44a97a
            data2['requests'][0]['repo_from']['date_created'] = '1431414800'
farhaanbukhsh 44a97a
            data2['requests'][0]['uid'] = '1431414800'
farhaanbukhsh 44a97a
            self.assertDictEqual(data, data2)
farhaanbukhsh 44a97a
farhaanbukhsh 1df65f
if __name__ == '__main__':
farhaanbukhsh 1df65f
    SUITE = unittest.TestLoader().loadTestsFromTestCase(PagurePrivateRepotest)
farhaanbukhsh 1df65f
    unittest.TextTestRunner(verbosity=2).run(SUITE)