|
| 1 | +package com.descope.sdk.mgmt.impl; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.eq; |
| 8 | +import static org.mockito.Mockito.doReturn; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.mockStatic; |
| 11 | + |
| 12 | +import com.descope.exception.ServerCommonException; |
| 13 | +import com.descope.model.client.Client; |
| 14 | +import com.descope.model.jwt.response.ClientAssertionResponse; |
| 15 | +import com.descope.proxy.ApiProxy; |
| 16 | +import com.descope.proxy.impl.ApiProxyBuilder; |
| 17 | +import com.descope.sdk.mgmt.JwtService; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.mockito.MockedStatic; |
| 23 | + |
| 24 | +public class JwtServiceImplClientAssertionTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + void testGenerateClientAssertionJwtSuccess() { |
| 28 | + ApiProxy apiProxy = mock(ApiProxy.class); |
| 29 | + ClientAssertionResponse mockResponse = new ClientAssertionResponse("mock.jwt.token"); |
| 30 | + doReturn(mockResponse).when(apiProxy).post(any(), any(), eq(ClientAssertionResponse.class)); |
| 31 | + |
| 32 | + try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { |
| 33 | + mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy); |
| 34 | + |
| 35 | + Client client = Client.builder() |
| 36 | + .projectId("test-project") |
| 37 | + .managementKey("test-key") |
| 38 | + .build(); |
| 39 | + JwtService jwtService = new JwtServiceImpl(client); |
| 40 | + |
| 41 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 42 | + ClientAssertionResponse response = jwtService.generateClientAssertionJwt( |
| 43 | + "client-id", |
| 44 | + "client-id", |
| 45 | + audience, |
| 46 | + 3600, |
| 47 | + null, |
| 48 | + null |
| 49 | + ); |
| 50 | + |
| 51 | + assertNotNull(response); |
| 52 | + assertEquals("mock.jwt.token", response.getJwt()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testGenerateClientAssertionJwtWithOptionalParams() { |
| 58 | + ApiProxy apiProxy = mock(ApiProxy.class); |
| 59 | + ClientAssertionResponse mockResponse = new ClientAssertionResponse("mock.jwt.token"); |
| 60 | + doReturn(mockResponse).when(apiProxy).post(any(), any(), eq(ClientAssertionResponse.class)); |
| 61 | + |
| 62 | + try (MockedStatic<ApiProxyBuilder> mockedBuilder = mockStatic(ApiProxyBuilder.class)) { |
| 63 | + mockedBuilder.when(() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy); |
| 64 | + |
| 65 | + Client client = Client.builder() |
| 66 | + .projectId("test-project") |
| 67 | + .managementKey("test-key") |
| 68 | + .build(); |
| 69 | + JwtService jwtService = new JwtServiceImpl(client); |
| 70 | + |
| 71 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 72 | + ClientAssertionResponse response = jwtService.generateClientAssertionJwt( |
| 73 | + "client-id", |
| 74 | + "client-id", |
| 75 | + audience, |
| 76 | + 3600, |
| 77 | + true, |
| 78 | + "RS256" |
| 79 | + ); |
| 80 | + |
| 81 | + assertNotNull(response); |
| 82 | + assertEquals("mock.jwt.token", response.getJwt()); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testGenerateClientAssertionJwtEmptyIssuer() { |
| 88 | + Client client = Client.builder() |
| 89 | + .projectId("test-project") |
| 90 | + .managementKey("test-key") |
| 91 | + .build(); |
| 92 | + JwtService jwtService = new JwtServiceImpl(client); |
| 93 | + |
| 94 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 95 | + ServerCommonException thrown = assertThrows( |
| 96 | + ServerCommonException.class, |
| 97 | + () -> jwtService.generateClientAssertionJwt("", "client-id", audience, 3600, null, null) |
| 98 | + ); |
| 99 | + assertNotNull(thrown); |
| 100 | + assertEquals("The issuer argument is invalid", thrown.getMessage()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testGenerateClientAssertionJwtEmptySubject() { |
| 105 | + Client client = Client.builder() |
| 106 | + .projectId("test-project") |
| 107 | + .managementKey("test-key") |
| 108 | + .build(); |
| 109 | + JwtService jwtService = new JwtServiceImpl(client); |
| 110 | + |
| 111 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 112 | + ServerCommonException thrown = assertThrows( |
| 113 | + ServerCommonException.class, |
| 114 | + () -> jwtService.generateClientAssertionJwt("client-id", "", audience, 3600, null, null) |
| 115 | + ); |
| 116 | + assertNotNull(thrown); |
| 117 | + assertEquals("The subject argument is invalid", thrown.getMessage()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void testGenerateClientAssertionJwtNullAudience() { |
| 122 | + Client client = Client.builder() |
| 123 | + .projectId("test-project") |
| 124 | + .managementKey("test-key") |
| 125 | + .build(); |
| 126 | + JwtService jwtService = new JwtServiceImpl(client); |
| 127 | + |
| 128 | + ServerCommonException thrown = assertThrows( |
| 129 | + ServerCommonException.class, |
| 130 | + () -> jwtService.generateClientAssertionJwt("client-id", "client-id", null, 3600, null, null) |
| 131 | + ); |
| 132 | + assertNotNull(thrown); |
| 133 | + assertEquals("The audience argument is invalid", thrown.getMessage()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testGenerateClientAssertionJwtEmptyAudience() { |
| 138 | + Client client = Client.builder() |
| 139 | + .projectId("test-project") |
| 140 | + .managementKey("test-key") |
| 141 | + .build(); |
| 142 | + JwtService jwtService = new JwtServiceImpl(client); |
| 143 | + |
| 144 | + List<String> audience = Collections.emptyList(); |
| 145 | + ServerCommonException thrown = assertThrows( |
| 146 | + ServerCommonException.class, |
| 147 | + () -> jwtService.generateClientAssertionJwt("client-id", "client-id", audience, 3600, null, null) |
| 148 | + ); |
| 149 | + assertNotNull(thrown); |
| 150 | + assertEquals("The audience argument is invalid", thrown.getMessage()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void testGenerateClientAssertionJwtNullExpiresIn() { |
| 155 | + Client client = Client.builder() |
| 156 | + .projectId("test-project") |
| 157 | + .managementKey("test-key") |
| 158 | + .build(); |
| 159 | + JwtService jwtService = new JwtServiceImpl(client); |
| 160 | + |
| 161 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 162 | + ServerCommonException thrown = assertThrows( |
| 163 | + ServerCommonException.class, |
| 164 | + () -> jwtService.generateClientAssertionJwt("client-id", "client-id", audience, null, null, null) |
| 165 | + ); |
| 166 | + assertNotNull(thrown); |
| 167 | + assertEquals("The expiresIn argument is invalid", thrown.getMessage()); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void testGenerateClientAssertionJwtZeroExpiresIn() { |
| 172 | + Client client = Client.builder() |
| 173 | + .projectId("test-project") |
| 174 | + .managementKey("test-key") |
| 175 | + .build(); |
| 176 | + JwtService jwtService = new JwtServiceImpl(client); |
| 177 | + |
| 178 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 179 | + ServerCommonException thrown = assertThrows( |
| 180 | + ServerCommonException.class, |
| 181 | + () -> jwtService.generateClientAssertionJwt("client-id", "client-id", audience, 0, null, null) |
| 182 | + ); |
| 183 | + assertNotNull(thrown); |
| 184 | + assertEquals("The expiresIn argument is invalid", thrown.getMessage()); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void testGenerateClientAssertionJwtNegativeExpiresIn() { |
| 189 | + Client client = Client.builder() |
| 190 | + .projectId("test-project") |
| 191 | + .managementKey("test-key") |
| 192 | + .build(); |
| 193 | + JwtService jwtService = new JwtServiceImpl(client); |
| 194 | + |
| 195 | + List<String> audience = Arrays.asList("https://auth.example.com/token"); |
| 196 | + ServerCommonException thrown = assertThrows( |
| 197 | + ServerCommonException.class, |
| 198 | + () -> jwtService.generateClientAssertionJwt("client-id", "client-id", audience, -1, null, null) |
| 199 | + ); |
| 200 | + assertNotNull(thrown); |
| 201 | + assertEquals("The expiresIn argument is invalid", thrown.getMessage()); |
| 202 | + } |
| 203 | +} |
0 commit comments