Skip to content

Commit

Permalink
Add a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alextu committed Oct 24, 2023
1 parent 2e89d7e commit 63fb342
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class BuildScansInjectionSettings extends JenkinsConfig {
private static final String GE_PLUGIN_VERSION_FIELD = "gradlePluginVersion";
private static final String GIT_REPOSITORY_FILTERS_FIELD = "vcsRepositoryFilter";
private static final String GE_ACCESS_KEY_FIELD = "accessKey";
private static final String GE_GRADLE_PLUGIN_REPOSITORY_PASSWORD_FIELD = "gradlePluginRepositoryPassword";

public BuildScansInjectionSettings(Jenkins jenkins) {
super(jenkins);
Expand Down Expand Up @@ -55,6 +56,10 @@ public void setGradleEnterpriseAccessKey(String accessKey) {
setBuildScansInjectionFormValue(GE_ACCESS_KEY_FIELD, accessKey);
}

public void setGradleEnterpriseGradlePluginRepoPassword(String password) {
setBuildScansInjectionFormValue(GE_GRADLE_PLUGIN_REPOSITORY_PASSWORD_FIELD, password);
}

public void setGradleEnterprisePluginVersion(String version) {
setBuildScansInjectionFormValue(GE_PLUGIN_VERSION_FIELD, version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ protected final void enableBuildScansForMaven() {
});
}

protected final void setGradlePluginRepositoryPassword(String password) {
updateBuildScansInjectionSettings(settings -> {
settings.setGradleEnterpriseGradlePluginRepoPassword(password);
});
}

private void updateBuildScansInjectionSettings(Consumer<BuildScansInjectionSettings> spec) {
BuildScansInjectionSettings settings = new BuildScansInjectionSettings(jenkins);
settings.configure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ public void accessKeyIsMasked() {
build.action(EnvInjectAction.class).shouldContain("GRADLE_ENTERPRISE_ACCESS_KEY", "[*******]");
}

@Test
@WithPlugins("envinject")
public void gradlePluginRepoPasswordIsMasked() {
// given
setGradlePluginRepositoryPassword("foo");

FreeStyleJob job = jenkins.jobs.create(FreeStyleJob.class);
job.copyDir(resource("/simple_gradle_project"));
GradleStep gradle = job.addBuildStep(GradleStep.class);
gradle.setVersion(GRADLE_VERSION);
gradle.setSwitches("--no-daemon");
gradle.setTasks("helloWorld");
job.save();

// when
Build build = job.startBuild();

// then
build.shouldSucceed();
assertBuildScanPublished(build);
build.action(EnvInjectAction.class).shouldContain("JENKINSGRADLEPLUGIN_GRADLE_PLUGIN_REPOSITORY_PASSWORD", "[*******]");
}

@Test
public void logsErrorIfBuildScanUploadFailed() {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hudson.plugins.gradle.injection

import hudson.EnvVars
import hudson.model.PasswordParameterValue
import hudson.model.ParameterValue
import hudson.model.Run
import hudson.model.TaskListener
import hudson.plugins.gradle.BaseJenkinsIntegrationTest
Expand Down Expand Up @@ -29,6 +29,19 @@ class BuildScanEnvironmentContributorTest extends BaseJenkinsIntegrationTest {
0 * run.addAction(_)
}

def 'does nothing if no password'() {
given:
def config = InjectionConfig.get()
config.setGradlePluginRepositoryPassword(Secret.fromString(""))
config.save()

when:
buildScanEnvironmentContributor.buildEnvironmentFor(run, new EnvVars(), TaskListener.NULL)

then:
0 * run.addAction(_)
}

def 'adds empty action if access key is invalid'() {
given:
def config = InjectionConfig.get()
Expand All @@ -44,6 +57,24 @@ class BuildScanEnvironmentContributorTest extends BaseJenkinsIntegrationTest {
}
}

def 'adds action if access key is invalid but password is there'() {
given:
def config = InjectionConfig.get()
config.setAccessKey(Secret.fromString("secret"))
config.setGradlePluginRepositoryPassword(Secret.fromString("foo"))
config.save()

when:
buildScanEnvironmentContributor.buildEnvironmentFor(run, new EnvVars(), TaskListener.NULL)

then:
1 * run.addAction { GradleEnterpriseParametersAction action ->
def parameters = action.getAllParameters()
parameters.size() == 1
paramEquals(parameters.first(), 'JENKINSGRADLEPLUGIN_GRADLE_PLUGIN_REPOSITORY_PASSWORD', 'foo')
}
}

def 'adds an action with the access key'() {
given:
def accessKey = "server=secret"
Expand All @@ -58,7 +89,48 @@ class BuildScanEnvironmentContributorTest extends BaseJenkinsIntegrationTest {
1 * run.addAction { GradleEnterpriseParametersAction action ->
def parameters = action.getAllParameters()
parameters.size() == 1
parameters.first() == new PasswordParameterValue('GRADLE_ENTERPRISE_ACCESS_KEY', accessKey, null)
paramEquals(parameters.first(), 'GRADLE_ENTERPRISE_ACCESS_KEY', accessKey)
}
}

def 'adds an action with the password'() {
given:
def config = InjectionConfig.get()
config.setGradlePluginRepositoryPassword(Secret.fromString("foo"))
config.save()

when:
buildScanEnvironmentContributor.buildEnvironmentFor(run, new EnvVars(), TaskListener.NULL)

then:
1 * run.addAction { GradleEnterpriseParametersAction action ->
def parameters = action.getAllParameters()
parameters.size() == 1
paramEquals(parameters.first(), 'JENKINSGRADLEPLUGIN_GRADLE_PLUGIN_REPOSITORY_PASSWORD', 'foo')
}
}

def 'adds an action with access key and password'() {
given:
def config = InjectionConfig.get()
config.setAccessKey(Secret.fromString("server=secret"))
config.setGradlePluginRepositoryPassword(Secret.fromString("foo"))
config.save()

when:
buildScanEnvironmentContributor.buildEnvironmentFor(run, new EnvVars(), TaskListener.NULL)

then:
1 * run.addAction { GradleEnterpriseParametersAction action ->
def parameters = action.getAllParameters()
parameters.size() == 2
paramEquals(parameters[0], 'GRADLE_ENTERPRISE_ACCESS_KEY', 'server=secret')
paramEquals(parameters[1], 'JENKINSGRADLEPLUGIN_GRADLE_PLUGIN_REPOSITORY_PASSWORD', 'foo')
}
}

private boolean paramEquals(ParameterValue param, String name, String value) {
param.name == name && param.value?.plainText == value
}

}

0 comments on commit 63fb342

Please sign in to comment.