<commit_before># -*- encoding: utf-8 -*-

import mock

from awscfncli import cfn
from click.testing import CliRunner

from .boto_mock import mock_config_with_templateurl, mock_cfn_client, mock_cfn

__author__ = 'ray'
__date__ = '1/14/17'


def test_cfn_stack_deploy(
        mock_config_with_templateurl, mock_cfn, mock_cfn_client):
    with mock.patch('boto3.client') as mock_client, \
            mock.patch('boto3.resource') as mock_resource:
        # Mocks
        mock_resource.return_value = mock_cfn
        mock_client.return_value = mock_cfn_client

        runner = CliRunner()
        runner.invoke(cfn, [
            'stack', 'deploy', mock_config_with_templateurl, '--on-failure',
            'ROLLBACK', '--canned-policy', 'DENY_DELETE'])

        mock_resource.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn.create_stack.assert_called_once()

        mock_client.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn_client.get_waiter.assert_called_once()


def test_cfn_stack_describe(mock_config_with_templateurl, mock_cfn):
    with mock.patch('boto3.resource') as mock_resource:
        # Mocks
        mock_resource.return_value = mock_cfn

        runner = CliRunner()
        runner.invoke(cfn,
                      ['stack', 'describe', mock_config_with_templateurl, '--stack-resources'])

        mock_resource.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn.Stack.assert_called_once_with(
            'ExampleStack'
        )


def test_cfn_stack_update(mock_config_with_templateurl, mock_cfn, mock_cfn_client):
    with mock.patch('boto3.client') as mock_client, \
            mock.patch('boto3.resource') as mock_resource:
        # Mocks
        mock_resource.return_value = mock_cfn
        mock_client.return_value = mock_cfn_client

        runner = CliRunner()
        runner.invoke(cfn, ['stack', 'update', mock_config_with_templateurl,
                            '--use-previous-template',
                            '--canned-policy', 'DENY_DELETE',
                            '--override-policy', 'ALLOW_ALL'])

        mock_resource.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn.Stack.assert_called_once_with(
            'ExampleStack'
        )

        mock_cfn.Stack.return_value.update.assert_called_once_with(
            StackName='ExampleStack',
            UsePreviousTemplate=True,
            StackPolicyBody='''
{
  "Statement" : [
    {
      "Effect" : "Deny",
      "Action" : "Update:Delete",
      "Principal": "*",
      "Resource" : "*"
    }
  ]
}<commit_msg>Fix unittest error.<commit_after># -*- encoding: utf-8 -*-

import mock

from awscfncli import cfn
from click.testing import CliRunner

from .boto_mock import mock_config_with_templateurl, mock_cfn_client, mock_cfn

__author__ = 'ray'
__date__ = '1/14/17'


def test_cfn_stack_deploy(
        mock_config_with_templateurl, mock_cfn, mock_cfn_client):
    with mock.patch('boto3.client') as mock_client, \
            mock.patch('boto3.resource') as mock_resource:
        # Mocks
        mock_resource.return_value = mock_cfn
        mock_client.return_value = mock_cfn_client

        runner = CliRunner()
        runner.invoke(cfn, [
            'stack', 'deploy', mock_config_with_templateurl, '--on-failure',
            'ROLLBACK', '--canned-policy', 'DENY_DELETE'])

        mock_resource.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn.create_stack.assert_called_once()

        mock_client.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn_client.get_waiter.assert_called_once()


def test_cfn_stack_describe(mock_config_with_templateurl, mock_cfn):
    with mock.patch('boto3.resource') as mock_resource:
        # Mocks
        mock_resource.return_value = mock_cfn

        runner = CliRunner()
        runner.invoke(cfn,
                      ['stack', 'describe', mock_config_with_templateurl, '--stack-resources'])

        mock_resource.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn.Stack.assert_called_once_with(
            'ExampleStack'
        )


def test_cfn_stack_update(mock_config_with_templateurl, mock_cfn, mock_cfn_client):
    with mock.patch('boto3.client') as mock_client, \
            mock.patch('boto3.resource') as mock_resource:
        # Mocks
        mock_resource.return_value = mock_cfn
        mock_client.return_value = mock_cfn_client

        runner = CliRunner()
        runner.invoke(cfn, ['stack', 'update', mock_config_with_templateurl,
                            '--use-previous-template',
                            '--canned-policy', 'DENY_DELETE',
                            '--override-policy', 'ALLOW_ALL'])

        mock_resource.assert_called_once_with(
            'cloudformation', region_name='us-east-1')
        mock_cfn.Stack.assert_called_once_with(
            'ExampleStack'
        )

        mock_cfn.Stack.return_value.update.assert_called_once_with(
            StackName='ExampleStack',
            UsePreviousTemplate=True,
            StackPolicyBody='''
{
  "Statement" : [
    {
      "Effect" : "Allow",
      "NotAction" : "Update:Delete",
      "Principal": "*",
      "Resource" : "*"
    }
  ]
}
{'fraction_non_alphanumeric': 0.10904744391320338, 'fraction_numerical': 0.005516734093416698, 'mean_word_length': 2.5595549738219896, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2202453', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1692, 'n_words': 248}