aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-auth-gitlab
diff options
context:
space:
mode:
authorPierre <pierre.guillot@sonarsource.com>2021-09-15 12:10:05 +0200
committersonartech <sonartech@sonarsource.com>2021-09-16 20:03:30 +0000
commit2c93d7e5ed4bdafa8054cbb7a139d8dfc149365b (patch)
tree23d2bb3279735c31ba740254293f58f4df4cce95 /server/sonar-auth-gitlab
parente44a7826206596b4c3f07a1139ab577656805b5a (diff)
downloadsonarqube-2c93d7e5ed4bdafa8054cbb7a139d8dfc149365b.tar.gz
sonarqube-2c93d7e5ed4bdafa8054cbb7a139d8dfc149365b.zip
SONAR-15171 filter groups sync on min_access_level 10
Diffstat (limited to 'server/sonar-auth-gitlab')
-rw-r--r--server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java2
-rw-r--r--server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabIdentityProviderTest.java11
-rw-r--r--server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/IntegrationTest.java23
3 files changed, 16 insertions, 20 deletions
diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
index 22334164639..5c19193a4ab 100644
--- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
+++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GitLabRestClient.java
@@ -46,6 +46,6 @@ public class GitLabRestClient {
}
List<GsonGroup> getGroups(OAuth20Service scribe, OAuth2AccessToken accessToken) {
- return OAuthRestClient.executePaginatedRequest(settings.url() + API_SUFFIX + "/groups", scribe, accessToken, GsonGroup::parse);
+ return OAuthRestClient.executePaginatedRequest(settings.url() + API_SUFFIX + "/groups?min_access_level=10", scribe, accessToken, GsonGroup::parse);
}
}
diff --git a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabIdentityProviderTest.java b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabIdentityProviderTest.java
index 778de10eef2..5d2e086a03c 100644
--- a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabIdentityProviderTest.java
+++ b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GitLabIdentityProviderTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.auth.gitlab;
+import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -32,9 +33,6 @@ import static org.mockito.Mockito.when;
public class GitLabIdentityProviderTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
@Test
public void test_identity_provider() {
GitLabSettings gitLabSettings = mock(GitLabSettings.class);
@@ -106,9 +104,8 @@ public class GitLabIdentityProviderTest {
OAuth2IdentityProvider.InitContext initContext = mock(OAuth2IdentityProvider.InitContext.class);
when(initContext.getCallbackUrl()).thenReturn("http://server/callback");
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("GitLab authentication is disabled");
-
- gitLabIdentityProvider.init(initContext);
+ Assertions.assertThatThrownBy(() -> gitLabIdentityProvider.init(initContext))
+ .hasMessage("GitLab authentication is disabled")
+ .isInstanceOf(IllegalStateException.class);
}
}
diff --git a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/IntegrationTest.java b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/IntegrationTest.java
index d5e656106c3..7d84c561d3b 100644
--- a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/IntegrationTest.java
+++ b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/IntegrationTest.java
@@ -22,10 +22,10 @@ package org.sonar.auth.gitlab;
import javax.servlet.http.HttpServletRequest;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
+import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.sonar.api.config.internal.MapSettings;
@@ -48,18 +48,15 @@ public class IntegrationTest {
private static final String ANY_CODE_VALUE = "ANY_CODE";
@Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Rule
public MockWebServer gitlab = new MockWebServer();
- private MapSettings mapSettings = new MapSettings();
+ private final MapSettings mapSettings = new MapSettings();
- private GitLabSettings gitLabSettings = new GitLabSettings(mapSettings.asConfig());
+ private final GitLabSettings gitLabSettings = new GitLabSettings(mapSettings.asConfig());
private String gitLabUrl;
- private GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings,
+ private final GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings,
new GitLabRestClient(gitLabSettings),
new ScribeGitLabOauth2Api(gitLabSettings));
@@ -102,7 +99,7 @@ public class IntegrationTest {
}
@Test
- public void synchronize_groups() {
+ public void synchronize_groups() throws InterruptedException {
mapSettings.setProperty(GITLAB_AUTH_SYNC_USER_GROUPS, "true");
OAuth2IdentityProvider.CallbackContext callbackContext = Mockito.mock(OAuth2IdentityProvider.CallbackContext.class);
when(callbackContext.getCallbackUrl()).thenReturn("http://server/callback");
@@ -125,6 +122,9 @@ public class IntegrationTest {
verify(callbackContext).authenticate(captor.capture());
UserIdentity value = captor.getValue();
assertThat(value.getGroups()).contains("group1", "group2");
+ assertThat(gitlab.takeRequest().getPath()).isEqualTo("/oauth/token");
+ assertThat(gitlab.takeRequest().getPath()).isEqualTo("/api/v4/user");
+ assertThat(gitlab.takeRequest().getPath()).isEqualTo("/api/v4/groups?min_access_level=10&per_page=100");
}
@Test
@@ -182,10 +182,9 @@ public class IntegrationTest {
+ " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}"));
gitlab.enqueue(new MockResponse().setResponseCode(404).setBody("empty"));
- expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("Fail to execute request '" + gitLabSettings.url() + "/api/v4/user'. HTTP code: 404, response: empty");
-
- gitLabIdentityProvider.callback(callbackContext);
+ Assertions.assertThatThrownBy(() -> gitLabIdentityProvider.callback(callbackContext))
+ .hasMessage("Fail to execute request '" + gitLabSettings.url() + "/api/v4/user'. HTTP code: 404, response: empty")
+ .isInstanceOf((IllegalStateException.class));
}
}