2424import com .cloud .user .dao .UserDao ;
2525import com .cloud .utils .Pair ;
2626import org .apache .cloudstack .auth .UserOAuth2Authenticator ;
27+ import org .junit .After ;
2728import org .junit .Before ;
2829import org .junit .Test ;
30+ import org .junit .runner .RunWith ;
2931import org .mockito .InjectMocks ;
3032import org .mockito .Mock ;
3133import org .mockito .MockitoAnnotations ;
34+ import org .mockito .Spy ;
35+ import org .mockito .junit .MockitoJUnitRunner ;
3236
3337import java .util .HashMap ;
3438import java .util .Map ;
3539
3640import 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 ;
3744import static org .mockito .ArgumentMatchers .anyLong ;
3845import static org .mockito .ArgumentMatchers .anyString ;
46+ import static org .mockito .Mockito .doReturn ;
3947import static org .mockito .Mockito .mock ;
4048import static org .mockito .Mockito .never ;
4149import static org .mockito .Mockito .verify ;
4250import static org .mockito .Mockito .when ;
4351
52+ @ RunWith (MockitoJUnitRunner .class )
4453public class OAuth2UserAuthenticatorTest {
4554
4655 @ Mock
@@ -53,13 +62,22 @@ public class OAuth2UserAuthenticatorTest {
5362 private OAuth2AuthManager userOAuth2mgr ;
5463
5564 @ InjectMocks
65+ @ Spy
5666 private OAuth2UserAuthenticator authenticator ;
67+ private AutoCloseable closeable ;
5768
5869 @ Before
5970 public void setUp () {
60- MockitoAnnotations .initMocks (this );
71+ closeable = MockitoAnnotations .openMocks (this );
72+ doReturn (true ).when (authenticator ).isOAuthPluginEnabled ();
6173 }
6274
75+ @ After
76+ public void tearDown () throws Exception {
77+ closeable .close ();
78+ }
79+
80+
6381 @ Test
6482 public void testAuthenticateWithValidCredentials () {
6583 String username = "testuser" ;
@@ -84,13 +102,13 @@ public void testAuthenticateWithValidCredentials() {
84102
85103 Pair <Boolean , OAuth2UserAuthenticator .ActionOnFailedAuthentication > result = authenticator .authenticate (username , null , domainId , requestParameters );
86104
105+ assertTrue (result .first ());
106+ assertNull (result .second ());
107+
87108 verify (userAccountDao ).getUserAccount (username , domainId );
88109 verify (userDao ).getUser (userAccount .getId ());
89110 verify (userOAuth2mgr ).getUserOAuth2AuthenticationProvider (provider [0 ]);
90111 verify (userOAuth2Authenticator ).verifyUser (email [0 ], secretCode [0 ]);
91-
92- assertEquals (true , result .first ().booleanValue ());
93- assertEquals (null , result .second ());
94112 }
95113
96114 @ Test
@@ -106,7 +124,7 @@ public void testAuthenticateWithInvalidCredentials() {
106124 UserOAuth2Authenticator userOAuth2Authenticator = mock (UserOAuth2Authenticator .class );
107125
108126 when (userAccountDao .getUserAccount (username , domainId )).thenReturn (userAccount );
109- when (userDao .getUser (userAccount .getId ())).thenReturn ( user );
127+ when (userDao .getUser (userAccount .getId ())).thenReturn (user );
110128 when (userOAuth2mgr .getUserOAuth2AuthenticationProvider (provider [0 ])).thenReturn (userOAuth2Authenticator );
111129 when (userOAuth2Authenticator .verifyUser (email [0 ], secretCode [0 ])).thenReturn (false );
112130
@@ -117,13 +135,13 @@ public void testAuthenticateWithInvalidCredentials() {
117135
118136 Pair <Boolean , OAuth2UserAuthenticator .ActionOnFailedAuthentication > result = authenticator .authenticate (username , null , domainId , requestParameters );
119137
138+ assertFalse (result .first ());
139+ assertEquals (OAuth2UserAuthenticator .ActionOnFailedAuthentication .INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT , result .second ());
140+
120141 verify (userAccountDao ).getUserAccount (username , domainId );
121142 verify (userDao ).getUser (userAccount .getId ());
122143 verify (userOAuth2mgr ).getUserOAuth2AuthenticationProvider (provider [0 ]);
123144 verify (userOAuth2Authenticator ).verifyUser (email [0 ], secretCode [0 ]);
124-
125- assertEquals (false , result .first ().booleanValue ());
126- assertEquals (OAuth2UserAuthenticator .ActionOnFailedAuthentication .INCREMENT_INCORRECT_LOGIN_ATTEMPT_COUNT , result .second ());
127145 }
128146
129147 @ Test
@@ -143,11 +161,11 @@ public void testAuthenticateWithInvalidUserAccount() {
143161
144162 Pair <Boolean , OAuth2UserAuthenticator .ActionOnFailedAuthentication > result = authenticator .authenticate (username , null , domainId , requestParameters );
145163
164+ assertFalse (result .first ());
165+ assertNull (result .second ());
166+
146167 verify (userAccountDao ).getUserAccount (username , domainId );
147168 verify (userDao , never ()).getUser (anyLong ());
148169 verify (userOAuth2mgr , never ()).getUserOAuth2AuthenticationProvider (anyString ());
149-
150- assertEquals (false , result .first ().booleanValue ());
151- assertEquals (null , result .second ());
152170 }
153171}
0 commit comments