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 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 ArgumentCaptor<Cookie> cookieArgumentCaptor = ArgumentCaptor.forClass(Cookie.class);
46 private HttpServletResponse response = mock(HttpServletResponse.class);
47 private HttpServletRequest request = mock(HttpServletRequest.class);
49 private OAuth2AuthenticationParameters underTest = new OAuth2AuthenticationParametersImpl();
53 when(request.getContextPath()).thenReturn("");
57 public void init_create_cookie() {
58 when(request.getParameter("return_to")).thenReturn("/settings");
60 underTest.init(request, response);
62 verify(response).addCookie(cookieArgumentCaptor.capture());
63 Cookie cookie = cookieArgumentCaptor.getValue();
64 assertThat(cookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
65 assertThat(cookie.getValue()).isNotEmpty();
66 assertThat(cookie.getPath()).isEqualTo("/");
67 assertThat(cookie.isHttpOnly()).isTrue();
68 assertThat(cookie.getMaxAge()).isEqualTo(300);
69 assertThat(cookie.getSecure()).isFalse();
73 public void init_does_not_create_cookie_when_no_parameter() {
74 underTest.init(request, response);
76 verify(response, never()).addCookie(any(Cookie.class));
80 public void init_does_not_create_cookie_when_parameters_are_empty() {
81 when(request.getParameter("return_to")).thenReturn("");
82 when(request.getParameter("allowEmailShift")).thenReturn("");
84 underTest.init(request, response);
86 verify(response, never()).addCookie(any(Cookie.class));
90 public void init_does_not_create_cookie_when_parameters_are_null() {
91 when(request.getParameter("return_to")).thenReturn(null);
92 when(request.getParameter("allowEmailShift")).thenReturn(null);
94 underTest.init(request, response);
96 verify(response, never()).addCookie(any(Cookie.class));
100 @DataProvider({"http://example.com", "/\t/example.com", "//local_file", "/\\local_file", "something_else"})
101 public void return_to_is_not_set_when_not_local(String url) {
102 when(request.getParameter("return_to")).thenReturn(url);
104 underTest.init(request, response);
106 verify(response, never()).addCookie(any());
110 public void get_return_to_parameter() {
111 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});
113 Optional<String> redirection = underTest.getReturnTo(request);
115 assertThat(redirection).isNotEmpty();
116 assertThat(redirection.get()).isEqualTo("/settings");
120 public void get_return_to_is_empty_when_no_cookie() {
121 when(request.getCookies()).thenReturn(new Cookie[] {});
123 Optional<String> redirection = underTest.getReturnTo(request);
125 assertThat(redirection).isEmpty();
129 public void get_return_to_is_empty_when_no_value() {
130 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{}")});
132 Optional<String> redirection = underTest.getReturnTo(request);
134 assertThat(redirection).isEmpty();
138 public void get_allowEmailShift_parameter() {
139 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"allowEmailShift\":\"true\"}")});
141 Optional<Boolean> allowEmailShift = underTest.getAllowEmailShift(request);
143 assertThat(allowEmailShift).isNotEmpty();
144 assertThat(allowEmailShift.get()).isTrue();
148 public void get_allowEmailShift_is_empty_when_no_cookie() {
149 when(request.getCookies()).thenReturn(new Cookie[] {});
151 Optional<Boolean> allowEmailShift = underTest.getAllowEmailShift(request);
153 assertThat(allowEmailShift).isEmpty();
157 public void get_allowEmailShift_is_empty_when_no_value() {
158 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{}")});
160 Optional<Boolean> allowEmailShift = underTest.getAllowEmailShift(request);
162 assertThat(allowEmailShift).isEmpty();
166 public void getAllowUpdateLogin_is_empty_when_no_cookie() {
167 when(request.getCookies()).thenReturn(new Cookie[] {});
169 Optional<Boolean> allowLoginUpdate = underTest.getAllowUpdateLogin(request);
171 assertThat(allowLoginUpdate).isEmpty();
175 public void getAllowUpdateLogin_is_empty_when_no_value() {
176 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{}")});
178 Optional<Boolean> allowLoginUpdate = underTest.getAllowUpdateLogin(request);
180 assertThat(allowLoginUpdate).isEmpty();
184 public void delete() {
185 when(request.getCookies()).thenReturn(new Cookie[] {new Cookie(AUTHENTICATION_COOKIE_NAME, "{\"return_to\":\"/settings\"}")});
187 underTest.delete(request, response);
189 verify(response).addCookie(cookieArgumentCaptor.capture());
190 Cookie updatedCookie = cookieArgumentCaptor.getValue();
191 assertThat(updatedCookie.getName()).isEqualTo(AUTHENTICATION_COOKIE_NAME);
192 assertThat(updatedCookie.getValue()).isNull();
193 assertThat(updatedCookie.getPath()).isEqualTo("/");
194 assertThat(updatedCookie.getMaxAge()).isZero();