import datetime

import pytest
import pytz
from mock import Mock

from config import settings
from lyfeshoppe.app import create_app
from lyfeshoppe.lib.util_datetime import timedelta_months
from lyfeshoppe.extensions import db as _db
from lyfeshoppe.blueprints.user.models import User
from lyfeshoppe.blueprints.issue.models import Issue


# App and database fixtures ---------------------------------------------------
@pytest.yield_fixture(scope='session')
def app():
    """
    Setup our flask test app, this only gets executed once.

    :return: Flask app
    """
    db_uri = '{0}_test'.format(settings.SQLALCHEMY_DATABASE_URI)
    params = {
        'DEBUG': False,
        'TESTING': True,
        'WTF_CSRF_ENABLED': False,
        'SQLALCHEMY_DATABASE_URI': db_uri
    }

    _app = create_app(settings_override=params)

    # Establish an application context before running the tests.
    ctx = _app.app_context()
    ctx.push()

    yield _app

    ctx.pop()


@pytest.yield_fixture(scope='function')
def client(app):
    """
    Setup an app client, this gets executed for each test function.

    :param app: Pytest fixture
    :return: Flask app client
    """
    yield app.test_client()


@pytest.fixture(scope='session')
def db(app):
    """
    Setup our database, this only gets executed once per session.

    :param app: Pytest fixture
    :return: SQLAlchemy database session
    """
    _db.drop_all()
    _db.create_all()

    # Create a single user because a lot of tests do not mutate this user.
    # It will result in quite a bit faster tests.
    params = {
        'role': 'admin',
        'email': 'admin@localhost.com',
        'name': 'Dev',
        'password': 'password'
    }

    admin = User(**params)

    _db.session.add(admin)
    _db.session.commit()

    return _db


@pytest.yield_fixture(scope='function')
def session(db):
    """
    Allow very fast tests by using rollbacks and nested sessions. This does
    require that your database supports SQL savepoints, and postgres does.

    Read more about this at:
    http://stackoverflow.com/a/26624146

    :param db: Pytest fixture
    :return: None
    """
    db.session.begin_nested()

    yield db.session

    db.session.rollback()


# Model fixtures --------------------------------------------------------------
@pytest.fixture(scope='session')
def token(db):
    """
    Serialize a JWS token.

    :param db: Pytest fixture
    :return: JWS token
    """
    user = User.find_by_identity('admin@localhost.com')
    return user.serialize_token()


@pytest.fixture(scope='function')
def users(db):
    """
    Create user fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(User).delete()

    users = [
        {
            'role': 'admin',
            'email': 'admin@localhost.com',
            'password': 'password'
        },
        {
            'active': False,
            'email': 'disabled@localhost.com',
            'password': 'password'
        }
    ]

    for user in users:
        db.session.add(User(**user))

    db.session.commit()

    return db


@pytest.fixture(scope='function')
def issues(db):
    """
    Create issue fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Issue).delete()

    issues = [
        {
            'label': 'login',
            'email': 'admin@localhost.com',
            'question': '42.',
            'status': 'unread'
        },
        {
            'label': 'login',
            'email': 'admin@localhost.com',
            'question': 'Hello.',
            'status': 'unread'
        }
    ]

    for issue in issues:
        db.session.add(Issue(**issue))

    db.session.commit()

    return db

aabf7abdae1459a3ef1baf08571897834b2d2171
<mask token>


@pytest.yield_fixture(scope='function')
def session(db):
    """
    Allow very fast tests by using rollbacks and nested sessions. This does
    require that your database supports SQL savepoints, and postgres does.

    Read more about this at:
    http://stackoverflow.com/a/26624146

    :param db: Pytest fixture
    :return: None
    """
    db.session.begin_nested()
    yield db.session
    db.session.rollback()


<mask token>


@pytest.fixture(scope='function')
def issues(db):
    """
    Create issue fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Issue).delete()
    issues = [{'label': 'login', 'email': 'admin@localhost.com', 'question':
        '42.', 'status': 'unread'}, {'label': 'login', 'email':
        'admin@localhost.com', 'question': 'Hello.', 'status': 'unread'}]
    for issue in issues:
        db.session.add(Issue(**issue))
    db.session.commit()
    return db

<mask token>


@pytest.yield_fixture(scope='session')
def app():
    """
    Setup our flask test app, this only gets executed once.

    :return: Flask app
    """
    db_uri = '{0}_test'.format(settings.SQLALCHEMY_DATABASE_URI)
    params = {'DEBUG': False, 'TESTING': True, 'WTF_CSRF_ENABLED': False,
        'SQLALCHEMY_DATABASE_URI': db_uri}
    _app = create_app(settings_override=params)
    ctx = _app.app_context()
    ctx.push()
    yield _app
    ctx.pop()


@pytest.yield_fixture(scope='function')
def client(app):
    """
    Setup an app client, this gets executed for each test function.

    :param app: Pytest fixture
    :return: Flask app client
    """
    yield app.test_client()


<mask token>


@pytest.yield_fixture(scope='function')
def session(db):
    """
    Allow very fast tests by using rollbacks and nested sessions. This does
    require that your database supports SQL savepoints, and postgres does.

    Read more about this at:
    http://stackoverflow.com/a/26624146

    :param db: Pytest fixture
    :return: None
    """
    db.session.begin_nested()
    yield db.session
    db.session.rollback()


@pytest.fixture(scope='session')
def token(db):
    """
    Serialize a JWS token.

    :param db: Pytest fixture
    :return: JWS token
    """
    user = User.find_by_identity('admin@localhost.com')
    return user.serialize_token()


@pytest.fixture(scope='function')
def users(db):
    """
    Create user fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(User).delete()
    users = [{'role': 'admin', 'email': 'admin@localhost.com', 'password':
        'password'}, {'active': False, 'email': 'disabled@localhost.com',
        'password': 'password'}]
    for user in users:
        db.session.add(User(**user))
    db.session.commit()
    return db


@pytest.fixture(scope='function')
def issues(db):
    """
    Create issue fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Issue).delete()
    issues = [{'label': 'login', 'email': 'admin@localhost.com', 'question':
        '42.', 'status': 'unread'}, {'label': 'login', 'email':
        'admin@localhost.com', 'question': 'Hello.', 'status': 'unread'}]
    for issue in issues:
        db.session.add(Issue(**issue))
    db.session.commit()
    return db

<mask token>


@pytest.yield_fixture(scope='session')
def app():
    """
    Setup our flask test app, this only gets executed once.

    :return: Flask app
    """
    db_uri = '{0}_test'.format(settings.SQLALCHEMY_DATABASE_URI)
    params = {'DEBUG': False, 'TESTING': True, 'WTF_CSRF_ENABLED': False,
        'SQLALCHEMY_DATABASE_URI': db_uri}
    _app = create_app(settings_override=params)
    ctx = _app.app_context()
    ctx.push()
    yield _app
    ctx.pop()


@pytest.yield_fixture(scope='function')
def client(app):
    """
    Setup an app client, this gets executed for each test function.

    :param app: Pytest fixture
    :return: Flask app client
    """
    yield app.test_client()


@pytest.fixture(scope='session')
def db(app):
    """
    Setup our database, this only gets executed once per session.

    :param app: Pytest fixture
    :return: SQLAlchemy database session
    """
    _db.drop_all()
    _db.create_all()
    params = {'role': 'admin', 'email': 'admin@localhost.com', 'name':
        'Dev', 'password': 'password'}
    admin = User(**params)
    _db.session.add(admin)
    _db.session.commit()
    return _db


@pytest.yield_fixture(scope='function')
def session(db):
    """
    Allow very fast tests by using rollbacks and nested sessions. This does
    require that your database supports SQL savepoints, and postgres does.

    Read more about this at:
    http://stackoverflow.com/a/26624146

    :param db: Pytest fixture
    :return: None
    """
    db.session.begin_nested()
    yield db.session
    db.session.rollback()


@pytest.fixture(scope='session')
def token(db):
    """
    Serialize a JWS token.

    :param db: Pytest fixture
    :return: JWS token
    """
    user = User.find_by_identity('admin@localhost.com')
    return user.serialize_token()


@pytest.fixture(scope='function')
def users(db):
    """
    Create user fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(User).delete()
    users = [{'role': 'admin', 'email': 'admin@localhost.com', 'password':
        'password'}, {'active': False, 'email': 'disabled@localhost.com',
        'password': 'password'}]
    for user in users:
        db.session.add(User(**user))
    db.session.commit()
    return db


@pytest.fixture(scope='function')
def issues(db):
    """
    Create issue fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Issue).delete()
    issues = [{'label': 'login', 'email': 'admin@localhost.com', 'question':
        '42.', 'status': 'unread'}, {'label': 'login', 'email':
        'admin@localhost.com', 'question': 'Hello.', 'status': 'unread'}]
    for issue in issues:
        db.session.add(Issue(**issue))
    db.session.commit()
    return db

import datetime
import pytest
import pytz
from mock import Mock
from config import settings
from lyfeshoppe.app import create_app
from lyfeshoppe.lib.util_datetime import timedelta_months
from lyfeshoppe.extensions import db as _db
from lyfeshoppe.blueprints.user.models import User
from lyfeshoppe.blueprints.issue.models import Issue


@pytest.yield_fixture(scope='session')
def app():
    """
    Setup our flask test app, this only gets executed once.

    :return: Flask app
    """
    db_uri = '{0}_test'.format(settings.SQLALCHEMY_DATABASE_URI)
    params = {'DEBUG': False, 'TESTING': True, 'WTF_CSRF_ENABLED': False,
        'SQLALCHEMY_DATABASE_URI': db_uri}
    _app = create_app(settings_override=params)
    ctx = _app.app_context()
    ctx.push()
    yield _app
    ctx.pop()


@pytest.yield_fixture(scope='function')
def client(app):
    """
    Setup an app client, this gets executed for each test function.

    :param app: Pytest fixture
    :return: Flask app client
    """
    yield app.test_client()


@pytest.fixture(scope='session')
def db(app):
    """
    Setup our database, this only gets executed once per session.

    :param app: Pytest fixture
    :return: SQLAlchemy database session
    """
    _db.drop_all()
    _db.create_all()
    params = {'role': 'admin', 'email': 'admin@localhost.com', 'name':
        'Dev', 'password': 'password'}
    admin = User(**params)
    _db.session.add(admin)
    _db.session.commit()
    return _db


@pytest.yield_fixture(scope='function')
def session(db):
    """
    Allow very fast tests by using rollbacks and nested sessions. This does
    require that your database supports SQL savepoints, and postgres does.

    Read more about this at:
    http://stackoverflow.com/a/26624146

    :param db: Pytest fixture
    :return: None
    """
    db.session.begin_nested()
    yield db.session
    db.session.rollback()


@pytest.fixture(scope='session')
def token(db):
    """
    Serialize a JWS token.

    :param db: Pytest fixture
    :return: JWS token
    """
    user = User.find_by_identity('admin@localhost.com')
    return user.serialize_token()


@pytest.fixture(scope='function')
def users(db):
    """
    Create user fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(User).delete()
    users = [{'role': 'admin', 'email': 'admin@localhost.com', 'password':
        'password'}, {'active': False, 'email': 'disabled@localhost.com',
        'password': 'password'}]
    for user in users:
        db.session.add(User(**user))
    db.session.commit()
    return db


@pytest.fixture(scope='function')
def issues(db):
    """
    Create issue fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Issue).delete()
    issues = [{'label': 'login', 'email': 'admin@localhost.com', 'question':
        '42.', 'status': 'unread'}, {'label': 'login', 'email':
        'admin@localhost.com', 'question': 'Hello.', 'status': 'unread'}]
    for issue in issues:
        db.session.add(Issue(**issue))
    db.session.commit()
    return db

import datetime

import pytest
import pytz
from mock import Mock

from config import settings
from lyfeshoppe.app import create_app
from lyfeshoppe.lib.util_datetime import timedelta_months
from lyfeshoppe.extensions import db as _db
from lyfeshoppe.blueprints.user.models import User
from lyfeshoppe.blueprints.issue.models import Issue


# App and database fixtures ---------------------------------------------------
@pytest.yield_fixture(scope='session')
def app():
    """
    Setup our flask test app, this only gets executed once.

    :return: Flask app
    """
    db_uri = '{0}_test'.format(settings.SQLALCHEMY_DATABASE_URI)
    params = {
        'DEBUG': False,
        'TESTING': True,
        'WTF_CSRF_ENABLED': False,
        'SQLALCHEMY_DATABASE_URI': db_uri
    }

    _app = create_app(settings_override=params)

    # Establish an application context before running the tests.
    ctx = _app.app_context()
    ctx.push()

    yield _app

    ctx.pop()


@pytest.yield_fixture(scope='function')
def client(app):
    """
    Setup an app client, this gets executed for each test function.

    :param app: Pytest fixture
    :return: Flask app client
    """
    yield app.test_client()


@pytest.fixture(scope='session')
def db(app):
    """
    Setup our database, this only gets executed once per session.

    :param app: Pytest fixture
    :return: SQLAlchemy database session
    """
    _db.drop_all()
    _db.create_all()

    # Create a single user because a lot of tests do not mutate this user.
    # It will result in quite a bit faster tests.
    params = {
        'role': 'admin',
        'email': 'admin@localhost.com',
        'name': 'Dev',
        'password': 'password'
    }

    admin = User(**params)

    _db.session.add(admin)
    _db.session.commit()

    return _db


@pytest.yield_fixture(scope='function')
def session(db):
    """
    Allow very fast tests by using rollbacks and nested sessions. This does
    require that your database supports SQL savepoints, and postgres does.

    Read more about this at:
    http://stackoverflow.com/a/26624146

    :param db: Pytest fixture
    :return: None
    """
    db.session.begin_nested()

    yield db.session

    db.session.rollback()


# Model fixtures --------------------------------------------------------------
@pytest.fixture(scope='session')
def token(db):
    """
    Serialize a JWS token.

    :param db: Pytest fixture
    :return: JWS token
    """
    user = User.find_by_identity('admin@localhost.com')
    return user.serialize_token()


@pytest.fixture(scope='function')
def users(db):
    """
    Create user fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(User).delete()

    users = [
        {
            'role': 'admin',
            'email': 'admin@localhost.com',
            'password': 'password'
        },
        {
            'active': False,
            'email': 'disabled@localhost.com',
            'password': 'password'
        }
    ]

    for user in users:
        db.session.add(User(**user))

    db.session.commit()

    return db


@pytest.fixture(scope='function')
def issues(db):
    """
    Create issue fixtures. They reset per test.

    :param db: Pytest fixture
    :return: SQLAlchemy database session
    """
    db.session.query(Issue).delete()

    issues = [
        {
            'label': 'login',
            'email': 'admin@localhost.com',
            'question': '42.',
            'status': 'unread'
        },
        {
            'label': 'login',
            'email': 'admin@localhost.com',
            'question': 'Hello.',
            'status': 'unread'
        }
    ]

    for issue in issues:
        db.session.add(Issue(**issue))

    db.session.commit()

    return db
