123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- /*
- * SonarQube
- * Copyright (C) 2009-2019 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 org.junit.Test;
- import org.sonar.api.config.PropertyDefinitions;
- import org.sonar.api.config.internal.MapSettings;
-
- import static org.assertj.core.api.Assertions.assertThat;
- import static org.sonar.auth.github.GitHubSettings.LOGIN_STRATEGY_DEFAULT_VALUE;
- import static org.sonar.auth.github.GitHubSettings.LOGIN_STRATEGY_PROVIDER_ID;
-
- public class GitHubSettingsTest {
-
- private MapSettings settings = new MapSettings(new PropertyDefinitions(GitHubSettings.definitions()));
-
- private GitHubSettings underTest = new GitHubSettings(settings.asConfig());
-
- @Test
- public void is_enabled() {
- settings.setProperty("sonar.auth.github.clientId.secured", "id");
- settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
- settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_DEFAULT_VALUE);
-
- settings.setProperty("sonar.auth.github.enabled", true);
- 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");
- settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_DEFAULT_VALUE);
-
- 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);
- settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_DEFAULT_VALUE);
-
- assertThat(underTest.isEnabled()).isFalse();
- }
-
- @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_login_strategy() {
- settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_PROVIDER_ID);
- assertThat(underTest.loginStrategy()).isEqualTo(LOGIN_STRATEGY_PROVIDER_ID);
- }
-
- @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);
- String[] expected = new String[] {"example"};
- String[] actual = underTest.organizations();
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- public void return_organizations_multiple() {
- String setting = "example0,example1";
- settings.setProperty("sonar.auth.github.organizations", setting);
- String[] expected = new String[] {"example0", "example1"};
- String[] actual = underTest.organizations();
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- public void return_organizations_empty_list() {
- String[] setting = null;
- settings.setProperty("sonar.auth.github.organizations", setting);
- String[] expected = new String[] {};
- String[] actual = underTest.organizations();
- assertThat(actual).isEqualTo(expected);
- }
-
- @Test
- public void definitions() {
- assertThat(GitHubSettings.definitions()).hasSize(9);
- }
- }
|