-
Notifications
You must be signed in to change notification settings - Fork 4
test(auth): add comprehensive coverage for auth management key feature #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package com.descope.client; | ||
|
|
||
| import static com.descope.literals.AppConstants.AUTH_MANAGEMENT_KEY_ENV_VAR; | ||
| import static com.descope.literals.AppConstants.MANAGEMENT_KEY_ENV_VAR; | ||
| import static com.descope.literals.AppConstants.PROJECT_ID_ENV_VAR; | ||
| import static com.descope.literals.AppConstants.PUBLIC_KEY_ENV_VAR; | ||
|
|
@@ -123,4 +124,62 @@ void testEmptyConfig() { | |
| .isInstanceOf(ServerCommonException.class) | ||
| .hasMessage("The Config argument is invalid"); | ||
| } | ||
|
|
||
| @Test | ||
| void testAuthManagementKeyFromEnvVariable() throws Exception { | ||
| String expectedProjectID = "P123456789012345678901234567"; | ||
| String expectedAuthManagementKey = "someAuthManagementKey"; | ||
| EnvironmentVariables env = | ||
| new EnvironmentVariables(PROJECT_ID_ENV_VAR, expectedProjectID) | ||
| .and(AUTH_MANAGEMENT_KEY_ENV_VAR, expectedAuthManagementKey); | ||
| env.execute( | ||
| () -> { | ||
| DescopeClient descopeClient = new DescopeClient(); | ||
| Config config = descopeClient.getConfig(); | ||
| Assertions.assertThat(config.getProjectId()).isEqualTo(expectedProjectID); | ||
| Assertions.assertThat(config.getAuthManagementKey()).isEqualTo(expectedAuthManagementKey); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void testAuthManagementKeyFromConfig() throws Exception { | ||
| String expectedProjectID = "P123456789012345678901234567"; | ||
| String expectedAuthManagementKey = "someAuthManagementKey"; | ||
| Config config = Config.builder() | ||
| .projectId(expectedProjectID) | ||
| .authManagementKey(expectedAuthManagementKey) | ||
| .build(); | ||
| DescopeClient descopeClient = new DescopeClient(config); | ||
| Assertions.assertThat(descopeClient.getConfig().getProjectId()).isEqualTo(expectedProjectID); | ||
| Assertions.assertThat(descopeClient.getConfig().getAuthManagementKey()).isEqualTo(expectedAuthManagementKey); | ||
| } | ||
|
|
||
| @Test | ||
| void testAuthManagementKeyAndManagementKeyTogether() throws Exception { | ||
| String expectedProjectID = "P123456789012345678901234567"; | ||
| String expectedManagementKey = "someManagementKey"; | ||
| String expectedAuthManagementKey = "someAuthManagementKey"; | ||
| EnvironmentVariables env = | ||
| new EnvironmentVariables(PROJECT_ID_ENV_VAR, expectedProjectID) | ||
| .and(MANAGEMENT_KEY_ENV_VAR, expectedManagementKey) | ||
| .and(AUTH_MANAGEMENT_KEY_ENV_VAR, expectedAuthManagementKey); | ||
| env.execute( | ||
| () -> { | ||
| DescopeClient descopeClient = new DescopeClient(); | ||
| Config config = descopeClient.getConfig(); | ||
| Assertions.assertThat(config.getProjectId()).isEqualTo(expectedProjectID); | ||
| Assertions.assertThat(config.getManagementKey()).isEqualTo(expectedManagementKey); | ||
| Assertions.assertThat(config.getAuthManagementKey()).isEqualTo(expectedAuthManagementKey); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void testAuthManagementKeyConfigMethod() throws Exception { | ||
| String expectedProjectID = "P123456789012345678901234567"; | ||
| Config config = Config.builder() | ||
| .projectId(expectedProjectID) | ||
| .build(); | ||
| DescopeClient descopeClient = new DescopeClient(config); | ||
| Assertions.assertThat(descopeClient.getConfig().getProjectId()).isEqualTo(expectedProjectID); | ||
| } | ||
|
Comment on lines
+176
to
+184
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package com.descope.sdk.auth.impl; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.descope.model.client.Client; | ||
| import com.descope.model.client.SdkInfo; | ||
| import com.descope.proxy.ApiProxy; | ||
| import com.descope.proxy.impl.ApiProxyBuilder; | ||
| import java.util.function.Supplier; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| public class AuthenticationsBaseTest { | ||
|
|
||
| @Test | ||
| void testGetApiProxyWithAuthManagementKey() { | ||
| String projectId = "P123456789012345678901234567"; | ||
| String authManagementKey = "auth-mgmt-key-123"; | ||
|
|
||
| Client client = Client.builder() | ||
| .projectId(projectId) | ||
| .authManagementKey(authManagementKey) | ||
| .sdkInfo(SdkInfo.builder().name("test").build()) | ||
| .build(); | ||
|
|
||
| ApiProxy mockProxy = mock(ApiProxy.class); | ||
| ArgumentCaptor<Supplier<String>> authHeaderCaptor = ArgumentCaptor.forClass(Supplier.class); | ||
|
|
||
| try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { | ||
| mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(authHeaderCaptor.capture(), any(Client.class))) | ||
| .thenReturn(mockProxy); | ||
|
|
||
| OTPServiceImpl otpService = new OTPServiceImpl(client); | ||
| otpService.getApiProxy(); | ||
|
Comment on lines
+39
to
+40
|
||
|
|
||
| String authHeader = authHeaderCaptor.getValue().get(); | ||
| assertThat(authHeader).isEqualTo("Bearer " + projectId + ":" + authManagementKey); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testGetApiProxyWithoutAuthManagementKey() { | ||
| String projectId = "P123456789012345678901234567"; | ||
|
|
||
| Client client = Client.builder() | ||
| .projectId(projectId) | ||
| .sdkInfo(SdkInfo.builder().name("test").build()) | ||
| .build(); | ||
|
|
||
| ApiProxy mockProxy = mock(ApiProxy.class); | ||
| ArgumentCaptor<Supplier<String>> authHeaderCaptor = ArgumentCaptor.forClass(Supplier.class); | ||
|
|
||
| try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { | ||
| mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(authHeaderCaptor.capture(), any(Client.class))) | ||
| .thenReturn(mockProxy); | ||
|
|
||
| OTPServiceImpl otpService = new OTPServiceImpl(client); | ||
| otpService.getApiProxy(); | ||
|
|
||
| String authHeader = authHeaderCaptor.getValue().get(); | ||
| assertThat(authHeader).isEqualTo("Bearer " + projectId); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testGetApiProxyWithRefreshTokenAndAuthManagementKey() { | ||
| String projectId = "P123456789012345678901234567"; | ||
| String authManagementKey = "auth-mgmt-key-123"; | ||
| String refreshToken = "refresh-token-456"; | ||
|
|
||
| Client client = Client.builder() | ||
| .projectId(projectId) | ||
| .authManagementKey(authManagementKey) | ||
| .sdkInfo(SdkInfo.builder().name("test").build()) | ||
| .build(); | ||
|
|
||
| ApiProxy mockProxy = mock(ApiProxy.class); | ||
| ArgumentCaptor<Supplier<String>> authHeaderCaptor = ArgumentCaptor.forClass(Supplier.class); | ||
|
|
||
| try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { | ||
| mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(authHeaderCaptor.capture(), any(Client.class))) | ||
| .thenReturn(mockProxy); | ||
|
|
||
| OTPServiceImpl otpService = new OTPServiceImpl(client); | ||
| otpService.getApiProxy(refreshToken); | ||
|
|
||
| String authHeader = authHeaderCaptor.getValue().get(); | ||
| assertThat(authHeader).isEqualTo("Bearer " + projectId + ":" + refreshToken + ":" + authManagementKey); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testGetApiProxyWithRefreshTokenWithoutAuthManagementKey() { | ||
| String projectId = "P123456789012345678901234567"; | ||
| String refreshToken = "refresh-token-456"; | ||
|
|
||
| Client client = Client.builder() | ||
| .projectId(projectId) | ||
| .sdkInfo(SdkInfo.builder().name("test").build()) | ||
| .build(); | ||
|
|
||
| ApiProxy mockProxy = mock(ApiProxy.class); | ||
| ArgumentCaptor<Supplier<String>> authHeaderCaptor = ArgumentCaptor.forClass(Supplier.class); | ||
|
|
||
| try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { | ||
| mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(authHeaderCaptor.capture(), any(Client.class))) | ||
| .thenReturn(mockProxy); | ||
|
|
||
| OTPServiceImpl otpService = new OTPServiceImpl(client); | ||
| otpService.getApiProxy(refreshToken); | ||
|
|
||
| String authHeader = authHeaderCaptor.getValue().get(); | ||
| assertThat(authHeader).isEqualTo("Bearer " + projectId + ":" + refreshToken); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testGetApiProxyWithEmptyAuthManagementKey() { | ||
| String projectId = "P123456789012345678901234567"; | ||
|
|
||
| Client client = Client.builder() | ||
| .projectId(projectId) | ||
| .authManagementKey("") | ||
| .sdkInfo(SdkInfo.builder().name("test").build()) | ||
| .build(); | ||
|
|
||
| ApiProxy mockProxy = mock(ApiProxy.class); | ||
| ArgumentCaptor<Supplier<String>> authHeaderCaptor = ArgumentCaptor.forClass(Supplier.class); | ||
|
|
||
| try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { | ||
| mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(authHeaderCaptor.capture(), any(Client.class))) | ||
| .thenReturn(mockProxy); | ||
|
|
||
| OTPServiceImpl otpService = new OTPServiceImpl(client); | ||
| otpService.getApiProxy(); | ||
|
|
||
| String authHeader = authHeaderCaptor.getValue().get(); | ||
| assertThat(authHeader).isEqualTo("Bearer " + projectId); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test name
testAuthManagementKeyConfigMethodis misleading since it doesn't actually test auth management key configuration. The test should be renamed to reflect what it actually tests (e.g.,testConfigWithoutAuthManagementKey) or updated to include auth management key assertions.