def setUp(self):
    self.testbed = testbed.Testbed()
    self.testbed.activate()

    self.testbed.init_datastore_v3_stub()
    self.testbed.init_memcache_stub()

    self.config = explorer_config.ExplorerConfigModel.Get()
    self.config.grant_view_to_public = True
    self.config.grant_save_to_public = True
    self.config.put()

    self.provided_query = 'SELECT foo FROM bar'

    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    dashboard_json = json.dumps({'children': [
        {'container': {'children': [
            {'id': '1'},
            {'id': '2'}
        ]}},
        {'container': {'children': [
            {'id': '3', 'datasource': {'query': self.provided_query}},
            {'id': '4'}
        ]}}]})
    self.dashboard_model = dashboard.Dashboard(data=dashboard_json)
    self.dashboard_model.put()

    gae_test_util.setCurrentUser(self.testbed, is_admin=False)
示例#2
0
    def testLoad(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        provided_project = 'MODIFIED_PROJECT'
        provided_data = {
            'default_project': provided_project,
            'grant_query_to_public': True,
        }

        expected_config = {
            'default_project': provided_project,
            'default_dataset': explorer_config.DEFAULT_DATASET,
            'default_table': explorer_config.DEFAULT_TABLE,
            'analytics_key': explorer_config.DEFAULT_ANALYTICS_KEY,
            'cache_duration': explorer_config.DEFAULT_CACHE_DURATION,
            'grant_save_to_public': False,
            'grant_view_to_public': False,
            'grant_query_to_public': True,
        }

        initial_config_row = explorer_config.ExplorerConfigModel.Get()
        self.assertEquals(initial_config_row.default_project,
                          explorer_config.DEFAULT_PROJECT)

        initial_config_row.Load(provided_data)

        actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

        self.assertEquals(initial_config_row.to_dict(), actual_config)
        self.assertEquals(expected_config, actual_config)
  def testUpdateExisting(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)

    provided_project = 'EXPECTED_PROJECT'
    provided_data = {
        'default_project': provided_project,
        'grant_view_to_public': True
    }

    initial_config = explorer_config.ExplorerConfigModel.Get()
    self.assertNotEqual(initial_config.default_project, provided_project)
    expected_config = {
        'default_project': provided_project,
        'default_dataset': initial_config.default_dataset,
        'default_table': initial_config.default_table,
        'analytics_key': initial_config.analytics_key,
        'cache_duration': initial_config.cache_duration,
        'grant_save_to_public': False,
        'grant_view_to_public': True,
        'grant_query_to_public': False,
    }

    explorer_config.ExplorerConfigModel.Update(provided_data)

    actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

    self.assertEquals(expected_config, actual_config)
示例#4
0
    def testSqlHandlerPassesBuiltinQueryForPublicWithoutCustomQuery(self):
        dashboard_json = json.dumps(self.VALID_DASHBOARD)
        self.dashboard_model = dashboard.Dashboard(data=dashboard_json)

        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        self.dashboard_model.put()
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        data = {
            'dashboard_id': 1,
            'id': 3,
            'datasource': {
                'query': self.VALID_SQL,
                'config': {
                    'results': {}
                }
            }
        }

        self.maxDiff = None
        resp = self.app.post(url='/data/sql',
                             params=json.dumps(data),
                             headers={
                                 'Content-type': 'application/json',
                                 'Accept': 'text/plain'
                             })
        self.assertEqual(resp.json['results'], self.VALID_RESULTS)
示例#5
0
    def testSqlHandlerFailsCustomQueryForPublicWithoutRights(self):
        custom_query = 'SELECT stuff FROM mysource'

        dashboard_json = json.dumps(self.VALID_DASHBOARD)
        self.dashboard_model = dashboard.Dashboard(data=dashboard_json)

        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        self.dashboard_model.put()
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        expected_message = 'The user is not authorized to run custom queries'
        data = {
            'dashboard_id': 1,
            'id': 2,
            'datasource': {
                'query': custom_query,
                'config': {
                    'results': {}
                }
            }
        }

        resp = self.app.post(url='/data/sql',
                             params=json.dumps(data),
                             headers={
                                 'Content-type': 'application/json',
                                 'Accept': 'text/plain'
                             })
        self.assertEqual(resp.json['error'], expected_message)
  def testLoad(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    provided_project = 'MODIFIED_PROJECT'
    provided_data = {
        'default_project': provided_project,
        'grant_query_to_public': True,
    }

    expected_config = {
        'default_project': provided_project,
        'default_dataset': explorer_config.DEFAULT_DATASET,
        'default_table': explorer_config.DEFAULT_TABLE,
        'analytics_key': explorer_config.DEFAULT_ANALYTICS_KEY,
        'cache_duration': explorer_config.DEFAULT_CACHE_DURATION,
        'grant_save_to_public': False,
        'grant_view_to_public': False,
        'grant_query_to_public': True,
    }

    initial_config_row = explorer_config.ExplorerConfigModel.Get()
    self.assertEquals(
        initial_config_row.default_project, explorer_config.DEFAULT_PROJECT)

    initial_config_row.Load(provided_data)

    actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

    self.assertEquals(initial_config_row.to_dict(), actual_config)
    self.assertEquals(expected_config, actual_config)
  def testUpdateDefault(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)

    provided_project = 'MODIFIED_PROJECT'
    provided_data = {
        'default_project': provided_project,
        'grant_save_to_public': True
    }

    expected_config = {
        'default_project': provided_project,
        'default_dataset': explorer_config.DEFAULT_DATASET,
        'default_table': explorer_config.DEFAULT_TABLE,
        'analytics_key': explorer_config.DEFAULT_ANALYTICS_KEY,
        'cache_duration': explorer_config.DEFAULT_CACHE_DURATION,
        'max_parallel_queries': explorer_config.DEFAULT_MAX_PARALLEL_QUERIES,
        'grant_save_to_public': True,
        'grant_view_to_public': False,
        'grant_query_to_public': False,
    }

    explorer_config.ExplorerConfigModel.Update(provided_data)
    actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

    self.assertEquals(expected_config, actual_config)
示例#8
0
    def testSqlHandlerPassesWithoutDashboardIdForAdminWithoutCustomQuery(self):
        self.explorer_config.grant_query_to_public = False

        dashboard_json = json.dumps(self.VALID_DASHBOARD)
        self.dashboard_model = dashboard.Dashboard(data=dashboard_json)

        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        self.dashboard_model.put()

        data = {
            'datasource': {
                'query': self.VALID_SQL,
                'config': {
                    'results': {}
                }
            }
        }

        self.maxDiff = None
        resp = self.app.post(url='/data/sql',
                             params=json.dumps(data),
                             headers={
                                 'Content-type': 'application/json',
                                 'Accept': 'text/plain'
                             })
        logging.error(resp.json)
        self.assertEqual(resp.json['results'], self.VALID_RESULTS)
示例#9
0
    def testUpdateExisting(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        provided_project = 'EXPECTED_PROJECT'
        provided_data = {
            'default_project': provided_project,
            'grant_view_to_public': True
        }

        initial_config = explorer_config.ExplorerConfigModel.Get()
        self.assertNotEqual(initial_config.default_project, provided_project)
        expected_config = {
            'default_project': provided_project,
            'default_dataset': initial_config.default_dataset,
            'default_table': initial_config.default_table,
            'analytics_key': initial_config.analytics_key,
            'cache_duration': initial_config.cache_duration,
            'grant_save_to_public': False,
            'grant_view_to_public': True,
            'grant_query_to_public': False,
        }

        explorer_config.ExplorerConfigModel.Update(provided_data)

        actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
示例#10
0
    def testUpdateRejectForNonAdmin(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        provided_data = {'username': '******'}

        self.assertRaises(cloudsql_config.SecurityError,
                          cloudsql_config.CloudsqlConfigModel.Update,
                          provided_data)
示例#11
0
    def testGetDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        expected_config = {'username': None, 'password': None}

        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
    def testUpdateRejectForNonAdmin(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        provided_data = {'default_project': 'MODIFIED_PROJECT'}

        self.assertRaises(explorer_config.SecurityError,
                          explorer_config.ExplorerConfigModel.Update,
                          provided_data)
    def testGetDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        expected_config = {"username": None, "password": None}

        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
  def testUpdateRejectForNonAdmin(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=False)

    provided_data = {'default_project': 'MODIFIED_PROJECT'}

    self.assertRaises(
        explorer_config.SecurityError,
        explorer_config.ExplorerConfigModel.Update,
        provided_data)
  def testSqlHandler(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)

    data = {'dashboard_id': 1, 'id': 2, 'datasource': {'query': self.VALID_SQL, 'config': {'results': {}}}}

    resp = self.app.post(url='/data/sql',
                         params=json.dumps(data),
                         headers={'Content-type': 'application/json',
                                  'Accept': 'text/plain'})
    self.assertEqual(resp.json['results'], self.VALID_RESULTS)
  def testPostDefault(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    expected_data = {
        'username': PROVIDED_USERNAME,
        'password': PROVIDED_PASSWORD
    }

    self.app.post(url='/cloudsql/config', params=json.dumps(expected_data))
    resp = self.app.get(url='/cloudsql/config')
    self.assertDictEqual(resp.json, expected_data)
  def testGetDefault(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)

    expected_data = {
        'username': None,
        'password': None
    }

    resp = self.app.get(url='/cloudsql/config')
    self.assertDictEqual(resp.json, expected_data)
示例#18
0
    def testUpdateDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        provided_username = '******'
        provided_data = {'username': provided_username}

        expected_config = {'username': provided_username, 'password': None}

        cloudsql_config.CloudsqlConfigModel.Update(provided_data)
        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
    def testUpdateDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        provided_username = "******"
        provided_data = {"username": provided_username}

        expected_config = {"username": provided_username, "password": None}

        cloudsql_config.CloudsqlConfigModel.Update(provided_data)
        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
  def testPostDefault(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    provided_project = 'PROVIDED_PROJECT'
    expected_data = {
        'default_project': provided_project,
        'default_dataset': explorer_config_model.DEFAULT_DATASET,
        'default_table': explorer_config_model.DEFAULT_TABLE,
        'analytics_key': explorer_config_model.DEFAULT_ANALYTICS_KEY,
        'cache_duration': explorer_config_model.DEFAULT_CACHE_DURATION
    }

    self.app.post(url='/config', params=json.dumps(expected_data))
    resp = self.app.get(url='/config')
    self.assertDictEqual(resp.json, expected_data)
示例#21
0
    def testPostDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        provided_project = 'PROVIDED_PROJECT'
        expected_data = {
            'default_project': provided_project,
            'default_dataset': explorer_config_model.DEFAULT_DATASET,
            'default_table': explorer_config_model.DEFAULT_TABLE,
            'analytics_key': explorer_config_model.DEFAULT_ANALYTICS_KEY,
            'cache_duration': explorer_config_model.DEFAULT_CACHE_DURATION
        }

        self.app.post(url='/config', params=json.dumps(expected_data))
        resp = self.app.get(url='/config')
        self.assertDictEqual(resp.json, expected_data)
    def testGetDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        expected_config = {
            'default_project': explorer_config.DEFAULT_PROJECT,
            'default_dataset': explorer_config.DEFAULT_DATASET,
            'default_table': explorer_config.DEFAULT_TABLE,
            'analytics_key': explorer_config.DEFAULT_ANALYTICS_KEY,
            'cache_duration': explorer_config.DEFAULT_CACHE_DURATION
        }

        actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
  def testSqlHandlerPassesBuiltinQueryForPublicWithoutCustomQuery(self):
    dashboard_json = json.dumps(self.VALID_DASHBOARD)
    self.dashboard_model = dashboard.Dashboard(data=dashboard_json)

    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    self.dashboard_model.put()
    gae_test_util.setCurrentUser(self.testbed, is_admin=False)

    data = {'dashboard_id': 1, 'id': 3, 'datasource': {'query': self.VALID_SQL, 'config': {'results': {}}}}

    self.maxDiff = None
    resp = self.app.post(url='/data/sql',
                         params=json.dumps(data),
                         headers={'Content-type': 'application/json',
                                  'Accept': 'text/plain'})
    self.assertEqual(resp.json['results'], self.VALID_RESULTS)
  def testGetDefault(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=False)

    expected_config = {
        'default_project': explorer_config.DEFAULT_PROJECT,
        'default_dataset': explorer_config.DEFAULT_DATASET,
        'default_table': explorer_config.DEFAULT_TABLE,
        'analytics_key': explorer_config.DEFAULT_ANALYTICS_KEY,
        'cache_duration': explorer_config.DEFAULT_CACHE_DURATION,
        'grant_save_to_public': False,
        'grant_view_to_public': False,
        'grant_query_to_public': False,
    }

    actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

    self.assertEquals(expected_config, actual_config)
    def testUpdateExisting(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        provided_username = "******"
        provided_password = "******"

        provided_data = {"username": provided_username, "password": provided_password}

        initial_config = cloudsql_config.CloudsqlConfigModel.Get()
        self.assertNotEqual(initial_config.username, provided_username)
        expected_config = {"username": provided_username, "password": provided_password}

        cloudsql_config.CloudsqlConfigModel.Update(provided_data)

        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
  def testSqlHandlerFailsCustomQueryForPublicWithoutRights(self):
    custom_query = 'SELECT stuff FROM mysource'

    dashboard_json = json.dumps(self.VALID_DASHBOARD)
    self.dashboard_model = dashboard.Dashboard(data=dashboard_json)

    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    self.dashboard_model.put()
    gae_test_util.setCurrentUser(self.testbed, is_admin=False)

    expected_message = 'The user is not authorized to run custom queries'
    data = {'dashboard_id': 1, 'id': 2, 'datasource': {'query': custom_query, 'config': {'results': {}}}}

    resp = self.app.post(url='/data/sql',
                         params=json.dumps(data),
                         headers={'Content-type': 'application/json',
                                  'Accept': 'text/plain'})
    self.assertEqual(resp.json['error'], expected_message)
    def testLoad(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        provided_username = "******"
        provided_password = "******"

        provided_data = {"username": provided_username, "password": provided_password}

        expected_config = {"username": provided_username, "password": provided_password}

        initial_config_row = cloudsql_config.CloudsqlConfigModel.Get()
        self.assertIsNone(initial_config_row.username, None)

        initial_config_row.Load(provided_data)

        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(initial_config_row.to_dict(), actual_config)
        self.assertEquals(expected_config, actual_config)
    def testUpdateDefault(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        provided_project = 'MODIFIED_PROJECT'
        provided_data = {'default_project': provided_project}

        expected_config = {
            'default_project': provided_project,
            'default_dataset': explorer_config.DEFAULT_DATASET,
            'default_table': explorer_config.DEFAULT_TABLE,
            'analytics_key': explorer_config.DEFAULT_ANALYTICS_KEY,
            'cache_duration': explorer_config.DEFAULT_CACHE_DURATION
        }

        explorer_config.ExplorerConfigModel.Update(provided_data)
        actual_config = explorer_config.ExplorerConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
  def testSqlHandlerPassesWithoutDashboardIdForAdminWithoutCustomQuery(self):
    self.explorer_config.grant_query_to_public = False

    dashboard_json = json.dumps(self.VALID_DASHBOARD)
    self.dashboard_model = dashboard.Dashboard(data=dashboard_json)

    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    self.dashboard_model.put()

    data = {'datasource': {'query': self.VALID_SQL, 'config': {'results': {}}}}

    self.maxDiff = None
    resp = self.app.post(url='/data/sql',
                         params=json.dumps(data),
                         headers={'Content-type': 'application/json',
                                  'Accept': 'text/plain'})
    logging.error(resp.json)
    self.assertEqual(resp.json['results'], self.VALID_RESULTS)
示例#30
0
    def setUp(self):
        self.testbed = testbed.Testbed()
        self.testbed.activate()

        self.testbed.init_datastore_v3_stub()
        self.testbed.init_memcache_stub()

        self.config = explorer_config.ExplorerConfigModel.Get()
        self.config.grant_view_to_public = True
        self.config.grant_save_to_public = True
        self.config.put()

        self.provided_query = 'SELECT foo FROM bar'

        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        dashboard_json = json.dumps({
            'children': [{
                'container': {
                    'children': [{
                        'id': '1'
                    }, {
                        'id': '2'
                    }]
                }
            }, {
                'container': {
                    'children': [{
                        'id': '3',
                        'datasource': {
                            'query': self.provided_query
                        }
                    }, {
                        'id': '4'
                    }]
                }
            }]
        })
        self.dashboard_model = dashboard.Dashboard(data=dashboard_json)
        self.dashboard_model.put()

        gae_test_util.setCurrentUser(self.testbed, is_admin=False)
示例#31
0
    def testSqlHandler(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        data = {
            'dashboard_id': 1,
            'id': 2,
            'datasource': {
                'query': self.VALID_SQL,
                'config': {
                    'results': {}
                }
            }
        }

        resp = self.app.post(url='/data/sql',
                             params=json.dumps(data),
                             headers={
                                 'Content-type': 'application/json',
                                 'Accept': 'text/plain'
                             })
        self.assertEqual(resp.json['results'], self.VALID_RESULTS)
示例#32
0
    def testUpdateExisting(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)

        provided_username = '******'
        provided_password = '******'

        provided_data = {
            'username': provided_username,
            'password': provided_password
        }

        initial_config = cloudsql_config.CloudsqlConfigModel.Get()
        self.assertNotEqual(initial_config.username, provided_username)
        expected_config = {
            'username': provided_username,
            'password': provided_password
        }

        cloudsql_config.CloudsqlConfigModel.Update(provided_data)

        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(expected_config, actual_config)
示例#33
0
    def testLoad(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=True)
        provided_username = '******'
        provided_password = '******'

        provided_data = {
            'username': provided_username,
            'password': provided_password
        }

        expected_config = {
            'username': provided_username,
            'password': provided_password
        }

        initial_config_row = cloudsql_config.CloudsqlConfigModel.Get()
        self.assertIsNone(initial_config_row.username, None)

        initial_config_row.Load(provided_data)

        actual_config = cloudsql_config.CloudsqlConfigModel.Get().to_dict()

        self.assertEquals(initial_config_row.to_dict(), actual_config)
        self.assertEquals(expected_config, actual_config)
    def testGetRejectForNonAdmin(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        self.assertRaises(cloudsql_config.SecurityError, cloudsql_config.CloudsqlConfigModel.Get)
    def testUpdateRejectForNonAdmin(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        provided_data = {"username": "******"}

        self.assertRaises(cloudsql_config.SecurityError, cloudsql_config.CloudsqlConfigModel.Update, provided_data)
  def testCanQueryTrueForAdminWithoutRights(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    self.config.grant_query_to_public = False

    self.assertEqual(
        explorer_config_util.ExplorerConfigUtil.CanQuery(self.config), True)
  def testCanSaveTrueForAdminWithRights(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=True)
    self.config.grant_save_to_public = True

    self.assertEqual(
        explorer_config_util.ExplorerConfigUtil.CanSave(self.config), True)
  def testCanSaveFalseForPublicWithoutRights(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=False)
    self.config.grant_save_to_public = False

    self.assertEqual(
        explorer_config_util.ExplorerConfigUtil.CanSave(self.config), False)
  def testCanViewTrueForPublicWithRights(self):
    gae_test_util.setCurrentUser(self.testbed, is_admin=False)
    self.config.grant_view_to_public = True

    self.assertEqual(
        explorer_config_util.ExplorerConfigUtil.CanView(self.config), True)
示例#40
0
    def testGetRejectForNonAdmin(self):
        gae_test_util.setCurrentUser(self.testbed, is_admin=False)

        self.assertRaises(cloudsql_config.SecurityError,
                          cloudsql_config.CloudsqlConfigModel.Get)