1+ package org .owasp .esapi .logging .appender ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertTrue ;
5+ import static org .mockito .Mockito .mock ;
6+ import static org .mockito .Mockito .times ;
7+ import static org .mockito .Mockito .verify ;
8+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
9+ import static org .powermock .api .mockito .PowerMockito .mockStatic ;
10+ import static org .powermock .api .mockito .PowerMockito .when ;
11+
12+ import javax .servlet .http .HttpServletRequest ;
13+ import javax .servlet .http .HttpSession ;
14+
15+ import org .junit .Before ;
16+ import org .junit .Rule ;
17+ import org .junit .Test ;
18+ import org .junit .rules .TestName ;
19+ import org .junit .runner .RunWith ;
20+ import org .mockito .ArgumentMatchers ;
21+ import org .owasp .esapi .Authenticator ;
22+ import org .owasp .esapi .ESAPI ;
23+ import org .owasp .esapi .Randomizer ;
24+ import org .owasp .esapi .User ;
25+ import org .powermock .core .classloader .annotations .PowerMockIgnore ;
26+ import org .powermock .core .classloader .annotations .PrepareForTest ;
27+ import org .powermock .modules .junit4 .PowerMockRunner ;
28+
29+ @ RunWith (PowerMockRunner .class )
30+ @ PrepareForTest ({ESAPI .class })
31+ @ PowerMockIgnore ("javax.security.*" ) //Required since User extends javax.security.Principal
32+ public class UserInfoSupplierTest {
33+ private static final String ESAPI_SESSION_ATTR = "ESAPI_SESSION" ;
34+
35+ @ Rule
36+ public TestName testName = new TestName ();
37+
38+ private Authenticator mockAuth ;
39+ private User mockUser ;
40+
41+ @ Before
42+ public void before () throws Exception {
43+ mockAuth =mock (Authenticator .class );
44+ mockUser =mock (User .class );
45+
46+ mockStatic (ESAPI .class );
47+ when (ESAPI .class , "authenticator" ).thenReturn (mockAuth );
48+
49+ when (mockUser .getAccountName ()).thenReturn (testName .getMethodName () + "-USER" );
50+
51+
52+ when (mockAuth .getCurrentUser ()).thenReturn (mockUser );
53+ }
54+
55+ @ Test
56+ public void testHappyPath () throws Exception {
57+ UserInfoSupplier uis = new UserInfoSupplier ();
58+ uis .setLogUserInfo (true );
59+ String result = uis .get ();
60+
61+ assertEquals (testName .getMethodName () + "-USER" , result );
62+
63+ verify (mockAuth ,times (1 )).getCurrentUser ();
64+ verify (mockUser ,times (1 )).getAccountName ();
65+
66+ verifyNoMoreInteractions (mockAuth , mockUser );
67+ }
68+
69+ @ Test
70+ public void testLogUserOff () {
71+ UserInfoSupplier uis = new UserInfoSupplier ();
72+ uis .setLogUserInfo (false );
73+ String result = uis .get ();
74+
75+ assertTrue (result .isEmpty ());
76+ verify (mockAuth ,times (1 )).getCurrentUser ();
77+
78+ verifyNoMoreInteractions (mockAuth , mockUser );
79+ }
80+
81+ @ Test
82+ public void testLogUserNull () {
83+ when (mockAuth .getCurrentUser ()).thenReturn (null );
84+ UserInfoSupplier uis = new UserInfoSupplier ();
85+ uis .setLogUserInfo (true );
86+ String result = uis .get ();
87+
88+ assertEquals ("#ANONYMOUS#" , result );
89+
90+ verify (mockAuth ,times (1 )).getCurrentUser ();
91+
92+ verifyNoMoreInteractions (mockAuth , mockUser );
93+ }
94+
95+ }
0 commit comments