|
| 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