# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat, 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 yaml
import os
from unittest import TestCase
from webtest import TestApp
import tempfile

from pecan import load_app

from managesf.tests import dummy_conf
from managesf import policy
from managesf.policies import base


""""Test the policy engine"""


class TestPolicyEngine(TestCase):
    def setUp(self):
        c = dummy_conf()
        self.config = {'services': c.services,
                       'gerrit': c.gerrit,
                       'app': c.app,
                       'admin': c.admin,
                       'sqlalchemy': c.sqlalchemy,
                       'auth': c.auth,
                       'htpasswd': c.htpasswd,
                       'managesf': c.managesf,
                       'storyboard': c.storyboard,
                       'mysql': c.mysql,
                       'policy': c.policy, }
        self.app = TestApp(load_app(self.config))

    def tearDown(self):
        # Remove the sqlite db
        os.unlink(self.config['sqlalchemy']['url'][len('sqlite:///'):])

    def test_config_policies(self):
        """Test the default config endpoint policies"""
        credentials = {}
        target = {}
        self.assertFalse(policy.authorize('managesf.config:get',
                                          target, credentials))
        credentials = {'username': 'BirdPerson'}
        self.assertTrue(policy.authorize('managesf.config:get',
                                         target, credentials))

    def test_hooks_policies(self):
        """Test the default hooks endpoint policies"""
        credentials = {}
        target = {}
        self.assertFalse(policy.authorize('managesf.hooks:trigger',
                                          target, credentials))
        credentials = {'username': 'RickSanchez'}
        self.assertFalse(policy.authorize('managesf.hooks:trigger',
                                          target, credentials))
        credentials = {'username': 'admin'}
        self.assertTrue(policy.authorize('managesf.hooks:trigger',
                                         target, credentials))
        credentials = {'username': 'SF_SERVICE_USER'}
        self.assertTrue(policy.authorize('managesf.hooks:trigger',
                                         target, credentials))

    def test_htpasswd_policies(self):
        """Test the default htpasswd endpoint policies"""
        credentials = {}
        target = {}
        self.assertFalse(policy.authorize('managesf.htpasswd:get',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.htpasswd:create_update',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.htpasswd:delete',
                                          target, credentials))
        credentials = {'username': 'RickSanchez'}
        self.assertTrue(policy.authorize('managesf.htpasswd:get',
                                         target, credentials))
        self.assertTrue(policy.authorize('managesf.htpasswd:create_update',
                                         target, credentials))
        self.assertTrue(policy.authorize('managesf.htpasswd:delete',
                                         target, credentials))

    def test_localuser_policies(self):
        """Test the default localuser endpoint policies"""
        credentials = {}
        target = {}
        self.assertFalse(policy.authorize('managesf.localuser:get',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.localuser:create_update',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.localuser:delete',
                                          target, credentials))
        self.assertTrue(policy.authorize('managesf.localuser:bind',
                                         target, credentials))
        credentials = {'username': 'RickSanchez',
                       'groups': []}
        self.assertTrue(policy.authorize('managesf.localuser:get',
                                         target, credentials))
        self.assertFalse(policy.authorize('managesf.localuser:create_update',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.localuser:delete',
                                          target, credentials))
        self.assertTrue(policy.authorize('managesf.localuser:bind',
                                         target, credentials))
        target = {'username': 'RickSanchez'}
        self.assertTrue(policy.authorize('managesf.localuser:create_update',
                                         target, credentials))
        self.assertTrue(policy.authorize('managesf.localuser:delete',
                                         target, credentials))
        target = {'username': 'Morty'}
        self.assertTrue(policy.authorize('managesf.localuser:get',
                                         target, credentials))
        self.assertFalse(policy.authorize('managesf.localuser:create_update',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.localuser:delete',
                                          target, credentials))
        credentials['username'] = 'Morty'
        self.assertTrue(policy.authorize('managesf.localuser:create_update',
                                         target, credentials))
        self.assertTrue(policy.authorize('managesf.localuser:delete',
                                         target, credentials))

    def test_backup_policies(self):
        """Test the default backup endpoint policies"""
        credentials = {}
        target = {}
        self.assertFalse(policy.authorize('managesf.backup:create',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.backup:get',
                                          target, credentials))
        credentials = {'username': 'RickSanchez'}
        self.assertFalse(policy.authorize('managesf.backup:create',
                                          target, credentials))
        self.assertFalse(policy.authorize('managesf.backup:get',