|
2 | 2 |
|
3 | 3 | import static java.util.Map.entry;
|
4 | 4 |
|
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.absent; |
5 | 6 | import static com.github.tomakehurst.wiremock.client.WireMock.containing;
|
6 | 7 | import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
|
7 | 8 | import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
|
8 | 9 | import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
9 | 10 | import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
|
10 | 11 | import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
| 12 | +import static com.github.tomakehurst.wiremock.client.WireMock.unauthorized; |
11 | 13 | import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
12 | 14 | import static com.github.tomakehurst.wiremock.client.WireMock.verify;
|
13 | 15 | import static com.sap.cloud.sdk.cloudplatform.connectivity.ServiceBindingTestUtility.bindingWithCredentials;
|
14 | 16 | import static org.assertj.core.api.Assertions.assertThat;
|
| 17 | +import static org.assertj.core.api.Assertions.assertThatCode; |
15 | 18 |
|
16 | 19 | import java.io.IOException;
|
17 | 20 | import java.net.URI;
|
|
33 | 36 | import com.github.tomakehurst.wiremock.junit5.WireMockTest;
|
34 | 37 | import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
|
35 | 38 | import com.sap.cloud.environment.servicebinding.api.ServiceIdentifier;
|
| 39 | +import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException; |
36 | 40 | import com.sap.cloud.sdk.cloudplatform.connectivity.exception.HttpClientInstantiationException;
|
37 | 41 | import com.sap.cloud.sdk.cloudplatform.tenant.DefaultTenant;
|
38 | 42 | import com.sap.cloud.sdk.cloudplatform.tenant.TenantAccessor;
|
39 | 43 | import com.sap.cloud.security.client.HttpClientFactory;
|
40 | 44 | import com.sap.cloud.security.config.ClientIdentity;
|
| 45 | +import com.sap.cloud.security.xsuaa.client.OAuth2ServiceException; |
41 | 46 |
|
42 | 47 | import io.vavr.control.Try;
|
43 | 48 |
|
@@ -152,6 +157,55 @@ void testIasTokenFlow()
|
152 | 157 | }
|
153 | 158 | }
|
154 | 159 |
|
| 160 | + @Test |
| 161 | + void testExtended401ErrorMessage() |
| 162 | + { |
| 163 | + final ServiceBinding binding = |
| 164 | + bindingWithCredentials( |
| 165 | + ServiceIdentifier.DESTINATION, |
| 166 | + entry("credential-type", "binding-secret"), |
| 167 | + entry("clientid", "myClientId2"), |
| 168 | + entry("clientsecret", "myClientSecret2"), |
| 169 | + entry("uri", "http://provider.destination.domain"), |
| 170 | + entry("url", "http://provider.destination.domain")); |
| 171 | + final ServiceBindingDestinationOptions options = ServiceBindingDestinationOptions.forService(binding).build(); |
| 172 | + |
| 173 | + final Try<HttpDestination> maybeDestination = |
| 174 | + new OAuth2ServiceBindingDestinationLoader().tryGetDestination(options); |
| 175 | + assertThat(maybeDestination.isSuccess()).isTrue(); |
| 176 | + final HttpDestination destination = maybeDestination.get(); |
| 177 | + |
| 178 | + { |
| 179 | + // provider case - no tenant: |
| 180 | + // Here, the short error message is returned. |
| 181 | + stubFor( |
| 182 | + post("/oauth/token") |
| 183 | + .withHost(equalTo("provider.destination.domain")) |
| 184 | + .withHeader("X-zid", absent()) |
| 185 | + .willReturn(unauthorized())); |
| 186 | + assertThatCode(destination::getHeaders) |
| 187 | + .isInstanceOf(DestinationAccessException.class) |
| 188 | + .hasMessageEndingWith("Failed to resolve access token.") |
| 189 | + .hasRootCauseInstanceOf(OAuth2ServiceException.class); |
| 190 | + } |
| 191 | + { |
| 192 | + // subscriber tenant: |
| 193 | + // Here, the error message contains a note about updating the SaaS registry. |
| 194 | + stubFor( |
| 195 | + post("/oauth/token") |
| 196 | + .withHost(equalTo("provider.destination.domain")) |
| 197 | + .withHeader("X-zid", equalTo("subscriber")) |
| 198 | + .willReturn(unauthorized())); |
| 199 | + |
| 200 | + TenantAccessor.executeWithTenant(new DefaultTenant("subscriber", "subscriber"), () -> { |
| 201 | + assertThatCode(destination::getHeaders) |
| 202 | + .isInstanceOf(DestinationAccessException.class) |
| 203 | + .hasMessageEndingWith("subscribed for the current tenant.") |
| 204 | + .hasRootCauseInstanceOf(OAuth2ServiceException.class); |
| 205 | + }); |
| 206 | + } |
| 207 | + } |
| 208 | + |
155 | 209 | @Test
|
156 | 210 | @DisplayName( "The subdomain should be replaced for subscriber tenants when using IAS and ZTIS" )
|
157 | 211 | void testIasFlowWithZeroTrustAndSubscriberTenant()
|
|
0 commit comments