Skip to content

Commit

Permalink
Use and include test from (#3168)
Browse files Browse the repository at this point in the history
  • Loading branch information
strehle authored Nov 28, 2024
1 parent b01541d commit 16142fe
Showing 1 changed file with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -108,12 +110,16 @@ void setUp() throws Exception {
uaaProvider = mock(IdentityProvider.class);
when(uaaProvider.getType()).thenReturn(OriginKeys.UAA);
when(uaaProvider.getOriginKey()).thenReturn(OriginKeys.UAA);
when(uaaProvider.isActive()).thenReturn(true);
ldapProvider = mock(IdentityProvider.class);
when(ldapProvider.getType()).thenReturn(OriginKeys.LDAP);
when(ldapProvider.getOriginKey()).thenReturn(OriginKeys.LDAP);
when(ldapProvider.isActive()).thenReturn(true);

when(identityProviderProvisioning.retrieveActive("uaa")).thenReturn(Arrays.asList(idp, uaaProvider, ldapProvider));
when(identityProviderProvisioning.retrieveByOrigin("oidcprovider", "uaa")).thenReturn(idp);
when(identityProviderProvisioning.retrieveByOrigin("uaa", "uaa")).thenReturn(uaaProvider);
when(identityProviderProvisioning.retrieveByOrigin("ldap", "uaa")).thenReturn(ldapProvider);

Authentication clientAuth = mock(Authentication.class);
when(clientAuth.getName()).thenReturn("clientid");
Expand Down Expand Up @@ -627,22 +633,29 @@ void testUaaPasswordGrant_defaultProviderUaa() {
verify(zoneAwareAuthzAuthenticationManager, times(0)).setLoginHint(any(), any());
}

@Test
void testPasswordGrant_NoLoginHintWithDefaultUaa() {
@ParameterizedTest
@ValueSource(strings = { OriginKeys.UAA, OriginKeys.LDAP })
void testPasswordGrant_NoLoginHintWithDefaultUaaOrLdap(final String loginHintOrigin) {
Authentication auth = mock(Authentication.class);
when(zoneAwareAuthzAuthenticationManager.extractLoginHint(auth)).thenReturn(null);
Map<String, Object> additionalInformation = new HashMap<>();
additionalInformation.put(ClientConstants.ALLOWED_PROVIDERS, Collections.singletonList("uaa"));
additionalInformation.put(ClientConstants.ALLOWED_PROVIDERS, Collections.singletonList(loginHintOrigin));
when(uaaClient.getAdditionalInformation()).thenReturn(additionalInformation);
IdentityZoneHolder.get().getConfig().setDefaultIdentityProvider("uaa");
IdentityZoneHolder.get().getConfig().setDefaultIdentityProvider(loginHintOrigin);

instance.authenticate(auth);

/* should not read all in the zone during lookup of possible providers
* - "uaa" or "ldap" is used, but not as login hint */
final String idzId = IdentityZoneHolder.get().getId();
verify(identityProviderProvisioning, times(0)).retrieveActive(idzId);
verify(identityProviderProvisioning, times(1)).retrieveByOrigin(loginHintOrigin, idzId);

verify(zoneAwareAuthzAuthenticationManager, times(1)).authenticate(auth);
ArgumentCaptor<UaaLoginHint> captor = ArgumentCaptor.forClass(UaaLoginHint.class);
verify(zoneAwareAuthzAuthenticationManager, times(1)).setLoginHint(eq(auth), captor.capture());
assertNotNull(captor.getValue());
assertEquals("uaa", captor.getValue().getOrigin());
assertEquals(loginHintOrigin, captor.getValue().getOrigin());
}

@Test
Expand Down Expand Up @@ -719,24 +732,30 @@ void testOIDCPasswordGrant_LoginHintOidcOverridesDefaultUaa() {
verify(identityProviderProvisioning, times(0)).retrieveActive(any());
}

@Test
void testOIDCPasswordGrant_LoginHintUaaOverridesDefaultOidc() {
@ParameterizedTest
@ValueSource(strings = { OriginKeys.UAA, OriginKeys.LDAP })
void testOIDCPasswordGrant_LoginHintUaaOrLdapOverridesDefaultOidc(final String loginHintOrigin) {
UaaLoginHint loginHint = mock(UaaLoginHint.class);
when(loginHint.getOrigin()).thenReturn("uaa");
when(loginHint.getOrigin()).thenReturn(loginHintOrigin);
Authentication auth = mock(Authentication.class);
when(zoneAwareAuthzAuthenticationManager.extractLoginHint(auth)).thenReturn(null);
when(zoneAwareAuthzAuthenticationManager.extractLoginHint(auth)).thenReturn(loginHint);
Map<String, Object> additionalInformation = new HashMap<>();
additionalInformation.put(ClientConstants.ALLOWED_PROVIDERS, Collections.singletonList("uaa"));
additionalInformation.put(ClientConstants.ALLOWED_PROVIDERS, Collections.singletonList(loginHintOrigin));
when(uaaClient.getAdditionalInformation()).thenReturn(additionalInformation);
IdentityZoneHolder.get().getConfig().setDefaultIdentityProvider("oidcprovider");

instance.authenticate(auth);

// should read only "uaa" or "ldap" IdP during lookup of possible providers
final String idzId = IdentityZoneHolder.get().getId();
verify(identityProviderProvisioning, times(0)).retrieveActive(idzId);
verify(identityProviderProvisioning, times(1)).retrieveByOrigin(loginHintOrigin, idzId);

verify(zoneAwareAuthzAuthenticationManager, times(1)).authenticate(auth);
ArgumentCaptor<UaaLoginHint> captor = ArgumentCaptor.forClass(UaaLoginHint.class);
verify(zoneAwareAuthzAuthenticationManager, times(1)).setLoginHint(eq(auth), captor.capture());
assertNotNull(captor.getValue());
assertEquals("uaa", captor.getValue().getOrigin());
assertEquals(loginHintOrigin, captor.getValue().getOrigin());
}

@Test
Expand Down

0 comments on commit 16142fe

Please sign in to comment.