# Path: testing/factories/alias.py
# def create_alias_with_employee_username(
#     name='jda',
#     username='jd',
# ):
#     alias = Alias(
#         name=name,
#         owner_key=Employee.get_key_for_username(username),
#     )
#     alias.put()
#     return alias
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })
# 
# Path: testing/factories/love.py
# def create_love(
#     sender_key,
#     recipient_key,
#     message=DEFAULT_LOVE_MESSAGE,
#     secret=False,
#     company_values=()
# ):
# 
#     love = Love(
#         sender_key=sender_key,
#         recipient_key=recipient_key,
#         message=message,
#         secret=secret,
#         company_values=company_values
#     )
#     love.put()
# 
#     return love
# 
# Path: testing/factories/love_link.py
# def create_love_link(
#     hash_key='lOvEr',
#     message='I <3 love links',
#     recipient_list='johndoe, janedoe',
# ):
# 
#     new_love_link = LoveLink(
#         hash_key=hash_key,
#         message=message,
#         recipient_list=recipient_list,
#     )
#     new_love_link.put()
# 
#     return new_love_link
# 
# Path: testing/factories/subscription.py
# def create_subscription(
#     request_url='johndoe',
#     active=False,
#     event='lovesent',
#     secret='secret',
# ):
#     return Subscription.create_from_dict({
#         'request_url': request_url,
#         'active': active,
#         'event': event,
#         'secret': secret,
#     })
# 
# Path: testing/util.py
# class LoggedInAdminBaseTest(LoggedInUserBaseTest):
# 
#     nosegae_user_kwargs = dict(
#         USER_ID='johndoe',
#         USER_EMAIL='johndoe@example.com',
#         USER_IS_ADMIN='1',
#     )
# 
# Path: testing/util.py
# class LoggedInUserBaseTest(YelpLoveTestCase):
# 
#     nosegae_datastore_v3 = True
#     nosegae_datastore_v3_kwargs = {
#         'datastore_file': '/tmp/nosegae.sqlite3',
#         'use_sqlite': True
#     }
# 
#     nosegae_user = True
#     nosegae_user_kwargs = dict(
#         USER_ID='johndoe',
#         USER_EMAIL='johndoe@example.com',
#         USER_IS_ADMIN='0',
#     )
# 
#     @mock.patch('models.employee.config')
#     def setUp(self, mock_config):
#         mock_config.DOMAIN = 'example.com'
#         self.current_user = create_employee(username='johndoe')
# 
#     def tearDown(self):
#         self.current_user.key.delete()
# 
# Path: testing/util.py
# class YelpLoveTestCase(unittest.TestCase):
# 
#     app = get_test_app()
# 
#     def assertRequiresLogin(self, response):
#         self.assertEqual(response.status_int, 302)
#         self.assert_(
#             response.headers['Location'].startswith('https://www.google.com/accounts/Login'),
#             'Unexpected Location header: {0}'.format(response.headers['Location']),
#         )
# 
#     def assertRequiresAdmin(self, response):
#         self.assertEqual(response.status_int, 401)
# 
#     def assertHasCsrf(self, form, session):
#         """Make sure the response form contains the correct CSRF token.
# 
#         :param form: a form entry from response.forms
#         :param session: response.session
#         """
#         self.assertIsNotNone(form.get('_csrf_token'))
#         self.assertEqual(
#             form['_csrf_token'].value, session['_csrf_token'],
#         )
# 
#     def addCsrfTokenToSession(self):
#         csrf_token = 'MY_TOKEN'
#         with self.app.session_transaction() as session:
#             session['_csrf_token'] = csrf_token
#         return csrf_token


import mock
import logic
from webtest.app import AppError
from config import CompanyValue
from testing.factories import create_alias_with_employee_username
from testing.factories import create_employee
from testing.factories import create_love
from testing.factories import create_love_link
from testing.factories import create_subscription
from testing.util import LoggedInAdminBaseTest
from testing.util import LoggedInUserBaseTest
from testing.util import YelpLoveTestCase

class MeOrExploreTest(LoggedInUserBaseTest):
    """
    Testing redirect to /me or /explore?user=johnd
    """

    def test_no_such_employee(self):
        with self.assertRaises(AppError) as caught:
            self.app.get('/panda')

        self.assert_(
            caught.exception.message.startswith('Bad response: 404'),
            'Expected request for unknown employee to return 404',
        )

    def test_redirect_to_me(self):
        response = self.app.get('/{username}'.format(username=self.current_user.username))

        self.assertEqual(response.status_int, 302)
        self.assertIn('/me', response.location)

    def test_redirect_to_explore(self):
        create_employee(username='buddy')
        response = self.app.get('/buddy')

        self.assertEqual(response.status_int, 302)
        self.assertIn('/explore?user=buddy', response.location)

    def test_with_alias(self):
        create_employee(username='buddy')

# Path: testing/factories/alias.py
# def create_alias_with_employee_username(
#     name='jda',
#     username='jd',
# ):
#     alias = Alias(
#         name=name,
#         owner_key=Employee.get_key_for_username(username),
#     )
#     alias.put()
#     return alias
# 
# Path: testing/factories/employee.py
# def create_employee(
#     username='johndoe',
#     department='Engineering',
#     first_name='John',
#     last_name='Doe',
#     photo_url=None,
#     office=None,
# ):
# 
#     if photo_url is None:
#         photo_url = 'http://example.com/photos/{0}.jpg'.format(username)
# 
#     return Employee.create_from_dict({
#         'username': username,
#         'department': department,
#         'first_name': first_name,
#         'last_name': last_name,
#         'photo_url': photo_url,
#         'office': office,
#     })
# 
# Path: testing/factories/love.py
# def create_love(
#     sender_key,
#     recipient_key,
#     message=DEFAULT_LOVE_MESSAGE,
#     secret=False,
#     company_values=()
# ):
# 
#     love = Love(
#         sender_key=sender_key,
#         recipient_key=recipient_key,
#         message=message,
#         secret=secret,
#         company_values=company_values
#     )
#     love.put()
# 
#     return love
# 
# Path: testing/factories/love_link.py
# def create_love_link(
#     hash_key='lOvEr',
#     message='I <3 love links',
#     recipient_list='johndoe, janedoe',
# ):
# 
#     new_love_link = LoveLink(
#         hash_key=hash_key,
#         message=message,
#         recipient_list=recipient_list,
#     )
#     new_love_link.put()
# 
#     return new_love_link
# 
# Path: testing/factories/subscription.py
# def create_subscription(
#     request_url='johndoe',
#     active=False,
#     event='lovesent',
#     secret='secret',
# ):
#     return Subscription.create_from_dict({
#         'request_url': request_url,
#         'active': active,
#         'event': event,
#         'secret': secret,
#     })
# 
# Path: testing/util.py
# class LoggedInAdminBaseTest(LoggedInUserBaseTest):
# 
#     nosegae_user_kwargs = dict(
#         USER_ID='johndoe',
#         USER_EMAIL='johndoe@example.com',
#         USER_IS_ADMIN='1',
#     )
# 
# Path: testing/util.py
# class LoggedInUserBaseTest(YelpLoveTestCase):
# 
#     nosegae_datastore_v3 = True
#     nosegae_datastore_v3_kwargs = {
#         'datastore_file': '/tmp/nosegae.sqlite3',
#         'use_sqlite': True
#     }
# 
#     nosegae_user = True
#     nosegae_user_kwargs = dict(
#         USER_ID='johndoe',
#         USER_EMAIL='johndoe@example.com',
#         USER_IS_ADMIN='0',
#     )
# 
#     @mock.patch('models.employee.config')
#     def setUp(self, mock_config):
#         mock_config.DOMAIN = 'example.com'
#         self.current_user = create_employee(username='johndoe')
# 
#     def tearDown(self):
#         self.current_user.key.delete()
# 
# Path: testing/util.py
# class YelpLoveTestCase(unittest.TestCase):
# 
#     app = get_test_app()
# 
#     def assertRequiresLogin(self, response):
#         self.assertEqual(response.status_int, 302)
#         self.assert_(
#             response.headers['Location'].startswith('https://www.google.com/accounts/Login'),
#             'Unexpected Location header: {0}'.format(response.headers['Location']),
#         )
# 
#     def assertRequiresAdmin(self, response):
#         self.assertEqual(response.status_int, 401)
# 
#     def assertHasCsrf(self, form, session):
#         """Make sure the response form contains the correct CSRF token.
# 
#         :param form: a form entry from response.forms
#         :param session: response.session
#         """
#         self.assertIsNotNone(form.get('_csrf_token'))
#         self.assertEqual(
#             form['_csrf_token'].value, session['_csrf_token'],
#         )
# 
#     def addCsrfTokenToSession(self):
#         csrf_token = 'MY_TOKEN'
#         with self.app.session_transaction() as session:
#             session['_csrf_token'] = csrf_token
#         return csrf_token


# Path: tests/views/web_test.py
import mock
import logic
from webtest.app import AppError
from config import CompanyValue
from testing.factories import create_alias_with_employee_username
from testing.factories import create_employee
from testing.factories import create_love
from testing.factories import create_love_link
from testing.factories import create_subscription
from testing.util import LoggedInAdminBaseTest
from testing.util import LoggedInUserBaseTest
from testing.util import YelpLoveTestCase


class MeOrExploreTest(LoggedInUserBaseTest):
    """
    Testing redirect to /me or /explore?user=johnd
    """

    def test_no_such_employee(self):
        with self.assertRaises(AppError) as caught:
            self.app.get('/panda')

        self.assert_(
            caught.exception.message.startswith('Bad response: 404'),
            'Expected request for unknown employee to return 404',
        )

    def test_redirect_to_me(self):
        response = self.app.get('/{username}'.format(username=self.current_user.username))

        self.assertEqual(response.status_int, 302)
        self.assertIn('/me', response.location)

    def test_redirect_to_explore(self):
        create_employee(username='buddy')
        response = self.app.get('/buddy')

        self.assertEqual(response.status_int, 302)
        self.assertIn('/explore?user=buddy', response.location)

    def test_with_alias(self):
        create_employee(username='buddy')

        create_alias_with_employee_username(name='buddyalias', username='buddy')