Chromium Code Reviews| Index: dashboard/dashboard/create_health_report_test.py |
| diff --git a/dashboard/dashboard/create_health_report_test.py b/dashboard/dashboard/create_health_report_test.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6333a5fd79395d4f3bd56d77cb46267b8db6b55c |
| --- /dev/null |
| +++ b/dashboard/dashboard/create_health_report_test.py |
| @@ -0,0 +1,173 @@ |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import webapp2 |
| +import webtest |
| + |
| +from google.appengine.ext import ndb |
| +from google.appengine.api import users |
| + |
| +from dashboard import create_health_report |
| +from dashboard.common import testing_common |
| +from dashboard.common import xsrf |
| +from dashboard.models import table_config |
| +from dashboard.models import graph_data |
| + |
| + |
| +# Sample table config that contains all the required fields. |
| +_SAMPLE_TABLE_CONFIG = { |
| + 'tableName': 'my_sample_config', |
| + 'tableBots': 'ChromiumPerf/win\nChromiumPerf/linux', |
| + 'tableTests': 'my_test_suite/my_test\nmy_test_suite/my_other_test', |
| + 'tableLayout': '{ "system_health.memory_mobile/foreground/ashmem":' |
| + '["Foreground", "Ashmem"]}', |
| +} |
| + |
| +class CreateHealthReportTest(testing_common.TestCase): |
| + |
| + def setUp(self): |
| + super(CreateHealthReportTest, self).setUp() |
| + app = webapp2.WSGIApplication([( |
| + '/create_health_report', |
| + create_health_report.CreateHealthReportHandler)]) |
| + self.testapp = webtest.TestApp(app) |
| + testing_common.SetSheriffDomains(['chromium.org']) |
| + testing_common.SetIsInternalUser('internal@chromium.org', True) |
| + self.SetCurrentUser('internal@chromium.org', is_admin=True) |
| + |
| + def tearDown(self): |
| + super(CreateHealthReportTest, self).tearDown() |
| + self.UnsetCurrentUser() |
| + |
| + def _AddInternalBotsToDataStore(self): |
| + """Adds sample bot/master pairs.""" |
| + master_key = ndb.Key('Master', 'ChromiumPerf') |
| + graph_data.Bot( |
| + id='win', parent=master_key, internal_only=True).put() |
| + graph_data.Bot( |
| + id='linux', parent=master_key, internal_only=True).put() |
| + |
| + def _AddMixedBotsToDataStore(self): |
| + """Adds sample bot/master pairs.""" |
| + master_key = ndb.Key('Master', 'ChromiumPerf') |
| + graph_data.Bot( |
| + id='win', parent=master_key, internal_only=False).put() |
| + graph_data.Bot( |
| + id='linux', parent=master_key, internal_only=True).put() |
| + |
| + def _AddPublicBotsToDataStore(self): |
| + """Adds sample bot/master pairs.""" |
| + master_key = ndb.Key('Master', 'ChromiumPerf') |
| + graph_data.Bot( |
| + id='win', parent=master_key, internal_only=False).put() |
| + graph_data.Bot( |
| + id='linux', parent=master_key, internal_only=False).put() |
| + |
| + def testPost_NoXSRFToken_Returns403Error(self): |
| + self.testapp.post('/create_health_report', { |
| + }, status=403) |
|
sullivan
2017/01/13 22:48:56
Please also check nothing got added to the datasto
|
| + |
| + def testPost_GetXsrfToken(self): |
| + response = self.testapp.post('/create_health_report', { |
| + 'getToken': 'true', |
| + }) |
| + self.assertIn(xsrf.GenerateToken(users.get_current_user()), response) |
| + |
| + def testGet_ShowPage(self): |
| + response = self.testapp.get('/create_health_report') |
| + self.assertIn('create-health-report-page', response) |
| + |
| + def testPost_ExternalUserReturnsNotLoggedIn(self): |
| + self.UnsetCurrentUser() |
| + response = self.testapp.post('/create_health_report', {}) |
| + self.assertIn('User not logged in.', response) |
| + |
| + def testPost_ValidData(self): |
| + self._AddInternalBotsToDataStore() |
| + _SAMPLE_TABLE_CONFIG['xsrf_token'] = xsrf.GenerateToken( |
| + users.get_current_user()) |
| + response = self.testapp.post('/create_health_report', |
| + _SAMPLE_TABLE_CONFIG) |
| + self.assertIn('my_sample_config', response) |
| + table_entity = ndb.Key('TableConfig', 'my_sample_config').get() |
| + self.assertTrue(table_entity.internal_only) |
| + self.assertEqual('internal@chromium.org', table_entity.username) |
| + self.assertEqual( |
| + ['my_test_suite/my_test', 'my_test_suite/my_other_test'], |
| + table_entity.tests) |
| + master_key = ndb.Key('Master', 'ChromiumPerf') |
| + win_bot = graph_data.Bot( |
| + id='win', parent=master_key, internal_only=False).key |
| + linux_bot = graph_data.Bot( |
| + id='linux', parent=master_key, internal_only=False).key |
| + bots = [win_bot, linux_bot] |
| + self.assertEqual(bots, table_entity.bots) |
| + self.assertEqual( |
| + '{ "system_health.memory_mobile/foreground/ashmem":' |
| + '["Foreground", "Ashmem"]}', table_entity.table_layout) |
| + |
| + def testPost_EmptyForm(self): |
| + response = self.testapp.post('/create_health_report', { |
| + 'xsrf_token': xsrf.GenerateToken(users.get_current_user()), |
| + }) |
| + self.assertIn('Please fill out the form entirely.', response) |
| + query = table_config.TableConfig.query() |
| + table_values = query.fetch() |
| + self.assertEqual(len(table_values), 0) |
| + |
| + def testPost_TwoPostsSameNameReturnsError(self): |
| + self._AddInternalBotsToDataStore() |
| + _SAMPLE_TABLE_CONFIG['xsrf_token'] = xsrf.GenerateToken( |
| + users.get_current_user()) |
| + self.testapp.post('/create_health_report', |
| + _SAMPLE_TABLE_CONFIG) |
| + response = self.testapp.post('/create_health_report', |
| + _SAMPLE_TABLE_CONFIG) |
| + self.assertIn('my_sample_config already exists.', response) |
| + |
| + def testPost_InvalidBots(self): |
| + self._AddInternalBotsToDataStore() |
| + response = self.testapp.post('/create_health_report', { |
| + 'tableName': 'myName', |
| + 'tableBots': 'garbage/moarGarbage', |
| + 'tableTests': 'someTests', |
| + 'tableLayout': '{A layout}', |
| + 'xsrf_token': xsrf.GenerateToken(users.get_current_user()), |
| + }) |
| + self.assertIn('Invalid Master/Bot: garbage/moarGarbage', response) |
| + query = table_config.TableConfig.query() |
| + table_values = query.fetch() |
| + self.assertEqual(len(table_values), 0) |
| + |
| + def testPost_InternalOnlyAndPublicBots(self): |
| + self._AddMixedBotsToDataStore() |
| + _SAMPLE_TABLE_CONFIG['xsrf_token'] = xsrf.GenerateToken( |
| + users.get_current_user()) |
| + self.testapp.post('/create_health_report', |
| + _SAMPLE_TABLE_CONFIG) |
| + table_entity = ndb.Key('TableConfig', 'my_sample_config').get() |
| + self.assertTrue(table_entity.internal_only) |
| + |
| + def testPost_PublicOnlyBots(self): |
| + self._AddPublicBotsToDataStore() |
| + _SAMPLE_TABLE_CONFIG['xsrf_token'] = xsrf.GenerateToken( |
| + users.get_current_user()) |
| + self.testapp.post('/create_health_report', |
| + _SAMPLE_TABLE_CONFIG) |
| + table_entity = ndb.Key('TableConfig', 'my_sample_config').get() |
| + self.assertFalse(table_entity.internal_only) |
| + |
| + def testPost_ValidBotsBadLayout(self): |
| + self._AddPublicBotsToDataStore() |
| + response = self.testapp.post('/create_health_report', { |
| + 'tableName': 'myName', |
| + 'tableBots': 'ChromiumPerf/linux', |
| + 'tableTests': 'someTests', |
| + 'tableLayout': 'garbage', |
| + 'xsrf_token': xsrf.GenerateToken(users.get_current_user()), |
| + }) |
| + self.assertIn('Invalid JSON for table layout', response) |
| + query = table_config.TableConfig.query() |
| + table_values = query.fetch() |
| + self.assertEqual(len(table_values), 0) |