# -*- coding: utf-8 -*-
# Copyright 2022 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 permissions_api module, verifying permission rules for different user types."""

import flask

import testing_config  # Must be imported before the module under test.
from api import permissions_api
from framework import permissions

test_app = flask.Flask(__name__)


class PermissionsAPITest(testing_config.CustomTestCase):
    """Tests for PermissionsAPI."""

    def setUp(self):
        """Set up the test environment."""
        self.handler = permissions_api.PermissionsAPI()
        self.request_path = '/api/v0/currentuser/permissions'

    def test_get__anon(self):
        """Returns no user object if not signed in"""  # noqa: D415
        testing_config.sign_out()
        with test_app.test_request_context(self.request_path):
            actual = self.handler.do_get()
        self.assertEqual({'user': None}, actual)

    def test_get__non_googler(self):
        """Non-googlers have no permissions by default"""  # noqa: D415
        testing_config.sign_in('one@example.com', 12345)
        with test_app.test_request_context(self.request_path):
            actual = self.handler.do_get()
        expected = {
            'user': {
                'can_create_feature': False,
                'approvable_gate_types': [],
                'can_comment': False,
                'can_edit_all': False,
                'can_review_release_notes': False,
                'is_admin': False,
                'email': 'one@example.com',
                'editable_features': [],
            }
        }
        self.assertEqual(expected, actual)

    def test_get__googler(self):
        """Googlers have default permissions to create features."""
        testing_config.sign_in('one@google.com', 67890)
        with test_app.test_request_context(self.request_path):
            actual = self.handler.do_get()
        expected = {
            'user': {
                'can_create_feature': True,
                'approvable_gate_types': [],
                'can_comment': True,
                'can_edit_all': False,
                'can_review_release_notes': False,
                'is_admin': False,
                'email': 'one@google.com',
                'editable_features': [],
            }
        }
        self.assertEqual(expected, actual)

    def test_get__googler_paired_user(self):
        """Googlers have default permissions to create features."""
        testing_config.sign_in('one@google.com', 67890)
        with test_app.test_request_context(
            self.request_path + '?returnPairedUser'
        ):
            actual = self.handler.do_get()
        expected = {
            'user': {
                'can_create_feature': True,
                'approvable_gate_types': [],
                'can_comment': True,
                'can_edit_all': False,
                'can_review_release_notes': False,
                'is_admin': False,
                'email': 'one@chromium.org',
                'editable_features': [],
            }
        }
        self.assertEqual(expected, actual)

    def test_get__registered_org_user(self):
        """Users from registered orgs have default permissions to create features."""
        for domain in permissions.REGISTERED_USER_ORGANIZATIONS:
            email = f'one{domain}'
            testing_config.sign_in(email, 67890)
            with test_app.test_request_context(self.request_path):
                actual = self.handler.do_get()
            expected = {
                'user': {
                    'can_create_feature': True,
                    'approvable_gate_types': [],
                    'can_comment': True,
                    'can_edit_all': False,
                    'can_review_release_notes': False,
                    'is_admin': False,
                    'email': email,
                    'editable_features': [],
                }
            }
            self.assertEqual(expected, actual)
