3 * Copyright (C) 2009-2021 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 com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.sonar.server.user.ThreadLocalUserSession;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.never;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.verifyNoInteractions;
40 import static org.mockito.Mockito.when;
42 @RunWith(DataProviderRunner.class)
43 public class ResetPasswordFilterTest {
45 private final HttpServletRequest request = mock(HttpServletRequest.class);
46 private final HttpServletResponse response = mock(HttpServletResponse.class);
47 private final FilterChain chain = mock(FilterChain.class);
48 private final ThreadLocalUserSession session = mock(ThreadLocalUserSession.class);
50 private final ResetPasswordFilter underTest = new ResetPasswordFilter(session);
53 public void before() {
54 // set URI to valid for redirect
55 when(request.getRequestURI()).thenReturn("/");
56 when(request.getContextPath()).thenReturn("");
58 // set reset password conditions
59 when(session.hasSession()).thenReturn(true);
60 when(session.isLoggedIn()).thenReturn(true);
61 when(session.shouldResetPassword()).thenReturn(true);
65 public void verify_other_methods() {
66 underTest.init(mock(FilterConfig.class));
69 verifyNoInteractions(request, response, chain, session);
73 public void redirect_if_reset_password_set() throws Exception {
74 underTest.doFilter(request, response, chain);
76 verify(response).sendRedirect(eq("/account/reset_password"));
80 public void redirect_if_reset_password_set_and_web_context_configured() throws Exception {
81 when(request.getContextPath()).thenReturn("/sonarqube");
83 underTest.doFilter(request, response, chain);
85 verify(response).sendRedirect(eq("/sonarqube/account/reset_password"));
89 public void redirect_if_request_uri_ends_with_slash() throws Exception {
90 when(request.getRequestURI()).thenReturn("/projects/");
91 when(request.getContextPath()).thenReturn("/sonarqube");
93 underTest.doFilter(request, response, chain);
95 verify(response).sendRedirect(eq("/sonarqube/account/reset_password"));
99 public void do_not_redirect_if_no_session() throws Exception {
100 when(session.hasSession()).thenReturn(false);
102 underTest.doFilter(request, response, chain);
104 verify(response, never()).sendRedirect(any());
108 public void do_not_redirect_if_not_logged_in() throws Exception {
109 when(session.isLoggedIn()).thenReturn(false);
111 underTest.doFilter(request, response, chain);
113 verify(response, never()).sendRedirect(any());
117 public void do_not_redirect_if_reset_password_not_set() throws Exception {
118 when(session.shouldResetPassword()).thenReturn(false);
120 underTest.doFilter(request, response, chain);
122 verify(response, never()).sendRedirect(any());
126 @UseDataProvider("skipped_urls")
127 public void doGetPattern_verify(String urltoSkip) throws Exception {
128 when(request.getRequestURI()).thenReturn(urltoSkip);
129 when(request.getContextPath()).thenReturn("");
130 underTest.doGetPattern().matches(urltoSkip);
132 verify(response, never()).sendRedirect(any());
136 public static Object[][] skipped_urls() {
137 return new Object[][] {
143 {"/account/reset_password"},