|
| 1 | +package uk.gov.di.orchestration.shared.utils; |
| 2 | + |
| 3 | +import com.nimbusds.oauth2.sdk.auth.ClientAuthenticationMethod; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import uk.gov.di.orchestration.shared.entity.ClientRegistry; |
| 7 | +import uk.gov.di.orchestration.shared.services.ConfigurationService; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +public class ClientUtilsTest { |
| 15 | + private final ConfigurationService configurationService = mock(ConfigurationService.class); |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + public void setup() { |
| 19 | + when(configurationService.isUseDefaultTokenAuthMethod()).thenReturn(false); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void shouldDefaultToPrivateKeyJwtIfFeatureFlagIsEnabledAndTokenAuthMethodIsNull() { |
| 24 | + when(configurationService.isUseDefaultTokenAuthMethod()).thenReturn(true); |
| 25 | + var client = clientWithTokenAuthMethod(null); |
| 26 | + |
| 27 | + var actualTokenAuthMethod = |
| 28 | + ClientUtils.getTokenAuthMethodOrDefault(client, configurationService); |
| 29 | + assertEquals(actualTokenAuthMethod, ClientAuthenticationMethod.PRIVATE_KEY_JWT.getValue()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void shouldNotDefaultToPrivateKeyJwtIfFeatureFlagDisabled() { |
| 34 | + when(configurationService.isUseDefaultTokenAuthMethod()).thenReturn(false); |
| 35 | + var client = clientWithTokenAuthMethod(null); |
| 36 | + |
| 37 | + var actualTokenAuthMethod = |
| 38 | + ClientUtils.getTokenAuthMethodOrDefault(client, configurationService); |
| 39 | + assertNull(actualTokenAuthMethod); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void shouldNotDefaultToPrivateKeyJwtIfFeatureFlagIsEnabledAndTokenAuthMethodIsAlreadySet() { |
| 44 | + when(configurationService.isUseDefaultTokenAuthMethod()).thenReturn(true); |
| 45 | + var client = |
| 46 | + clientWithTokenAuthMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST.getValue()); |
| 47 | + |
| 48 | + var actualTokenAuthMethod = |
| 49 | + ClientUtils.getTokenAuthMethodOrDefault(client, configurationService); |
| 50 | + assertEquals( |
| 51 | + actualTokenAuthMethod, ClientAuthenticationMethod.CLIENT_SECRET_POST.getValue()); |
| 52 | + } |
| 53 | + |
| 54 | + private ClientRegistry clientWithTokenAuthMethod(String tokenAuthMethod) { |
| 55 | + return new ClientRegistry() |
| 56 | + .withClientID("client-id") |
| 57 | + .withClientName("client-one") |
| 58 | + .withTokenAuthMethod(tokenAuthMethod); |
| 59 | + } |
| 60 | +} |
0 commit comments