Changeset 18786 in josm for trunk/test
- Timestamp:
- 2023-07-27T15:47:48+02:00 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/oauth/OAuth20AuthorizationTest.java
r18666 r18786 2 2 package org.openstreetmap.josm.data.oauth; 3 3 4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 4 5 import static org.junit.jupiter.api.Assertions.assertEquals; 5 6 import static org.junit.jupiter.api.Assertions.assertNotNull; … … 8 9 9 10 import java.io.IOException; 11 import java.net.URL; 10 12 import java.util.HashMap; 11 13 import java.util.Map; … … 20 22 import com.github.tomakehurst.wiremock.extension.Parameters; 21 23 import com.github.tomakehurst.wiremock.extension.ResponseTransformer; 24 import com.github.tomakehurst.wiremock.http.FixedDelayDistribution; 22 25 import com.github.tomakehurst.wiremock.http.HttpHeader; 23 26 import com.github.tomakehurst.wiremock.http.HttpHeaders; … … 62 65 private static final String CODE_CHALLENGE = "code_challenge"; 63 66 67 private enum ConnectionProblems { 68 NONE, 69 SOCKET_TIMEOUT 70 } 71 64 72 private static class OAuthServerWireMock extends ResponseTransformer { 65 73 String stateToReturn; 74 ConnectionProblems connectionProblems = ConnectionProblems.NONE; 66 75 @Override 67 76 public Response transform(Request request, Response response, FileSource files, Parameters parameters) { … … 89 98 return Response.Builder.like(response).but().status(500).build(); 90 99 } 91 return Response.Builder.like(response).but().body("{\"token_type\": \"bearer\", \"access_token\": \"test_access_token\"}").build(); 100 switch (connectionProblems) { 101 case SOCKET_TIMEOUT: 102 return Response.Builder.like(response).but().configureDelay(null, null, 103 10_000, new FixedDelayDistribution(0)).build(); 104 case NONE: 105 default: 106 return Response.Builder.like(response).but() 107 .body("{\"token_type\": \"bearer\", \"access_token\": \"test_access_token\"}").build(); 108 } 92 109 } 93 110 … … 136 153 RemoteControl.stop(); // Ensure remote control is stopped 137 154 oauthServer.stateToReturn = null; 155 oauthServer.connectionProblems = ConnectionProblems.NONE; 138 156 } 139 157 … … 164 182 } 165 183 166 @Test 167 void testAuthorize(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException { 184 private HttpClient generateClient(WireMockRuntimeInfo wireMockRuntimeInfo, AtomicReference<Optional<IOAuthToken>> consumer) { 168 185 final OAuth20Authorization authorization = new OAuth20Authorization(); 169 final AtomicReference<Optional<IOAuthToken>> consumer = new AtomicReference<>();170 186 OAuth20Parameters parameters = (OAuth20Parameters) OAuthParameters.createDefault(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20); 171 187 RemoteControl.start(); … … 174 190 parameters.getRedirectUri()), consumer::set, OsmScopes.read_gpx); 175 191 assertEquals(1, OpenBrowserMocker.getCalledURIs().size()); 176 HttpClient client = HttpClient.create(OpenBrowserMocker.getCalledURIs().get(0).toURL()); 192 final URL url = assertDoesNotThrow(() -> OpenBrowserMocker.getCalledURIs().get(0).toURL()); 193 return HttpClient.create(url); 194 } 195 196 @Test 197 void testAuthorize(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException { 198 final AtomicReference<Optional<IOAuthToken>> consumer = new AtomicReference<>(); 199 final HttpClient client = generateClient(wireMockRuntimeInfo, consumer); 177 200 try { 178 201 HttpClient.Response response = client.connect(); … … 191 214 void testAuthorizeBadState(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException { 192 215 oauthServer.stateToReturn = "Bad_State"; 193 final OAuth20Authorization authorization = new OAuth20Authorization();194 216 final AtomicReference<Optional<IOAuthToken>> consumer = new AtomicReference<>(); 195 OAuth20Parameters parameters = (OAuth20Parameters) OAuthParameters.createDefault(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20); 196 RemoteControl.start(); 197 authorization.authorize(new OAuth20Parameters(parameters.getClientId(), parameters.getClientSecret(), 198 wireMockRuntimeInfo.getHttpBaseUrl() + "/oauth2", wireMockRuntimeInfo.getHttpBaseUrl() + "/api", 199 parameters.getRedirectUri()), consumer::set, OsmScopes.read_gpx); 200 assertEquals(1, OpenBrowserMocker.getCalledURIs().size()); 201 HttpClient client = HttpClient.create(OpenBrowserMocker.getCalledURIs().get(0).toURL()); 217 final HttpClient client = generateClient(wireMockRuntimeInfo, consumer); 202 218 try { 203 219 HttpClient.Response response = client.connect(); … … 210 226 assertNull(consumer.get(), "The OAuth consumer should not be called since the state does not match"); 211 227 } 228 229 @Test 230 void testSocketTimeout(WireMockRuntimeInfo wireMockRuntimeInfo) throws Exception { 231 // 1s before timeout 232 Config.getPref().putInt("socket.timeout.connect", 1); 233 Config.getPref().putInt("socket.timeout.read", 1); 234 oauthServer.connectionProblems = ConnectionProblems.SOCKET_TIMEOUT; 235 236 final AtomicReference<Optional<IOAuthToken>> consumer = new AtomicReference<>(); 237 final HttpClient client = generateClient(wireMockRuntimeInfo, consumer) 238 .setConnectTimeout(15_000).setReadTimeout(30_000); 239 try { 240 HttpClient.Response response = client.connect(); 241 assertEquals(500, response.getResponseCode()); 242 String content = response.fetchContent(); 243 assertTrue(content.contains("java.net.SocketTimeoutException: Read timed out")); 244 } finally { 245 client.disconnect(); 246 } 247 assertEquals(Optional.empty(), consumer.get()); 248 } 212 249 }
Note:
See TracChangeset
for help on using the changeset viewer.
