예제 #1
0
파일: auth_test.py 프로젝트: IanLewis/kay
class DatastoreBackendTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True
  KIND_PREFIX_IN_TEST = 't2'
  
  def setUp(self):
    from kay.auth import create_new_user
    s = LazySettings(settings_module='kay.tests.datastore_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    create_new_user("foobar", "password", is_admin=False)

  def tearDown(self):
    self.client.test_logout()

  def test_login(self):
    response = self.client.get(url_for('auth_testapp/index'))
    self.assertEqual(response.status_code, 200)
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 302)
    self.assert_(response.headers.get('Location').endswith(
        '/auth/login?next=http%253A%252F%252Flocalhost%252Fsecret'))

    self.client.test_login(username='******')
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 200)
    self.client.test_logout()
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 302)
예제 #2
0
class SessionMiddlewareWithSecureCookieTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.securecookie_session_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def tearDown(self):
    pass

  def test_countup(self):
    response = self.client.get('/countup')
    self.assertEqual(response.status_code, 200)
    self.assertEqual(response.data, '1')
    response = self.client.get('/countup')
    self.assertEqual(response.data, '2')
    response = self.client.get('/countup')
    self.assertEqual(response.data, '3')
    response = self.client.get('/countup')
    self.assertEqual(response.data, '4')
    response = self.client.get('/countup')
    self.assertEqual(response.data, '5')
예제 #3
0
class EReporterTest(GAETestBase):
  def setUp(self):
    s = LazySettings(
      settings_module='kay.tests.ereporter_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    #self.client.test_logout()

    # Suppress logging error messages
    self._base_logger = logging.getLogger("")
    self._old_logging_handlers = self._base_logger.handlers
    self._base_logger.handlers = filter(
        lambda h: not isinstance(h, logging.StreamHandler),
        self._old_logging_handlers,
    ) 

  def tearDown(self):
    self.client.test_logout()
    self._base_logger.handlers = self._old_logging_handlers

  def test_ereporter(self):
    from kay.ext.ereporter.models import ExceptionRecord
    self.assertEqual(ExceptionRecord.all().count(), 0, "ExceptionRecord objects already exist!")

    response = self.client.get(url_for('ereporter_testapp/index'))
    self.assertEqual(response.status_code, 500, "Expected 500 error code.")
    self.assertEqual(ExceptionRecord.all().count(), 1)

    # Simulate the key expiring.
    memcache.flush_all();

    response = self.client.get(url_for('ereporter_testapp/index'))
    self.assertEqual(response.status_code, 500, "Expected 500 error code.")
    self.assertEqual(ExceptionRecord.all().count(), 1, "More than one ExceptionRecord object was created!")
    self.assertEqual(ExceptionRecord.all()[0].count, 2, "ExceptionRecord count not incremented!")
예제 #4
0
class ServerErrorTest(GAETestBase):
    """
  Regression test for issue 54 that checks to make sure 
  that responses for error conditions extend BaseResponse.

  http://code.google.com/p/kay-framework/issues/detail?id=54
  """

    def setUp(self):
        s = LazySettings(settings_module="kay.tests.regressiontests.server_error_settings")
        app = get_application(settings=s)
        self.client = Client(app, BaseResponse)
        self.client.test_logout()

        # Suppress logging error messages
        self._base_logger = logging.getLogger("")
        self._old_logging_handlers = self._base_logger.handlers
        self._base_logger.handlers = [NullHandler()]

    def tearDown(self):
        self.client.test_logout()
        self._base_logger.handlers = self._old_logging_handlers

    def test_login(self):
        response = self.client.get(url_for("server_error_testapp/index"))
        self.assertEqual(response.status_code, 500)
        self.assertTrue(isinstance(response, BaseResponse))
예제 #5
0
class CronOnlyDebugTestCase(GAETestBase):

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.settings')
    s.DEBUG = True
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def test_cron_only_failure(self):
    from kay.utils import is_dev_server
    response = self.client.get("/cron")
    if is_dev_server():
      self.assertEqual(response.status_code, 200)
    else:
      self.assertEqual(response.status_code, 403)
    response = self.client.get("/class_based_test_root/cron")
    if is_dev_server():
      self.assertEqual(response.status_code, 200)
    else:
      self.assertEqual(response.status_code, 403)

  def test_cron_only(self):
    response = self.client.get("/cron",
            headers=(('X-AppEngine-Cron', 'true'),))
    self.assertEqual(response.status_code, 200)
    self.assertTrue(response.data == "OK")
    response = self.client.get("/class_based_test_root/cron",
            headers=(('X-AppEngine-Cron', 'true'),))
    self.assertEqual(response.status_code, 200)
    self.assertTrue(response.data == "OK")
예제 #6
0
class MaintenanceCheckTestCase(unittest.TestCase):
  
  def setUp(self):
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    stub = datastore_file_stub.DatastoreFileStub('test','/dev/null',
                                                 '/dev/null')
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)

    apiproxy_stub_map.apiproxy.RegisterStub(
      'user', user_service_stub.UserServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
      'memcache', memcache_stub.MemcacheServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
      'urlfetch', urlfetch_stub.URLFetchServiceStub())

    s = LazySettings(settings_module='kay.tests.settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    if apiproxy_stub_map.apiproxy\
          ._APIProxyStubMap__stub_map.has_key('capability_service'):
      del(apiproxy_stub_map.apiproxy\
            ._APIProxyStubMap__stub_map['capability_service'])

  def tearDown(self):
    pass

  def test_success(self):
    """Test with normal CapabilityServiceStub"""
    apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      capability_stub.CapabilityServiceStub())
    response = self.client.get('/')
    self.assertEqual(response.status_code, 200)

  def test_failure(self):
    """Test with DisabledCapabilityServiceStub
    """
    apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      mocked_capability_stub.DisabledCapabilityServiceStub())
    response = self.client.get('/')
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response.headers['Location'],
                     'http://localhost/maintenance_page')

    response = self.client.get('/index2')
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response.headers['Location'],
                     'http://localhost/no_decorator')
    response = self.client.get('/no_decorator')
    self.assertEqual(response.status_code, 200)
예제 #7
0
class CronOnlyTestCase(GAETestBase):

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def test_cron_only(self):
    response = self.client.get("/cron",
            headers=(('X-AppEngine-Cron', 'true'),))
    self.assertEqual(response.status_code, 200)
    self.assertTrue(response.data == "OK")

  def test_cron_only_failure(self):
    response = self.client.get("/cron")
    self.assertEqual(response.status_code, 403)
예제 #8
0
파일: auth_test.py 프로젝트: gmist/kay-ru
class DatastoreBackendTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True
  KIND_PREFIX_IN_TEST = 't2'
  
  def setUp(self):
    s = LazySettings(settings_module='kay.tests.datastore_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def tearDown(self):
    self.client.test_logout()

  def test_login(self):
    from kay.auth import create_new_user
    create_new_user("foobar", "password", is_admin=False)
    response = self.client.get(url_for('auth_testapp/index'))
    self.assertEqual(response.status_code, 200)
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 302)
    self.assert_(response.headers.get('Location').endswith(
        '/auth/login?next=http%253A%252F%252Flocalhost%252Fsecret'))

    self.client.test_login(username='******')
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 200)
    self.client.test_logout()
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 302)

  def test_create_inactive_user(self):
      from kay.auth.models import DatastoreUser
      from kay.utils import crypto

      user = DatastoreUser.create_inactive_user("testuser", "password",
                                                "*****@*****.**",
                                                do_registration=False) 
      self.assertEqual(user.key().name(),
                       DatastoreUser.get_key_name("testuser"))
      self.assertEqual(user.user_name, "testuser")
    
      self.assertTrue(crypto.check_pwhash(user.password, "password"))
      self.assertEqual(user.email, "*****@*****.**")
예제 #9
0
    def setUp(self):
        s = LazySettings(settings_module="kay.tests.regressiontests.server_error_settings")
        app = get_application(settings=s)
        self.client = Client(app, BaseResponse)
        self.client.test_logout()

        # Suppress logging error messages
        self._base_logger = logging.getLogger("")
        self._old_logging_handlers = self._base_logger.handlers
        self._base_logger.handlers = [NullHandler()]
예제 #10
0
파일: auth_test.py 프로젝트: IanLewis/kay
 def setUp(self):
   try:
     self.original_user = os.environ['USER_EMAIL']
     self.original_is_admin = os.environ['USER_IS_ADMIN']
     del os.environ['USER_EMAIL']
     del os.environ['USER_IS_ADMIN']
   except Exception:
     pass
   s = LazySettings(settings_module='kay.tests.google_settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
   self.client.test_logout()
예제 #11
0
class ServerErrorTest(GAETestBase):
  def setUp(self):
    s = LazySettings(
      settings_module='kay.tests.regressiontests.server_error_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    self.client.test_logout()

    # Suppress logging error messages
    self._base_logger = logging.getLogger("")
    self._old_logging_handlers = self._base_logger.handlers
    self._base_logger.handlers = [NullHandler()] 

  def tearDown(self):
    self.client.test_logout()
    self._base_logger.handlers = self._old_logging_handlers

  def test_login(self):
    response = self.client.get(url_for('server_error_testapp/index'))
    self.assertEqual(response.status_code, 500)
    self.assertTrue(isinstance(response, BaseResponse))
예제 #12
0
class CronOnlyAdminTestCase(GAETestBase):

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.decorator_settings')
    s.DEBUG = True
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def test_cron_only_admin(self):
    from kay.auth.models import DatastoreUser
    user = DatastoreUser(
        key_name=DatastoreUser.get_key_name("foobar"),
        user_name="foobar",
        password=DatastoreUser.hash_password("password")
    )
    user.is_admin = True
    user.put()

    self.client.test_login(username='******')
    response = self.client.get('/cron')
    self.assertEqual(response.status_code, 200)
    self.assertTrue(response.data == "OK")
예제 #13
0
  def setUp(self):
    s = LazySettings(
      settings_module='kay.tests.ereporter_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    #self.client.test_logout()

    # Suppress logging error messages
    self._base_logger = logging.getLogger("")
    self._old_logging_handlers = self._base_logger.handlers
    self._base_logger.handlers = filter(
        lambda h: not isinstance(h, logging.StreamHandler),
        self._old_logging_handlers,
    ) 
예제 #14
0
class SessionMiddlewareOnCookieWithSecureAttributeTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True

  def setUp(self):
    import os
    s = LazySettings(settings_module='kay.tests.cookie_session_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    self.server_name = os.environ['SERVER_NAME']

  def tearDown(self):
    pass

  def test_if_cookie_is_marked_as_secure(self):
    url = 'https://%s/countup' % self.server_name
    response = self.client.get('/countup', url)
    self.assert_('secure' in response.headers['Set-Cookie'],
                 'The Set-Cookie header does not contain secure flag.')
예제 #15
0
  def setUp(self):
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    stub = datastore_file_stub.DatastoreFileStub('test','/dev/null',
                                                 '/dev/null')
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)

    apiproxy_stub_map.apiproxy.RegisterStub(
      'user', user_service_stub.UserServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
      'memcache', memcache_stub.MemcacheServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
      'urlfetch', urlfetch_stub.URLFetchServiceStub())

    s = LazySettings(settings_module='kay.tests.settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    if apiproxy_stub_map.apiproxy\
          ._APIProxyStubMap__stub_map.has_key('capability_service'):
      del(apiproxy_stub_map.apiproxy\
            ._APIProxyStubMap__stub_map['capability_service'])
예제 #16
0
class StackedDecoratorsTestCase(GAETestBase):

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.stacked_decorators.settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def test_stacked_decorators(self):
    self.client.test_login(email='*****@*****.**')
    response = self.client.get('/')
    self.assertEqual(response.status_code, 200)
    response = self.client.get('/class')
    self.assertEqual(response.status_code, 200)
    response = self.client.get('/ndb')
    self.assertEqual(response.status_code, 200)
    response = self.client.get('/class_ndb')
    self.assertEqual(response.status_code, 200)
예제 #17
0
class RestTestCase(GAETestBase):
    KIND_NAME_UNSWAPPED = False
    USE_PRODUCTION_STUBS = True
    CLEANUP_USED_KIND = True
    KIND_PREFIX_IN_TEST = "t2"

    def setUp(self):
        s = LazySettings(settings_module='kay.tests.rest_settings')
        app = get_application(settings=s)
        self.client = Client(app, BaseResponse)
        self.client.test_logout()

    def tearDown(self):
        self.client.test_logout()

    def test_rest_operations(self):
        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.get('/rest/metadata')
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/metadata/RestModel')
        self.assertEqual(response.status_code, 200)

        response = self.client.post(
            '/rest/RestModel',
            data=
            '<?xml version="1.0" encoding="utf-8"?><RestModel><i_prop>12</i_prop><s_prop>string</s_prop></RestModel>'
        )
        self.assertEqual(response.status_code, 200)
        key = response.data
        elm = RestModel.get(key)
        self.assertEqual(elm.s_prop, "string")
        self.assertEqual(elm.i_prop, 12)

        response = self.client.post(
            '/rest/RestModel/%s' % key,
            data=
            '<?xml version="1.0" encoding="utf-8"?><RestModel><i_prop>14</i_prop></RestModel>'
        )
        self.assertEqual(response.status_code, 200)
        key2 = response.data
        self.assertEqual(key, key2)
        elm = RestModel.get(key)
        self.assertEqual(elm.s_prop, "string")
        self.assertEqual(elm.i_prop, 14)

        response = self.client.get('/rest/RestModel')
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/RestModel/%s' % key)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/RestModel/%s/s_prop' % key)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, "string")

        response = self.client.get('/rest/RestModel/%s/i_prop' % key)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, "14")

        response = self.client.delete('/rest/RestModel/%s' % key)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/RestModel/%s' % key)
        self.assertEqual(response.status_code, 404)
예제 #18
0
class RestJSONTestCase(GAETestBase):
    KIND_NAME_UNSWAPPED = False
    USE_PRODUCTION_STUBS = True
    CLEANUP_USED_KIND = True

    def setUp(self):
        s = LazySettings(settings_module='kay.tests.rest_settings')
        app = get_application(settings=s)
        self.client = Client(app, BaseResponse)
        self.client.test_logout()

    def tearDown(self):
        self.client.test_logout()

    def test_rest_json(self):

        headers = Headers({"Accept": "application/json"})

        response = self.client.get('/rest/metadata', headers=headers)
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**")
        response = self.client.get('/rest/metadata', headers=headers)
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.get('/rest/metadata', headers=headers)
        self.assertEqual(response.status_code, 200)

        self.client.test_logout()
        response = self.client.get('/rest/metadata/RestModel', headers=headers)
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**")
        response = self.client.get('/rest/metadata/RestModel', headers=headers)
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.get('/rest/metadata/RestModel', headers=headers)
        self.assertEqual(response.status_code, 200)

        self.client.test_logout()
        response = self.client.post(
            '/rest/RestModel',
            data='{"RestModel": {"i_prop": 12, "s_prop": "string"}}',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**")
        response = self.client.post(
            '/rest/RestModel',
            data='{"RestModel": {"i_prop": 12, "s_prop": "string"}}',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.post(
            '/rest/RestModel',
            data='{"RestModel": {"i_prop": 12, "s_prop": "string"}}',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 200)

        key = response.data
        elm = RestModel.get(key)
        self.assertEqual(elm.s_prop, "string")
        self.assertEqual(elm.i_prop, 12)

        self.client.test_logout()
        response = self.client.post(
            '/rest/RestModel/%s' % key,
            data='{"RestModel": {"i_prop": 14}}',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**")
        response = self.client.post(
            '/rest/RestModel/%s' % key,
            data='{"RestModel": {"i_prop": 14}}',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.post(
            '/rest/RestModel/%s' % key,
            data='{"RestModel": {"i_prop": 14}}',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 200)

        key2 = response.data
        self.assertEqual(key, key2)
        elm = RestModel.get(key)
        self.assertEqual(elm.s_prop, "string")
        self.assertEqual(elm.i_prop, 14)

        response = self.client.post(
            '/rest/RestModel',
            data=
            '[{"RestModel": {"i_prop": 1, "s_prop": "foobar1"}},{"RestModel": {"i_prop": 2, "s_prop": "foobar2"}}]',
            content_type="application/json; charset=utf-8")
        self.assertEqual(response.status_code, 200)
        key3, key4 = response.data.split(',')
        elm3 = RestModel.get(key3)
        elm4 = RestModel.get(key4)
        self.assertEqual(elm3.s_prop, "foobar1")
        self.assertEqual(elm3.i_prop, 1)
        self.assertEqual(elm4.s_prop, "foobar2")
        self.assertEqual(elm4.i_prop, 2)

        response = self.client.get('/rest/RestModel', headers=headers)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/RestModel/%s' % key, headers=headers)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/RestModel/%s/s_prop' % key,
                                   headers=headers)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, "string")

        response = self.client.get('/rest/RestModel/%s/i_prop' % key,
                                   headers=headers)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, "14")

        self.client.test_logout()
        response = self.client.delete('/rest/RestModel/%s' % key,
                                      headers=headers)
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**")
        response = self.client.delete('/rest/RestModel/%s' % key,
                                      headers=headers)
        self.assertEqual(response.status_code, 403)

        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.delete('/rest/RestModel/%s' % key,
                                      headers=headers)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/rest/RestModel/%s' % key, headers=headers)
        self.assertEqual(response.status_code, 404)
예제 #19
0
 def setUp(self):
   s = LazySettings(settings_module='kay.tests.settings')
   s.DEBUG = True
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
예제 #20
0
 def setUp(self):
     s = LazySettings(
         settings_module='kay.tests.securecookie_session_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)
예제 #21
0
파일: rest_test.py 프로젝트: dbordak/pyblog
 def setUp(self):
   s = LazySettings(settings_module='kay.tests.rest_settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
   self.client.test_logout()
예제 #22
0
파일: rest_test.py 프로젝트: dbordak/pyblog
class RestJSONTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.rest_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    self.client.test_logout()

  def tearDown(self):
    self.client.test_logout()

  def test_rest_json(self):

    headers = Headers({"Accept": "application/json"})

    response = self.client.get('/rest/metadata', headers=headers)
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**")
    response = self.client.get('/rest/metadata', headers=headers)
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.get('/rest/metadata', headers=headers)
    self.assertEqual(response.status_code, 200)

    self.client.test_logout()
    response = self.client.get('/rest/metadata/RestModel', headers=headers)
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**")
    response = self.client.get('/rest/metadata/RestModel', headers=headers)
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.get('/rest/metadata/RestModel', headers=headers)
    self.assertEqual(response.status_code, 200)


    self.client.test_logout()
    response = self.client.post(
      '/rest/RestModel',
      data='{"RestModel": {"i_prop": 12, "s_prop": "string"}}',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**")
    response = self.client.post(
      '/rest/RestModel',
      data='{"RestModel": {"i_prop": 12, "s_prop": "string"}}',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.post(
      '/rest/RestModel',
      data='{"RestModel": {"i_prop": 12, "s_prop": "string"}}',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 200)

    key = response.data
    elm = RestModel.get(key)
    self.assertEqual(elm.s_prop, "string")
    self.assertEqual(elm.i_prop, 12)

    self.client.test_logout()
    response = self.client.post(
      '/rest/RestModel/%s' % key,
      data='{"RestModel": {"i_prop": 14}}',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**")
    response = self.client.post(
      '/rest/RestModel/%s' % key,
      data='{"RestModel": {"i_prop": 14}}',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.post(
      '/rest/RestModel/%s' % key,
      data='{"RestModel": {"i_prop": 14}}',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 200)

    key2 = response.data
    self.assertEqual(key, key2)
    elm = RestModel.get(key)
    self.assertEqual(elm.s_prop, "string")
    self.assertEqual(elm.i_prop, 14)
    
    response = self.client.post(
      '/rest/RestModel',
      data='[{"RestModel": {"i_prop": 1, "s_prop": "foobar1"}},{"RestModel": {"i_prop": 2, "s_prop": "foobar2"}}]',
      content_type="application/json; charset=utf-8")
    self.assertEqual(response.status_code, 200)
    key3, key4 = response.data.split(',')
    elm3 = RestModel.get(key3)
    elm4 = RestModel.get(key4)
    self.assertEqual(elm3.s_prop, "foobar1")
    self.assertEqual(elm3.i_prop, 1)
    self.assertEqual(elm4.s_prop, "foobar2")
    self.assertEqual(elm4.i_prop, 2)

    response = self.client.get('/rest/RestModel', headers=headers)
    self.assertEqual(response.status_code, 200)

    response = self.client.get('/rest/RestModel/%s' % key, headers=headers)
    self.assertEqual(response.status_code, 200)

    response = self.client.get('/rest/RestModel/%s/s_prop' % key,
                               headers=headers)
    self.assertEqual(response.status_code, 200)
    self.assertEqual(response.data, "string")

    response = self.client.get('/rest/RestModel/%s/i_prop' % key,
                               headers=headers)
    self.assertEqual(response.status_code, 200)
    self.assertEqual(response.data, "14")

    self.client.test_logout()
    response = self.client.delete('/rest/RestModel/%s' % key,
                                  headers=headers)
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**")
    response = self.client.delete('/rest/RestModel/%s' % key,
                                  headers=headers)
    self.assertEqual(response.status_code, 403)

    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.delete('/rest/RestModel/%s' % key,
                                  headers=headers)
    self.assertEqual(response.status_code, 200)


    response = self.client.get('/rest/RestModel/%s' % key,
                               headers=headers)
    self.assertEqual(response.status_code, 404)
예제 #23
0
 def setUp(self):
     from kay.auth import create_new_user
     s = LazySettings(settings_module='kay.tests.datastore_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)
     create_new_user("foobar", "password", is_admin=False)
예제 #24
0
파일: auth_test.py 프로젝트: IanLewis/kay
 def setUp(self):
   from kay.auth import create_new_user
   s = LazySettings(settings_module='kay.tests.datastore_settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
   create_new_user("foobar", "password", is_admin=False)
예제 #25
0
 def setUp(self):
     import os
     s = LazySettings(settings_module='kay.tests.cookie_session_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)
     self.server_name = os.environ['SERVER_NAME']
예제 #26
0
파일: tests.py 프로젝트: dbordak/pyblog
class CacheTestCase(GAETestBase):
  def setUp(self):
    s = LazySettings(settings_module='kay.tests.cache_test.settings')
    self.app = get_application(settings=s)
    self.client = Client(self.app, BaseResponse)
    memcache.flush_all()
    
  def test_cache_middleware(self):
    # make sure that CACHE_MIDDLEWARE_ANONYMOUSE_ONLY works
    self.client.test_login(email='*****@*****.**')
    response = self.client.get(url_for('cache_testapp/index'))
    self.assertEqual(response.status_code, 200)
    c = memcache.get('http://localhost/?lang=en', namespace='CACHE_MIDDLEWARE')
    self.assertEqual(c, None)
    self.client.test_logout()
    # user logout, so the cache works
    response = self.client.get(url_for('cache_testapp/index'))
    self.assertEqual(response.status_code, 200)
    c = memcache.get('http://localhost/?lang=en', namespace='CACHE_MIDDLEWARE')
    self.assertEqual(c.data, response.data)

  def test_cache_decorator_with_function_view(self):
    # make sure that CACHE_MIDDLEWARE_ANONYMOUSE_ONLY works
    self.client.test_login(email='*****@*****.**')
    response = self.client.get(url_for('cache_testapp/decorator'))
    self.assertEqual(response.status_code, 200)
    c = memcache.get('http://localhost/decorator?lang=en',
                     namespace='CACHE_DECORATOR')
    self.assertEqual(c, None)
    self.client.test_logout()
    # user logout, so the cache works
    response = self.client.get(url_for('cache_testapp/decorator'))
    self.assertEqual(response.status_code, 200)
    c = memcache.get('http://localhost/decorator?lang=en',
                     namespace='CACHE_DECORATOR')
    self.assertEqual(c.data, response.data)

  def test_cache_decorator_with_class_view(self):
    # make sure that CACHE_MIDDLEWARE_ANONYMOUSE_ONLY works
    self.client.test_login(email='*****@*****.**')
    response = self.client.get(url_for('cache_testapp/decorator_class'))
    self.assertEqual(response.status_code, 200)
    c = memcache.get('http://localhost/decorator_class?lang=en',
                     namespace='CACHE_DECORATOR')
    self.assertEqual(c, None)
    self.client.test_logout()
    # user logout, so the cache works
    response = self.client.get(url_for('cache_testapp/decorator_class'))
    self.assertEqual(response.status_code, 200)
    c = memcache.get('http://localhost/decorator_class?lang=en',
                     namespace='CACHE_DECORATOR')
    self.assertEqual(c.data, response.data)
예제 #27
0
class MaintenanceCheckTestCase(unittest.TestCase):
  
  def setUp(self):
    apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
    stub = datastore_file_stub.DatastoreFileStub('test','/dev/null',
                                                 '/dev/null')
    apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)

    apiproxy_stub_map.apiproxy.RegisterStub(
      'user', user_service_stub.UserServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
      'memcache', memcache_stub.MemcacheServiceStub())

    apiproxy_stub_map.apiproxy.RegisterStub(
      'urlfetch', urlfetch_stub.URLFetchServiceStub())

    s = LazySettings(settings_module='kay.tests.settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    if apiproxy_stub_map.apiproxy\
          ._APIProxyStubMap__stub_map.has_key('capability_service'):
      del(apiproxy_stub_map.apiproxy\
            ._APIProxyStubMap__stub_map['capability_service'])

  def tearDown(self):
    pass

  def test_success(self):
    """Test with normal CapabilityServiceStub"""
    apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      capability_stub.CapabilityServiceStub())
    response = self.client.get('/')
    self.assertEqual(response.status_code, 200)
    response = self.client.get('/class_based_test_root/')
    self.assertEqual(response.status_code, 200)

  def test_failure(self):
    """Test with DisabledCapabilityServiceStub
    """
    apiproxy_stub_map.apiproxy.RegisterStub(
      'capability_service',
      mocked_capability_stub.DisabledCapabilityServiceStub())
    response = self.client.get('/')
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response.headers['Location'],
                     'http://localhost/maintenance_page')

    response = self.client.get('/class_based_test_root/')
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response.headers['Location'],
                     'http://localhost/maintenance_page')

    response = self.client.get('/index2')
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response.headers['Location'],
                     'http://localhost/no_decorator')

    response = self.client.get('/class_based_test_root/index2')
    self.assertEqual(response.status_code, 302)
    self.assertEqual(response.headers['Location'],
                     'http://localhost/no_decorator')

    response = self.client.get('/no_decorator')
    self.assertEqual(response.status_code, 200)
예제 #28
0
 def setUp(self):
   s = LazySettings(settings_module='kay.tests.stacked_decorators.settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
예제 #29
0
 def setUp(self):
     s = LazySettings(settings_module='kay.tests.rest_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)
     self.client.test_logout()
예제 #30
0
 def setUp(self):
   s = LazySettings(settings_module='kay.tests.securecookie_session_settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
예제 #31
0
class GoogleBackendTestCase(GAETestBase):
    KIND_NAME_UNSWAPPED = False
    USE_PRODUCTION_STUBS = True
    CLEANUP_USED_KIND = True
    KIND_PREFIX_IN_TEST = 't1'

    def setUp(self):
        try:
            self.original_user = os.environ['USER_EMAIL']
            self.original_is_admin = os.environ['USER_IS_ADMIN']
            del os.environ['USER_EMAIL']
            del os.environ['USER_IS_ADMIN']
        except Exception:
            pass
        s = LazySettings(settings_module='kay.tests.google_settings')
        app = get_application(settings=s)
        self.client = Client(app, BaseResponse)
        self.client.test_logout()

    def tearDown(self):
        self.client.test_logout()

    def test_login(self):
        response = self.client.get(url_for('auth_testapp/index'))
        self.assertEqual(response.status_code, 200)
        response = self.client.get(url_for('auth_testapp/secret'))
        self.assertEqual(response.status_code, 302)
        self.client.test_login(email="*****@*****.**", is_admin="1")
        response = self.client.get(url_for('auth_testapp/secret'))
        self.assertEqual(response.status_code, 200)
        self.client.test_logout()
        response = self.client.get(url_for('auth_testapp/secret'))
        self.assertEqual(response.status_code, 302)
예제 #32
0
파일: auth_test.py 프로젝트: IanLewis/kay
class GoogleBackendTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True
  KIND_PREFIX_IN_TEST = 't1'
  
  def setUp(self):
    try:
      self.original_user = os.environ['USER_EMAIL']
      self.original_is_admin = os.environ['USER_IS_ADMIN']
      del os.environ['USER_EMAIL']
      del os.environ['USER_IS_ADMIN']
    except Exception:
      pass
    s = LazySettings(settings_module='kay.tests.google_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    self.client.test_logout()

  def tearDown(self):
    self.client.test_logout()

  def test_login(self):
    response = self.client.get(url_for('auth_testapp/index'))
    self.assertEqual(response.status_code, 200)
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 302)
    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 200)
    self.client.test_logout()
    response = self.client.get(url_for('auth_testapp/secret'))
    self.assertEqual(response.status_code, 302)
예제 #33
0
class GAEMABackendTestCase(GAETestBase):
    KIND_NAME_UNSWAPPED = False
    USE_PRODUCTION_STUBS = True
    CLEANUP_USED_KIND = True
    KIND_PREFIX_IN_TEST = 't3'

    def setUp(self):
        s = LazySettings(settings_module='kay.tests.gaema_settings')
        app = get_application(settings=s)
        self.client = Client(app, BaseResponse)

    def tearDown(self):
        self.client.test_logout(service='shehas.net')

    def test_login(self):
        response = self.client.get(url_for('gaema_testapp/index'))
        self.assertEqual(response.status_code, 200)
        response = self.client.get(
            url_for('gaema_testapp/secret', domain_name='shehas.net'))
        self.assertEqual(response.status_code, 302)
        self.assert_(
            response.headers.get('Location').endswith(
                '/_ah/gaema/marketplace_login/a/shehas.net'))

        self.client.test_login(service='shehas.net',
                               user_data={
                                   'claimed_id': 'http://shehas.net/123',
                                   'email': '*****@*****.**'
                               })

        response = self.client.get(
            url_for('gaema_testapp/secret', domain_name='shehas.net'))
        self.assertEqual(response.status_code, 200)

        response = self.client.get(
            url_for('gaema_testapp/secret', domain_name='example.com'))
        self.assertEqual(response.status_code, 302)
        self.assert_(
            response.headers.get('Location').endswith(
                '/_ah/gaema/marketplace_login/a/example.com'))

        self.client.test_logout(service='shehas.net')

        response = self.client.get(
            url_for('gaema_testapp/secret', domain_name='shehas.net'))
        self.assertEqual(response.status_code, 302)
예제 #34
0
파일: auth_test.py 프로젝트: dbordak/pyblog
 def setUp(self):
   s = LazySettings(settings_module='kay.tests.datastore_settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
예제 #35
0
파일: rest_test.py 프로젝트: dbordak/pyblog
class RestTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.rest_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)
    self.client.test_logout()

  def tearDown(self):
    self.client.test_logout()

  def test_rest_operations(self):
    self.client.test_login(email="*****@*****.**", is_admin="1")
    response = self.client.get('/rest/metadata')
    self.assertEqual(response.status_code, 200)

    response = self.client.get('/rest/metadata/RestModel')
    self.assertEqual(response.status_code, 200)

    response = self.client.post('/rest/RestModel', data='<?xml version="1.0" encoding="utf-8"?><RestModel><i_prop>12</i_prop><s_prop>string</s_prop></RestModel>')
    self.assertEqual(response.status_code, 200)
    key = response.data
    elm = RestModel.get(key)
    self.assertEqual(elm.s_prop, "string")
    self.assertEqual(elm.i_prop, 12)

    response = self.client.post(
      '/rest/RestModel/%s' % key,
      data='<?xml version="1.0" encoding="utf-8"?><RestModel><i_prop>14</i_prop></RestModel>')
    self.assertEqual(response.status_code, 200)
    key2 = response.data
    self.assertEqual(key, key2)
    elm = RestModel.get(key)
    self.assertEqual(elm.s_prop, "string")
    self.assertEqual(elm.i_prop, 14)

    response = self.client.get('/rest/RestModel')
    self.assertEqual(response.status_code, 200)

    response = self.client.get('/rest/RestModel/%s' % key)
    self.assertEqual(response.status_code, 200)

    response = self.client.get('/rest/RestModel/%s/s_prop' % key)
    self.assertEqual(response.status_code, 200)
    self.assertEqual(response.data, "string")

    response = self.client.get('/rest/RestModel/%s/i_prop' % key)
    self.assertEqual(response.status_code, 200)
    self.assertEqual(response.data, "14")

    response = self.client.delete('/rest/RestModel/%s' % key)
    self.assertEqual(response.status_code, 200)

    response = self.client.get('/rest/RestModel/%s' % key)
    self.assertEqual(response.status_code, 404)
예제 #36
0
class CacheTestCase(GAETestBase):
    def setUp(self):
        s = LazySettings(settings_module='kay.tests.cache_test.settings')
        self.app = get_application(settings=s)
        self.client = Client(self.app, BaseResponse)
        memcache.flush_all()

    def test_cache_middleware(self):
        # make sure that CACHE_MIDDLEWARE_ANONYMOUSE_ONLY works
        self.client.test_login(email='*****@*****.**')
        response = self.client.get(url_for('cache_testapp/index'))
        self.assertEqual(response.status_code, 200)
        c = memcache.get('http://localhost/?lang=en',
                         namespace='CACHE_MIDDLEWARE')
        self.assertEqual(c, None)
        self.client.test_logout()
        # user logout, so the cache works
        response = self.client.get(url_for('cache_testapp/index'))
        self.assertEqual(response.status_code, 200)
        c = memcache.get('http://localhost/?lang=en',
                         namespace='CACHE_MIDDLEWARE')
        self.assertEqual(c.data, response.data)

    def test_cache_decorator_with_function_view(self):
        # make sure that CACHE_MIDDLEWARE_ANONYMOUSE_ONLY works
        self.client.test_login(email='*****@*****.**')
        response = self.client.get(url_for('cache_testapp/decorator'))
        self.assertEqual(response.status_code, 200)
        c = memcache.get('http://localhost/decorator?lang=en',
                         namespace='CACHE_DECORATOR')
        self.assertEqual(c, None)
        self.client.test_logout()
        # user logout, so the cache works
        response = self.client.get(url_for('cache_testapp/decorator'))
        self.assertEqual(response.status_code, 200)
        c = memcache.get('http://localhost/decorator?lang=en',
                         namespace='CACHE_DECORATOR')
        self.assertEqual(c.data, response.data)

    def test_cache_decorator_with_class_view(self):
        # make sure that CACHE_MIDDLEWARE_ANONYMOUSE_ONLY works
        self.client.test_login(email='*****@*****.**')
        response = self.client.get(url_for('cache_testapp/decorator_class'))
        self.assertEqual(response.status_code, 200)
        c = memcache.get('http://localhost/decorator_class?lang=en',
                         namespace='CACHE_DECORATOR')
        self.assertEqual(c, None)
        self.client.test_logout()
        # user logout, so the cache works
        response = self.client.get(url_for('cache_testapp/decorator_class'))
        self.assertEqual(response.status_code, 200)
        c = memcache.get('http://localhost/decorator_class?lang=en',
                         namespace='CACHE_DECORATOR')
        self.assertEqual(c.data, response.data)
예제 #37
0
 def setUp(self):
     init_recording()
     app = get_application()
     self.client = Client(app, BaseResponse)
     self.client.test_login()
예제 #38
0
 def setUp(self):
     s = LazySettings(settings_module='kay.tests.cache_test.settings')
     self.app = get_application(settings=s)
     self.client = Client(self.app, BaseResponse)
     memcache.flush_all()
예제 #39
0
 def setUp(self):
   import os
   s = LazySettings(settings_module='kay.tests.cookie_session_settings')
   app = get_application(settings=s)
   self.client = Client(app, BaseResponse)
   self.server_name = os.environ['SERVER_NAME']
예제 #40
0
파일: auth_test.py 프로젝트: IanLewis/kay
class GAEMABackendTestCase(GAETestBase):
  KIND_NAME_UNSWAPPED = False
  USE_PRODUCTION_STUBS = True
  CLEANUP_USED_KIND = True
  KIND_PREFIX_IN_TEST = 't3'

  def setUp(self):
    s = LazySettings(settings_module='kay.tests.gaema_settings')
    app = get_application(settings=s)
    self.client = Client(app, BaseResponse)

  def tearDown(self):
    self.client.test_logout(service='shehas.net')

  def test_login(self):
    response = self.client.get(url_for('gaema_testapp/index'))
    self.assertEqual(response.status_code, 200)
    response = self.client.get(url_for('gaema_testapp/secret',
                                       domain_name='shehas.net'))
    self.assertEqual(response.status_code, 302)
    self.assert_(response.headers.get('Location').endswith(
        '/_ah/gaema/marketplace_login/a/shehas.net'))

    self.client.test_login(service='shehas.net',
                           user_data={'claimed_id': 'http://shehas.net/123',
                                      'email': '*****@*****.**'})

    response = self.client.get(url_for('gaema_testapp/secret',
                                       domain_name='shehas.net'))
    self.assertEqual(response.status_code, 200)

    response = self.client.get(url_for('gaema_testapp/secret',
                                       domain_name='example.com'))
    self.assertEqual(response.status_code, 302)
    self.assert_(response.headers.get('Location').endswith(
        '/_ah/gaema/marketplace_login/a/example.com'))

    self.client.test_logout(service='shehas.net')

    response = self.client.get(url_for('gaema_testapp/secret',
                                       domain_name='shehas.net'))
    self.assertEqual(response.status_code, 302)
예제 #41
0
파일: tests.py 프로젝트: dbordak/pyblog
 def setUp(self):
   s = LazySettings(settings_module='kay.tests.cache_test.settings')
   self.app = get_application(settings=s)
   self.client = Client(self.app, BaseResponse)
   memcache.flush_all()
예제 #42
0
 def setUp(self):
     s = LazySettings(settings_module='kay.tests.datastore_settings')
     app = get_application(settings=s)
     self.client = Client(app, BaseResponse)