def test_simple_rotation_both_active(config, mock_iam_two_keys_both_active):
    """
    Simple use case of rotating one profile.
    """
    fake_creds_fp = mock_open(read_data=credential_section("default", "asdf"))
    with patch("builtins.open", fake_creds_fp):
        rotator.IAMKeyRotator(config).main()
    writes = "".join(
        [call[0][0] for call in fake_creds_fp().write.call_args_list])
    assert credential_section("default", "asdf2") in writes
def test_simple_rotation_one_inactive(config, mock_iam_two_keys_one_inactive):
    """
    Rotating credentials with one active and one inactive key. When run regularly, this
    is the most common use case.
    """
    fake_creds_fp = mock_open(read_data=credential_section("default", "asdf"))
    with patch("builtins.open", fake_creds_fp):
        rotator.IAMKeyRotator(config).main()
    writes = "".join(
        [call[0][0] for call in fake_creds_fp().write.call_args_list])
    assert credential_section("default", "asdf2") in writes
def test_bad_credentials_nothing_happens(config, mock_iam_one_key):
    """
    If the credentials file doesn't contain both keys, nothing should happen.
    """
    creds_data = credential_section("default", "asdf")
    creds_data = creds_data.replace("aws_secret_access_key", "bad")
    fake_creds_fp = mock_open(read_data=creds_data)
    with patch("builtins.open", fake_creds_fp):
        rotator.IAMKeyRotator(config).main()
    total_calls = (mock_iam_one_key.create_access_key.call_count +
                   mock_iam_one_key.update_access_key.call_count +
                   mock_iam_one_key.delete_access_key.call_count)
    assert total_calls == 0
def test_two_profiles(config, mock_iam_one_key):
    """
    Rotating two different profiles.
    """
    creds_data = "{}\n\n{}".format(credential_section("default", "asdf"),
                                   credential_section("nondefault", "asdf"))
    fake_creds_fp = mock_open(read_data=creds_data)
    with patch("builtins.open", fake_creds_fp):
        rotator.IAMKeyRotator(config).main()
    writes = "".join(
        [call[0][0] for call in fake_creds_fp().write.call_args_list])
    write_one = credential_section("default", "asdf2") in writes
    write_two = credential_section("nondefault", "asdf2") in writes
    assert write_one and write_two