|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.saml2.provider.service.web; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.cache.Cache; |
| 22 | +import org.springframework.cache.concurrent.ConcurrentMapCache; |
| 23 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 24 | +import org.springframework.security.saml2.core.Saml2ParameterNames; |
| 25 | +import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest; |
| 26 | +import org.springframework.security.saml2.provider.service.authentication.Saml2PostAuthenticationRequest; |
| 27 | +import org.springframework.security.saml2.provider.service.authentication.TestSaml2PostAuthenticationRequests; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 31 | +import static org.mockito.ArgumentMatchers.any; |
| 32 | +import static org.mockito.ArgumentMatchers.eq; |
| 33 | +import static org.mockito.Mockito.spy; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link CacheSaml2AuthenticationRequestRepository} |
| 38 | + */ |
| 39 | +class CacheSaml2AuthenticationRequestRepositoryTests { |
| 40 | + |
| 41 | + CacheSaml2AuthenticationRequestRepository repository = new CacheSaml2AuthenticationRequestRepository(); |
| 42 | + |
| 43 | + @Test |
| 44 | + void loadAuthenticationRequestWhenCachedThenReturns() { |
| 45 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 46 | + request.setParameter(Saml2ParameterNames.RELAY_STATE, "test"); |
| 47 | + Saml2PostAuthenticationRequest authenticationRequest = TestSaml2PostAuthenticationRequests.create(); |
| 48 | + this.repository.saveAuthenticationRequest(authenticationRequest, request, null); |
| 49 | + assertThat(this.repository.loadAuthenticationRequest(request)).isEqualTo(authenticationRequest); |
| 50 | + this.repository.removeAuthenticationRequest(request, null); |
| 51 | + assertThat(this.repository.loadAuthenticationRequest(request)).isNull(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void loadAuthenticationRequestWhenNoRelayStateThenException() { |
| 56 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 57 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 58 | + .isThrownBy(() -> this.repository.loadAuthenticationRequest(request)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void saveAuthenticationRequestWhenNoRelayStateThenException() { |
| 63 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 64 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 65 | + .isThrownBy(() -> this.repository.saveAuthenticationRequest(null, request, null)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void removeAuthenticationRequestWhenNoRelayStateThenException() { |
| 70 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 71 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 72 | + .isThrownBy(() -> this.repository.removeAuthenticationRequest(request, null)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void repositoryWhenCustomCacheThenUses() { |
| 77 | + CacheSaml2AuthenticationRequestRepository repository = new CacheSaml2AuthenticationRequestRepository(); |
| 78 | + Cache cache = spy(new ConcurrentMapCache("requests")); |
| 79 | + repository.setCache(cache); |
| 80 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 81 | + request.setParameter(Saml2ParameterNames.RELAY_STATE, "test"); |
| 82 | + Saml2PostAuthenticationRequest authenticationRequest = TestSaml2PostAuthenticationRequests.create(); |
| 83 | + repository.saveAuthenticationRequest(authenticationRequest, request, null); |
| 84 | + verify(cache).put(eq("test"), any()); |
| 85 | + repository.loadAuthenticationRequest(request); |
| 86 | + verify(cache).get("test", AbstractSaml2AuthenticationRequest.class); |
| 87 | + repository.removeAuthenticationRequest(request, null); |
| 88 | + verify(cache).evict("test"); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments