def test_credentials_file_helper_not_found_no_default(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file = os.path.join(str(tmpdir), 'fake_aws_config')
    tmpdir.join('fake_aws_config').write('')

    credentials = watchdog.credentials_file_helper(fake_file)

    assert credentials is None
    assert 'No [default] section found in config file' in [
        rec.message for rec in caplog.records
    ][0]
def test_credentials_file_helper_not_found(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file = os.path.join(str(tmpdir), 'fake_aws_config')
    tmpdir.join('fake_aws_config').write('')

    credentials = watchdog.credentials_file_helper(fake_file, AWSPROFILE)

    assert credentials['AccessKeyId'] is None
    assert credentials['SecretAccessKey'] is None
    assert credentials['Token'] is None
    assert 'No [%s] section found in config file' % AWSPROFILE in [rec.message for rec in caplog.records][0]
def test_credentials_file_helper_not_found(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file = os.path.join(str(tmpdir), "fake_aws_config")
    tmpdir.join("fake_aws_config").write("")

    credentials = watchdog.credentials_file_helper(fake_file, AWSPROFILE)

    assert credentials["AccessKeyId"] is None
    assert credentials["SecretAccessKey"] is None
    assert credentials["Token"] is None
    assert ("No [%s] section found in config file" % AWSPROFILE
            in [rec.message for rec in caplog.records][0])
def test_credentials_file_helper_found_with_token_default(tmpdir):
    fake_file, config = _config_helper(tmpdir)

    config.set(DEFAULT_PROFILE, ACCESS_KEY_ID_KEY, ACCESS_KEY_ID_VAL)
    config.set(DEFAULT_PROFILE, SECRET_ACCESS_KEY_KEY, SECRET_ACCESS_KEY_VAL)
    config.set(DEFAULT_PROFILE, SESSION_TOKEN_KEY, SESSION_TOKEN_VAL)
    with open(fake_file, 'w') as f:
        config.write(f)

    credentials = watchdog.credentials_file_helper(fake_file)

    assert credentials['AccessKeyId'] == ACCESS_KEY_ID_VAL
    assert credentials['SecretAccessKey'] == SECRET_ACCESS_KEY_VAL
    assert credentials['Token'] == SESSION_TOKEN_VAL
def test_credentials_file_helper_not_found_with_awsprofile(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file, config = _config_helper(tmpdir, add_test_profile=True)

    config.set(DEFAULT_PROFILE, SECRET_ACCESS_KEY_KEY, WRONG_SECRET_ACCESS_KEY_VAL)
    config.set(AWSPROFILE, SECRET_ACCESS_KEY_KEY, SECRET_ACCESS_KEY_VAL)
    with open(fake_file, 'w') as f:
        config.write(f)

    credentials = watchdog.credentials_file_helper(fake_file, awsprofile=AWSPROFILE)

    assert credentials['AccessKeyId'] is None
    assert credentials['SecretAccessKey'] is None
    assert credentials['Token'] is None
    assert 'aws_access_key_id or aws_secret_access_key not found' in [rec.message for rec in caplog.records][0]
def test_credentials_file_helper_not_found_with_default(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file, config = _config_helper(tmpdir)

    config.set(DEFAULT_PROFILE, SECRET_ACCESS_KEY_KEY,
               WRONG_SECRET_ACCESS_KEY_VAL)
    with open(fake_file, 'w') as f:
        config.write(f)

    credentials = watchdog.credentials_file_helper(fake_file)

    assert credentials is None
    assert 'No [default] section found in config file' in [
        rec.message for rec in caplog.records
    ][0]
def test_credentials_file_helper_found_without_token_default(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file, config = _config_helper(tmpdir)

    config.set(DEFAULT_PROFILE, ACCESS_KEY_ID_KEY, ACCESS_KEY_ID_VAL)
    config.set(DEFAULT_PROFILE, SECRET_ACCESS_KEY_KEY, SECRET_ACCESS_KEY_VAL)
    with open(fake_file, 'w') as f:
        config.write(f)

    credentials = watchdog.credentials_file_helper(fake_file)

    assert credentials['AccessKeyId'] == ACCESS_KEY_ID_VAL
    assert credentials['SecretAccessKey'] == SECRET_ACCESS_KEY_VAL
    assert credentials['Token'] is None
    assert 'aws_session_token' in [rec.message for rec in caplog.records][0]
def test_credentials_file_helper_found_with_token(tmpdir):
    fake_file = get_fake_aws_config_file(tmpdir)
    config = get_fake_config(add_test_profile=True)

    config.set(DEFAULT_PROFILE, ACCESS_KEY_ID_KEY, WRONG_ACCESS_KEY_ID_VAL)
    config.set(DEFAULT_PROFILE, SECRET_ACCESS_KEY_KEY,
               WRONG_SECRET_ACCESS_KEY_VAL)
    config.set(DEFAULT_PROFILE, SESSION_TOKEN_KEY, WRONG_SESSION_TOKEN_VAL)
    config.set(AWSPROFILE, ACCESS_KEY_ID_KEY, ACCESS_KEY_ID_VAL)
    config.set(AWSPROFILE, SECRET_ACCESS_KEY_KEY, SECRET_ACCESS_KEY_VAL)
    config.set(AWSPROFILE, SESSION_TOKEN_KEY, SESSION_TOKEN_VAL)
    with open(fake_file, 'w') as f:
        config.write(f)

    credentials = watchdog.credentials_file_helper(fake_file, AWSPROFILE)

    assert credentials['AccessKeyId'] == ACCESS_KEY_ID_VAL
    assert credentials['SecretAccessKey'] == SECRET_ACCESS_KEY_VAL
    assert credentials['Token'] == SESSION_TOKEN_VAL
def test_credentials_file_helper_found_without_token(caplog, tmpdir):
    caplog.set_level(logging.DEBUG)
    fake_file = get_fake_aws_config_file(tmpdir)
    config = get_fake_config(add_test_profile=True)

    config.set(DEFAULT_PROFILE, ACCESS_KEY_ID_KEY, WRONG_ACCESS_KEY_ID_VAL)
    config.set(DEFAULT_PROFILE, SECRET_ACCESS_KEY_KEY,
               WRONG_SECRET_ACCESS_KEY_VAL)
    config.set(AWSPROFILE, ACCESS_KEY_ID_KEY, ACCESS_KEY_ID_VAL)
    config.set(AWSPROFILE, SECRET_ACCESS_KEY_KEY, SECRET_ACCESS_KEY_VAL)
    with open(fake_file, "w") as f:
        config.write(f)

    credentials = watchdog.credentials_file_helper(fake_file, AWSPROFILE)

    assert credentials["AccessKeyId"] == ACCESS_KEY_ID_VAL
    assert credentials["SecretAccessKey"] == SECRET_ACCESS_KEY_VAL
    assert credentials["Token"] is None
    assert "aws_session_token" in [rec.message for rec in caplog.records][0]