Blame tests/__init__.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
"""
Pierre-Yves Chibon 413073
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
 Authors:
Pierre-Yves Chibon 413073
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
"""
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
__requires__ = ['SQLAlchemy >= 0.7']
Pierre-Yves Chibon 413073
import pkg_resources
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
import unittest
Pierre-Yves Chibon 59d644
import shutil
Pierre-Yves Chibon 413073
import sys
Pierre-Yves Chibon c27b0d
import tempfile
Pierre-Yves Chibon 413073
import os
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
from datetime import date
Pierre-Yves Chibon 21f499
from datetime import datetime
Pierre-Yves Chibon 413073
from datetime import timedelta
Pierre-Yves Chibon 413073
from functools import wraps
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 4e9b76
import pygit2
Pierre-Yves Chibon 4e9b76
Pierre-Yves Chibon 413073
from contextlib import contextmanager
Pierre-Yves Chibon 413073
from sqlalchemy import create_engine
Pierre-Yves Chibon 413073
from sqlalchemy.orm import sessionmaker
Pierre-Yves Chibon 413073
from sqlalchemy.orm import scoped_session
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 413073
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 413073
Pierre-Yves Chibon fe5017
import pagure
Pierre-Yves Chibon fe5017
import pagure.lib
Pierre-Yves Chibon fe5017
import pagure.lib.model
Pierre-Yves Chibon 27a73d
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
DB_PATH = 'sqlite:///:memory:'
Pierre-Yves Chibon c2fe6f
FAITOUT_URL = 'http://faitout.cloud.fedoraproject.org/faitout/'
Pierre-Yves Chibon f62635
HERE = os.path.join(os.path.dirname(os.path.abspath(__file__)))
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
if os.environ.get('BUILD_ID'):
Pierre-Yves Chibon 413073
    try:
Pierre-Yves Chibon 413073
        import requests
Pierre-Yves Chibon 413073
        req = requests.get('%s/new' % FAITOUT_URL)
Pierre-Yves Chibon 413073
        if req.status_code == 200:
Pierre-Yves Chibon 413073
            DB_PATH = req.text
Pierre-Yves Chibon 413073
            print 'Using faitout at: %s' % DB_PATH
Pierre-Yves Chibon 413073
    except:
Pierre-Yves Chibon 413073
        pass
Pierre-Yves Chibon 413073
Pierre-Yves Chibon d14b61
# Remove the log handlers for the tests
Pierre-Yves Chibon fe5017
pagure.LOG.handlers = []
Pierre-Yves Chibon d14b61
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
@contextmanager
Pierre-Yves Chibon 413073
def user_set(APP, user):
Pierre-Yves Chibon 413073
    """ Set the provided user as fas_user in the provided application."""
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    # Hack used to remove the before_request function set by
Pierre-Yves Chibon 413073
    # flask.ext.fas_openid.FAS which otherwise kills our effort to set a
Pierre-Yves Chibon 413073
    # flask.g.fas_user.
Pierre-Yves Chibon 413073
    from flask import appcontext_pushed, g
Pierre-Yves Chibon 413073
    APP.before_request_funcs[None] = []
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    def handler(sender, **kwargs):
Pierre-Yves Chibon 413073
        g.fas_user = user
Pierre-Yves Chibon e120d5
        g.fas_session_id = b'123'
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    with appcontext_pushed.connected_to(handler, APP):
Pierre-Yves Chibon 413073
        yield
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
class Modeltests(unittest.TestCase):
Pierre-Yves Chibon 413073
    """ Model tests. """
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    def __init__(self, method_name='runTest'):
Pierre-Yves Chibon 413073
        """ Constructor. """
Pierre-Yves Chibon 413073
        unittest.TestCase.__init__(self, method_name)
Pierre-Yves Chibon 413073
        self.session = None
Pierre-Yves Chibon fe5017
        self.path = tempfile.mkdtemp(prefix='pagure-tests')
Pierre-Yves Chibon 3a4d42
        self.gitrepo = None
Pierre-Yves Chibon 619b9a
        self.gitrepos = None
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    # pylint: disable=C0103
Pierre-Yves Chibon 413073
    def setUp(self):
Pierre-Yves Chibon 413073
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon aab707
        # Clean up eventual git repo left in the present folder.
Pierre-Yves Chibon aab707
        for filename in os.listdir(HERE):
Pierre-Yves Chibon ad1cac
            filename = os.path.join(HERE, filename)
Pierre-Yves Chibon aab707
            if filename.endswith('.git') and os.path.isdir(filename):
Pierre-Yves Chibon aab707
                shutil.rmtree(filename)
Pierre-Yves Chibon aab707
Pierre-Yves Chibon 1c965f
        for folder in ['tickets', 'repos', 'forks', 'docs',
Pierre-Yves Chibon 1c965f
                       'requests' ,'releases']:
Pierre-Yves Chibon 58e731
            folder = os.path.join(HERE, folder)
Pierre-Yves Chibon 58e731
            if os.path.exists(folder):
Pierre-Yves Chibon 58e731
                shutil.rmtree(folder)
Pierre-Yves Chibon 58e731
            os.mkdir(folder)
Pierre-Yves Chibon 58e731
Pierre-Yves Chibon 973fb9
        self.session = pagure.lib.model.create_tables(
Pierre-Yves Chibon 973fb9
            DB_PATH, acls=pagure.APP.config.get('ACLS', {}))
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
        # Create a couple of users
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.User(
Pierre-Yves Chibon 413073
            user='pingou',
Pierre-Yves Chibon 413073
            fullname='PY C',
Pierre-Yves Chibon 413073
            password='foo',
Pierre-Yves Chibon 0e3e63
            default_email='bar@pingou.com',
Pierre-Yves Chibon 413073
        )
Pierre-Yves Chibon 413073
        self.session.add(item)
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 0daa1b
            user_id=1,
Pierre-Yves Chibon 0daa1b
            email='bar@pingou.com')
Pierre-Yves Chibon 0daa1b
        self.session.add(item)
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 0daa1b
            user_id=1,
Pierre-Yves Chibon 0daa1b
            email='foo@pingou.com')
Pierre-Yves Chibon 0daa1b
        self.session.add(item)
Pierre-Yves Chibon 413073
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.User(
Pierre-Yves Chibon 413073
            user='foo',
Pierre-Yves Chibon 413073
            fullname='foo bar',
Pierre-Yves Chibon 413073
            password='foo',
Pierre-Yves Chibon 0e3e63
            default_email='foo@bar.com',
Pierre-Yves Chibon 413073
        )
Pierre-Yves Chibon 413073
        self.session.add(item)
Pierre-Yves Chibon fe5017
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 0daa1b
            user_id=2,
Pierre-Yves Chibon 0daa1b
            email='foo@bar.com')
Pierre-Yves Chibon 0daa1b
        self.session.add(item)
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
        self.session.commit()
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 564e63
        # Prevent unit-tests to send email, globally
Pierre-Yves Chibon 564e63
        pagure.APP.config['EMAIL_SEND'] = False
Pierre-Yves Chibon 564e63
Pierre-Yves Chibon 413073
    # pylint: disable=C0103
Pierre-Yves Chibon 413073
    def tearDown(self):
Pierre-Yves Chibon 413073
        """ Remove the test.db database if there is one. """
Pierre-Yves Chibon 413073
        self.session.close()
Pierre-Yves Chibon 7cbc4e
Pierre-Yves Chibon 7cbc4e
        # Clear temp directory
Pierre-Yves Chibon c480d9
        shutil.rmtree(self.path)
Pierre-Yves Chibon 7cbc4e
Pierre-Yves Chibon 7cbc4e
        # Clear DB
Pierre-Yves Chibon 413073
        if os.path.exists(DB_PATH):
Pierre-Yves Chibon 413073
            os.unlink(DB_PATH)
Pierre-Yves Chibon 413073
        if DB_PATH.startswith('postgres'):
Pierre-Yves Chibon 413073
            if 'localhost' in DB_PATH:
Pierre-Yves Chibon fe5017
                pagure.lib.model.drop_tables(DB_PATH, self.session.bind)
Pierre-Yves Chibon 413073
            else:
Pierre-Yves Chibon 413073
                db_name = DB_PATH.rsplit('/', 1)[1]
Pierre-Yves Chibon 413073
                requests.get('%s/clean/%s' % (FAITOUT_URL, db_name))
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
class FakeGroup(object):
Pierre-Yves Chibon 413073
    """ Fake object used to make the FakeUser object closer to the
Pierre-Yves Chibon 413073
    expectations.
Pierre-Yves Chibon 413073
    """
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    def __init__(self, name):
Pierre-Yves Chibon 413073
        """ Constructor.
Pierre-Yves Chibon 413073
        :arg name: the name given to the name attribute of this object.
Pierre-Yves Chibon 413073
        """
Pierre-Yves Chibon 413073
        self.name = name
Pierre-Yves Chibon 413073
        self.group_type = 'cla'
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
# pylint: disable=R0903
Pierre-Yves Chibon 413073
class FakeUser(object):
Pierre-Yves Chibon 413073
    """ Fake user used to test the fedocallib library. """
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    def __init__(self, groups=[], username='username', cla_done=True):
Pierre-Yves Chibon 413073
        """ Constructor.
Pierre-Yves Chibon 413073
        :arg groups: list of the groups in which this fake user is
Pierre-Yves Chibon 413073
            supposed to be.
Pierre-Yves Chibon 413073
        """
Pierre-Yves Chibon 413073
        if isinstance(groups, basestring):
Pierre-Yves Chibon 413073
            groups = [groups]
Pierre-Yves Chibon 413073
        self.groups = groups
Pierre-Yves Chibon 413073
        self.username = username
Pierre-Yves Chibon 413073
        self.name = username
Pierre-Yves Chibon c52718
        self.email = 'foo@bar.com'
Pierre-Yves Chibon 413073
        self.approved_memberships = [
Pierre-Yves Chibon 413073
            FakeGroup('packager'),
Pierre-Yves Chibon 413073
            FakeGroup('design-team')
Pierre-Yves Chibon 413073
        ]
Pierre-Yves Chibon 413073
        self.dic = {}
Pierre-Yves Chibon 413073
        self.dic['timezone'] = 'Europe/Paris'
Pierre-Yves Chibon 9fe7c1
        self.login_time = datetime.utcnow()
Pierre-Yves Chibon 413073
        self.cla_done = cla_done
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    def __getitem__(self, key):
Pierre-Yves Chibon 413073
        return self.dic[key]
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
def create_projects(session):
Pierre-Yves Chibon 413073
    """ Create some projects in the database. """
Pierre-Yves Chibon fe5017
    item = pagure.lib.model.Project(
Pierre-Yves Chibon 413073
        user_id=1,  # pingou
Pierre-Yves Chibon 413073
        name='test',
Pierre-Yves Chibon 413073
        description='test project #1',
Pierre-Yves Chibon e2259d
        hook_token='aaabbbccc',
Pierre-Yves Chibon 413073
    )
Pierre-Yves Chibon 413073
    session.add(item)
Pierre-Yves Chibon 413073
Pierre-Yves Chibon fe5017
    item = pagure.lib.model.Project(
Pierre-Yves Chibon 413073
        user_id=1,  # pingou
Pierre-Yves Chibon 413073
        name='test2',
Pierre-Yves Chibon 413073
        description='test project #2',
Pierre-Yves Chibon e2259d
        hook_token='aaabbbddd',
Pierre-Yves Chibon 413073
    )
Pierre-Yves Chibon 413073
    session.add(item)
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    session.commit()
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 05b648
def create_projects_git(folder, bare=False):
Pierre-Yves Chibon 4e9b76
    """ Create some projects in the database. """
Pierre-Yves Chibon 4e9b76
    repos = []
Pierre-Yves Chibon 4e9b76
    for project in ['test.git', 'test2.git']:
Pierre-Yves Chibon 4e9b76
        repo_path = os.path.join(folder, project)
Pierre-Yves Chibon 4e9b76
        repos.append(repo_path)
Pierre-Yves Chibon 63d161
        if not os.path.exists(repo_path):
Pierre-Yves Chibon 63d161
            os.makedirs(repo_path)
Pierre-Yves Chibon 05b648
        pygit2.init_repository(repo_path, bare=bare)
Pierre-Yves Chibon 4e9b76
Pierre-Yves Chibon 4e9b76
    return repos
Pierre-Yves Chibon 4e9b76
Pierre-Yves Chibon 4e9b76
Pierre-Yves Chibon c0b635
def create_tokens(session, user_id=1):
Pierre-Yves Chibon c0b635
    """ Create some tokens for the project in the database. """
Pierre-Yves Chibon c0b635
    item = pagure.lib.model.Token(
Pierre-Yves Chibon c0b635
        id='aaabbbcccddd',
Pierre-Yves Chibon c0b635
        user_id=user_id,
Pierre-Yves Chibon c0b635
        project_id=1,
Pierre-Yves Chibon c0b635
        expiration=datetime.utcnow() + timedelta(days=30)
Pierre-Yves Chibon c0b635
    )
Pierre-Yves Chibon c0b635
    session.add(item)
Pierre-Yves Chibon c0b635
Pierre-Yves Chibon c0b635
    item = pagure.lib.model.Token(
Pierre-Yves Chibon c0b635
        id='foo_token',
Pierre-Yves Chibon c0b635
        user_id=user_id,
Pierre-Yves Chibon c0b635
        project_id=1,
Pierre-Yves Chibon c0b635
        expiration=datetime.utcnow() + timedelta(days=30)
Pierre-Yves Chibon c0b635
    )
Pierre-Yves Chibon c0b635
    session.add(item)
Pierre-Yves Chibon c0b635
Pierre-Yves Chibon c0b635
    item = pagure.lib.model.Token(
Pierre-Yves Chibon c0b635
        id='expired_token',
Pierre-Yves Chibon c0b635
        user_id=user_id,
Pierre-Yves Chibon c0b635
        project_id=1,
Pierre-Yves Chibon c0b635
        expiration=datetime.utcnow() - timedelta(days=1)
Pierre-Yves Chibon c0b635
    )
Pierre-Yves Chibon c0b635
    session.add(item)
Pierre-Yves Chibon c0b635
Pierre-Yves Chibon c0b635
    session.commit()
Pierre-Yves Chibon c0b635
Pierre-Yves Chibon c0b635
Pierre-Yves Chibon 631c33
def create_tokens_acl(session, token_id='aaabbbcccddd'):
Pierre-Yves Chibon 955beb
    """ Create some acls for the tokens. """
Pierre-Yves Chibon e18f81
    for aclid in range(7):
Pierre-Yves Chibon 955beb
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon 631c33
            token_id=token_id,
Pierre-Yves Chibon 9ed075
            acl_id=aclid + 1,
Pierre-Yves Chibon 955beb
        )
Pierre-Yves Chibon 955beb
        session.add(item)
Pierre-Yves Chibon 955beb
Pierre-Yves Chibon 955beb
    session.commit()
Pierre-Yves Chibon 955beb
Pierre-Yves Chibon 955beb
Pierre-Yves Chibon f0ad75
def add_content_git_repo(folder):
Pierre-Yves Chibon f0ad75
    """ Create some content for the specified git repo. """
Pierre-Yves Chibon f0ad75
    if not os.path.exists(folder):
Pierre-Yves Chibon f0ad75
        os.makedirs(folder)
Pierre-Yves Chibon 128dfb
    brepo = pygit2.init_repository(folder, bare=True)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    newfolder = tempfile.mkdtemp(prefix='pagure-tests')
Pierre-Yves Chibon 128dfb
    repo = pygit2.clone_repository(folder, newfolder)
Pierre-Yves Chibon f0ad75
Pierre-Yves Chibon f0ad75
    # Create a file in that git repo
Pierre-Yves Chibon 128dfb
    with open(os.path.join(newfolder, 'sources'), 'w') as stream:
Pierre-Yves Chibon f0ad75
        stream.write('foo\n bar')
Pierre-Yves Chibon f0ad75
    repo.index.add('sources')
Pierre-Yves Chibon f0ad75
    repo.index.write()
Pierre-Yves Chibon f0ad75
Pierre-Yves Chibon a71c8d
    parents = []
Pierre-Yves Chibon a71c8d
    commit = None
Pierre-Yves Chibon a71c8d
    try:
Pierre-Yves Chibon a71c8d
        commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon a71c8d
    except KeyError:
Pierre-Yves Chibon a71c8d
        pass
Pierre-Yves Chibon a71c8d
    if commit:
Pierre-Yves Chibon a71c8d
        parents = [commit.oid.hex]
Pierre-Yves Chibon a71c8d
Pierre-Yves Chibon f0ad75
    # Commits the files added
Pierre-Yves Chibon f0ad75
    tree = repo.index.write_tree()
Pierre-Yves Chibon f0ad75
    author = pygit2.Signature(
Pierre-Yves Chibon f0ad75
        'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon f0ad75
    committer = pygit2.Signature(
Pierre-Yves Chibon f0ad75
        'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon f0ad75
    repo.create_commit(
Pierre-Yves Chibon f0ad75
        'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon f0ad75
        author,
Pierre-Yves Chibon f0ad75
        committer,
Pierre-Yves Chibon f0ad75
        'Add sources file for testing',
Pierre-Yves Chibon f0ad75
        # binary string representing the tree object ID
Pierre-Yves Chibon f0ad75
        tree,
Pierre-Yves Chibon f0ad75
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon a71c8d
        parents,
Pierre-Yves Chibon f0ad75
    )
Pierre-Yves Chibon a71c8d
Pierre-Yves Chibon a71c8d
    parents = []
Pierre-Yves Chibon a71c8d
    commit = None
Pierre-Yves Chibon a71c8d
    try:
Pierre-Yves Chibon a71c8d
        commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon a71c8d
    except KeyError:
Pierre-Yves Chibon a71c8d
        pass
Pierre-Yves Chibon a71c8d
    if commit:
Pierre-Yves Chibon a71c8d
        parents = [commit.oid.hex]
Pierre-Yves Chibon f0ad75
Pierre-Yves Chibon f0ad75
    subfolder = os.path.join('folder1', 'folder2')
Pierre-Yves Chibon 128dfb
    if not os.path.exists(os.path.join(newfolder, subfolder)):
Pierre-Yves Chibon 128dfb
        os.makedirs(os.path.join(newfolder, subfolder))
Pierre-Yves Chibon f0ad75
    # Create a file in that git repo
Pierre-Yves Chibon 128dfb
    with open(os.path.join(newfolder, subfolder, 'file'), 'w') as stream:
Pierre-Yves Chibon f0ad75
        stream.write('foo\n bar\nbaz')
Pierre-Yves Chibon f0ad75
    repo.index.add(os.path.join(subfolder, 'file'))
Pierre-Yves Chibon f0ad75
    repo.index.write()
Pierre-Yves Chibon f0ad75
Pierre-Yves Chibon f0ad75
    # Commits the files added
Pierre-Yves Chibon f0ad75
    tree = repo.index.write_tree()
Pierre-Yves Chibon f0ad75
    author = pygit2.Signature(
Pierre-Yves Chibon f0ad75
        'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon f0ad75
    committer = pygit2.Signature(
Pierre-Yves Chibon f0ad75
        'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon f0ad75
    repo.create_commit(
Pierre-Yves Chibon f0ad75
        'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon f0ad75
        author,
Pierre-Yves Chibon f0ad75
        committer,
Pierre-Yves Chibon f0ad75
        'Add some directory and a file for more testing',
Pierre-Yves Chibon f0ad75
        # binary string representing the tree object ID
Pierre-Yves Chibon f0ad75
        tree,
Pierre-Yves Chibon f0ad75
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon a71c8d
        parents
Pierre-Yves Chibon f0ad75
    )
Pierre-Yves Chibon f0ad75
Pierre-Yves Chibon 128dfb
    # Push to origin
Pierre-Yves Chibon 128dfb
    ori_remote = repo.remotes[0]
Pierre-Yves Chibon 128dfb
    master_ref = repo.lookup_reference('HEAD').resolve()
Pierre-Yves Chibon 128dfb
    refname = '%s:%s' % (master_ref.name, master_ref.name)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 27a73d
    PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    shutil.rmtree(newfolder)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon f0ad75
Pierre-Yves Chibon 87ffe2
def add_readme_git_repo(folder):
Pierre-Yves Chibon 87ffe2
    """ Create a README file for the specified git repo. """
Pierre-Yves Chibon 87ffe2
    if not os.path.exists(folder):
Pierre-Yves Chibon 87ffe2
        os.makedirs(folder)
Pierre-Yves Chibon 128dfb
    brepo = pygit2.init_repository(folder, bare=True)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    newfolder = tempfile.mkdtemp(prefix='pagure-tests')
Pierre-Yves Chibon 128dfb
    repo = pygit2.clone_repository(folder, newfolder)
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon fe5017
    content = """Pagure
Pierre-Yves Chibon 87ffe2
======
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
:Author: Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon fe5017
Pagure is a light-weight git-centered forge based on pygit2.
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon fe5017
Currently, Pagure offers a web-interface for git repositories, a ticket
Pierre-Yves Chibon fe5017
system and possibilities to create new projects, fork existing ones and
Pierre-Yves Chibon fe5017
create/merge pull-requests across or within projects.
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon fe5017
Homepage: https://github.com/pypingou/pagure
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
Dev instance: http://209.132.184.222/ (/!\\ May change unexpectedly, it's a dev instance ;-))
Pierre-Yves Chibon 87ffe2
"""
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
    parents = []
Pierre-Yves Chibon a71c8d
    commit = None
Pierre-Yves Chibon a71c8d
    try:
Pierre-Yves Chibon a71c8d
        commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon a71c8d
    except KeyError:
Pierre-Yves Chibon a71c8d
        pass
Pierre-Yves Chibon 87ffe2
    if commit:
Pierre-Yves Chibon 87ffe2
        parents = [commit.oid.hex]
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
    # Create a file in that git repo
Pierre-Yves Chibon 128dfb
    with open(os.path.join(newfolder, 'README.rst'), 'w') as stream:
Pierre-Yves Chibon 87ffe2
        stream.write(content)
Pierre-Yves Chibon 87ffe2
    repo.index.add('README.rst')
Pierre-Yves Chibon 87ffe2
    repo.index.write()
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 87ffe2
    # Commits the files added
Pierre-Yves Chibon 87ffe2
    tree = repo.index.write_tree()
Pierre-Yves Chibon 87ffe2
    author = pygit2.Signature(
Pierre-Yves Chibon 87ffe2
        'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 87ffe2
    committer = pygit2.Signature(
Pierre-Yves Chibon 87ffe2
        'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 87ffe2
    repo.create_commit(
Pierre-Yves Chibon 87ffe2
        'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 87ffe2
        author,
Pierre-Yves Chibon 87ffe2
        committer,
Pierre-Yves Chibon 87ffe2
        'Add a README file',
Pierre-Yves Chibon 87ffe2
        # binary string representing the tree object ID
Pierre-Yves Chibon 87ffe2
        tree,
Pierre-Yves Chibon 87ffe2
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 87ffe2
        parents
Pierre-Yves Chibon 87ffe2
    )
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 128dfb
    # Push to origin
Pierre-Yves Chibon 128dfb
    ori_remote = repo.remotes[0]
Pierre-Yves Chibon 128dfb
    master_ref = repo.lookup_reference('HEAD').resolve()
Pierre-Yves Chibon 128dfb
    refname = '%s:%s' % (master_ref.name, master_ref.name)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 27a73d
    PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    shutil.rmtree(newfolder)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 87ffe2
Pierre-Yves Chibon 595b4e
def add_commit_git_repo(folder, ncommits=10):
Pierre-Yves Chibon 595b4e
    """ Create some more commits for the specified git repo. """
Pierre-Yves Chibon 595b4e
    if not os.path.exists(folder):
Pierre-Yves Chibon 595b4e
        os.makedirs(folder)
Pierre-Yves Chibon 128dfb
    brepo = pygit2.init_repository(folder, bare=True)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    newfolder = tempfile.mkdtemp(prefix='pagure-tests')
Pierre-Yves Chibon 128dfb
    repo = pygit2.clone_repository(folder, newfolder)
Pierre-Yves Chibon 595b4e
Pierre-Yves Chibon 595b4e
    for index in range(ncommits):
Pierre-Yves Chibon 595b4e
        # Create a file in that git repo
Pierre-Yves Chibon 128dfb
        with open(os.path.join(newfolder, 'sources'), 'a') as stream:
Pierre-Yves Chibon 595b4e
            stream.write('Row %s\n' % index)
Pierre-Yves Chibon 595b4e
        repo.index.add('sources')
Pierre-Yves Chibon 595b4e
        repo.index.write()
Pierre-Yves Chibon 595b4e
Pierre-Yves Chibon 595b4e
        parents = []
Pierre-Yves Chibon 595b4e
        commit = None
Pierre-Yves Chibon 595b4e
        try:
Pierre-Yves Chibon 595b4e
            commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 595b4e
        except KeyError:
Pierre-Yves Chibon 595b4e
            pass
Pierre-Yves Chibon 595b4e
        if commit:
Pierre-Yves Chibon 595b4e
            parents = [commit.oid.hex]
Pierre-Yves Chibon 595b4e
Pierre-Yves Chibon 595b4e
        # Commits the files added
Pierre-Yves Chibon 595b4e
        tree = repo.index.write_tree()
Pierre-Yves Chibon 595b4e
        author = pygit2.Signature(
Pierre-Yves Chibon 595b4e
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 595b4e
        committer = pygit2.Signature(
Pierre-Yves Chibon 595b4e
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 595b4e
        repo.create_commit(
Pierre-Yves Chibon 595b4e
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 595b4e
            author,
Pierre-Yves Chibon 595b4e
            committer,
Pierre-Yves Chibon 595b4e
            'Add row %s to sources file' % index,
Pierre-Yves Chibon 595b4e
            # binary string representing the tree object ID
Pierre-Yves Chibon 595b4e
            tree,
Pierre-Yves Chibon 595b4e
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 595b4e
            parents,
Pierre-Yves Chibon 595b4e
        )
Pierre-Yves Chibon 595b4e
Pierre-Yves Chibon 128dfb
    # Push to origin
Pierre-Yves Chibon 128dfb
    ori_remote = repo.remotes[0]
Pierre-Yves Chibon 128dfb
    master_ref = repo.lookup_reference('HEAD').resolve()
Pierre-Yves Chibon 128dfb
    refname = '%s:%s' % (master_ref.name, master_ref.name)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 27a73d
    PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    shutil.rmtree(newfolder)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 595b4e
Pierre-Yves Chibon 6e857b
def add_binary_git_repo(folder, filename):
Pierre-Yves Chibon 6e857b
    """ Create a fake image file for the specified git repo. """
Pierre-Yves Chibon 6e857b
    if not os.path.exists(folder):
Pierre-Yves Chibon 6e857b
        os.makedirs(folder)
Pierre-Yves Chibon 128dfb
    brepo = pygit2.init_repository(folder, bare=True)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    newfolder = tempfile.mkdtemp(prefix='pagure-tests')
Pierre-Yves Chibon 128dfb
    repo = pygit2.clone_repository(folder, newfolder)
Pierre-Yves Chibon 6e857b
Pierre-Yves Chibon 6e857b
    content = """<89>PNG^M
Pierre-Yves Chibon 6e857b
^Z
Pierre-Yves Chibon 6e857b
^@^@^@^MIHDR^@^@^@K^@^@^@K^H^F^@^@^@8Nzê^@^@^@^FbKGD^@ÿ^@ÿ^@ÿ ½§<93>^@^@^@  pHYs^@^@^M×^@^@^M×^AB(<9b>x^@^@^@^GtIM
Pierre-Yves Chibon 6e857b
E^GÞ
Pierre-Yves Chibon 6e857b
^N^U^F^[<88>]·<9c>^@^@  <8a>IDATxÚí<9c>ÛO^Tg^_Ç?3³»ì^B
Pierre-Yves Chibon 6e857b
<8b>®ËË<8b>X^NÕõ^EQÚ^Z­^Qc<82>^Pk5Úô¦iMÄ^[{×^K<9b>&^^Xÿ^A<8d>WM^S^SmÒ<8b>j¯Zê<8d>   6^QO^Dª¶´/Ö^M^T5^^*¼¬<9c>^Oî<8
Pierre-Yves Chibon 6e857b
1><99>÷<82>Y<8b>03;3»<83>hù&d óìÃÌw~§çûüf`^Q<8b>XÄ"^V±<88>^?:<84>^Er^N^R ª¿^K3ÎK<99>ñ3^EÈêïÿ8²ò<81> <90>¥C^T^Z<84>
Pierre-Yves Chibon 6e857b
É@^Tè^E<86>_g²²<80>^\<95>$^?<86>æ^\TI^[SI|åÉ^R<81>Õ*QNb^\èVÝõ<95>#Ë^M^T^C^Eóì-<83>ÀC þ*<90>%^B+<80>^?¿äÄñ^XèϤ¥e<9
Pierre-Yves Chibon 6e857b
a>,^O°^Vp-<90>l<9f>^@Â<99><8a>gR^FOÌ^O<84>TËZ(HZù3õ'íÉ2<81>^R Ìé+oll¤½½<9d>þþ~^TEAQ^T"<91>^HW¯^åèÑ£¸\º^F]^F¬|Ùn(^@
Pierre-Yves Chibon 6e857b
å@<9e>S^DíÚµ<8b>cÇ<8e>±iÓ¦<94>cãñ8Ç<8f>^_§©©<89>^[7nh^M^Y^Fþ|YdU8ET0^X¤©©<89>Í<9b>7[þî^W_|ÁÄÄ^DçÏ<9f>çÑ£G^Y#,<9d><
Pierre-Yves Chibon 6e857b
98>µ^RXæ^DQõõõ´¶¶RVfϳÇÇÇyøð!<95><95><95>dggsïÞ½<99><87>½j^B^Z<99>¯<98>åW^CgƱsçN<9a><9b><9b>ÉÎζ=G<þw<89>µaÃ^F^Z^
Pierre-Yves Chibon 6e857b
Z^Zf^OYag^UaDz<jÖË86nÜÈåË<97>§ã<83>`?B<9c>9sæï<85>¥¢^P^L^Fµ,Ì^O^LX©Ã$^[<96>XéTyðË/¿<90><9b><9b>kûûCCC<9c>:u<8a>ÁÁÁ</jÖË86nÜÈåË<97>
Pierre-Yves Chibon 6e857b
^WÈN^RöøñcFF^ð¾^B bVÉ°Z<^F<9c>*8¿ùæ^[<82>Á á<98>X,FKK^K'O<9e>äâÅ<8b>ȲLAA^A[·n¥¸¸^XA^Pp»ÝºV¹wï^¾üòËÙ×^_PU<8c><8c>f
Pierre-Yves Chibon 6e857b
C7Pí^DQeee<84>ÃaÜn·î<98><9e><9e>^^¶oß®<95>ݦM^^T©®®¦®®<8e>©©)Ý1ׯ_§½½}ö¡ßͬ%­¸S±SµÔ<9e>={^L<89>úé§<9f>¨¨¨Ð%
Pierre-Yves Chibon 6e857b
"""
Pierre-Yves Chibon 6e857b
Pierre-Yves Chibon 6e857b
    parents = []
Pierre-Yves Chibon 6e857b
    commit = None
Pierre-Yves Chibon 6e857b
    try:
Pierre-Yves Chibon 6e857b
        commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 6e857b
    except KeyError:
Pierre-Yves Chibon 6e857b
        pass
Pierre-Yves Chibon 6e857b
    if commit:
Pierre-Yves Chibon 6e857b
        parents = [commit.oid.hex]
Pierre-Yves Chibon 6e857b
Pierre-Yves Chibon 6e857b
    # Create a file in that git repo
Pierre-Yves Chibon 128dfb
    with open(os.path.join(newfolder, filename), 'w') as stream:
Pierre-Yves Chibon 6e857b
        stream.write(content)
Pierre-Yves Chibon 6e857b
    repo.index.add(filename)
Pierre-Yves Chibon 6e857b
    repo.index.write()
Pierre-Yves Chibon 6e857b
Pierre-Yves Chibon 6e857b
    # Commits the files added
Pierre-Yves Chibon 6e857b
    tree = repo.index.write_tree()
Pierre-Yves Chibon 6e857b
    author = pygit2.Signature(
Pierre-Yves Chibon 6e857b
        'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 6e857b
    committer = pygit2.Signature(
Pierre-Yves Chibon 6e857b
        'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 6e857b
    repo.create_commit(
Pierre-Yves Chibon 6e857b
        'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 6e857b
        author,
Pierre-Yves Chibon 6e857b
        committer,
Pierre-Yves Chibon 6e857b
        'Add a fake image file',
Pierre-Yves Chibon 6e857b
        # binary string representing the tree object ID
Pierre-Yves Chibon 6e857b
        tree,
Pierre-Yves Chibon 6e857b
        # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 6e857b
        parents
Pierre-Yves Chibon 6e857b
    )
Pierre-Yves Chibon 6e857b
Pierre-Yves Chibon 128dfb
    # Push to origin
Pierre-Yves Chibon 128dfb
    ori_remote = repo.remotes[0]
Pierre-Yves Chibon 128dfb
    master_ref = repo.lookup_reference('HEAD').resolve()
Pierre-Yves Chibon 128dfb
    refname = '%s:%s' % (master_ref.name, master_ref.name)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 27a73d
    PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 128dfb
    shutil.rmtree(newfolder)
Pierre-Yves Chibon 128dfb
Pierre-Yves Chibon 652aff
Pierre-Yves Chibon 413073
if __name__ == '__main__':
Pierre-Yves Chibon 413073
    SUITE = unittest.TestLoader().loadTestsFromTestCase(Modeltests)
Pierre-Yves Chibon 413073
    unittest.TextTestRunner(verbosity=2).run(SUITE)