# Path: extrapypi/app.py
# def create_app(testing=False, config=None):
#     """Main application factory
#     """
# 
#     # the following line ensure that we search templates
#     # based on the app file, not working_directory
#     tmpl_path = os.path.join(
#         os.path.dirname(os.path.abspath(__file__)),
#         'templates'
#     )
#     static_path = os.path.join(
#         os.path.dirname(os.path.abspath(__file__)),
#         'static'
#     )
# 
#     app = Flask(
#         'extra-pypi',
#         template_folder=tmpl_path,
#         static_folder=static_path
#     )
#     configure_app(app, testing, config)
#     configure_logging(app)
#     configure_extensions(app)
#     register_blueprints(app)
#     register_filters(app)
# 
#     return app
# 
# Path: extrapypi/extensions.py

# 
# Path: extrapypi/models/package.py
# class Package(db.Model):
#     """Represent a simple package
#     """
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(255, convert_unicode=True), nullable=False, unique=True)
#     summary = db.Column(db.String(255, convert_unicode=True))
#     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#     updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
# 
#     maintainers = db.relationship('User', secondary=package_maintainers, lazy='dynamic', backref='packages')
# 
#     @property
#     def sorted_releases(self):
#         releases = self.releases.all()
#         return sorted(
#             releases,
#             key=lambda r: parse_version(r.version),
#             reverse=True
#         )
# 
#     @property
#     def latest_release(self):
#         return next(iter(self.sorted_releases), None)
# 
#     def __repr__(self):
#         return "<Package {0.name}>".format(self)
# 
# Path: extrapypi/models/release.py
# class Release(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     description = db.Column(UnicodeText, nullable=False)
#     download_url = db.Column(db.String(2048, convert_unicode=True))
#     home_page = db.Column(db.String(2048, convert_unicode=True))
#     version = db.Column(db.String(80, convert_unicode=True))
#     keywords = db.Column(db.String(255, convert_unicode=True))
#     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#     updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
# 
#     package_id = db.Column(
#         db.Integer,
#         db.ForeignKey('package.id', name='_fk_release_package'), nullable=False
#     )
# 
#     package = db.relationship(
#         'Package',
#         backref=db.backref(
#             'releases',
#             lazy='dynamic',
#             cascade='all, delete-orphan'
#         ),
#         lazy='joined',
#     )
# 
#     def __repr__(self):
#         return "<Release {0.version} for package {0.package.name}>".format(self)
# 
# Path: extrapypi/models/user.py
# class User(db.Model):
#     ROLES = [
#         'admin',
#         'developer',
#         'installer',
#         'maintainer'
#     ]
# 
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(255), nullable=False, unique=True)
#     email = db.Column(db.String(255), nullable=False, unique=True)
#     password_hash = db.Column(db.String(255), nullable=False)
#     is_active = db.Column(db.Boolean, default=True)
#     role = db.Column(db.String(80), nullable=True)
# 
#     @validates('role')
#     def validate_role(self, key, role):
#         assert role in self.ROLES or role is None
#         return role
# 
#     @property
#     def is_authenticated(self):
#         return True
# 
#     @property
#     def is_anonymous(self):
#         return False
# 
#     @property
#     def is_admin(self):
#         return self.role == 'admin'
# 
#     def get_id(self):
#         try:
#             return unicode(self.id)
#         except NameError:
#             return str(self.id)
# 
#     def __repr__(self):
#         return "<User {0.username}>".format(self)


import os
import shutil
import pytest
from base64 import b64encode
from passlib.apps import custom_app_context
from werkzeug.datastructures import FileStorage
from extrapypi.app import create_app
from extrapypi.extensions import db as _db
from extrapypi.models import Package, User, Release

@pytest.fixture(scope='session')
def app(tmpdir_factory):
    pkgs = tmpdir_factory.mktemp("packages")
    app = create_app(testing=True)
    app.config['STORAGE'] = 'LocalStorage'
    app.config['STORAGE_PARAMS'] = {
        'packages_root': str(pkgs)
    }
    return app


@pytest.fixture(scope='session')
def db(app, request):

    _db.app = app
    _db.create_all()

    def fin():
        _db.drop_all()

    request.addfinalizer(fin)
    return _db


@pytest.fixture
def admin_user(db):
    user = User.query.filter_by(username='admin').first()

    if user is None:

# Path: extrapypi/app.py
# def create_app(testing=False, config=None):
#     """Main application factory
#     """
# 
#     # the following line ensure that we search templates
#     # based on the app file, not working_directory
#     tmpl_path = os.path.join(
#         os.path.dirname(os.path.abspath(__file__)),
#         'templates'
#     )
#     static_path = os.path.join(
#         os.path.dirname(os.path.abspath(__file__)),
#         'static'
#     )
# 
#     app = Flask(
#         'extra-pypi',
#         template_folder=tmpl_path,
#         static_folder=static_path
#     )
#     configure_app(app, testing, config)
#     configure_logging(app)
#     configure_extensions(app)
#     register_blueprints(app)
#     register_filters(app)
# 
#     return app
# 
# Path: extrapypi/extensions.py

# 
# Path: extrapypi/models/package.py
# class Package(db.Model):
#     """Represent a simple package
#     """
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(255, convert_unicode=True), nullable=False, unique=True)
#     summary = db.Column(db.String(255, convert_unicode=True))
#     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#     updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
# 
#     maintainers = db.relationship('User', secondary=package_maintainers, lazy='dynamic', backref='packages')
# 
#     @property
#     def sorted_releases(self):
#         releases = self.releases.all()
#         return sorted(
#             releases,
#             key=lambda r: parse_version(r.version),
#             reverse=True
#         )
# 
#     @property
#     def latest_release(self):
#         return next(iter(self.sorted_releases), None)
# 
#     def __repr__(self):
#         return "<Package {0.name}>".format(self)
# 
# Path: extrapypi/models/release.py
# class Release(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     description = db.Column(UnicodeText, nullable=False)
#     download_url = db.Column(db.String(2048, convert_unicode=True))
#     home_page = db.Column(db.String(2048, convert_unicode=True))
#     version = db.Column(db.String(80, convert_unicode=True))
#     keywords = db.Column(db.String(255, convert_unicode=True))
#     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#     updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
# 
#     package_id = db.Column(
#         db.Integer,
#         db.ForeignKey('package.id', name='_fk_release_package'), nullable=False
#     )
# 
#     package = db.relationship(
#         'Package',
#         backref=db.backref(
#             'releases',
#             lazy='dynamic',
#             cascade='all, delete-orphan'
#         ),
#         lazy='joined',
#     )
# 
#     def __repr__(self):
#         return "<Release {0.version} for package {0.package.name}>".format(self)
# 
# Path: extrapypi/models/user.py
# class User(db.Model):
#     ROLES = [
#         'admin',
#         'developer',
#         'installer',
#         'maintainer'
#     ]
# 
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(255), nullable=False, unique=True)
#     email = db.Column(db.String(255), nullable=False, unique=True)
#     password_hash = db.Column(db.String(255), nullable=False)
#     is_active = db.Column(db.Boolean, default=True)
#     role = db.Column(db.String(80), nullable=True)
# 
#     @validates('role')
#     def validate_role(self, key, role):
#         assert role in self.ROLES or role is None
#         return role
# 
#     @property
#     def is_authenticated(self):
#         return True
# 
#     @property
#     def is_anonymous(self):
#         return False
# 
#     @property
#     def is_admin(self):
#         return self.role == 'admin'
# 
#     def get_id(self):
#         try:
#             return unicode(self.id)
#         except NameError:
#             return str(self.id)
# 
#     def __repr__(self):
#         return "<User {0.username}>".format(self)


# Path: tests/conftest.py
import os
import shutil
import pytest
from base64 import b64encode
from passlib.apps import custom_app_context
from werkzeug.datastructures import FileStorage
from extrapypi.app import create_app
from extrapypi.extensions import db as _db
from extrapypi.models import Package, User, Release


@pytest.fixture(scope='session')
def app(tmpdir_factory):
    pkgs = tmpdir_factory.mktemp("packages")
    app = create_app(testing=True)
    app.config['STORAGE'] = 'LocalStorage'
    app.config['STORAGE_PARAMS'] = {
        'packages_root': str(pkgs)
    }
    return app


@pytest.fixture(scope='session')
def db(app, request):

    _db.app = app
    _db.create_all()

    def fin():
        _db.drop_all()

    request.addfinalizer(fin)
    return _db


@pytest.fixture
def admin_user(db):
    user = User.query.filter_by(username='admin').first()

    if user is None:

        user = User(