# Path: tests/blindrtest.py
# class BlindrTest(TestCase):
#     def create_app(self):
#         return create_app('config.test')
# 
#     def setUp(self):
#         db.create_all()
#         self.auth_user = UserFactory(
#             id='auth_user',
#             fake_name='Foo Bar',
#             real_name='Auth User',
#             last_poll=datetime(2000,1,1)
#         )
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
# 
#     def auth_post(self, *args, **kwargs):
#         self._add_header_auth_token(kwargs)
#         return self.client.post(*args, **kwargs)
# 
#     def auth_get(self, *args, **kwargs):
#         self._add_header_auth_token(kwargs)
#         return self.client.get(*args, **kwargs)
# 
#     def _add_header_auth_token(self, kwargs):
#         s = itsdangerous.Signer(self.app.config['AUTH_SECRET'])
#         token = s.sign(b'auth_user').decode('utf-8')
# 
#         if not 'headers' in kwargs:
#             kwargs['headers'] = {}
# 
#         kwargs['headers']['X-User-Token'] = token
# 
# Path: tests/factory_boy/user_factory.py
# class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
#     class Meta:
#         model = User
#         sqlalchemy_session = db.session
# 
#     id = factory.Sequence(lambda n: str(n))
# 
#     fake_name = 'Fakename'
#     real_name = 'Realname'
#     gender = 'm'
# 
# Path: blindr/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id =	db.Column(db.String, primary_key=True)
# 
#     OAuth = 	db.Column(db.String)
#     fake_name =	db.Column(db.String(50), nullable = False)
#     real_name =	db.Column(db.String(50))
#     gender =	db.Column(db.CHAR, nullable = False)
#     looking_for = db.Column(db.String)
#     last_poll =	db.Column(db.TIMESTAMP)
#     facebook_urls = db.Column(db.String)
# 
#     @staticmethod
#     def from_facebook(fb_token):
#         user = None
#         graph = facebook.GraphAPI(access_token=fb_token)
#         fb_user = graph.get_object(id='me')
# 
#         session = db.session
#         user = session.query(User).filter_by(id=fb_user['id']).first()
#         if not user:
#             user = User(id= fb_user['id'],
#                     OAuth= fb_token,
#                     fake_name= name_generator.generate_name(),
#                     real_name= fb_user['name'],
#                     gender= _fb_gender_map[fb_user['gender'] or 'male'],
#                     last_poll= datetime.utcnow(),
#                     facebook_urls= "",
#                     looking_for="")
# 
#             session.add(user)
# 
#         else:
#             user.OAuth= fb_token
#             user.gender= _fb_gender_map[fb_user['gender'] or 'male']
#             user.last_poll= datetime.utcnow()
#             user.real_name= fb_user['name']
# 
#         session.commit()
#         return user


from datetime import datetime
from freezegun import freeze_time
from unittest import mock
from tests.blindrtest import BlindrTest
from tests.factory_boy.user_factory import UserFactory
from blindr.models.user import User
        self.assertEqual('bob', user.real_name)
        self.assertEqual('Fake Name', user.fake_name)
        self.assertEqual('', user.facebook_urls)

    @freeze_time('2015-01-01')
    @mock.patch('blindr.models.user.facebook.GraphAPI')
    def test_from_facebook_update_user(self, mock_graphapi):
        graph_instance = mock_graphapi.return_value
        graph_instance.get_object.return_value = {
            'id': '123123',
            'name': 'bob',
            'gender': 'male'
        }

        UserFactory(
            id='123123',
            OAuth='oauth_old',
            gender='f',
            last_poll=datetime(2000,1,1),
            real_name='Alice'
        )

        user = User.query.get('123123')
        self.assertIsNotNone(user)

        User.from_facebook('oauth_234234')

        mock_graphapi.assert_called_with(access_token='oauth_234234')
        graph_instance.get_object.assert_called_with(id='me')


# Path: tests/blindrtest.py
# class BlindrTest(TestCase):
#     def create_app(self):
#         return create_app('config.test')
# 
#     def setUp(self):
#         db.create_all()
#         self.auth_user = UserFactory(
#             id='auth_user',
#             fake_name='Foo Bar',
#             real_name='Auth User',
#             last_poll=datetime(2000,1,1)
#         )
# 
#     def tearDown(self):
#         db.session.remove()
#         db.drop_all()
# 
#     def auth_post(self, *args, **kwargs):
#         self._add_header_auth_token(kwargs)
#         return self.client.post(*args, **kwargs)
# 
#     def auth_get(self, *args, **kwargs):
#         self._add_header_auth_token(kwargs)
#         return self.client.get(*args, **kwargs)
# 
#     def _add_header_auth_token(self, kwargs):
#         s = itsdangerous.Signer(self.app.config['AUTH_SECRET'])
#         token = s.sign(b'auth_user').decode('utf-8')
# 
#         if not 'headers' in kwargs:
#             kwargs['headers'] = {}
# 
#         kwargs['headers']['X-User-Token'] = token
# 
# Path: tests/factory_boy/user_factory.py
# class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
#     class Meta:
#         model = User
#         sqlalchemy_session = db.session
# 
#     id = factory.Sequence(lambda n: str(n))
# 
#     fake_name = 'Fakename'
#     real_name = 'Realname'
#     gender = 'm'
# 
# Path: blindr/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id =	db.Column(db.String, primary_key=True)
# 
#     OAuth = 	db.Column(db.String)
#     fake_name =	db.Column(db.String(50), nullable = False)
#     real_name =	db.Column(db.String(50))
#     gender =	db.Column(db.CHAR, nullable = False)
#     looking_for = db.Column(db.String)
#     last_poll =	db.Column(db.TIMESTAMP)
#     facebook_urls = db.Column(db.String)
# 
#     @staticmethod
#     def from_facebook(fb_token):
#         user = None
#         graph = facebook.GraphAPI(access_token=fb_token)
#         fb_user = graph.get_object(id='me')
# 
#         session = db.session
#         user = session.query(User).filter_by(id=fb_user['id']).first()
#         if not user:
#             user = User(id= fb_user['id'],
#                     OAuth= fb_token,
#                     fake_name= name_generator.generate_name(),
#                     real_name= fb_user['name'],
#                     gender= _fb_gender_map[fb_user['gender'] or 'male'],
#                     last_poll= datetime.utcnow(),
#                     facebook_urls= "",
#                     looking_for="")
# 
#             session.add(user)
# 
#         else:
#             user.OAuth= fb_token
#             user.gender= _fb_gender_map[fb_user['gender'] or 'male']
#             user.last_poll= datetime.utcnow()
#             user.real_name= fb_user['name']
# 
#         session.commit()
#         return user


# Path: tests/models/test_user.py
from datetime import datetime
from freezegun import freeze_time
from unittest import mock
from tests.blindrtest import BlindrTest
from tests.factory_boy.user_factory import UserFactory
from blindr.models.user import User

        self.assertEqual('bob', user.real_name)
        self.assertEqual('Fake Name', user.fake_name)
        self.assertEqual('', user.facebook_urls)

    @freeze_time('2015-01-01')
    @mock.patch('blindr.models.user.facebook.GraphAPI')
    def test_from_facebook_update_user(self, mock_graphapi):
        graph_instance = mock_graphapi.return_value
        graph_instance.get_object.return_value = {
            'id': '123123',
            'name': 'bob',
            'gender': 'male'
        }

        UserFactory(
            id='123123',
            OAuth='oauth_old',
            gender='f',
            last_poll=datetime(2000,1,1),
            real_name='Alice'
        )

        user = User.query.get('123123')
        self.assertIsNotNone(user)

        User.from_facebook('oauth_234234')

        mock_graphapi.assert_called_with(access_token='oauth_234234')
        graph_instance.get_object.assert_called_with(id='me')


        user = User.query.get('123123')