Search in sources :

Example 1 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class IntegrationTest method authenticate_user.

@Test
public void authenticate_user() {
    OAuth2IdentityProvider.CallbackContext callbackContext = Mockito.mock(OAuth2IdentityProvider.CallbackContext.class);
    when(callbackContext.getCallbackUrl()).thenReturn("http://server/callback");
    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    when(httpServletRequest.getParameter("code")).thenReturn(ANY_CODE_VALUE);
    when(callbackContext.getRequest()).thenReturn(httpServletRequest);
    gitlab.enqueue(new MockResponse().setBody("{\n" + " \"access_token\": \"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": 7200,\n" + " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}"));
    // response of /user
    gitlab.enqueue(new MockResponse().setBody("{\"id\": 123, \"username\":\"toto\", \"name\":\"Toto Toto\",\"email\":\"toto@toto.com\"}"));
    gitLabIdentityProvider.callback(callbackContext);
    ArgumentCaptor<UserIdentity> argument = ArgumentCaptor.forClass(UserIdentity.class);
    verify(callbackContext).authenticate(argument.capture());
    assertThat(argument.getValue()).isNotNull();
    assertThat(argument.getValue().getProviderId()).isEqualTo("123");
    assertThat(argument.getValue().getProviderLogin()).isEqualTo("toto");
    assertThat(argument.getValue().getName()).isEqualTo("Toto Toto");
    assertThat(argument.getValue().getEmail()).isEqualTo("toto@toto.com");
    verify(callbackContext).redirectToRequestedPage();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OAuth2IdentityProvider(org.sonar.api.server.authentication.OAuth2IdentityProvider) UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Example 2 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class IntegrationTest method synchronize_groups_on_many_pages.

@Test
public void synchronize_groups_on_many_pages() {
    mapSettings.setProperty(GITLAB_AUTH_SYNC_USER_GROUPS, "true");
    OAuth2IdentityProvider.CallbackContext callbackContext = Mockito.mock(OAuth2IdentityProvider.CallbackContext.class);
    when(callbackContext.getCallbackUrl()).thenReturn("http://server/callback");
    HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
    when(httpServletRequest.getParameter("code")).thenReturn(ANY_CODE_VALUE);
    when(callbackContext.getRequest()).thenReturn(httpServletRequest);
    gitlab.enqueue(new MockResponse().setBody("{\n" + " \"access_token\": \"de6780bc506a0446309bd9362820ba8aed28aa506c71eedbe1c5c4f9dd350e54\",\n" + " \"token_type\": \"bearer\",\n" + " \"expires_in\": 7200,\n" + " \"refresh_token\": \"8257e65c97202ed1726cf9571600918f3bffb2544b26e00a61df9897668c33a1\"\n" + "}"));
    // response of /user
    gitlab.enqueue(new MockResponse().setBody("{\"id\": 123, \"username\": \"username\", \"name\": \"name\"}"));
    // response of /groups, first page
    gitlab.enqueue(new MockResponse().setBody("[{\"full_path\": \"group1\"}, {\"full_path\": \"group2\"}]").setHeader("Link", format(" <%s/groups?per_page=100&page=2>; rel=\"next\"," + "  <%s/groups?per_page=100&&page=3>; rel=\"last\"," + "  <%s/groups?per_page=100&&page=1>; rel=\"first\"", gitLabUrl, gitLabUrl, gitLabUrl)));
    // response of /groups, page 2
    gitlab.enqueue(new MockResponse().setBody("[{\"full_path\": \"group3\"}, {\"full_path\": \"group4\"}]").setHeader("Link", format("<%s/groups?per_page=100&page=3>; rel=\"next\"," + "  <%s/groups?per_page=100&&page=3>; rel=\"last\"," + "  <%s/groups?per_page=100&&page=1>; rel=\"first\"", gitLabUrl, gitLabUrl, gitLabUrl)));
    // response of /groups, page 3
    gitlab.enqueue(new MockResponse().setBody("[{\"full_path\": \"group5\"}, {\"full_path\": \"group6\"}]").setHeader("Link", format("<%s/groups?per_page=100&&page=3>; rel=\"last\"," + "  <%s/groups?per_page=100&&page=1>; rel=\"first\"", gitLabUrl, gitLabUrl)));
    gitLabIdentityProvider.callback(callbackContext);
    ArgumentCaptor<UserIdentity> captor = ArgumentCaptor.forClass(UserIdentity.class);
    verify(callbackContext).authenticate(captor.capture());
    UserIdentity value = captor.getValue();
    assertThat(value.getGroups()).contains("group1", "group2", "group3", "group4", "group5", "group6");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OAuth2IdentityProvider(org.sonar.api.server.authentication.OAuth2IdentityProvider) UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Example 3 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class UserIdentityFactoryTest method empty_name_is_replaced_by_provider_login.

@Test
public void empty_name_is_replaced_by_provider_login() {
    GsonUser gson = new GsonUser("john", "", "ABCD");
    UserIdentity identity = underTest.create(gson, null);
    assertThat(identity.getName()).isEqualTo("john");
}
Also used : UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Example 4 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class UserIdentityFactoryTest method null_name_is_replaced_by_provider_login.

@Test
public void null_name_is_replaced_by_provider_login() {
    GsonUser gson = new GsonUser("john", null, "ABCD");
    UserIdentity identity = underTest.create(gson, null);
    assertThat(identity.getName()).isEqualTo("john");
}
Also used : UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Example 5 with UserIdentity

use of org.sonar.api.server.authentication.UserIdentity in project sonarqube by SonarSource.

the class UserIdentityFactoryTest method create_login.

@Test
public void create_login() {
    GsonUser gson = new GsonUser("john", "John", "ABCD");
    UserIdentity identity = underTest.create(gson, null);
    assertThat(identity.getName()).isEqualTo("John");
    assertThat(identity.getEmail()).isNull();
    assertThat(identity.getProviderId()).isEqualTo("ABCD");
}
Also used : UserIdentity(org.sonar.api.server.authentication.UserIdentity) Test(org.junit.Test)

Aggregations

UserIdentity (org.sonar.api.server.authentication.UserIdentity)18 Test (org.junit.Test)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Source (org.sonar.server.authentication.event.AuthenticationEvent.Source)5 IdentityProvider (org.sonar.api.server.authentication.IdentityProvider)4 MockResponse (okhttp3.mockwebserver.MockResponse)3 OAuth2IdentityProvider (org.sonar.api.server.authentication.OAuth2IdentityProvider)3 UserDto (org.sonar.db.user.UserDto)3 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)3 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)2 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)2 UserDetails (org.sonar.api.security.UserDetails)1