16
16
17
17
package org .springframework .security .core .context ;
18
18
19
+ import java .util .function .Supplier ;
20
+
19
21
import org .junit .jupiter .api .Test ;
22
+ import org .mockito .ArgumentCaptor ;
23
+
24
+ import org .springframework .security .authentication .TestingAuthenticationToken ;
20
25
26
+ import static org .assertj .core .api .Assertions .assertThat ;
21
27
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
22
28
import static org .mockito .ArgumentMatchers .any ;
23
29
import static org .mockito .BDDMockito .given ;
24
30
import static org .mockito .Mockito .mock ;
31
+ import static org .mockito .Mockito .reset ;
32
+ import static org .mockito .Mockito .spy ;
25
33
import static org .mockito .Mockito .verify ;
26
34
import static org .mockito .Mockito .verifyNoInteractions ;
35
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
27
36
28
37
public class ListeningSecurityContextHolderStrategyTests {
29
38
30
39
@ Test
31
40
public void setContextWhenInvokedThenListenersAreNotified () {
32
- SecurityContextHolderStrategy delegate = mock ( SecurityContextHolderStrategy . class );
41
+ SecurityContextHolderStrategy delegate = spy ( new MockSecurityContextHolderStrategy () );
33
42
SecurityContextChangedListener one = mock (SecurityContextChangedListener .class );
34
43
SecurityContextChangedListener two = mock (SecurityContextChangedListener .class );
35
44
SecurityContextHolderStrategy strategy = new ListeningSecurityContextHolderStrategy (delegate , one , two );
36
45
given (delegate .createEmptyContext ()).willReturn (new SecurityContextImpl ());
37
46
SecurityContext context = strategy .createEmptyContext ();
38
47
strategy .setContext (context );
39
- verify ( delegate ). setContext ( context );
48
+ strategy . getContext ( );
40
49
verify (one ).securityContextChanged (any ());
41
50
verify (two ).securityContextChanged (any ());
42
51
}
@@ -49,8 +58,66 @@ public void setContextWhenNoChangeToContextThenListenersAreNotNotified() {
49
58
SecurityContext context = new SecurityContextImpl ();
50
59
given (delegate .getContext ()).willReturn (context );
51
60
strategy .setContext (strategy .getContext ());
52
- verify (delegate ).setContext (context );
61
+ strategy .getContext ();
62
+ verifyNoInteractions (listener );
63
+ }
64
+
65
+ @ Test
66
+ public void clearContextWhenNoGetContextThenContextIsNotRead () {
67
+ SecurityContextHolderStrategy delegate = mock (SecurityContextHolderStrategy .class );
68
+ SecurityContextChangedListener listener = mock (SecurityContextChangedListener .class );
69
+ SecurityContextHolderStrategy strategy = new ListeningSecurityContextHolderStrategy (delegate , listener );
70
+ Supplier <SecurityContext > context = mock (Supplier .class );
71
+ ArgumentCaptor <SecurityContextChangedEvent > event = ArgumentCaptor .forClass (SecurityContextChangedEvent .class );
72
+ given (delegate .getDeferredContext ()).willReturn (context );
73
+ given (delegate .getContext ()).willAnswer ((invocation ) -> context .get ());
74
+ strategy .clearContext ();
75
+ verifyNoInteractions (context );
76
+ verify (listener ).securityContextChanged (event .capture ());
77
+ assertThat (event .getValue ().isCleared ()).isTrue ();
78
+ strategy .getContext ();
79
+ verify (context ).get ();
80
+ strategy .clearContext ();
81
+ verifyNoMoreInteractions (context );
82
+ }
83
+
84
+ @ Test
85
+ public void getContextWhenCalledMultipleTimesThenEventPublishedOnce () {
86
+ SecurityContextHolderStrategy delegate = new MockSecurityContextHolderStrategy ();
87
+ SecurityContextChangedListener listener = mock (SecurityContextChangedListener .class );
88
+ SecurityContextHolderStrategy strategy = new ListeningSecurityContextHolderStrategy (delegate , listener );
89
+ strategy .setContext (new SecurityContextImpl ());
90
+ verifyNoInteractions (listener );
91
+ strategy .getContext ();
92
+ verify (listener ).securityContextChanged (any ());
93
+ strategy .getContext ();
94
+ verifyNoMoreInteractions (listener );
95
+ }
96
+
97
+ @ Test
98
+ public void setContextWhenCalledMultipleTimesThenPublishedEventsAlign () {
99
+ SecurityContextHolderStrategy delegate = new MockSecurityContextHolderStrategy ();
100
+ SecurityContextChangedListener listener = mock (SecurityContextChangedListener .class );
101
+ SecurityContextHolderStrategy strategy = new ListeningSecurityContextHolderStrategy (delegate , listener );
102
+ SecurityContext one = new SecurityContextImpl (new TestingAuthenticationToken ("user" , "pass" ));
103
+ SecurityContext two = new SecurityContextImpl (new TestingAuthenticationToken ("admin" , "pass" ));
104
+ ArgumentCaptor <SecurityContextChangedEvent > event = ArgumentCaptor .forClass (SecurityContextChangedEvent .class );
105
+ strategy .setContext (one );
106
+ strategy .setContext (two );
53
107
verifyNoInteractions (listener );
108
+ strategy .getContext ();
109
+ verify (listener ).securityContextChanged (event .capture ());
110
+ assertThat (event .getValue ().getOldContext ()).isEqualTo (one );
111
+ assertThat (event .getValue ().getNewContext ()).isEqualTo (two );
112
+ strategy .getContext ();
113
+ verifyNoMoreInteractions (listener );
114
+ strategy .setContext (one );
115
+ verifyNoMoreInteractions (listener );
116
+ reset (listener );
117
+ strategy .getContext ();
118
+ verify (listener ).securityContextChanged (event .capture ());
119
+ assertThat (event .getValue ().getOldContext ()).isEqualTo (two );
120
+ assertThat (event .getValue ().getNewContext ()).isEqualTo (one );
54
121
}
55
122
56
123
@ Test
0 commit comments