# Copyright 2016-2017 Capital One Services, LLC
#
# 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 json
import logging
import os
import shutil
import tempfile

from common import BaseTest


class HandleTest(BaseTest):

    def test_handler(self):
        level = logging.root.level
        botocore_level = logging.getLogger('botocore').level

        self.run_dir = tempfile.mkdtemp()
        cur_dir = os.path.abspath(os.getcwd())
        os.chdir(self.run_dir)

        def cleanup():
            os.chdir(cur_dir)
            shutil.rmtree(self.run_dir)
            logging.root.setLevel(level)
            logging.getLogger('botocore').setLevel(botocore_level)

        self.addCleanup(cleanup)
        self.change_environment(C7N_OUTPUT_DIR=self.run_dir)

        from c7n import handler

        with open(os.path.join(self.run_dir, 'config.json'), 'w') as fh:
            json.dump({'policies': []}, fh)

        self.assertEqual(
            handler.dispatch_event(
                {'detail': {'errorCode': '404'}}, None),
            None)
        self.assertEqual(
            handler.dispatch_event({'detail': {}}, None), True)

        config = handler.Config.empty()
        self.assertEqual(config.assume_role, None)
        try:
            config.foobar
        except AttributeError:
            pass
        else:
            self.fail("should have raised an error")
