Skip to content

Commit 5f54e39

Browse files
committed
Added unit tests
1 parent f9c1eeb commit 5f54e39

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed

src/main/java/com/faforever/api/user/SteamService.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,31 @@ void validateSteamRedirect(HttpServletRequest request) {
9393
}
9494

9595
void handleInvalidOpenIdRedirect(final HttpServletRequest request, final String openIdResponseBody) {
96-
final String steamId = parseSteamIdFromLoginRedirect(request);
96+
boolean containsIdentityParam = request.getParameterMap().containsKey("openid.identity");
97+
final String steamId;
98+
99+
if (containsIdentityParam)
100+
{
101+
steamId = parseSteamIdFromLoginRedirect(request);
102+
} else {
103+
log.warn("Steam redirect could not be validated! The request does not contain 'openid.identity' parameter. Original OpenID response:\n {}", openIdResponseBody);
104+
throw ApiException.of(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED);
105+
}
97106

98107
if (StringUtils.isNotBlank(steamId)) {
99108
accountLinkRepository.findOneByServiceIdAndServiceType(steamId,
100109
LinkedServiceType.STEAM).map(AccountLink::getUser).ifPresentOrElse(u ->
101110
log.warn(
102-
"Steam redirect could not be validated for user with id: ''{}'' and login: ''{}''. Original OpenId response:\n{}",
111+
"Steam redirect could not be validated for user with id: ''{}'' and login: ''{}''. Original OpenID response:\n {}",
103112
u.getId(), u.getLogin(), openIdResponseBody),
104113
() ->
105114
log.warn(
106-
"Steam redirect could not be validated! The steam id ''{}'' does not match any account. Original OpenId response:\n{}",
115+
"Steam redirect could not be validated! The steam id ''{}'' does not match any account. Original OpenID response:\n {}",
107116
StringUtils.deleteWhitespace(steamId).replace("'", ""), // prevent potential log poisoning attack
108117
openIdResponseBody)
109118
);
110-
}
111-
else {
112-
log.warn("Steam redirect could not be validated! The steamId from the OpenId redirect is blank. Original OpenId response:\n{}", openIdResponseBody);
119+
} else {
120+
log.warn("Steam redirect could not be validated! The steamId from the OpenId redirect is blank. Original OpenID response:\n {}", openIdResponseBody);
113121
}
114122

115123
throw ApiException.of(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.faforever.api.user;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.ArgumentMatchers.any;
5+
import static org.mockito.ArgumentMatchers.anyString;
6+
import static org.mockito.Mockito.when;
7+
8+
import com.faforever.api.data.domain.AccountLink;
9+
import com.faforever.api.data.domain.LinkedServiceType;
10+
import com.faforever.api.data.domain.User;
11+
import com.faforever.api.error.ApiException;
12+
import com.faforever.api.error.ErrorCode;
13+
import jakarta.servlet.http.HttpServletRequest;
14+
import java.util.Collections;
15+
import java.util.Map;
16+
import java.util.Optional;
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.api.extension.ExtendWith;
19+
import org.mockito.InjectMocks;
20+
import org.mockito.Mock;
21+
import org.mockito.Mockito;
22+
import org.mockito.junit.jupiter.MockitoExtension;
23+
24+
@ExtendWith(MockitoExtension.class)
25+
class SteamServiceTest {
26+
27+
private static final String IDENTITY_NAME_PARAM = "openid.identity";
28+
private static final String DUMMY_URL = "valid.url.domain/login/123";
29+
private static final String DUMMY_RESPONSE = "dummy response";
30+
@Mock
31+
private AccountLinkRepository accountLinkRepositoryMock;
32+
@InjectMocks
33+
private SteamService beanUnderTest;
34+
35+
@Test
36+
void testHandleInvalidOpenIdRedirect() {
37+
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
38+
when(requestMock.getParameterMap())
39+
.thenReturn(Map.of(IDENTITY_NAME_PARAM, new String[]{DUMMY_URL}));
40+
when(requestMock.getParameter(IDENTITY_NAME_PARAM)).thenReturn(DUMMY_URL);
41+
42+
ApiException thrownException = assertThrows(ApiException.class,
43+
() -> beanUnderTest.handleInvalidOpenIdRedirect(requestMock, DUMMY_RESPONSE));
44+
assertEquals(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED,
45+
thrownException.getErrors()[0].getErrorCode());
46+
}
47+
48+
@Test
49+
void testHandleInvalidOpenIdRedirectBlankIdentityParam() {
50+
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
51+
final String blankDummyUrl = "";
52+
when(requestMock.getParameterMap())
53+
.thenReturn(Map.of(IDENTITY_NAME_PARAM, new String[]{blankDummyUrl}));
54+
when(requestMock.getParameter(IDENTITY_NAME_PARAM)).thenReturn(blankDummyUrl);
55+
56+
ApiException thrownException = assertThrows(ApiException.class,
57+
() -> beanUnderTest.handleInvalidOpenIdRedirect(requestMock, DUMMY_RESPONSE));
58+
assertEquals(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED,
59+
thrownException.getErrors()[0].getErrorCode());
60+
}
61+
62+
@Test
63+
void testHandleInvalidOpenIdRedirectNoIdentityInRequest() {
64+
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
65+
when(requestMock.getParameterMap()).thenReturn(Collections.emptyMap());
66+
67+
ApiException thrownException = assertThrows(ApiException.class,
68+
() -> beanUnderTest.handleInvalidOpenIdRedirect(requestMock, DUMMY_RESPONSE));
69+
assertEquals(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED,
70+
thrownException.getErrors()[0].getErrorCode());
71+
}
72+
73+
@Test
74+
void testHandleInvalidOpenIdRedirectLinkedAccountExists() {
75+
HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);
76+
User userMock = Mockito.mock(User.class);
77+
when(userMock.getId()).thenReturn(1);
78+
when(userMock.getLogin()).thenReturn("dummyLogin");
79+
AccountLink accountLinkMock = Mockito.mock(AccountLink.class);
80+
when(accountLinkMock.getUser()).thenReturn(userMock);
81+
when(requestMock.getParameterMap())
82+
.thenReturn(Map.of(IDENTITY_NAME_PARAM, new String[]{DUMMY_URL}));
83+
when(requestMock.getParameter(IDENTITY_NAME_PARAM)).thenReturn(DUMMY_URL);
84+
when(accountLinkRepositoryMock.findOneByServiceIdAndServiceType(anyString(),
85+
any(LinkedServiceType.class))).thenReturn(
86+
Optional.of(accountLinkMock));
87+
88+
ApiException thrownException = assertThrows(ApiException.class,
89+
() -> beanUnderTest.handleInvalidOpenIdRedirect(requestMock, DUMMY_RESPONSE));
90+
assertEquals(ErrorCode.STEAM_LOGIN_VALIDATION_FAILED,
91+
thrownException.getErrors()[0].getErrorCode());
92+
}
93+
94+
}

0 commit comments

Comments
 (0)