Skip to content

Commit dc30f14

Browse files
committed
Add Multiple-domain OAuth tests
1 parent b4b7420 commit dc30f14

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

plugins/user-authenticators/oauth2/src/test/java/org/apache/cloudstack/oauth2/OAuth2AuthManagerImplTest.java

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,122 @@ public void testGetUserOAuth2AuthenticationProviderNotFound() {
397397
}
398398
}
399399

400+
// Multiple-domain OAuth tests
401+
402+
@Test
403+
public void testSameProviderRegisteredInTwoDifferentDomains() {
404+
when(_authManager.isOAuthPluginEnabled(Mockito.nullable(Long.class))).thenReturn(true);
405+
406+
// Register github for domain 5
407+
RegisterOAuthProviderCmd cmd1 = Mockito.mock(RegisterOAuthProviderCmd.class);
408+
when(cmd1.getProvider()).thenReturn("github");
409+
when(cmd1.getDomainId()).thenReturn(5L);
410+
when(cmd1.getSecretKey()).thenReturn("secret1");
411+
when(_oauthProviderDao.findByProviderAndDomain("github", 5L)).thenReturn(null);
412+
when(_oauthProviderDao.persist(Mockito.any(OauthProviderVO.class))).thenAnswer(i -> i.getArgument(0));
413+
414+
OauthProviderVO result1 = _authManager.registerOauthProvider(cmd1);
415+
assertEquals("github", result1.getProvider());
416+
assertEquals(Long.valueOf(5L), result1.getDomainId());
417+
418+
// Register github for domain 10 — should succeed independently
419+
RegisterOAuthProviderCmd cmd2 = Mockito.mock(RegisterOAuthProviderCmd.class);
420+
when(cmd2.getProvider()).thenReturn("github");
421+
when(cmd2.getDomainId()).thenReturn(10L);
422+
when(cmd2.getSecretKey()).thenReturn("secret2");
423+
when(_oauthProviderDao.findByProviderAndDomain("github", 10L)).thenReturn(null);
424+
425+
OauthProviderVO result2 = _authManager.registerOauthProvider(cmd2);
426+
assertEquals("github", result2.getProvider());
427+
assertEquals(Long.valueOf(10L), result2.getDomainId());
428+
}
429+
430+
@Test
431+
public void testSameProviderRegisteredGloballyAndForDomain() {
432+
when(_authManager.isOAuthPluginEnabled(Mockito.nullable(Long.class))).thenReturn(true);
433+
434+
// Global registration (domainId = null)
435+
RegisterOAuthProviderCmd globalCmd = Mockito.mock(RegisterOAuthProviderCmd.class);
436+
when(globalCmd.getProvider()).thenReturn("google");
437+
when(globalCmd.getDomainId()).thenReturn(null);
438+
when(_oauthProviderDao.findByProviderAndDomain("google", null)).thenReturn(null);
439+
when(_oauthProviderDao.persist(Mockito.any(OauthProviderVO.class))).thenAnswer(i -> i.getArgument(0));
440+
441+
OauthProviderVO globalResult = _authManager.registerOauthProvider(globalCmd);
442+
assertNull(globalResult.getDomainId());
443+
444+
// Domain-specific registration for same provider — should succeed (different scope)
445+
RegisterOAuthProviderCmd domainCmd = Mockito.mock(RegisterOAuthProviderCmd.class);
446+
when(domainCmd.getProvider()).thenReturn("google");
447+
when(domainCmd.getDomainId()).thenReturn(7L);
448+
when(_oauthProviderDao.findByProviderAndDomain("google", 7L)).thenReturn(null);
449+
450+
OauthProviderVO domainResult = _authManager.registerOauthProvider(domainCmd);
451+
assertEquals(Long.valueOf(7L), domainResult.getDomainId());
452+
}
453+
454+
@Test
455+
public void testListOauthProvidersForDomainIncludesGlobalProviders() {
456+
Long domainId = 5L;
457+
OauthProviderVO globalGoogle = new OauthProviderVO();
458+
globalGoogle.setProvider("google");
459+
// domainId is null — global
460+
461+
OauthProviderVO domainGithub = new OauthProviderVO();
462+
domainGithub.setProvider("github");
463+
domainGithub.setDomainId(domainId);
464+
465+
OauthProviderVO otherDomainGoogle = new OauthProviderVO();
466+
otherDomainGoogle.setProvider("google");
467+
otherDomainGoogle.setDomainId(10L);
468+
469+
// listByDomainIncludingGlobal returns providers for domain 5 + global (not domain 10)
470+
when(_oauthProviderDao.listByDomainIncludingGlobal(domainId))
471+
.thenReturn(Arrays.asList(globalGoogle, domainGithub));
472+
473+
List<OauthProviderVO> result = _authManager.listOauthProviders(null, null, domainId);
474+
assertEquals(2, result.size());
475+
assertTrue(result.stream().anyMatch(p -> p.getDomainId() == null)); // global included
476+
assertTrue(result.stream().anyMatch(p -> Long.valueOf(5L).equals(p.getDomainId()))); // domain-specific included
477+
assertTrue(result.stream().noneMatch(p -> Long.valueOf(10L).equals(p.getDomainId()))); // other domain excluded
478+
}
479+
480+
@Test
481+
public void testListAllProvidersAcrossAllDomains() {
482+
OauthProviderVO global = new OauthProviderVO();
483+
global.setProvider("google");
484+
485+
OauthProviderVO domain5 = new OauthProviderVO();
486+
domain5.setProvider("github");
487+
domain5.setDomainId(5L);
488+
489+
OauthProviderVO domain10 = new OauthProviderVO();
490+
domain10.setProvider("google");
491+
domain10.setDomainId(10L);
492+
493+
when(_oauthProviderDao.listAll()).thenReturn(Arrays.asList(global, domain5, domain10));
494+
495+
List<OauthProviderVO> result = _authManager.listOauthProviders(null, null, null);
496+
assertEquals(3, result.size());
497+
}
498+
499+
@Test
500+
public void testDuplicateGlobalProviderRejected() {
501+
when(_authManager.isOAuthPluginEnabled(Mockito.nullable(Long.class))).thenReturn(true);
502+
RegisterOAuthProviderCmd cmd = Mockito.mock(RegisterOAuthProviderCmd.class);
503+
when(cmd.getProvider()).thenReturn("google");
504+
when(cmd.getDomainId()).thenReturn(null);
505+
506+
OauthProviderVO existing = new OauthProviderVO();
507+
existing.setProvider("google");
508+
when(_oauthProviderDao.findByProviderAndDomain("google", null)).thenReturn(existing);
509+
510+
try {
511+
_authManager.registerOauthProvider(cmd);
512+
Assert.fail("Expected CloudRuntimeException was not thrown");
513+
} catch (CloudRuntimeException e) {
514+
assertEquals("Global provider with the name google is already registered", e.getMessage());
515+
}
516+
}
517+
400518
}

0 commit comments

Comments
 (0)