1919
2020package org .apache .cloudstack .oauth2 ;
2121
22+ import com .cloud .domain .DomainVO ;
23+ import com .cloud .domain .dao .DomainDao ;
2224import com .cloud .utils .exception .CloudRuntimeException ;
25+ import org .apache .cloudstack .api .ApiConstants ;
2326import org .apache .cloudstack .oauth2 .api .command .DeleteOAuthProviderCmd ;
2427import org .apache .cloudstack .oauth2 .api .command .RegisterOAuthProviderCmd ;
2528import org .apache .cloudstack .oauth2 .api .command .UpdateOAuthProviderCmd ;
3639import org .mockito .Spy ;
3740
3841import java .util .ArrayList ;
42+ import java .util .Arrays ;
3943import java .util .Collections ;
44+ import java .util .HashMap ;
4045import java .util .List ;
46+ import java .util .Map ;
4147
4248import static org .junit .Assert .assertEquals ;
49+ import static org .junit .Assert .assertNull ;
4350import static org .junit .Assert .assertTrue ;
4451import static org .mockito .Mockito .doNothing ;
4552import static org .mockito .Mockito .when ;
@@ -53,6 +60,9 @@ public class OAuth2AuthManagerImplTest {
5360 @ Mock
5461 OauthProviderDao _oauthProviderDao ;
5562
63+ @ Mock
64+ DomainDao _domainDao ;
65+
5666 AutoCloseable closeable ;
5767 @ Before
5868 public void setUp () {
@@ -190,4 +200,201 @@ public void testStart() {
190200 assertTrue (result );
191201 }
192202
203+ @ Test
204+ public void testRegisterOauthProviderWithDomain () {
205+ when (_authManager .isOAuthPluginEnabled (Mockito .nullable (Long .class ))).thenReturn (true );
206+ RegisterOAuthProviderCmd cmd = Mockito .mock (RegisterOAuthProviderCmd .class );
207+ when (cmd .getProvider ()).thenReturn ("github" );
208+ when (cmd .getDomainId ()).thenReturn (5L );
209+ when (cmd .getSecretKey ()).thenReturn ("secret" );
210+ when (cmd .getClientId ()).thenReturn ("clientId" );
211+ when (cmd .getRedirectUri ()).thenReturn ("https://redirect" );
212+
213+ // No existing provider for this domain
214+ when (_oauthProviderDao .findByProviderAndDomain ("github" , 5L )).thenReturn (null );
215+ when (_oauthProviderDao .persist (Mockito .any (OauthProviderVO .class ))).thenAnswer (i -> i .getArgument (0 ));
216+
217+ OauthProviderVO result = _authManager .registerOauthProvider (cmd );
218+ assertEquals ("github" , result .getProvider ());
219+ assertEquals (Long .valueOf (5L ), result .getDomainId ());
220+ }
221+
222+ @ Test
223+ public void testRegisterOauthProviderDuplicateForDomain () {
224+ when (_authManager .isOAuthPluginEnabled (Mockito .nullable (Long .class ))).thenReturn (true );
225+ RegisterOAuthProviderCmd cmd = Mockito .mock (RegisterOAuthProviderCmd .class );
226+ when (cmd .getProvider ()).thenReturn ("github" );
227+ when (cmd .getDomainId ()).thenReturn (5L );
228+
229+ OauthProviderVO existing = new OauthProviderVO ();
230+ existing .setProvider ("github" );
231+ existing .setDomainId (5L );
232+ when (_oauthProviderDao .findByProviderAndDomain ("github" , 5L )).thenReturn (existing );
233+
234+ try {
235+ _authManager .registerOauthProvider (cmd );
236+ Assert .fail ("Expected CloudRuntimeException was not thrown" );
237+ } catch (CloudRuntimeException e ) {
238+ assertEquals ("Provider with the name github is already registered for domain 5" , e .getMessage ());
239+ }
240+ }
241+
242+ @ Test
243+ public void testListOauthProvidersWithDomainId () {
244+ Long domainId = 5L ;
245+ OauthProviderVO globalProvider = new OauthProviderVO ();
246+ globalProvider .setProvider ("google" );
247+ OauthProviderVO domainProvider = new OauthProviderVO ();
248+ domainProvider .setProvider ("github" );
249+ domainProvider .setDomainId (domainId );
250+ List <OauthProviderVO > providers = Arrays .asList (globalProvider , domainProvider );
251+
252+ when (_oauthProviderDao .listByDomainIncludingGlobal (domainId )).thenReturn (providers );
253+ List <OauthProviderVO > result = _authManager .listOauthProviders (null , null , domainId );
254+ assertEquals (2 , result .size ());
255+ }
256+
257+ @ Test
258+ public void testListOauthProvidersByProviderAndDomain () {
259+ Long domainId = 5L ;
260+ OauthProviderVO domainProvider = new OauthProviderVO ();
261+ domainProvider .setProvider ("github" );
262+ domainProvider .setDomainId (domainId );
263+
264+ when (_oauthProviderDao .findByProviderAndDomain ("github" , domainId )).thenReturn (domainProvider );
265+ List <OauthProviderVO > result = _authManager .listOauthProviders ("github" , null , domainId );
266+ assertEquals (1 , result .size ());
267+ assertEquals ("github" , result .get (0 ).getProvider ());
268+ assertEquals (Long .valueOf (5L ), result .get (0 ).getDomainId ());
269+ }
270+
271+ @ Test
272+ public void testResolveDomainIdFromDomainUuid () {
273+ Map <String , Object []> params = new HashMap <>();
274+ params .put (ApiConstants .DOMAIN_ID , new String []{"test-uuid-123" });
275+
276+ DomainVO domain = Mockito .mock (DomainVO .class );
277+ when (domain .getId ()).thenReturn (10L );
278+ when (_domainDao .findByUuid ("test-uuid-123" )).thenReturn (domain );
279+
280+ Long result = _authManager .resolveDomainId (params );
281+ assertEquals (Long .valueOf (10L ), result );
282+ }
283+
284+ @ Test
285+ public void testResolveDomainIdGlobalFilter () {
286+ Map <String , Object []> params = new HashMap <>();
287+ params .put (ApiConstants .DOMAIN_ID , new String []{"-1" });
288+
289+ Long result = _authManager .resolveDomainId (params );
290+ assertEquals (Long .valueOf (-1L ), result );
291+ }
292+
293+ @ Test
294+ public void testResolveDomainIdFromDomainPath () {
295+ Map <String , Object []> params = new HashMap <>();
296+ params .put (ApiConstants .DOMAIN , new String []{"ROOT/child" });
297+
298+ DomainVO domain = Mockito .mock (DomainVO .class );
299+ when (domain .getId ()).thenReturn (20L );
300+ when (_domainDao .findDomainByPath ("/ROOT/child/" )).thenReturn (domain );
301+
302+ Long result = _authManager .resolveDomainId (params );
303+ assertEquals (Long .valueOf (20L ), result );
304+ }
305+
306+ @ Test
307+ public void testResolveDomainIdFromDomainPathWithSlashes () {
308+ Map <String , Object []> params = new HashMap <>();
309+ params .put (ApiConstants .DOMAIN , new String []{"/ROOT/child/" });
310+
311+ DomainVO domain = Mockito .mock (DomainVO .class );
312+ when (domain .getId ()).thenReturn (20L );
313+ when (_domainDao .findDomainByPath ("/ROOT/child/" )).thenReturn (domain );
314+
315+ Long result = _authManager .resolveDomainId (params );
316+ assertEquals (Long .valueOf (20L ), result );
317+ }
318+
319+ @ Test
320+ public void testResolveDomainIdReturnsNullWhenNotFound () {
321+ Map <String , Object []> params = new HashMap <>();
322+ params .put (ApiConstants .DOMAIN_ID , new String []{"nonexistent-uuid" });
323+
324+ when (_domainDao .findByUuid ("nonexistent-uuid" )).thenReturn (null );
325+
326+ Long result = _authManager .resolveDomainId (params );
327+ assertNull (result );
328+ }
329+
330+ @ Test
331+ public void testResolveDomainIdReturnsNullForEmptyParams () {
332+ Map <String , Object []> params = new HashMap <>();
333+ Long result = _authManager .resolveDomainId (params );
334+ assertNull (result );
335+ }
336+
337+ @ Test
338+ public void testResolveDomainIdPrefersUuidOverPath () {
339+ Map <String , Object []> params = new HashMap <>();
340+ params .put (ApiConstants .DOMAIN_ID , new String []{"test-uuid" });
341+ params .put (ApiConstants .DOMAIN , new String []{"/ROOT/child/" });
342+
343+ DomainVO domain = Mockito .mock (DomainVO .class );
344+ when (domain .getId ()).thenReturn (10L );
345+ when (_domainDao .findByUuid ("test-uuid" )).thenReturn (domain );
346+
347+ Long result = _authManager .resolveDomainId (params );
348+ assertEquals (Long .valueOf (10L ), result );
349+ }
350+
351+ @ Test
352+ public void testResolveDomainIdFallsBackToPathWhenUuidNotFound () {
353+ Map <String , Object []> params = new HashMap <>();
354+ params .put (ApiConstants .DOMAIN_ID , new String []{"bad-uuid" });
355+ params .put (ApiConstants .DOMAIN , new String []{"/ROOT/" });
356+
357+ when (_domainDao .findByUuid ("bad-uuid" )).thenReturn (null );
358+ DomainVO domain = Mockito .mock (DomainVO .class );
359+ when (domain .getId ()).thenReturn (1L );
360+ when (_domainDao .findDomainByPath ("/ROOT/" )).thenReturn (domain );
361+
362+ Long result = _authManager .resolveDomainId (params );
363+ assertEquals (Long .valueOf (1L ), result );
364+ }
365+
366+ @ Test
367+ public void testUpdateOauthProviderNotFound () {
368+ UpdateOAuthProviderCmd cmd = Mockito .mock (UpdateOAuthProviderCmd .class );
369+ when (cmd .getId ()).thenReturn (999L );
370+ when (_oauthProviderDao .findById (999L )).thenReturn (null );
371+
372+ try {
373+ _authManager .updateOauthProvider (cmd );
374+ Assert .fail ("Expected CloudRuntimeException was not thrown" );
375+ } catch (CloudRuntimeException e ) {
376+ assertEquals ("Provider with the given id is not there" , e .getMessage ());
377+ }
378+ }
379+
380+ @ Test
381+ public void testGetUserOAuth2AuthenticationProviderEmptyName () {
382+ try {
383+ _authManager .getUserOAuth2AuthenticationProvider ("" );
384+ Assert .fail ("Expected CloudRuntimeException was not thrown" );
385+ } catch (CloudRuntimeException e ) {
386+ assertEquals ("OAuth2 authentication provider name is empty" , e .getMessage ());
387+ }
388+ }
389+
390+ @ Test
391+ public void testGetUserOAuth2AuthenticationProviderNotFound () {
392+ try {
393+ _authManager .getUserOAuth2AuthenticationProvider ("nonexistent" );
394+ Assert .fail ("Expected CloudRuntimeException was not thrown" );
395+ } catch (CloudRuntimeException e ) {
396+ assertTrue (e .getMessage ().contains ("nonexistent" ));
397+ }
398+ }
399+
193400}
0 commit comments