3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.authentication;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.http.HttpSession;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentCaptor;
28 import org.sonar.api.platform.Server;
29 import org.sonar.api.server.authentication.BaseIdentityProvider;
30 import org.sonar.api.server.authentication.UserIdentity;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.user.TestUserSessionFactory;
33 import org.sonar.server.user.ThreadLocalUserSession;
34 import org.sonar.server.user.UserSession;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.ArgumentMatchers.eq;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
43 public class BaseContextFactoryTest {
45 private static final String PUBLIC_ROOT_URL = "https://mydomain.com";
47 private static final UserIdentity USER_IDENTITY = UserIdentity.builder()
48 .setProviderId("ABCD")
49 .setProviderLogin("johndoo")
50 .setLogin("id:johndoo")
52 .setEmail("john@email.com")
55 private ThreadLocalUserSession threadLocalUserSession = mock(ThreadLocalUserSession.class);
57 private TestUserRegistrar userIdentityAuthenticator = new TestUserRegistrar();
58 private Server server = mock(Server.class);
60 private HttpServletRequest request = mock(HttpServletRequest.class);
61 private HttpServletResponse response = mock(HttpServletResponse.class);
62 private BaseIdentityProvider identityProvider = mock(BaseIdentityProvider.class);
63 private JwtHttpHandler jwtHttpHandler = mock(JwtHttpHandler.class);
64 private TestUserSessionFactory userSessionFactory = TestUserSessionFactory.standalone();
66 private BaseContextFactory underTest = new BaseContextFactory(userIdentityAuthenticator, server, jwtHttpHandler, threadLocalUserSession, userSessionFactory);
69 public void setUp() throws Exception {
70 when(server.getPublicRootUrl()).thenReturn(PUBLIC_ROOT_URL);
71 when(identityProvider.getName()).thenReturn("GitHub");
72 when(identityProvider.getKey()).thenReturn("github");
73 when(request.getSession()).thenReturn(mock(HttpSession.class));
77 public void create_context() {
78 BaseIdentityProvider.Context context = underTest.newContext(request, response, identityProvider);
80 assertThat(context.getRequest()).isEqualTo(request);
81 assertThat(context.getResponse()).isEqualTo(response);
82 assertThat(context.getServerBaseURL()).isEqualTo(PUBLIC_ROOT_URL);
86 public void authenticate() {
87 BaseIdentityProvider.Context context = underTest.newContext(request, response, identityProvider);
88 ArgumentCaptor<UserDto> userArgumentCaptor = ArgumentCaptor.forClass(UserDto.class);
90 context.authenticate(USER_IDENTITY);
92 assertThat(userIdentityAuthenticator.isAuthenticated()).isTrue();
93 verify(threadLocalUserSession).set(any(UserSession.class));
94 verify(jwtHttpHandler).generateToken(userArgumentCaptor.capture(), eq(request), eq(response));
95 assertThat(userArgumentCaptor.getValue().getLogin()).isEqualTo(USER_IDENTITY.getLogin());
96 assertThat(userArgumentCaptor.getValue().getExternalId()).isEqualTo(USER_IDENTITY.getProviderId());
97 assertThat(userArgumentCaptor.getValue().getExternalLogin()).isEqualTo(USER_IDENTITY.getProviderLogin());
98 assertThat(userArgumentCaptor.getValue().getExternalIdentityProvider()).isEqualTo("github");