|
16 | 16 |
|
17 | 17 | import static org.junit.jupiter.api.Assertions.assertEquals; |
18 | 18 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
19 | 20 | import static org.mockito.Mockito.mock; |
20 | 21 | import static org.mockito.Mockito.verify; |
21 | 22 | import static org.mockito.Mockito.verifyNoInteractions; |
@@ -122,4 +123,68 @@ void testCreateStaticResourceRegistrationWithDiscoveredData() { |
122 | 123 | verify(authorizationServerMetadataService).getAuthorizationServerMetadata( |
123 | 124 | resourceId, resourceEndpoint, protectedResourceMetadata, false); |
124 | 125 | } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testStaticEndpointsNotOverwrittenByDiscovery() { |
| 129 | + // Given |
| 130 | + String resourceId = "staticResource"; |
| 131 | + String resourceEndpoint = "https://test.endpoint.com"; |
| 132 | + String clientId = "staticClientId"; |
| 133 | + String clientSecret = "staticClientSecret"; |
| 134 | + String redirectUri = "https://static.redirect.uri"; |
| 135 | + |
| 136 | + // Static endpoints provided by the user |
| 137 | + String staticAuthorizationEndpoint = "https://static.auth.endpoint"; |
| 138 | + String staticTokenEndpoint = "https://static.token.endpoint"; |
| 139 | + List<String> scopesSupported = List.of("scope1", "scope2"); |
| 140 | + |
| 141 | + // Discovery returns different endpoints and a codeChallengeMethod |
| 142 | + String discoveredAuthorizationEndpoint = "https://discovered.auth.endpoint"; |
| 143 | + String discoveredTokenEndpoint = "https://discovered.token.endpoint"; |
| 144 | + String discoveredCodeChallengeMethod = "S256"; |
| 145 | + |
| 146 | + // codeChallengeMethod is null (simulates UI-created toolset), triggering discovery |
| 147 | + ResourceAuthSettings resourceAuthSettings = ResourceAuthSettings.builder() |
| 148 | + .clientId(clientId) |
| 149 | + .clientSecret(clientSecret) |
| 150 | + .redirectUri(redirectUri) |
| 151 | + .authorizationEndpoint(staticAuthorizationEndpoint) |
| 152 | + .tokenEndpoint(staticTokenEndpoint) |
| 153 | + .scopesSupported(scopesSupported) |
| 154 | + .build(); |
| 155 | + |
| 156 | + assertNull(resourceAuthSettings.getCodeChallengeMethod()); |
| 157 | + |
| 158 | + AuthorizationServerProtectedResourceMetadata protectedResourceMetadata = mock(AuthorizationServerProtectedResourceMetadata.class); |
| 159 | + when(protectedResourceMetadataService.getProtectedResourceMetadata(resourceId, resourceEndpoint)) |
| 160 | + .thenReturn(protectedResourceMetadata); |
| 161 | + |
| 162 | + AuthorizationServerMetadata authorizationServerMetadata = mock(AuthorizationServerMetadata.class); |
| 163 | + when(authorizationServerMetadata.getAuthorizationEndpoint()).thenReturn(discoveredAuthorizationEndpoint); |
| 164 | + when(authorizationServerMetadata.getTokenEndpoint()).thenReturn(discoveredTokenEndpoint); |
| 165 | + when(authorizationServerMetadata.getCodeChallengeMethodsSupported()).thenReturn(List.of(discoveredCodeChallengeMethod)); |
| 166 | + |
| 167 | + when(authorizationServerMetadataService.getAuthorizationServerMetadata( |
| 168 | + resourceId, resourceEndpoint, protectedResourceMetadata, false)) |
| 169 | + .thenReturn(authorizationServerMetadata); |
| 170 | + |
| 171 | + // When |
| 172 | + ClientRegistration result = resourceRegistrationStrategy.register(resourceId, resourceEndpoint, resourceAuthSettings); |
| 173 | + |
| 174 | + // Then |
| 175 | + assertNotNull(result); |
| 176 | + assertEquals(clientId, result.getClientId()); |
| 177 | + assertEquals(clientSecret, result.getClientSecret()); |
| 178 | + assertEquals(redirectUri, result.getRedirectUri()); |
| 179 | + // Static endpoints must be preserved (not overwritten by discovery) |
| 180 | + assertEquals(staticAuthorizationEndpoint, result.getAuthorizationEndpoint()); |
| 181 | + assertEquals(staticTokenEndpoint, result.getTokenEndpoint()); |
| 182 | + // codeChallengeMethod should be filled from discovery |
| 183 | + assertEquals(discoveredCodeChallengeMethod, result.getCodeChallengeMethod()); |
| 184 | + assertEquals(scopesSupported, result.getScopesSupported()); |
| 185 | + // Verify discovery was called (because codeChallengeMethod was null) |
| 186 | + verify(protectedResourceMetadataService).getProtectedResourceMetadata(resourceId, resourceEndpoint); |
| 187 | + verify(authorizationServerMetadataService).getAuthorizationServerMetadata( |
| 188 | + resourceId, resourceEndpoint, protectedResourceMetadata, false); |
| 189 | + } |
125 | 190 | } |
0 commit comments