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 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 413073
import progit.lib
Pierre-Yves Chibon 413073
import progit.lib.model
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
DB_PATH = 'sqlite:///:memory:'
Pierre-Yves Chibon 413073
FAITOUT_URL = 'http://209.132.184.152/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 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 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 c27b0d
        self.path = tempfile.mkdtemp(prefix='progit-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 58e731
        for folder in ['tickets', 'repos', 'forks', 'docs']:
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 413073
        self.session = progit.lib.model.create_tables(DB_PATH)
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
        # Create a couple of users
Pierre-Yves Chibon 413073
        item = progit.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 413073
        )
Pierre-Yves Chibon 413073
        self.session.add(item)
Pierre-Yves Chibon 0daa1b
        item = progit.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 0daa1b
        item = progit.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 413073
        item = progit.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 413073
        )
Pierre-Yves Chibon 413073
        self.session.add(item)
Pierre-Yves Chibon 0daa1b
        item = progit.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 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 7cbc4e
        if sys.exc_info() == (None, None, None):
Pierre-Yves Chibon 7cbc4e
            shutil.rmtree(self.path)
Pierre-Yves Chibon 7cbc4e
        else:
Pierre-Yves Chibon 7cbc4e
            print('FAILED TESTS AT %s' % 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 413073
                progit.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 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 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 413073
    item = progit.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 413073
    )
Pierre-Yves Chibon 413073
    session.add(item)
Pierre-Yves Chibon 413073
Pierre-Yves Chibon 413073
    item = progit.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 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 4e9b76
def create_projects_git(folder):
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 4e9b76
        os.makedirs(repo_path)
Pierre-Yves Chibon 4e9b76
        pygit2.init_repository(repo_path)
Pierre-Yves Chibon 4e9b76
Pierre-Yves Chibon 4e9b76
    return repos
Pierre-Yves Chibon 4e9b76
Pierre-Yves Chibon 4e9b76
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)