|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.core.context; |
| 18 | + |
| 19 | +import java.util.function.Supplier; |
| 20 | + |
| 21 | +import io.micrometer.observation.Observation; |
| 22 | +import io.micrometer.observation.ObservationRegistry; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.mockito.ArgumentCaptor; |
| 26 | + |
| 27 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.mockito.BDDMockito.given; |
| 31 | +import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.verify; |
| 33 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for {@link ObservationSecurityContextChangedListener} |
| 37 | + */ |
| 38 | +public class ObservationSecurityContextChangedListenerTests { |
| 39 | + |
| 40 | + private SecurityContext one = new SecurityContextImpl(new TestingAuthenticationToken("user", "pass")); |
| 41 | + |
| 42 | + private SecurityContext two = new SecurityContextImpl(new TestingAuthenticationToken("admin", "pass")); |
| 43 | + |
| 44 | + private ObservationRegistry observationRegistry; |
| 45 | + |
| 46 | + private ObservationSecurityContextChangedListener tested; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + void setup() { |
| 50 | + this.observationRegistry = mock(ObservationRegistry.class); |
| 51 | + this.tested = new ObservationSecurityContextChangedListener(this.observationRegistry); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void securityContextChangedWhenNoObservationThenNoEvents() { |
| 56 | + given(this.observationRegistry.getCurrentObservation()).willReturn(null); |
| 57 | + this.tested.securityContextChanged(new SecurityContextChangedEvent(this.one, this.two)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void securityContextChangedWhenClearedEventThenAddsClearEventToObservation() { |
| 62 | + Observation observation = mock(Observation.class); |
| 63 | + given(this.observationRegistry.getCurrentObservation()).willReturn(observation); |
| 64 | + Supplier<SecurityContext> one = mock(Supplier.class); |
| 65 | + this.tested |
| 66 | + .securityContextChanged(new SecurityContextChangedEvent(one, SecurityContextChangedEvent.NO_CONTEXT)); |
| 67 | + ArgumentCaptor<Observation.Event> event = ArgumentCaptor.forClass(Observation.Event.class); |
| 68 | + verify(observation).event(event.capture()); |
| 69 | + assertThat(event.getValue().getName()).isEqualTo("security.context.cleared"); |
| 70 | + verifyNoInteractions(one); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void securityContextChangedWhenNoChangeThenNoEventAddedToObservation() { |
| 75 | + Observation observation = mock(Observation.class); |
| 76 | + given(this.observationRegistry.getCurrentObservation()).willReturn(observation); |
| 77 | + this.tested.securityContextChanged(new SecurityContextChangedEvent(this.one, this.one)); |
| 78 | + verifyNoInteractions(observation); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void securityContextChangedWhenChangedEventThenAddsChangeEventToObservation() { |
| 83 | + Observation observation = mock(Observation.class); |
| 84 | + given(this.observationRegistry.getCurrentObservation()).willReturn(observation); |
| 85 | + this.tested.securityContextChanged(new SecurityContextChangedEvent(this.one, this.two)); |
| 86 | + ArgumentCaptor<Observation.Event> event = ArgumentCaptor.forClass(Observation.Event.class); |
| 87 | + verify(observation).event(event.capture()); |
| 88 | + assertThat(event.getValue().getName()).isEqualTo("security.context.changed"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void securityContextChangedWhenCreatedEventThenAddsCreatedEventToObservation() { |
| 93 | + Observation observation = mock(Observation.class); |
| 94 | + given(this.observationRegistry.getCurrentObservation()).willReturn(observation); |
| 95 | + this.tested.securityContextChanged(new SecurityContextChangedEvent(null, this.one)); |
| 96 | + ArgumentCaptor<Observation.Event> event = ArgumentCaptor.forClass(Observation.Event.class); |
| 97 | + verify(observation).event(event.capture()); |
| 98 | + assertThat(event.getValue().getName()).isEqualTo("security.context.created"); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments