#   Foremast - Pipeline Tooling
#
#   Copyright 2016 Gogo, 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.
"""Test IAM Policies for correctness."""
import json
from unittest import mock

from foremast.iam.construct_policy import construct_policy


@mock.patch('foremast.utils.credentials.API_URL', 'http://test.com')
@mock.patch('foremast.utils.credentials.requests.get')
def test_iam_construct_policy(requests_get):
    """Check general assemblage."""
    settings = {}

    policy_json = construct_policy(pipeline_settings=settings)
    # checking empty policy
    assert policy_json is None

    settings = {'services': {'s3': True}}
    policy_json = construct_policy(app='unicornforrest', env='stage', group='forrest', pipeline_settings=settings)

    # checking s3 policy
    assert type(json.loads(policy_json)) == dict

    # TODO: Test other services besides S3
    settings.update({'services': {'dynamodb': ['coreforrest', 'edgeforrest', 'attendantdevops']}})
    policy_json = construct_policy(pipeline_settings=settings)
    policy = json.loads(policy_json)

@@ -1583,8 +1583,725 @@
 y_json)%0A
+%0A%0A@mock.patch('foremast.utils.credentials.API_URL', 'http://test.com')%0A@mock.patch('foremast.utils.credentials.requests.get')%0Adef test_construct_lambda(requests_get):%0A    %22%22%22Check Lambda Policy.%22%22%22%0A    policy_json = construct_policy(%0A        app='unicornforrest', env='dev', group='forrest', pipeline_settings=%7B%7D, deployment_type='lambda')%0A    policy = json.loads(policy_json)%0A%0A    statements = list(statement for statement in policy%5B'Statement'%5D if statement%5B'Sid'%5D == 'LambdaCloudWatchLogs')%0A    assert len(statements) == 1%0A%0A    statement = statements%5B0%5D%0A    assert statement%5B'Effect'%5D == 'Allow'%0A    assert len(statement%5B'Action'%5D) == 3%0A    assert all(action.startswith('logs:') for action in statement%5B'Action'%5D)%0A

Check CloudWatch Logs acces for Lambda