1# Copyright 2018 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import print_function 6from __future__ import division 7from __future__ import absolute_import 8 9import datetime 10import unittest 11 12from google.appengine.ext import ndb 13 14from dashboard.common import descriptor 15from dashboard.common import report_query 16from dashboard.common import stored_object 17from dashboard.common import testing_common 18from dashboard.models import graph_data 19from dashboard.models import report_template 20 21 22@report_template.Static( 23 internal_only=False, 24 template_id=584630894, 25 name='Test:External', 26 modified=datetime.datetime.now()) 27def _External(revisions): 28 template = { 29 'rows': [], 30 'statistics': ['avg'], 31 'url': 'http://exter.nal', 32 } 33 return report_query.ReportQuery(template, revisions) 34 35 36class ReportTemplateTest(testing_common.TestCase): 37 38 def setUp(self): 39 super(ReportTemplateTest, self).setUp() 40 stored_object.Set(descriptor.PARTIAL_TEST_SUITES_KEY, []) 41 stored_object.Set(descriptor.COMPOSITE_TEST_SUITES_KEY, []) 42 stored_object.Set(descriptor.GROUPABLE_TEST_SUITE_PREFIXES_KEY, []) 43 descriptor.Descriptor.ResetMemoizedConfigurationForTesting() 44 45 report_template.ReportTemplate( 46 id='ex-id', 47 name='external', 48 owners=[testing_common.EXTERNAL_USER.email()], 49 template={ 50 'rows': [], 51 'statistics': ['avg'] 52 }).put() 53 report_template.ReportTemplate( 54 internal_only=True, 55 name='internal', 56 id='in-id', 57 owners=[testing_common.INTERNAL_USER.email()], 58 template={ 59 'rows': [], 60 'statistics': ['avg'] 61 }).put() 62 63 def testInternal_PutTemplate(self): 64 self.SetCurrentUser(testing_common.INTERNAL_USER.email()) 65 66 with self.assertRaises(ValueError): 67 report_template.PutTemplate(None, 'Test:External', 68 [testing_common.INTERNAL_USER.email()], 69 {'rows': []}) 70 71 with self.assertRaises(ValueError): 72 report_template.PutTemplate('in-id', 'Test:External', 73 [testing_common.INTERNAL_USER.email()], 74 {'rows': []}) 75 76 with self.assertRaises(ValueError): 77 report_template.PutTemplate('invalid', 'bad', 78 [testing_common.INTERNAL_USER.email()], 79 {'rows': []}) 80 81 with self.assertRaises(ValueError): 82 report_template.PutTemplate('ex-id', 'bad', 83 [testing_common.INTERNAL_USER.email()], 84 {'rows': []}) 85 self.assertEqual('external', ndb.Key('ReportTemplate', 'ex-id').get().name) 86 87 with self.assertRaises(ValueError): 88 report_template.PutTemplate(584630894, 'bad', 89 [testing_common.INTERNAL_USER.email()], 90 {'rows': []}) 91 92 report_template.PutTemplate('in-id', 'foo', 93 [testing_common.INTERNAL_USER.email()], 94 {'rows': []}) 95 self.assertEqual('foo', ndb.Key('ReportTemplate', 'in-id').get().name) 96 97 def testPutTemplate_InternalOnly(self): 98 self.SetCurrentUser(testing_common.INTERNAL_USER.email()) 99 test = graph_data.TestMetadata( 100 has_rows=True, 101 internal_only=True, 102 id='master/internalbot/internalsuite/measure', 103 units='units') 104 test.put() 105 report_template.PutTemplate( 106 None, 'foo', [testing_common.INTERNAL_USER.email()], { 107 'rows': [{ 108 'testSuites': ['internalsuite'], 109 'bots': ['master:internalbot'], 110 'measurement': 'measure', 111 'testCases': [], 112 },], 113 }) 114 template = report_template.ReportTemplate.query( 115 report_template.ReportTemplate.name == 'foo').get() 116 self.assertTrue(template.internal_only) 117 118 def testPutTemplate_External(self): 119 self.SetCurrentUser(testing_common.EXTERNAL_USER.email()) 120 report_template.PutTemplate( 121 None, 'foo', [testing_common.EXTERNAL_USER.email()], { 122 'rows': [{ 123 'testSuites': ['externalsuite'], 124 'bots': ['master:externalbot'], 125 'measurement': 'measure', 126 'testCases': [], 127 },], 128 }) 129 template = report_template.ReportTemplate.query( 130 report_template.ReportTemplate.name == 'foo').get() 131 self.assertFalse(template.internal_only) 132 133 def testAnonymous_PutTemplate(self): 134 self.SetCurrentUser('') 135 with self.assertRaises(ValueError): 136 report_template.PutTemplate('ex-id', 'foo', 137 [testing_common.EXTERNAL_USER.email()], {}) 138 self.assertEqual('external', ndb.Key('ReportTemplate', 'ex-id').get().name) 139 140 def testInternal_GetReport(self): 141 self.SetCurrentUser(testing_common.INTERNAL_USER.email()) 142 report = report_template.GetReport('in-id', [10, 20]) 143 self.assertTrue(report['internal']) 144 self.assertEqual(0, len(report['report']['rows'])) 145 self.assertEqual('internal', report['name']) 146 self.assertTrue(report['editable']) 147 self.assertEqual([testing_common.INTERNAL_USER.email()], report['owners']) 148 149 def testAnonymous_GetReport(self): 150 self.SetCurrentUser('') 151 self.assertEqual(None, report_template.GetReport('in-id', [10, 20])) 152 report = report_template.GetReport('ex-id', [10, 20]) 153 self.assertFalse(report['internal']) 154 self.assertEqual(0, len(report['report']['rows'])) 155 self.assertEqual('external', report['name']) 156 self.assertFalse(report['editable']) 157 self.assertEqual([testing_common.EXTERNAL_USER.email()], report['owners']) 158 159 def testGetReport_Static(self): 160 self.SetCurrentUser(testing_common.EXTERNAL_USER.email()) 161 report = report_template.GetReport(584630894, ['latest']) 162 self.assertFalse(report['internal']) 163 self.assertEqual('http://exter.nal', report['report']['url']) 164 self.assertEqual('Test:External', report['name']) 165 self.assertFalse(report['editable']) 166 self.assertNotIn('owners', report) 167 168 169if __name__ == '__main__': 170 unittest.main() 171