import org.sonar.api.config.PropertyDefinition;
import org.sonar.server.property.InternalProperties;
+import static java.lang.String.format;
import static java.lang.String.valueOf;
+import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.sonar.api.PropertyType.BOOLEAN;
import static org.sonar.api.PropertyType.PASSWORD;
import static org.sonar.api.PropertyType.STRING;
private final InternalProperties internalProperties;
-
public GitHubSettings(Configuration configuration, InternalProperties internalProperties) {
this.configuration = configuration;
this.internalProperties = internalProperties;
public void setProvisioning(boolean enableProvisioning) {
if (enableProvisioning) {
- checkState(isEnabled(), "GitHub authentication must be enabled to enable GitHub provisioning.");
+ checkGithubConfigIsCompleteForProvisioning();
}
internalProperties.write(PROVISIONING, String.valueOf(enableProvisioning));
}
+ private void checkGithubConfigIsCompleteForProvisioning() {
+ checkState(isEnabled(), getErrorMessage("GitHub authentication must be enabled"));
+ checkState(isNotBlank(appId()), getErrorMessage("Application ID must be provided"));
+ checkState(isNotBlank(privateKey()), getErrorMessage("Private key must be provided"));
+ }
+
+ private static String getErrorMessage(String prefix) {
+ return format("%s to enable GitHub provisioning.", prefix);
+ }
+
public boolean isProvisioningEnabled() {
return isEnabled() && internalProperties.read(PROVISIONING).map(Boolean::parseBoolean).orElse(false);
}
assertThatIllegalStateException()
.isThrownBy(() -> underTest.setProvisioning(true))
.withMessage("GitHub authentication must be enabled to enable GitHub provisioning.");
+ assertThat(underTest.isProvisioningEnabled()).isFalse();
+ }
+
+ @Test
+ public void setProvisioning_whenPrivateKeyMissing_shouldThrow() {
+ enableGithubAuthenticationWithGithubApp();
+ settings.setProperty("sonar.auth.github.privateKey.secured", "");
+
+ assertThatIllegalStateException()
+ .isThrownBy(() -> underTest.setProvisioning(true))
+ .withMessage("Private key must be provided to enable GitHub provisioning.");
+ assertThat(underTest.isProvisioningEnabled()).isFalse();
+ }
+
+ @Test
+ public void setProvisioning_whenAppIdMissing_shouldThrow() {
+ enableGithubAuthenticationWithGithubApp();
+ settings.setProperty("sonar.auth.github.appId", "");
+
+ assertThatIllegalStateException()
+ .isThrownBy(() -> underTest.setProvisioning(true))
+ .withMessage("Application ID must be provided to enable GitHub provisioning.");
+ assertThat(underTest.isProvisioningEnabled()).isFalse();
}
@Test
public void setProvisioning_whenPassedTrue_delegatesToInternalPropertiesWrite() {
- enableGithubAuthentication();
+ enableGithubAuthenticationWithGithubApp();
underTest.setProvisioning(true);
verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.TRUE.toString());
}