# Copyright 2020 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.


"""Tests for the users admin page handler."""

import flask
import html5lib
from google.cloud import ndb  # type: ignore

import settings
import testing_config  # Must be imported first
from internals import user_models
from pages import users

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__)


class UsersListTemplateTest(testing_config.CustomTestCase):
    """Tests for the users list template rendering."""

    def setUp(self):
        """Sets up the test user and handler for the users list."""
        self.handler = users.UserListHandler()

        self.app_admin = user_models.AppUser(email='admin@example.com')
        self.app_admin.is_admin = True
        self.app_admin.key = ndb.Key('AppUser', 1)
        self.app_admin.put()
        testing_config.sign_in('admin@example.com', 123567890)

        with test_app.test_request_context('/request_path'):
            self.template_data = self.handler.get_template_data()
            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
        self.full_template_path = self.handler.get_template_path(
            self.template_data
        )
        self.maxDiff = None

    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
            )
        parser = html5lib.HTMLParser(strict=True)
        document = parser.parse(template_text)  # noqa: F841
        # TESTDATA.make_golden(template_text, 'test_html_rendering.html')
        self.assertMultiLineEqual(
            TESTDATA['test_html_rendering.html'], template_text
        )
