|
| 1 | +/** |
| 2 | + * Copyright (c) 2026 Eclipse Foundation AISBL |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made |
| 5 | + * available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package org.eclipse.openvsx.ratelimit; |
| 11 | + |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +import org.eclipse.openvsx.UserService; |
| 15 | +import org.eclipse.openvsx.accesstoken.AccessTokenService; |
| 16 | +import org.eclipse.openvsx.entities.UserData; |
| 17 | +import org.eclipse.openvsx.ratelimit.config.RateLimitProperties; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 22 | +import org.mockito.ArgumentMatchers; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 26 | +import org.springframework.boot.test.context.TestConfiguration; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.expression.ExpressionParser; |
| 29 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 30 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 32 | + |
| 33 | +import jakarta.servlet.http.HttpServletRequest; |
| 34 | + |
| 35 | +@ExtendWith(SpringExtension.class) |
| 36 | +@MockitoBean(types = { |
| 37 | + ConfigurableBeanFactory.class, |
| 38 | + AccessTokenService.class |
| 39 | +}) |
| 40 | +public class IdentityServiceTest { |
| 41 | + |
| 42 | + @MockitoBean |
| 43 | + CustomerService customerService; |
| 44 | + |
| 45 | + @MockitoBean |
| 46 | + UserService users; |
| 47 | + |
| 48 | + @MockitoBean |
| 49 | + TierService tierService; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + IdentityService service; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testResolveIdentityAuthenticatedUser() { |
| 56 | + var request = mockRequest(); |
| 57 | + var userData = mockUserData(); |
| 58 | + |
| 59 | + Mockito.when(customerService.getCustomerByIpAddress(ArgumentMatchers.anyString())).thenReturn(Optional.empty()); |
| 60 | + Mockito.when(tierService.getFreeTier()).thenReturn(Optional.empty()); |
| 61 | + Mockito.when(tierService.getSafetyTier()).thenReturn(Optional.empty()); |
| 62 | + |
| 63 | + var resolvedIdentity = service.resolveIdentity(request); |
| 64 | + |
| 65 | + assertTrue(resolvedIdentity.cacheKey().startsWith("user_"), "Cache key should start with 'user_'"); |
| 66 | + assertEquals("user_" + userData.getAuthId(), resolvedIdentity.cacheKey(), "Cache key should be based on user auth ID"); |
| 67 | + } |
| 68 | + |
| 69 | + private HttpServletRequest mockRequest() { |
| 70 | + var request = Mockito.mock(HttpServletRequest.class); |
| 71 | + Mockito.when(request.getParameter("token")).thenReturn(null); |
| 72 | + return request; |
| 73 | + } |
| 74 | + |
| 75 | + private UserData mockUserData() { |
| 76 | + var userData = new UserData(); |
| 77 | + userData.setLoginName("test_user"); |
| 78 | + userData.setFullName("Test User"); |
| 79 | + userData.setAuthId("test_auth_id"); |
| 80 | + userData.setProviderUrl("http://example.com/test"); |
| 81 | + Mockito.doReturn(userData).when(users).findLoggedInUser(); |
| 82 | + return userData; |
| 83 | + } |
| 84 | + |
| 85 | + @TestConfiguration |
| 86 | + static class TestConfig { |
| 87 | + |
| 88 | + @Bean |
| 89 | + public IdentityService identityService( |
| 90 | + ExpressionParser expressionParser, |
| 91 | + ConfigurableBeanFactory beanFactory, |
| 92 | + TierService tierService, |
| 93 | + CustomerService customerService, |
| 94 | + AccessTokenService tokenService, |
| 95 | + RateLimitProperties rateLimitProperties, |
| 96 | + UserService userService |
| 97 | + ) { |
| 98 | + return new IdentityService(expressionParser, beanFactory, tierService, customerService, tokenService, rateLimitProperties, userService); |
| 99 | + } |
| 100 | + |
| 101 | + @Bean |
| 102 | + public ExpressionParser expressionParser() { |
| 103 | + return new SpelExpressionParser(); |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public RateLimitProperties rateLimitProperties() { |
| 108 | + return new RateLimitProperties(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments