]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21244 Clean github_orgs_groups table when disabling GitHub auto provisioning.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Thu, 21 Dec 2023 10:15:22 +0000 (11:15 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 22 Dec 2023 20:03:03 +0000 (20:03 +0000)
server/sonar-auth-github/src/it/java/org/sonar/auth/github/GitHubSettingsIT.java [new file with mode: 0644]
server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java
server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java [deleted file]
server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubOrganizationGroupDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubOrganizationGroupMapper.xml

diff --git a/server/sonar-auth-github/src/it/java/org/sonar/auth/github/GitHubSettingsIT.java b/server/sonar-auth-github/src/it/java/org/sonar/auth/github/GitHubSettingsIT.java
new file mode 100644 (file)
index 0000000..b88b892
--- /dev/null
@@ -0,0 +1,316 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.auth.github;
+
+import java.util.Optional;
+import java.util.Set;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.config.PropertyDefinition;
+import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.api.config.internal.MapSettings;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.provisioning.GithubOrganizationGroupDto;
+import org.sonar.db.user.GroupDto;
+import org.sonar.server.property.InternalProperties;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.sonar.auth.github.GitHubSettings.PROVISION_VISIBILITY;
+import static org.sonar.auth.github.GitHubSettings.USER_CONSENT_FOR_PERMISSIONS_REQUIRED_AFTER_UPGRADE;
+
+public class GitHubSettingsIT {
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+
+  private final MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, GitHubSettings.definitions()));
+  private final InternalProperties internalProperties = mock(InternalProperties.class);
+
+  private final GitHubSettings underTest = new GitHubSettings(settings.asConfig(), internalProperties, db.getDbClient());
+
+  @Test
+  public void is_enabled() {
+    enableGithubAuthentication();
+
+    assertThat(underTest.isEnabled()).isTrue();
+
+    settings.setProperty("sonar.auth.github.enabled", false);
+    assertThat(underTest.isEnabled()).isFalse();
+  }
+
+  @Test
+  public void is_enabled_always_return_false_when_client_id_is_null() {
+    settings.setProperty("sonar.auth.github.enabled", true);
+    settings.setProperty("sonar.auth.github.clientId.secured", (String) null);
+    settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
+
+    assertThat(underTest.isEnabled()).isFalse();
+  }
+
+  @Test
+  public void is_enabled_always_return_false_when_client_secret_is_null() {
+    settings.setProperty("sonar.auth.github.enabled", true);
+    settings.setProperty("sonar.auth.github.clientId.secured", "id");
+    settings.setProperty("sonar.auth.github.clientSecret.secured", (String) null);
+
+    assertThat(underTest.isEnabled()).isFalse();
+  }
+
+  @Test
+  public void isProvisioningEnabled_returnsFalseByDefault() {
+    enableGithubAuthentication();
+    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.empty());
+    assertThat(underTest.isProvisioningEnabled()).isFalse();
+  }
+
+  @Test
+  public void isProvisioningEnabled_ifProvisioningEnabledButGithubAuthNotSet_returnsFalse() {
+    enableGithubAuthentication();
+    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.FALSE.toString()));
+    assertThat(underTest.isProvisioningEnabled()).isFalse();
+  }
+
+  @Test
+  public void isProvisioningEnabled_ifProvisioningEnabledButGithubAuthDisabled_returnsFalse() {
+    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.TRUE.toString()));
+    assertThat(underTest.isProvisioningEnabled()).isFalse();
+  }
+
+  @Test
+  public void isProvisioningEnabled_ifProvisioningEnabledAndGithubAuthEnabled_returnsTrue() {
+    enableGithubAuthenticationWithGithubApp();
+    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.TRUE.toString()));
+    assertThat(underTest.isProvisioningEnabled()).isTrue();
+  }
+
+  @Test
+  public void isUserConsentRequiredAfterUpgrade_returnsFalseByDefault() {
+    assertThat(underTest.isUserConsentRequiredAfterUpgrade()).isFalse();
+  }
+
+  @Test
+  public void isUserConsentRequiredAfterUpgrade_returnsTrueIfPropertyPresent() {
+    settings.setProperty(USER_CONSENT_FOR_PERMISSIONS_REQUIRED_AFTER_UPGRADE, "");
+    assertThat(underTest.isUserConsentRequiredAfterUpgrade()).isTrue();
+  }
+
+  @Test
+  public void isProjectVisibilitySynchronizationActivated_whenPropertyNotSet_returnsTrueByDefault() {
+    assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isTrue();
+  }
+
+  @Test
+  public void isProjectVisibilitySynchronizationActivated_whenPropertyIsSetToFalse_returnsFalse() {
+    settings.setProperty(PROVISION_VISIBILITY, "false");
+    assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isFalse();
+  }
+
+  @Test
+  public void isProjectVisibilitySynchronizationActivated_whenPropertyIsSetToTrue_returnsTrue() {
+    settings.setProperty(PROVISION_VISIBILITY, "true");
+    assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isTrue();
+  }
+
+  @Test
+  public void setProvisioning_whenGitHubAuthDisabled_shouldThrow() {
+    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() {
+    enableGithubAuthenticationWithGithubApp();
+    underTest.setProvisioning(true);
+    verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.TRUE.toString());
+  }
+
+  @Test
+  public void setProvisioning_whenPassedFalse_delegatesToInternalPropertiesWriteAndCleansUpExternalGroups() {
+    createGithubManagedGroup();
+    createGitHubOrganizationGroup();
+
+    underTest.setProvisioning(false);
+
+    verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.FALSE.toString());
+    assertThat(db.getDbClient().externalGroupDao().selectByIdentityProvider(db.getSession(), GitHubIdentityProvider.KEY)).isEmpty();
+    assertThat(db.getDbClient().githubOrganizationGroupDao().findAll(db.getSession())).isEmpty();
+  }
+
+  private void createGithubManagedGroup() {
+    GroupDto groupDto = db.users().insertGroup();
+    db.users().markGroupAsGithubManaged(groupDto.getUuid());
+  }
+
+  private void createGitHubOrganizationGroup() {
+    GroupDto groupDto = db.users().insertGroup();
+    db.getDbClient().githubOrganizationGroupDao().insert(db.getSession(), new GithubOrganizationGroupDto(groupDto.getUuid(), "org1"));
+    db.commit();
+  }
+
+  @Test
+  public void return_client_id() {
+    settings.setProperty("sonar.auth.github.clientId.secured", "id");
+    assertThat(underTest.clientId()).isEqualTo("id");
+  }
+
+  @Test
+  public void return_client_secret() {
+    settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
+    assertThat(underTest.clientSecret()).isEqualTo("secret");
+  }
+
+  @Test
+  public void return_app_id() {
+    settings.setProperty("sonar.auth.github.appId", "secret");
+    assertThat(underTest.appId()).isEqualTo("secret");
+  }
+
+  @Test
+  public void return_private_key() {
+    settings.setProperty("sonar.auth.github.privateKey.secured", "secret");
+    assertThat(underTest.privateKey()).isEqualTo("secret");
+  }
+
+  @Test
+  public void allow_users_to_sign_up() {
+    settings.setProperty("sonar.auth.github.allowUsersToSignUp", "true");
+    assertThat(underTest.allowUsersToSignUp()).isTrue();
+
+    settings.setProperty("sonar.auth.github.allowUsersToSignUp", "false");
+    assertThat(underTest.allowUsersToSignUp()).isFalse();
+
+    // default value
+    settings.setProperty("sonar.auth.github.allowUsersToSignUp", (String) null);
+    assertThat(underTest.allowUsersToSignUp()).isTrue();
+  }
+
+  @Test
+  public void sync_groups() {
+    settings.setProperty("sonar.auth.github.groupsSync", "true");
+    assertThat(underTest.syncGroups()).isTrue();
+
+    settings.setProperty("sonar.auth.github.groupsSync", "false");
+    assertThat(underTest.syncGroups()).isFalse();
+
+    // default value
+    settings.setProperty("sonar.auth.github.groupsSync", (String) null);
+    assertThat(underTest.syncGroups()).isFalse();
+  }
+
+  @Test
+  public void apiUrl_must_have_ending_slash() {
+    settings.setProperty("sonar.auth.github.apiUrl", "https://github.com");
+    assertThat(underTest.apiURL()).isEqualTo("https://github.com/");
+
+    settings.setProperty("sonar.auth.github.apiUrl", "https://github.com/");
+    assertThat(underTest.apiURL()).isEqualTo("https://github.com/");
+  }
+
+  @Test
+  public void webUrl_must_have_ending_slash() {
+    settings.setProperty("sonar.auth.github.webUrl", "https://github.com");
+    assertThat(underTest.webURL()).isEqualTo("https://github.com/");
+
+    settings.setProperty("sonar.auth.github.webUrl", "https://github.com/");
+    assertThat(underTest.webURL()).isEqualTo("https://github.com/");
+  }
+
+  @Test
+  public void return_organizations_single() {
+    String setting = "example";
+    settings.setProperty("sonar.auth.github.organizations", setting);
+    Set<String> actual = underTest.getOrganizations();
+    assertThat(actual).containsOnly(setting);
+  }
+
+  @Test
+  public void return_organizations_multiple() {
+    String setting = "example0,example1";
+    settings.setProperty("sonar.auth.github.organizations", setting);
+    Set<String> actual = underTest.getOrganizations();
+    assertThat(actual).containsOnly("example0", "example1");
+  }
+
+  @Test
+  public void return_organizations_empty_list() {
+    String[] setting = null;
+    settings.setProperty("sonar.auth.github.organizations", setting);
+    Set<String> actual = underTest.getOrganizations();
+    assertThat(actual).isEmpty();
+  }
+
+  @Test
+  public void definitions() {
+    assertThat(GitHubSettings.definitions().stream()
+      .map(PropertyDefinition::name))
+      .containsExactly(
+        "Enabled",
+        "Client ID",
+        "Client Secret",
+        "App ID",
+        "Private Key",
+        "Allow users to sign up",
+        "Synchronize teams as groups",
+        "The API url for a GitHub instance.",
+        "The WEB url for a GitHub instance.",
+        "Organizations",
+        "Provision project visibility");
+  }
+
+  private void enableGithubAuthentication() {
+    settings.setProperty("sonar.auth.github.clientId.secured", "id");
+    settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
+    settings.setProperty("sonar.auth.github.enabled", true);
+  }
+
+  private void enableGithubAuthenticationWithGithubApp() {
+    enableGithubAuthentication();
+    settings.setProperty("sonar.auth.github.appId", "id");
+    settings.setProperty("sonar.auth.github.privateKey.secured", "secret");
+  }
+}
index 5a50ee8980d953377acbe3ce90a3a6bc9378cb03..03093f981046f3164ddd2a01cfb08be7200d1109 100644 (file)
@@ -147,6 +147,7 @@ public class GitHubSettings implements DevOpsPlatformSettings {
   private void removeExternalGroupsForGithub() {
     try (DbSession dbSession = dbClient.openSession(false)) {
       dbClient.externalGroupDao().deleteByExternalIdentityProvider(dbSession, GitHubIdentityProvider.KEY);
+      dbClient.githubOrganizationGroupDao().deleteAll(dbSession);
       dbSession.commit();
     }
   }
diff --git a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java b/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java
deleted file mode 100644 (file)
index 5641897..0000000
+++ /dev/null
@@ -1,308 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.auth.github;
-
-import java.util.Optional;
-import java.util.Set;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.config.PropertyDefinition;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.utils.System2;
-import org.sonar.db.DbTester;
-import org.sonar.db.user.GroupDto;
-import org.sonar.server.property.InternalProperties;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import static org.sonar.auth.github.GitHubSettings.USER_CONSENT_FOR_PERMISSIONS_REQUIRED_AFTER_UPGRADE;
-import static org.sonar.auth.github.GitHubSettings.PROVISION_VISIBILITY;
-
-public class GitHubSettingsTest {
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
-
-  private final MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, GitHubSettings.definitions()));
-  private final InternalProperties internalProperties = mock(InternalProperties.class);
-
-  private final GitHubSettings underTest = new GitHubSettings(settings.asConfig(), internalProperties, db.getDbClient());
-
-  @Test
-  public void is_enabled() {
-    enableGithubAuthentication();
-
-    assertThat(underTest.isEnabled()).isTrue();
-
-    settings.setProperty("sonar.auth.github.enabled", false);
-    assertThat(underTest.isEnabled()).isFalse();
-  }
-
-  @Test
-  public void is_enabled_always_return_false_when_client_id_is_null() {
-    settings.setProperty("sonar.auth.github.enabled", true);
-    settings.setProperty("sonar.auth.github.clientId.secured", (String) null);
-    settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
-
-    assertThat(underTest.isEnabled()).isFalse();
-  }
-
-  @Test
-  public void is_enabled_always_return_false_when_client_secret_is_null() {
-    settings.setProperty("sonar.auth.github.enabled", true);
-    settings.setProperty("sonar.auth.github.clientId.secured", "id");
-    settings.setProperty("sonar.auth.github.clientSecret.secured", (String) null);
-
-    assertThat(underTest.isEnabled()).isFalse();
-  }
-
-  @Test
-  public void isProvisioningEnabled_returnsFalseByDefault() {
-    enableGithubAuthentication();
-    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.empty());
-    assertThat(underTest.isProvisioningEnabled()).isFalse();
-  }
-
-  @Test
-  public void isProvisioningEnabled_ifProvisioningEnabledButGithubAuthNotSet_returnsFalse() {
-    enableGithubAuthentication();
-    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.FALSE.toString()));
-    assertThat(underTest.isProvisioningEnabled()).isFalse();
-  }
-
-  @Test
-  public void isProvisioningEnabled_ifProvisioningEnabledButGithubAuthDisabled_returnsFalse() {
-    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.TRUE.toString()));
-    assertThat(underTest.isProvisioningEnabled()).isFalse();
-  }
-
-  @Test
-  public void isProvisioningEnabled_ifProvisioningEnabledAndGithubAuthEnabled_returnsTrue() {
-    enableGithubAuthenticationWithGithubApp();
-    when(internalProperties.read(GitHubSettings.PROVISIONING)).thenReturn(Optional.of(Boolean.TRUE.toString()));
-    assertThat(underTest.isProvisioningEnabled()).isTrue();
-  }
-
-  @Test
-  public void isUserConsentRequiredAfterUpgrade_returnsFalseByDefault() {
-    assertThat(underTest.isUserConsentRequiredAfterUpgrade()).isFalse();
-  }
-
-  @Test
-  public void isUserConsentRequiredAfterUpgrade_returnsTrueIfPropertyPresent() {
-    settings.setProperty(USER_CONSENT_FOR_PERMISSIONS_REQUIRED_AFTER_UPGRADE, "");
-    assertThat(underTest.isUserConsentRequiredAfterUpgrade()).isTrue();
-  }
-
-  @Test
-  public void isProjectVisibilitySynchronizationActivated_whenPropertyNotSet_returnsTrueByDefault() {
-    assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isTrue();
-  }
-
-  @Test
-  public void isProjectVisibilitySynchronizationActivated_whenPropertyIsSetToFalse_returnsFalse() {
-    settings.setProperty(PROVISION_VISIBILITY, "false");
-    assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isFalse();
-  }
-  @Test
-  public void isProjectVisibilitySynchronizationActivated_whenPropertyIsSetToTrue_returnsTrue() {
-    settings.setProperty(PROVISION_VISIBILITY, "true");
-    assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isTrue();
-  }
-
-  @Test
-  public void setProvisioning_whenGitHubAuthDisabled_shouldThrow() {
-    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() {
-    enableGithubAuthenticationWithGithubApp();
-    underTest.setProvisioning(true);
-    verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.TRUE.toString());
-  }
-
-  @Test
-  public void setProvisioning_whenPassedFalse_delegatesToInternalPropertiesWriteAndCleansUpExternalGroups() {
-    GroupDto groupDto = createGithubManagedGroup();
-
-    underTest.setProvisioning(false);
-
-    verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.FALSE.toString());
-    assertThat(db.getDbClient().externalGroupDao().selectByGroupUuid(db.getSession(), groupDto.getUuid())).isEmpty();
-  }
-
-  private GroupDto createGithubManagedGroup() {
-    GroupDto groupDto = db.users().insertGroup();
-    db.users().markGroupAsGithubManaged(groupDto.getUuid());
-    return groupDto;
-  }
-
-
-  @Test
-  public void return_client_id() {
-    settings.setProperty("sonar.auth.github.clientId.secured", "id");
-    assertThat(underTest.clientId()).isEqualTo("id");
-  }
-
-  @Test
-  public void return_client_secret() {
-    settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
-    assertThat(underTest.clientSecret()).isEqualTo("secret");
-  }
-
-  @Test
-  public void return_app_id() {
-    settings.setProperty("sonar.auth.github.appId", "secret");
-    assertThat(underTest.appId()).isEqualTo("secret");
-  }
-
-  @Test
-  public void return_private_key() {
-    settings.setProperty("sonar.auth.github.privateKey.secured", "secret");
-    assertThat(underTest.privateKey()).isEqualTo("secret");
-  }
-
-  @Test
-  public void allow_users_to_sign_up() {
-    settings.setProperty("sonar.auth.github.allowUsersToSignUp", "true");
-    assertThat(underTest.allowUsersToSignUp()).isTrue();
-
-    settings.setProperty("sonar.auth.github.allowUsersToSignUp", "false");
-    assertThat(underTest.allowUsersToSignUp()).isFalse();
-
-    // default value
-    settings.setProperty("sonar.auth.github.allowUsersToSignUp", (String) null);
-    assertThat(underTest.allowUsersToSignUp()).isTrue();
-  }
-
-  @Test
-  public void sync_groups() {
-    settings.setProperty("sonar.auth.github.groupsSync", "true");
-    assertThat(underTest.syncGroups()).isTrue();
-
-    settings.setProperty("sonar.auth.github.groupsSync", "false");
-    assertThat(underTest.syncGroups()).isFalse();
-
-    // default value
-    settings.setProperty("sonar.auth.github.groupsSync", (String) null);
-    assertThat(underTest.syncGroups()).isFalse();
-  }
-
-  @Test
-  public void apiUrl_must_have_ending_slash() {
-    settings.setProperty("sonar.auth.github.apiUrl", "https://github.com");
-    assertThat(underTest.apiURL()).isEqualTo("https://github.com/");
-
-    settings.setProperty("sonar.auth.github.apiUrl", "https://github.com/");
-    assertThat(underTest.apiURL()).isEqualTo("https://github.com/");
-  }
-
-  @Test
-  public void webUrl_must_have_ending_slash() {
-    settings.setProperty("sonar.auth.github.webUrl", "https://github.com");
-    assertThat(underTest.webURL()).isEqualTo("https://github.com/");
-
-    settings.setProperty("sonar.auth.github.webUrl", "https://github.com/");
-    assertThat(underTest.webURL()).isEqualTo("https://github.com/");
-  }
-
-  @Test
-  public void return_organizations_single() {
-    String setting = "example";
-    settings.setProperty("sonar.auth.github.organizations", setting);
-    Set<String> actual = underTest.getOrganizations();
-    assertThat(actual).containsOnly(setting);
-  }
-
-  @Test
-  public void return_organizations_multiple() {
-    String setting = "example0,example1";
-    settings.setProperty("sonar.auth.github.organizations", setting);
-    Set<String> actual = underTest.getOrganizations();
-    assertThat(actual).containsOnly("example0", "example1");
-  }
-
-  @Test
-  public void return_organizations_empty_list() {
-    String[] setting = null;
-    settings.setProperty("sonar.auth.github.organizations", setting);
-    Set<String> actual = underTest.getOrganizations();
-    assertThat(actual).isEmpty();
-  }
-
-  @Test
-  public void definitions() {
-    assertThat(GitHubSettings.definitions().stream()
-      .map(PropertyDefinition::name))
-      .containsExactly(
-        "Enabled",
-        "Client ID",
-        "Client Secret",
-        "App ID",
-        "Private Key",
-        "Allow users to sign up",
-        "Synchronize teams as groups",
-        "The API url for a GitHub instance.",
-        "The WEB url for a GitHub instance.",
-        "Organizations",
-        "Provision project visibility");
-  }
-
-  private void enableGithubAuthentication() {
-    settings.setProperty("sonar.auth.github.clientId.secured", "id");
-    settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
-    settings.setProperty("sonar.auth.github.enabled", true);
-  }
-
-  private void enableGithubAuthenticationWithGithubApp() {
-    enableGithubAuthentication();
-    settings.setProperty("sonar.auth.github.appId", "id");
-    settings.setProperty("sonar.auth.github.privateKey.secured", "secret");
-  }
-}
index 4502fe32c5df38e92592a7afab8b31b59cd9bb9d..96c77a8782d4def0a0f3b8618d47fd699f02ce6c 100644 (file)
@@ -84,8 +84,7 @@ public class GithubOrganizationGroupDaoIT {
     assertThat(all).hasSize(2)
       .containsExactlyInAnyOrder(
         new GithubOrganizationGroupDto(githubOrganizationGroupDto.groupUuid(), githubOrganizationGroupDto.organizationName(), groupDto.getName()),
-        new GithubOrganizationGroupDto(githubOrganizationGroupDto2.groupUuid(), githubOrganizationGroupDto2.organizationName(), groupDto2.getName())
-      );
+        new GithubOrganizationGroupDto(githubOrganizationGroupDto2.groupUuid(), githubOrganizationGroupDto2.organizationName(), groupDto2.getName()));
   }
 
   @Test
@@ -103,6 +102,21 @@ public class GithubOrganizationGroupDaoIT {
     assertThat(underTest.selectByGroupUuid(dbSession, githubOrganizationGroupDto2.groupUuid())).isPresent();
   }
 
+  @Test
+  public void deleteAll_shouldDeleteEverything() {
+    GroupDto groupDto = insertGroup(GROUP_UUID);
+    GithubOrganizationGroupDto githubOrganizationGroupDto = createGithubOrganizationGroupDto(groupDto.getUuid(), ORG_NAME);
+    underTest.insert(dbSession, githubOrganizationGroupDto);
+
+    GroupDto groupDto2 = insertGroup(GROUP_UUID + "2");
+    GithubOrganizationGroupDto githubOrganizationGroupDto2 = createGithubOrganizationGroupDto(groupDto2.getUuid(), ORG_NAME + "2");
+    underTest.insert(dbSession, githubOrganizationGroupDto2);
+
+    underTest.deleteAll(dbSession);
+
+    assertThat(underTest.findAll(dbSession)).isEmpty();
+  }
+
   private GroupDto insertGroup(String groupUuid) {
     GroupDto group = new GroupDto();
     group.setUuid(groupUuid);
index 4c9d13cbf425ac14eaa3c40797904088c8d1072c..ae4d699e2ae65d6e1bccbfba6ee2f67dc120663b 100644 (file)
@@ -29,15 +29,23 @@ public class GithubOrganizationGroupDao implements Dao {
   public Set<GithubOrganizationGroupDto> findAll(DbSession dbSession) {
     return mapper(dbSession).selectAll();
   }
+
   public void insert(DbSession dbSession, GithubOrganizationGroupDto githubOrganizationGroupDto) {
     mapper(dbSession).insert(githubOrganizationGroupDto);
   }
+
   public Optional<GithubOrganizationGroupDto> selectByGroupUuid(DbSession dbSession, String groupUuid) {
     return mapper(dbSession).selectByGroupUuid(groupUuid);
   }
+
   public void deleteByGroupUuid(DbSession dbSession, String groupUuid) {
     mapper(dbSession).deleteByGroupUuid(groupUuid);
   }
+
+  public void deleteAll(DbSession dbSession) {
+    mapper(dbSession).deleteAll();
+  }
+
   private static GithubOrganizationGroupMapper mapper(DbSession session) {
     return session.getMapper(GithubOrganizationGroupMapper.class);
   }
index 0392f2f76aeac30d2de374b8a43b79e231ff33ae..af47b2ed28eae84d2d5e832f6c13817f25ca9d07 100644 (file)
@@ -33,4 +33,6 @@ public interface GithubOrganizationGroupMapper {
 
   void deleteByGroupUuid(@Param("groupUuid") String groupUuid);
 
+  void deleteAll();
+
 }
index e5a98b3c2baf0cf5789380b10ceb74220b070108..ec6bbd91bf1baf054bb3d60a803f99be22a0ae85 100644 (file)
@@ -40,4 +40,8 @@
     delete from github_orgs_groups where group_uuid = #{groupUuid, jdbcType=VARCHAR}
   </delete>
 
+  <delete id="deleteAll" >
+    delete from github_orgs_groups
+  </delete>
+
 </mapper>