You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ResetPasswordFilterTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.authentication;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import javax.servlet.FilterChain;
  25. import javax.servlet.FilterConfig;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import org.sonar.server.user.ThreadLocalUserSession;
  32. import static org.mockito.ArgumentMatchers.any;
  33. import static org.mockito.ArgumentMatchers.eq;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.never;
  36. import static org.mockito.Mockito.verify;
  37. import static org.mockito.Mockito.verifyNoInteractions;
  38. import static org.mockito.Mockito.when;
  39. @RunWith(DataProviderRunner.class)
  40. public class ResetPasswordFilterTest {
  41. private final HttpServletRequest request = mock(HttpServletRequest.class);
  42. private final HttpServletResponse response = mock(HttpServletResponse.class);
  43. private final FilterChain chain = mock(FilterChain.class);
  44. private final ThreadLocalUserSession session = mock(ThreadLocalUserSession.class);
  45. private final ResetPasswordFilter underTest = new ResetPasswordFilter(session);
  46. @Before
  47. public void before() {
  48. // set URI to valid for redirect
  49. when(request.getRequestURI()).thenReturn("/");
  50. when(request.getContextPath()).thenReturn("");
  51. // set reset password conditions
  52. when(session.hasSession()).thenReturn(true);
  53. when(session.isLoggedIn()).thenReturn(true);
  54. when(session.shouldResetPassword()).thenReturn(true);
  55. }
  56. @Test
  57. public void verify_other_methods() {
  58. underTest.init(mock(FilterConfig.class));
  59. underTest.destroy();
  60. verifyNoInteractions(request, response, chain, session);
  61. }
  62. @Test
  63. public void redirect_if_reset_password_set() throws Exception {
  64. underTest.doFilter(request, response, chain);
  65. verify(response).sendRedirect("/account/reset_password");
  66. }
  67. @Test
  68. public void redirect_if_reset_password_set_and_web_context_configured() throws Exception {
  69. when(request.getContextPath()).thenReturn("/sonarqube");
  70. underTest.doFilter(request, response, chain);
  71. verify(response).sendRedirect("/sonarqube/account/reset_password");
  72. }
  73. @Test
  74. public void redirect_if_request_uri_ends_with_slash() throws Exception {
  75. when(request.getRequestURI()).thenReturn("/projects/");
  76. when(request.getContextPath()).thenReturn("/sonarqube");
  77. underTest.doFilter(request, response, chain);
  78. verify(response).sendRedirect("/sonarqube/account/reset_password");
  79. }
  80. @Test
  81. public void do_not_redirect_if_no_session() throws Exception {
  82. when(session.hasSession()).thenReturn(false);
  83. underTest.doFilter(request, response, chain);
  84. verify(response, never()).sendRedirect(any());
  85. }
  86. @Test
  87. public void do_not_redirect_if_not_logged_in() throws Exception {
  88. when(session.isLoggedIn()).thenReturn(false);
  89. underTest.doFilter(request, response, chain);
  90. verify(response, never()).sendRedirect(any());
  91. }
  92. @Test
  93. public void do_not_redirect_if_reset_password_not_set() throws Exception {
  94. when(session.shouldResetPassword()).thenReturn(false);
  95. underTest.doFilter(request, response, chain);
  96. verify(response, never()).sendRedirect(any());
  97. }
  98. @Test
  99. @UseDataProvider("skipped_urls")
  100. public void doGetPattern_verify(String urltoSkip) throws Exception {
  101. when(request.getRequestURI()).thenReturn(urltoSkip);
  102. when(request.getContextPath()).thenReturn("");
  103. underTest.doGetPattern().matches(urltoSkip);
  104. verify(response, never()).sendRedirect(any());
  105. }
  106. @DataProvider
  107. public static Object[][] skipped_urls() {
  108. return new Object[][] {
  109. {"/batch/index"},
  110. {"/batch/file"},
  111. {"/api/issues"},
  112. {"/api/issues/"},
  113. {"/api/*"},
  114. {"/account/reset_password"},
  115. };
  116. }
  117. }