# Path: template_maker_test/unit/test_base.py
# class BaseTestCase(TestCase):
#     '''
#     A base test case that boots our app
#     '''
#     def create_app(self):
#         return _create_app(TestingConfig)
# 
#     def setUp(self):
#         db.create_all()
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
#         db.get_engine(self.app).dispose()
# 
#     def assert_flashes(self, expected_message, expected_category='message'):
#         '''
#         Helper to test if we have flashes.
#         Taken from: http://blog.paulopoiati.com/2013/02/22/testing-flash-messages-in-flask/
#         '''
#         with self.client.session_transaction() as session:
#             try:
#                 category, message = session['_flashes'][0]
#             except KeyError:
#                 raise AssertionError('nothing flashed')
#             assert expected_message in message
#             assert expected_category == category
# 
# Path: template_maker_test/unit/util.py
# def insert_a_user(is_admin=False, email='foo@foo.com'):
#     user = create_a_user(is_admin, email)
#     db.session.add(user)
#     db.session.commit()
#     return user.id
# 
# Path: template_maker/users/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     email = Column(db.String(80), unique=True, nullable=False)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, email, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({email!r})>'.format(email=self.email)
# 
# Path: template_maker/app.py
# def create_app(config_object=DevConfig):
# def register_blueprints(app):
# def register_extensions(app):


from mock import Mock, patch
from flask.ext.login import current_user, login_user
from template_maker_test.unit.test_base import BaseTestCase
from template_maker_test.unit.util import insert_a_user
from template_maker.users.models import User
from template_maker.app import db
        urlopen.return_value = mock_open

        post = self.client.post('/users/auth', data=dict(
            assertion='test'
        ))

        self.assert403(post)

    @patch('urllib2.urlopen')
    def test_auth_success(self, urlopen):
        mock_open = Mock()
        mock_open.read.side_effect = ['{"status": "okay", "email": "' + self.email + '"}']
        urlopen.return_value = mock_open

        post = self.client.post('/users/auth?next=/build/8/sections/', data=dict(
            assertion='test'
        ))

        self.assert200(post)
        self.assertEquals(post.data, '/build/8/sections/')

    @patch('urllib2.urlopen')
    def test_logout(self, urlopen):

        login_user(User.query.all()[0])

        logout = self.client.get('/users/logout', follow_redirects=True)
        self.assert_flashes('Logged out successfully!', 'alert-success')
        self.assert_template_used('users/logout.html')


# Path: template_maker_test/unit/test_base.py
# class BaseTestCase(TestCase):
#     '''
#     A base test case that boots our app
#     '''
#     def create_app(self):
#         return _create_app(TestingConfig)
# 
#     def setUp(self):
#         db.create_all()
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
#         db.get_engine(self.app).dispose()
# 
#     def assert_flashes(self, expected_message, expected_category='message'):
#         '''
#         Helper to test if we have flashes.
#         Taken from: http://blog.paulopoiati.com/2013/02/22/testing-flash-messages-in-flask/
#         '''
#         with self.client.session_transaction() as session:
#             try:
#                 category, message = session['_flashes'][0]
#             except KeyError:
#                 raise AssertionError('nothing flashed')
#             assert expected_message in message
#             assert expected_category == category
# 
# Path: template_maker_test/unit/util.py
# def insert_a_user(is_admin=False, email='foo@foo.com'):
#     user = create_a_user(is_admin, email)
#     db.session.add(user)
#     db.session.commit()
#     return user.id
# 
# Path: template_maker/users/models.py
# class User(UserMixin, SurrogatePK, Model):
# 
#     __tablename__ = 'users'
#     email = Column(db.String(80), unique=True, nullable=False)
#     created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
#     first_name = Column(db.String(30), nullable=True)
#     last_name = Column(db.String(30), nullable=True)
#     active = Column(db.Boolean(), default=False)
#     is_admin = Column(db.Boolean(), default=False)
# 
#     def __init__(self, email, **kwargs):
#         db.Model.__init__(self, email=email, **kwargs)
# 
#     @property
#     def full_name(self):
#         return "{0} {1}".format(self.first_name, self.last_name)
# 
#     def __repr__(self):
#         return '<User({email!r})>'.format(email=self.email)
# 
# Path: template_maker/app.py
# def create_app(config_object=DevConfig):
# def register_blueprints(app):
# def register_extensions(app):


# Path: template_maker_test/unit/users/test_users.py
from mock import Mock, patch
from flask.ext.login import current_user, login_user
from template_maker_test.unit.test_base import BaseTestCase
from template_maker_test.unit.util import insert_a_user
from template_maker.users.models import User
from template_maker.app import db

        urlopen.return_value = mock_open

        post = self.client.post('/users/auth', data=dict(
            assertion='test'
        ))

        self.assert403(post)

    @patch('urllib2.urlopen')
    def test_auth_success(self, urlopen):
        mock_open = Mock()
        mock_open.read.side_effect = ['{"status": "okay", "email": "' + self.email + '"}']
        urlopen.return_value = mock_open

        post = self.client.post('/users/auth?next=/build/8/sections/', data=dict(
            assertion='test'
        ))

        self.assert200(post)
        self.assertEquals(post.data, '/build/8/sections/')

    @patch('urllib2.urlopen')
    def test_logout(self, urlopen):

        login_user(User.query.all()[0])

        logout = self.client.get('/users/logout', follow_redirects=True)
        self.assert_flashes('Logged out successfully!', 'alert-success')
        self.assert_template_used('users/logout.html')


        login_user(User.query.all()[0])