2727import org .junit .After ;
2828import org .junit .Before ;
2929import org .junit .Test ;
30+ import org .junit .runner .RunWith ;
3031import org .mockito .InjectMocks ;
3132import org .mockito .Mock ;
3233import org .mockito .MockitoAnnotations ;
34+ import org .mockito .Spy ;
35+ import org .mockito .junit .MockitoJUnitRunner ;
3336
3437import java .util .HashMap ;
3538import java .util .Map ;
3639
3740import static org .junit .Assert .assertEquals ;
41+ import static org .junit .Assert .assertFalse ;
42+ import static org .junit .Assert .assertNull ;
43+ import static org .junit .Assert .assertTrue ;
3844import static org .mockito .ArgumentMatchers .anyLong ;
3945import static org .mockito .ArgumentMatchers .anyString ;
46+ import static org .mockito .Mockito .doReturn ;
4047import static org .mockito .Mockito .mock ;
4148import static org .mockito .Mockito .never ;
4249import static org .mockito .Mockito .verify ;
4350import static org .mockito .Mockito .when ;
4451
52+ @ RunWith (MockitoJUnitRunner .class )
4553public class OAuth2UserAuthenticatorTest {
4654
4755 @ Mock
@@ -54,20 +62,24 @@ public class OAuth2UserAuthenticatorTest {
5462 private OAuth2AuthManager userOAuth2mgr ;
5563
5664 @ InjectMocks
65+ @ Spy
5766 private OAuth2UserAuthenticator authenticator ;
67+ private AutoCloseable closeable ;
5868
5969 private AutoCloseable closeable ;
6070
6171 @ Before
6272 public void setUp () {
6373 closeable = MockitoAnnotations .openMocks (this );
74+ doReturn (true ).when (authenticator ).isOAuthPluginEnabled ();
6475 }
6576
6677 @ After
6778 public void tearDown () throws Exception {
6879 closeable .close ();
6980 }
7081
82+
7183 @ Test
7284 public void testAuthenticateWithValidCredentials () {
7385 String username = "testuser" ;
@@ -92,13 +104,13 @@ public void testAuthenticateWithValidCredentials() {
92104
93105 Pair <Boolean , OAuth2UserAuthenticator .ActionOnFailedAuthentication > result = authenticator .authenticate (username , null , domainId , requestParameters );
94106
107+ assertTrue (result .first ());
108+ assertNull (result .second ());
109+
95110 verify (userAccountDao ).getUserAccount (username , domainId );
96111 verify (userDao ).getUser (userAccount .getId ());
97112 verify (userOAuth2mgr ).getUserOAuth2AuthenticationProvider (provider [0 ]);
98113 verify (userOAuth2Authenticator ).verifyUser (email [0 ], secretCode [0 ]);
99-
100- assertEquals (true , result .first ().booleanValue ());
101- assertEquals (null , result .second ());
102114 }
103115
104116 @ Test
@@ -114,7 +126,7 @@ public void testAuthenticateWithInvalidCredentials() {
114126 UserOAuth2Authenticator userOAuth2Authenticator = mock (UserOAuth2Authenticator .class );
115127
116128 when (userAccountDao .getUserAccount (username , domainId )).thenReturn (userAccount );
117- when (userDao .getUser (userAccount .getId ())).thenReturn ( user );
129+ when (userDao .getUser (userAccount .getId ())).thenReturn (user );
118130 when (userOAuth2mgr .getUserOAuth2AuthenticationProvider (provider [0 ])).thenReturn (userOAuth2Authenticator );
119131 when (userOAuth2Authenticator .verifyUser (email [0 ], secretCode [0 ])).thenReturn (false );
120132
@@ -125,13 +137,13 @@ public void testAuthenticateWithInvalidCredentials() {
125137
126138 Pair <Boolean , OAuth2UserAuthenticator .ActionOnFailedAuthentication > result = authenticator .authenticate (username , null , domainId , requestParameters );
127139
140+ assertFalse (result .first ());
141+ assertEquals (OAuth2UserAuthenticator .ActionOnFailedAuthentication .INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT , result .second ());
142+
128143 verify (userAccountDao ).getUserAccount (username , domainId );
129144 verify (userDao ).getUser (userAccount .getId ());
130145 verify (userOAuth2mgr ).getUserOAuth2AuthenticationProvider (provider [0 ]);
131146 verify (userOAuth2Authenticator ).verifyUser (email [0 ], secretCode [0 ]);
132-
133- assertEquals (false , result .first ().booleanValue ());
134- assertEquals (OAuth2UserAuthenticator .ActionOnFailedAuthentication .INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT , result .second ());
135147 }
136148
137149 @ Test
@@ -151,11 +163,11 @@ public void testAuthenticateWithInvalidUserAccount() {
151163
152164 Pair <Boolean , OAuth2UserAuthenticator .ActionOnFailedAuthentication > result = authenticator .authenticate (username , null , domainId , requestParameters );
153165
166+ assertFalse (result .first ());
167+ assertNull (result .second ());
168+
154169 verify (userAccountDao ).getUserAccount (username , domainId );
155170 verify (userDao , never ()).getUser (anyLong ());
156171 verify (userOAuth2mgr , never ()).getUserOAuth2AuthenticationProvider (anyString ());
157-
158- assertEquals (false , result .first ().booleanValue ());
159- assertEquals (null , result .second ());
160172 }
161173}
0 commit comments