# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.

from argparse import Namespace

from awscli.customizations.codedeploy.register import Register
from awscli.customizations.codedeploy.utils import MAX_TAGS_PER_INSTANCE
from awscli.testutils import mock, unittest


class TestRegister(unittest.TestCase):
    def setUp(self):
        self.path = '/AWS/CodeDeploy/'
        self.instance_name = 'instance-name'
        self.tags = [{'Key': 'k1', 'Value': 'v1'}]
        self.iam_user_arn = 'arn:aws:iam::012345678912:user/instance-name'
        self.access_key_id = 'ACCESSKEYID'
        self.secret_access_key = 'SECRETACCESSKEY'
        self.region = 'us-east-1'
        self.policy_name = 'codedeploy-agent'
        self.policy_document = (
            '{\n'
            '    "Version": "2012-10-17",\n'
            '    "Statement": [ {\n'
            '        "Action": [ "s3:Get*", "s3:List*" ],\n'
            '        "Effect": "Allow",\n'
            '        "Resource": "*"\n'
            '    } ]\n'
            '}'
        )
        self.config_file = 'codedeploy.onpremises.yml'
        self.endpoint_url = 'https://codedeploy.aws.amazon.com'

        self.args = Namespace()
        self.args.instance_name = self.instance_name
        self.args.tags = None
        self.args.iam_user_arn = None

        self.globals = Namespace()
        self.globals.region = self.region
        self.globals.endpoint_url = self.endpoint_url
        self.globals.verify_ssl = False

        self.os_open_patcher = mock.patch(
            'awscli.customizations.codedeploy.register.os.open', return_value=3
        )
        self.os_open = self.os_open_patcher.start()

        self.os_fdopen_patcher = mock.patch(
            'awscli.customizations.codedeploy.register.os.fdopen',
            mock.mock_open(),
        )
        self.os_fdopen = self.os_fdopen_patcher.start()

        self.os_chmod_patcher = mock.patch(
            'awscli.customizations.codedeploy.register.os.chmod'
        )
        self.os_chmod = self.os_chmod_patcher.start()

        self.codedeploy = mock.MagicMock()

        self.iam = mock.MagicMock()
        self.iam.create_user.return_value = {
            'User': {'Arn': self.iam_user_arn}
        }
        self.iam.create_access_key.return_value = {
            'AccessKey': {
                'AccessKeyId': self.access_key_id,
                'SecretAccessKey': self.secret_access_key,
            }
        }

        self.session = mock.MagicMock()
        self.session.create_client.side_effect = [self.codedeploy, self.iam]
        self.register = Register(self.session)

    def tearDown(self):
        self.os_open_patcher.stop()
        self.os_fdopen_patcher.stop()
        self.os_chmod_patcher.stop()

    def test_register_throws_on_invalid_region(self):
        self.globals.region = None
        self.session.get_config_variable.return_value = None
        with self.assertRaisesRegex(RuntimeError, 'Region not specified.'):
            self.register._run_main(self.args, self.globals)

    def test_register_throws_on_invalid_instance_name(self):
        self.args.instance_name = 'invalid%@^&%#&'
        with self.assertRaisesRegex(
            ValueError, 'Instance name contains invalid characters.'
        ):
            self.register._run_main(self.args, self.globals)

    def test_register_throws_on_invalid_tags(self):
        self.args.tags = [
            {'Key': 'k' + str(x), 'Value': 'v' + str(x)} for x in range(11)
        ]
        with self.assertRaisesRegex(
            ValueError,
            f'Instances can only have a maximum of {MAX_TAGS_PER_INSTANCE} tags.',
        ):
            self.register._run_main(self.args, self.globals)

    def test_register_throws_on_invalid_iam_user_arn(self):
        self.args.iam_user_arn = 'invalid%@^&%#&'
        with self.assertRaisesRegex(ValueError, 'Invalid IAM user ARN.'):
            self.register._run_main(self.args, self.globals)

    def test_register_creates_clients(self):
        self.register._run_main(self.args, self.globals)
        self.session.create_client.assert_has_calls(
            [
                mock.call(
                    'codedeploy',
                    region_name=self.region,
                    endpoint_url=self.endpoint_url,
                    verify=self.globals.verify_ssl,
                ),
                mock.call('iam', region_name=self.region),
            ]
        )

    def test_register_with_no_iam_user_arn(self):
        self.args.iam_user_arn = None
        self.register._run_main(self.args, self.globals)
        self.register.iam.create_user.assert_called_with(
            Path=self.path, UserName=self.instance_name
        )
        self.assertIn('iam_user_arn', self.args)
        self.assertEqual(self.iam_user_arn, self.args.iam_user_arn)
        self.register.iam.create_access_key.assert_called_with(
            UserName=self.instance_name
        )
        self.assertIn('access_key_id', self.args)
        self.assertEqual(self.access_key_id, self.args.access_key_id)
        self.assertIn('secret_access_key', self.args)
        self.assertEqual(self.secret_access_key, self.args.secret_access_key)
        self.register.iam.put_user_policy.assert_called_with(
            UserName=self.instance_name,
            PolicyName=self.policy_name,
            PolicyDocument=self.policy_document,
        )
        self.assertIn('policy_name', self.args)
        self.assertEqual(self.policy_name, self.args.policy_name)
        self.assertIn('policy_document', self.args)
        self.assertEqual(self.policy_document, self.args.policy_document)
        self.os_open.assert_called_with(
            self.config_file,
            mock.ANY,
            0o600,
        )
        self.os_fdopen.assert_called_with(3, 'w')
        self.os_fdopen().write.assert_called_with(
            '---\n'
            f'region: {self.region}\n'
            f'iam_user_arn: {self.iam_user_arn}\n'
            f'aws_access_key_id: {self.access_key_id}\n'
            f'aws_secret_access_key: {self.secret_access_key}\n'
        )
        self.register.codedeploy.register_on_premises_instance.assert_called_with(
            instanceName=self.instance_name, iamUserArn=self.iam_user_arn
        )

    def test_register_with_iam_user_arn(self):
        self.args.iam_user_arn = self.iam_user_arn
        self.register._run_main(self.args, self.globals)
        self.assertFalse(self.register.iam.create_user.called)
        self.assertFalse(self.register.iam.create_access_key.called)
        self.assertFalse(self.register.iam.put_user_policy.called)
        self.assertFalse(self.os_open.called)
        self.register.codedeploy.register_on_premises_instance.assert_called_with(
            instanceName=self.instance_name, iamUserArn=self.iam_user_arn
        )

    def test_register_with_no_tags(self):
        self.args.tags = None
        self.register._run_main(self.args, self.globals)
        self.register.codedeploy.register_on_premises_instance.assert_called_with(
            instanceName=self.instance_name, iamUserArn=self.iam_user_arn
        )
        self.assertFalse(
            self.register.codedeploy.add_tags_to_on_premises_instances.called
        )

    def test_register_with_tags(self):
        self.args.tags = self.tags
        self.register._run_main(self.args, self.globals)
        self.register.codedeploy.register_on_premises_instance.assert_called_with(
            instanceName=self.instance_name, iamUserArn=self.iam_user_arn
        )
        self.register.codedeploy.add_tags_to_on_premises_instances.assert_called_with(
            tags=self.tags, instanceNames=[self.instance_name]
        )

    def test_create_config_raises_runtime_error_on_open_failure(self):
        self.args.iam_user_arn = None
        self.os_open.side_effect = OSError('permission denied')
        with self.assertRaisesRegex(
            RuntimeError, 'Failed to create config file'
        ):
            self.register._create_config(self.args)

    def test_create_config_raises_runtime_error_on_chmod_failure(self):
        self.args.iam_user_arn = None
        self.os_chmod.side_effect = OSError('permission denied')
        with self.assertRaisesRegex(
            RuntimeError, 'Failed to create config file'
        ):
            self.register._create_config(self.args)

    def test_create_config_uses_restricted_permissions(self):
        self.args.iam_user_arn = None
        self.register._run_main(self.args, self.globals)
        self.os_open.assert_called_with(
            self.config_file,
            mock.ANY,
            0o600,
        )

    def test_create_config_chmods_existing_file(self):
        self.args.iam_user_arn = None
        self.register._run_main(self.args, self.globals)
        self.os_chmod.assert_called_with(self.config_file, 0o600)


if __name__ == "__main__":
    unittest.main()
