chromium-dashboard/pages/featurelist_test.py

195 строки
6.6 KiB
Python
Исходник Обычный вид История

# Copyright 2021 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import testing_config # Must be imported first
2022-02-10 22:07:45 +03:00
import os
import flask
import werkzeug
2022-02-10 22:07:45 +03:00
import html5lib
import settings
from internals import core_enums
from internals import core_models
from internals import user_models
from pages import featurelist
from framework import rediscache
test_app = flask.Flask(__name__,
template_folder=settings.get_flask_template_path())
# Load testdata to be used across all of the CustomTestCases
TESTDATA = testing_config.Testdata(__file__)
20210618 gae ndb to cloud ndb (#1365) * Added Cloud NDB to requirements * google-cloud-core 1.7.0 requires google-auth<2.0dev,>=1.24.0, but you'll have google-auth 1.20.1 which is incompatible. * Running DataStore Emulator on port 15606 and setting environment variable for the same * Replaced GAE NDB imports with Cloud NDB imports * Solved Error: No Context for Ramcache * Solved No NDB Context Error for Cloud NDB * Fixed Error No object QueryOptions * Removed clear_datstore flag * Fixed to_dict() method * Added explicit call to method for stashing values while editing a feature * Using Cloud NDB in testbed * Updated README with JRE instructions and setting the DJANGO_SETTTINGS_MODULE environment variable for django.setup * Added Env Variable for datastore emulator * Migrated accounts_api_test.py * Migrated approvals_api_test.py * Migrated Comments.py and Cues_api_test.py * Migrated Features.py * Migrated metricsdata_test.py * Migrated stars_api_test.py * Fixed Deprecation warning for query options * Migrated token_refresh_api_test.py * Migrated basehandlers_test.py * Migrated permissions_test.py * Migrated ramcache_test.py * Migrated secrets_test.py * Migrated xsrf_test.py * Migrated fetchmetrics_test.py * Migrated models_test.py * Migrated notifier_test.py * Migrated processes_test.py * Migrated featuredetail_test.py * Migrated featurelist_test.py * Migrated guide_test.py * Migrated intentpreview_test.py * Migrated samples_test.py * Removed some unused stubs * Set the consistency of datastore emulator to be 1 * Resetting the database emulator before running the tests * Added npm targets for starting and stopping emulator * Updated README and Developer Docs * Removed unused imports * Made NDB calls DRY
2021-06-29 05:05:04 +03:00
class TestWithFeature(testing_config.CustomTestCase):
REQUEST_PATH_FORMAT = 'subclasses fill this in'
HANDLER_CLASS = 'subclasses fill this in'
def setUp(self):
self.app_user = user_models.AppUser(email='registered@example.com')
self.app_user.put()
self.app_admin = user_models.AppUser(email='admin@example.com')
self.app_admin.is_admin = True
self.app_admin.put()
self.feature_1 = core_models.Feature(
name='feature one', summary='detailed sum', owner=['owner@example.com'],
category=1, intent_stage=core_enums.INTENT_IMPLEMENT)
self.feature_1.put()
self.feature_id = self.feature_1.key.integer_id()
self.request_path = self.REQUEST_PATH_FORMAT % {
'feature_id': self.feature_id,
}
self.handler = self.HANDLER_CLASS()
def tearDown(self):
self.feature_1.key.delete()
self.app_user.delete()
self.app_admin.delete()
rediscache.flushall()
class FeaturesJsonHandlerTest(TestWithFeature):
REQUEST_PATH_FORMAT = '/features.json'
HANDLER_CLASS = featurelist.FeaturesJsonHandler
def test_get_template_data(self):
"""User can get a JSON feed of all features."""
2021-09-14 23:23:16 +03:00
testing_config.sign_in('user@example.com', 111)
with test_app.test_request_context(self.request_path):
json_data = self.handler.get_template_data()
self.assertEqual(1, len(json_data))
self.assertEqual('feature one', json_data[0]['name'])
def test_get_template_data__unlisted_no_perms(self):
"""JSON feed does not include unlisted features for users who can't edit."""
self.feature_1.unlisted = True
self.feature_1.put()
testing_config.sign_out()
with test_app.test_request_context(self.request_path):
json_data = self.handler.get_template_data()
self.assertEqual(0, len(json_data))
testing_config.sign_in('user@example.com', 111)
with test_app.test_request_context(self.request_path):
json_data = self.handler.get_template_data()
self.assertEqual(0, len(json_data))
def test_get_template_data__unlisted_can_edit(self):
"""JSON feed includes unlisted features for site editors and admins."""
self.feature_1.unlisted = True
self.feature_1.put()
testing_config.sign_in('admin@example.com', 111)
with test_app.test_request_context(self.request_path):
json_data = self.handler.get_template_data()
self.assertEqual(1, len(json_data))
self.assertEqual('feature one', json_data[0]['name'])
class FeatureListHandlerTest(TestWithFeature):
REQUEST_PATH_FORMAT = '/features'
HANDLER_CLASS = featurelist.FeatureListHandler
def test_get_template_data(self):
"""User can get a feature list page."""
with test_app.test_request_context(self.request_path):
template_data = self.handler.get_template_data()
self.assertIn('IMPLEMENTATION_STATUSES', template_data)
2022-02-10 22:07:45 +03:00
class FeatureListTemplateTest(TestWithFeature):
HANDLER_CLASS = featurelist.FeatureListHandler
def setUp(self):
super(FeatureListTemplateTest, self).setUp()
with test_app.test_request_context(self.request_path):
self.template_data = self.handler.get_template_data(
feature_id=self.feature_id)
testing_config.sign_in('admin@example.com', 111)
2022-02-10 22:07:45 +03:00
self.template_data.update(self.handler.get_common_data())
self.template_data['nonce'] = 'fake nonce'
self.template_data['xsrf_token'] = ''
self.template_data['xsrf_token_expires'] = 0
2022-02-10 22:07:45 +03:00
template_path = self.handler.get_template_path(self.template_data)
self.full_template_path = os.path.join(template_path)
self.maxDiff = None
def tearDown(self):
testing_config.sign_out()
2022-02-10 22:07:45 +03:00
def test_html_rendering(self):
"""We can render the template with valid html."""
with test_app.app_context():
template_text = self.handler.render(
self.template_data, self.full_template_path)
2022-02-10 22:07:45 +03:00
parser = html5lib.HTMLParser(strict=True)
document = parser.parse(template_text)
# TESTDATA.make_golden(template_text, 'test_html_rendering.html')
self.assertMultiLineEqual(
TESTDATA['test_html_rendering.html'], template_text)
2022-02-10 22:07:45 +03:00
class FeatureListXMLHandlerTest(TestWithFeature):
REQUEST_PATH_FORMAT = '/features.xml'
HANDLER_CLASS = featurelist.FeatureListXMLHandler
def test_get_template_data__no_filters(self):
"""User can get an XML feed of all features."""
with test_app.test_request_context(self.request_path):
actual_text, actual_headers = self.handler.get_template_data()
self.assertTrue(actual_text.startswith('<?xml'))
self.assertIn('feature one', actual_text)
self.assertIn('detailed sum', actual_text)
self.assertIn(str(self.feature_id), actual_text)
self.assertIn('atom+xml', actual_headers['Content-Type'])
def test_get_template_data__category(self):
"""User can get an XML feed of features by category."""
request_path = self.request_path + '?category=web components'
with test_app.test_request_context(request_path):
actual_text, actual_headers = self.handler.get_template_data()
# It is an XML feed
self.assertTrue(actual_text.startswith('<?xml'))
self.assertIn('atom+xml', actual_headers['Content-Type'])
self.assertIn('Features', actual_text)
# feature_1 is in the list
self.assertIn('feature one', actual_text)
self.assertIn('detailed sum', actual_text)
self.assertIn(str(self.feature_id), actual_text)
request_path = self.request_path + '?category=css'
with test_app.test_request_context(request_path):
actual_text, actual_headers = self.handler.get_template_data()
self.assertTrue(actual_text.startswith('<?xml'))
self.assertNotIn('feature one', actual_text)