-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
OAuth2CallbackFilterTest.java
152 lines (117 loc) · 5.55 KB
/
OAuth2CallbackFilterTest.java
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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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.server.authentication;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
import org.sonar.api.server.authentication.UnauthorizedException;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class OAuth2CallbackFilterTest {
static String OAUTH2_PROVIDER_KEY = "github";
@Rule
public LogTester logTester = new LogTester();
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule();
OAuth2ContextFactory oAuth2ContextFactory = mock(OAuth2ContextFactory.class);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);
FakeOAuth2IdentityProvider oAuth2IdentityProvider = new FakeOAuth2IdentityProvider(OAUTH2_PROVIDER_KEY, true);
OAuth2IdentityProvider.InitContext oauth2Context = mock(OAuth2IdentityProvider.InitContext.class);
OAuth2CallbackFilter underTest = new OAuth2CallbackFilter(identityProviderRepository, oAuth2ContextFactory);
@Before
public void setUp() throws Exception {
when(oAuth2ContextFactory.newContext(request, response, oAuth2IdentityProvider)).thenReturn(oauth2Context);
}
@Test
public void do_get_pattern() throws Exception {
assertThat(underTest.doGetPattern()).isNotNull();
}
@Test
public void do_filter_on_auth2_identity_provider() throws Exception {
when(request.getRequestURI()).thenReturn("/oauth2/callback/" + OAUTH2_PROVIDER_KEY);
identityProviderRepository.addIdentityProvider(oAuth2IdentityProvider);
underTest.doFilter(request, response, chain);
assertCallbackCalled();
}
@Test
public void fail_on_not_oauth2_provider() throws Exception {
String providerKey = "openid";
when(request.getRequestURI()).thenReturn("/oauth2/callback/" + providerKey);
identityProviderRepository.addIdentityProvider(new FakeBasicIdentityProvider(providerKey, true));
underTest.doFilter(request, response, chain);
assertError("Not an OAuth2IdentityProvider: class org.sonar.server.authentication.FakeBasicIdentityProvider");
}
@Test
public void fail_on_disabled_provider() throws Exception {
when(request.getRequestURI()).thenReturn("/oauth2/callback/" + OAUTH2_PROVIDER_KEY);
identityProviderRepository.addIdentityProvider(new FakeOAuth2IdentityProvider(OAUTH2_PROVIDER_KEY, false));
underTest.doFilter(request, response, chain);
assertError("Fail to callback authentication with 'github'");
}
@Test
public void redirect_when_failing_because_of_UnauthorizedExceptionException() throws Exception {
TestIdentityProvider identityProvider = new FailWithUnauthorizedExceptionIdProvider()
.setKey("failing")
.setEnabled(true);
when(request.getRequestURI()).thenReturn("/oauth2/callback/" + identityProvider.getKey());
identityProviderRepository.addIdentityProvider(identityProvider);
underTest.doFilter(request, response, chain);
verify(response).sendRedirect("/sessions/unauthorized?message=Email+john%40email.com+is+already+used");
}
@Test
public void fail_when_no_oauth2_provider_provided() throws Exception {
String providerKey = "openid";
when(request.getRequestURI()).thenReturn("/oauth2/callback");
identityProviderRepository.addIdentityProvider(new FakeBasicIdentityProvider(providerKey, true));
underTest.doFilter(request, response, chain);
assertError("Fail to callback authentication");
}
private void assertCallbackCalled(){
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(oAuth2IdentityProvider.isCallbackCalled()).isTrue();
}
private void assertError(String expectedError) throws Exception {
assertThat(logTester.logs(LoggerLevel.ERROR)).contains(expectedError);
verify(response).sendRedirect("/sessions/unauthorized");
assertThat(oAuth2IdentityProvider.isInitCalled()).isFalse();
}
private static class FailWithUnauthorizedExceptionIdProvider extends TestIdentityProvider implements OAuth2IdentityProvider {
@Override
public void init(InitContext context) {
}
@Override
public void callback(CallbackContext context) {
throw new UnauthorizedException("Email john@email.com is already used");
}
}
}