3 * Copyright (C) 2009-2024 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 java.util.Optional;
25 import javax.servlet.http.Cookie;
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.mockito.ArgumentCaptor;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
40 @RunWith(DataProviderRunner.class)
41 public class OAuth2AuthenticationParametersImplTest {
43 private static final String AUTHENTICATION_COOKIE_NAME = "AUTH-PARAMS";
44 private final ArgumentCaptor<Cookie> cookieArgumentCaptor = ArgumentCaptor.forClass(Cookie.class);
45 private final HttpServletResponse response = mock(HttpServletResponse.class);
46 private final HttpServletRequest request = mock(HttpServletRequest.class);
48 private final OAuth2AuthenticationParameters underTest = new OAuth2AuthenticationParametersImpl();
52 when(request.getContextPath()).thenReturn("");
56 public void init_create_cookie() {
57 when(request.getParameter("return_to")).thenReturn("/settings");
59 underTest.init(request, response);
61 verify(response).addCookie(cookieArgumentCaptor.capture());
62 Cookie cookie = cookieArgumentCaptor.getValue();
63 assertThat(cookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
64 assertThat(cookie.getValue()).isNotEmpty();
65 assertThat(cookie.getPath()).isEqualTo("/");
66 assertThat(cookie.isHttpOnly()).isTrue();
67 assertThat(cookie.getMaxAge()).isEqualTo(300);
68 assertThat(cookie.getSecure()).isFalse();
72 public void init_does_not_create_cookie_when_no_parameter() {
73 underTest.init(request, response);
75 verify(response, never()).addCookie(any(Cookie.class));
79 public void init_does_not_create_cookie_when_parameters_are_empty() {
80 when(request.getParameter("return_to")).thenReturn("");
81 when(request.getParameter("allowEmailShift")).thenReturn("");
83 underTest.init(request, response);
85 verify(response, never()).addCookie(any(Cookie.class));
89 public void init_does_not_create_cookie_when_parameters_are_null() {
90 when(request.getParameter("return_to")).thenReturn(null);
91 when(request.getParameter("allowEmailShift")).thenReturn(null);
93 underTest.init(request, response);
95 verify(response, never()).addCookie(any(Cookie.class));
99 @DataProvider({"http://example.com", "/\t/example.com", "//local_file", "/\\local_file", "something_else"})
100 public void get_return_to_is_not_set_when_not_local(String url) {
101 when(request.getParameter("return_to")).thenReturn(url);
103 assertThat(underTest.getReturnTo(request)).isEmpty();
107 public void get_return_to_parameter() {
108 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});
110 Optional<String> redirection = underTest.getReturnTo(request);
112 assertThat(redirection).contains("/settings");
116 public void get_return_to_is_empty_when_no_cookie() {
117 when(request.getCookies()).thenReturn(new Cookie[] {});
119 Optional<String> redirection = underTest.getReturnTo(request);
121 assertThat(redirection).isEmpty();
125 public void get_return_to_is_empty_when_no_value() {
126 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{}")});
128 Optional<String> redirection = underTest.getReturnTo(request);
130 assertThat(redirection).isEmpty();
134 public void delete() {
135 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});
137 underTest.delete(request, response);
139 verify(response).addCookie(cookieArgumentCaptor.capture());
140 Cookie updatedCookie = cookieArgumentCaptor.getValue();
141 assertThat(updatedCookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
142 assertThat(updatedCookie.getValue()).isNull();
143 assertThat(updatedCookie.getPath()).isEqualTo("/");
144 assertThat(updatedCookie.getMaxAge()).isZero();