# Path: models/access_key.py
# class AccessKey(ndb.Model):
#     """Models an instance of an API key."""
#     access_key = ndb.StringProperty()
#     description = ndb.TextProperty()
# 
#     @staticmethod
#     def generate_uuid():
#         return uuid4().hex
# 
#     @classmethod
#     def create(cls, description):
#         new_key = cls()
#         new_key.access_key = cls.generate_uuid()
#         new_key.description = description
#         new_key.put()
# 
#         return new_key
# 
# 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/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 unittest
import mock
import logic.employee
import logic.love
from models import AccessKey
from testing.factories import create_alias_with_employee_username
from testing.factories import create_employee
from testing.util import YelpLoveTestCase
from webtest.app import AppError
        if cls is _ApiKeyRequiredTestCase:
            raise unittest.SkipTest('_ApiKeyRequiredTestCase is a base class')
        super(_ApiKeyRequiredTestCase, cls).setUpClass()

    def do_request(self, api_key):
        raise NotImplementedError('Implement this method with behavior which'
                                  ' requires an API key and returns the response')

    def test_with_api_key(self):
        api_key = AccessKey.create('test key').access_key
        response = self.do_request(api_key)
        self.assertEqual(response.status_int, self.successful_response_code)

    def test_without_api_key(self):
        bad_api_key = AccessKey.generate_uuid()
        with self.assertRaises(AppError) as caught:
            self.do_request(bad_api_key)

        self.assert_(caught.exception.message.startswith('Bad response: 401'),
                     'Expected request without valid API key to return 401')


class AutocompleteTest(_ApiKeyRequiredTestCase):
    nosegae_memcache = True
    nosegae_datastore_v3 = True
    nosegae_search = True

    def setUp(self):
        super(AutocompleteTest, self).setUp()
        create_employee(username='alice')

# Path: models/access_key.py
# class AccessKey(ndb.Model):
#     """Models an instance of an API key."""
#     access_key = ndb.StringProperty()
#     description = ndb.TextProperty()
# 
#     @staticmethod
#     def generate_uuid():
#         return uuid4().hex
# 
#     @classmethod
#     def create(cls, description):
#         new_key = cls()
#         new_key.access_key = cls.generate_uuid()
#         new_key.description = description
#         new_key.put()
# 
#         return new_key
# 
# 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/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/api_test.py
import unittest
import mock
import logic.employee
import logic.love
from models import AccessKey
from testing.factories import create_alias_with_employee_username
from testing.factories import create_employee
from testing.util import YelpLoveTestCase
from webtest.app import AppError

        if cls is _ApiKeyRequiredTestCase:
            raise unittest.SkipTest('_ApiKeyRequiredTestCase is a base class')
        super(_ApiKeyRequiredTestCase, cls).setUpClass()

    def do_request(self, api_key):
        raise NotImplementedError('Implement this method with behavior which'
                                  ' requires an API key and returns the response')

    def test_with_api_key(self):
        api_key = AccessKey.create('test key').access_key
        response = self.do_request(api_key)
        self.assertEqual(response.status_int, self.successful_response_code)

    def test_without_api_key(self):
        bad_api_key = AccessKey.generate_uuid()
        with self.assertRaises(AppError) as caught:
            self.do_request(bad_api_key)

        self.assert_(caught.exception.message.startswith('Bad response: 401'),
                     'Expected request without valid API key to return 401')


class AutocompleteTest(_ApiKeyRequiredTestCase):
    nosegae_memcache = True
    nosegae_datastore_v3 = True
    nosegae_search = True

    def setUp(self):
        super(AutocompleteTest, self).setUp()
        create_employee(username='alice')

        create_employee(username='alex')