aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-auth-saml/src/test/java/org/sonar/auth/saml/SamlAuthenticatorTest.java
blob: 8ed6894081fa26c3db1ba4b6dd83f86e6eb146f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.auth.saml;

import java.io.IOException;
import java.io.UncheckedIOException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
import org.sonar.api.server.authentication.UserIdentity;
import org.sonar.server.http.JakartaHttpRequest;
import org.sonar.server.http.JakartaHttpResponse;
import org.springframework.security.saml2.core.Saml2Error;
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticatedPrincipal;
import org.springframework.security.saml2.provider.service.authentication.Saml2AuthenticationException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class SamlAuthenticatorTest {

  public static final String CALLBACK_URL = "callbackUrl";
  public static final String RELAY_STATE = "relayState";
  public static final String REDIRECT_URL = "redirectUrl";

  @Mock
  private RedirectToUrlProvider redirectToUrlProvider;
  @Mock
  private SamlResponseAuthenticator samlResponseAuthenticator;
  @Mock
  private PrincipalToUserIdentityConverter principalToUserIdentityConverter;
  @Mock
  private SamlStatusChecker samlStatusChecker;
  @Mock
  private SamlAuthStatusPageGenerator samlAuthStatusPageGenerator;
  @Mock
  private SamlAuthenticationStatus samlAuthenticationStatus;

  @InjectMocks
  private SamlAuthenticator samlAuthenticator;

  @Mock
  private JakartaHttpRequest request;
  @Mock
  private JakartaHttpResponse response;

  @Test
  void initLogin_generatesUrlAndSendsRedirect() throws IOException {
    when(redirectToUrlProvider.getRedirectToUrl(request, CALLBACK_URL, RELAY_STATE)).thenReturn(REDIRECT_URL);

    samlAuthenticator.initLogin(CALLBACK_URL, RELAY_STATE, request, response);

    verify(response).sendRedirect(REDIRECT_URL);
  }

  @Test
  void initLogin_whenIoException_convertsToRuntimeException() throws IOException {
    when(redirectToUrlProvider.getRedirectToUrl(request, CALLBACK_URL, RELAY_STATE)).thenReturn(REDIRECT_URL);

    IOException ioException = new IOException();
    doThrow(ioException).when(response).sendRedirect(REDIRECT_URL);

    assertThatException()
      .isThrownBy(() -> samlAuthenticator.initLogin(CALLBACK_URL, RELAY_STATE, request, response))
      .isInstanceOf(UncheckedIOException.class)
      .withCause(ioException);
  }

  @Test
  void onCallback_verifiesCsrfStateAndAuthenticates() {
    OAuth2IdentityProvider.CallbackContext context = mock();
    when(context.getCallbackUrl()).thenReturn(CALLBACK_URL);

    Saml2AuthenticatedPrincipal principal = mock();
    when(samlResponseAuthenticator.authenticate(request, CALLBACK_URL)).thenReturn(principal);

    UserIdentity userIdentity = mock();
    when(principalToUserIdentityConverter.convertToUserIdentity(principal)).thenReturn(userIdentity);

    UserIdentity actualUserIdentity = samlAuthenticator.onCallback(context, request);

    verify(context).verifyCsrfState("RelayState");
    verify(samlResponseAuthenticator).authenticate(request, CALLBACK_URL);
    assertThat(actualUserIdentity).isEqualTo(userIdentity);
  }

  @Test
  void getAuthenticationStatusPage_returnsHtml() {
    String samlResponse = "samlResponse";
    when(request.getRequestURL()).thenReturn("url");
    when(request.getParameter("SAMLResponse")).thenReturn(samlResponse);

    Saml2AuthenticatedPrincipal principal = mock();
    when(samlResponseAuthenticator.authenticate(request, request.getRequestURL())).thenReturn(principal);

    when(samlStatusChecker.getSamlAuthenticationStatus(samlResponse, principal)).thenReturn(samlAuthenticationStatus);
    when(samlAuthStatusPageGenerator.getSamlAuthStatusHtml(request, samlAuthenticationStatus)).thenReturn("html");

    String authenticationStatusPage = samlAuthenticator.getAuthenticationStatusPage(request);

    assertThat(authenticationStatusPage).isEqualTo("html");
  }

  @Test
  void getAuthenticationStatusPage_whenSaml2AuthenticationException_returnsHtml() {
    when(request.getRequestURL()).thenReturn("url");

    String errorMessage = "error";
    when(samlResponseAuthenticator.authenticate(request, request.getRequestURL())).thenThrow(new Saml2AuthenticationException(new Saml2Error("erorCode", errorMessage)));

    when(samlStatusChecker.getSamlAuthenticationStatus(errorMessage)).thenReturn(samlAuthenticationStatus);
    when(samlAuthStatusPageGenerator.getSamlAuthStatusHtml(request, samlAuthenticationStatus)).thenReturn("html");

    String authenticationStatusPage = samlAuthenticator.getAuthenticationStatusPage(request);

    assertThat(authenticationStatusPage).isEqualTo("html");
  }

  @Test
  void getAuthenticationStatusPage_whenIllegalStateException_returnsHtml() {
    when(request.getRequestURL()).thenReturn("url");

    RuntimeException runtimeException = new RuntimeException("error");
    IllegalStateException illegalStateException = new IllegalStateException(runtimeException);
    when(samlResponseAuthenticator.authenticate(request, request.getRequestURL())).thenThrow(illegalStateException);

    when(samlStatusChecker.getSamlAuthenticationStatus(any())).thenReturn(samlAuthenticationStatus);
    when(samlAuthStatusPageGenerator.getSamlAuthStatusHtml(request, samlAuthenticationStatus)).thenReturn("html");

    String authenticationStatusPage = samlAuthenticator.getAuthenticationStatusPage(request);

    assertThat(authenticationStatusPage).isEqualTo("html");
    verify(samlAuthStatusPageGenerator).getSamlAuthStatusHtml(request, samlAuthenticationStatus);
  }

}