Skip to content

Commit

Permalink
Attempt to fix failing tests on AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
bdpiprava committed Nov 15, 2019
1 parent 25d92ea commit 80b881e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@
import static org.apache.commons.lang3.StringUtils.isBlank;

public class CredentialValidator implements Validator {
private final AWSCredentialsProviderChain credentialsProviderChain = new AWSCredentialsProviderChain();
private final AWSCredentialsProviderChain credentialsProviderChain;

public CredentialValidator() {
this(new AWSCredentialsProviderChain());
}

CredentialValidator(AWSCredentialsProviderChain credentialsProviderChain) {
this.credentialsProviderChain = credentialsProviderChain;
}

@Override
public ValidationResult validate(Map<String, String> requestBody) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,38 @@
package com.thoughtworks.gocd.secretmanager.aws.validators;

import cd.go.plugin.base.validation.ValidationResult;
import com.thoughtworks.gocd.secretmanager.aws.AWSCredentialsProviderChain;
import com.thoughtworks.gocd.secretmanager.aws.annotations.JsonSource;
import com.thoughtworks.gocd.secretmanager.aws.exceptions.AWSCredentialsException;
import com.thoughtworks.gocd.secretmanager.aws.extensions.EnvironmentVariable;
import com.thoughtworks.gocd.secretmanager.aws.extensions.SystemProperty;
import org.json.JSONException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.mockito.Mock;

import java.util.HashMap;
import java.util.Map;

import static com.amazonaws.SDKGlobalConfiguration.*;
import static cd.go.plugin.base.GsonTransformer.toJson;
import static com.amazonaws.SDKGlobalConfiguration.*;
import static com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig.ACCESS_KEY;
import static com.thoughtworks.gocd.secretmanager.aws.models.SecretConfig.SECRET_ACCESS_KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;

class CredentialValidatorTest {
@Mock
private AWSCredentialsProviderChain credentialsProviderChain;
private CredentialValidator credentialValidator;

@BeforeEach
void setUp() {
credentialValidator = new CredentialValidator();
initMocks(this);
credentialValidator = new CredentialValidator(credentialsProviderChain);
}

@Test
Expand Down Expand Up @@ -71,6 +79,8 @@ void shouldBeValidIfCredentialsAreProvidedAsSystemProperties() {
@ParameterizedTest
@JsonSource(jsonFiles = "/missing-credentials-validation-error.json")
void shouldBeInvalidWhenCredentialsAreNotProvidedAndFailsToDetectItUsingCredentialProviders(String expectedJson) throws JSONException {
when(credentialsProviderChain.autoDetectAWSCredentials()).thenThrow(new AWSCredentialsException("Boom!"));

ValidationResult result = credentialValidator.validate(secretConfig(null, null));

assertThat(result.isEmpty()).isFalse();
Expand All @@ -83,4 +93,4 @@ private Map<String, String> secretConfig(String accessKey, String secretAccessKe
secretConfigMap.put(SECRET_ACCESS_KEY, secretAccessKey);
return secretConfigMap;
}
}
}

0 comments on commit 80b881e

Please sign in to comment.