Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
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)
Exemple #4
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)
Exemple #5
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)
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)
Exemple #7
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)
Exemple #8
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)
Exemple #9
0
class DatastoreBackendTestCase(GAETestBase):
    KIND_NAME_UNSWAPPED = False
    USE_PRODUCTION_STUBS = True
    CLEANUP_USED_KIND = True

    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, "*****@*****.**")
Exemple #10
0
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, "*****@*****.**")
Exemple #11
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)
Exemple #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")
Exemple #13
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)
Exemple #14
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)
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)
Exemple #16
0
class TsuhanTest(GAETestBase):
    CLEANUP_USED_KIND = False
    USE_PRODUCTION_STUBS = True
    KIND_NAME_UNSWAPPED=False
    USE_REMOTE_STUBS=False

    def setUp(self):
        init_recording()
        app = get_application()
        self.client = Client(app, BaseResponse)
        self.client.test_login()

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

    def test_index(self):
        response = self.client.open(
            path=url_for('core/index'),
            method='GET',
            data=dict(),
        )
        self.assertEqual(response.status_code, 200)

    def test_products(self):
        response = self.client.open(
            path=url_for('core/products',category_code='01',list_per_page=20,page_index=0),
            method='GET',
            data=dict(),
        )
        self.assertEqual(response.status_code, 200)

    def test_tags(self):
        response = self.client.open(
            path=url_for('core/tags',tags=u'タグ',list_per_page=20,page_index=0),
            method='GET',
            data=dict(),
        )
        self.assertEqual(response.status_code, 200)

    def test_search(self):
        response = self.client.open(
            path=url_for('core/search',words=u'検索ワード',list_per_page=20,page_index=0),
            method='GET',
            data=dict(),
        )
        self.assertEqual(response.status_code, 200)

    def test_detail(self):
        response = self.client.open(
            path=url_for('core/detail',article_id='421135'),
            method='GET',
            data=dict(),
        )
        self.assertEqual(response.status_code, 200)

    def test_sitemap(self):
        response = self.client.open(
            path=url_for('core/sitemap'),
            method='GET',
            data=dict(),
        )
        self.assertEqual(response.status_code, 404)