Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java
r16643 r17196 47 47 private HttpClient connection; 48 48 49 pr ivatestatic class SessionId {50 pr ivateString id;51 pr ivateString token;52 pr ivateString userName;49 protected static class SessionId { 50 protected String id; 51 protected String token; 52 protected String userName; 53 53 } 54 54 -
trunk/test/unit/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClientTest.java
r13203 r17196 2 2 package org.openstreetmap.josm.gui.oauth; 3 3 4 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; 5 import static com.github.tomakehurst.wiremock.client.WireMock.get; 6 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; 7 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; 8 import static org.junit.Assert.assertEquals; 4 9 import static org.junit.Assert.assertNotNull; 10 import static org.junit.Assert.assertNull; 5 11 6 12 import java.net.CookieHandler; 7 13 import java.net.CookieManager; 8 import java.net.MalformedURLException;9 14 import java.net.URI; 10 import java.net.URL;11 15 import java.util.Collections; 12 16 … … 19 23 20 24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 25 26 import com.github.tomakehurst.wiremock.junit.WireMockRule; 21 27 22 28 /** … … 33 39 34 40 /** 41 * HTTP mock. 42 */ 43 @Rule 44 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort()); 45 46 /** 35 47 * Unit test of {@link OsmOAuthAuthorizationClient}. 36 48 * @throws OsmOAuthAuthorizationException if OAuth authorization error occurs 37 49 * @throws OsmTransferCanceledException if OSM transfer error occurs 38 * @throws MalformedURLException in case of invalid URL39 50 */ 40 51 @Test 41 public void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException, MalformedURLException { 42 OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(OAuthParameters.createDefault()); 52 public void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException { 53 // request token 54 wireMockRule.stubFor(get(urlEqualTo("/oauth/request_token")) 55 .willReturn(aResponse().withStatus(200).withBody(String.join("&", 56 "oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", 57 "oauth_token_secret=nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB")))); 58 OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(OAuthParameters.createDefault(wireMockRule.url("/api"))); 59 43 60 OAuthToken requestToken = client.getRequestToken(null); 44 assertNotNull(requestToken); 61 assertEquals("requestToken.key", "entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", requestToken.getKey()); 62 assertEquals("requestToken.secret", "nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB", requestToken.getSecret()); 45 63 String url = client.getAuthoriseUrl(requestToken); 46 assertNotNull(url); 47 System.out.println(new URL(url)); 48 //OAuthToken accessToken = client.getAccessToken(null); 49 //assertNotNull(accessToken); 64 assertEquals("url", wireMockRule.url("/oauth/authorize?oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR"), url); 65 66 // access token 67 wireMockRule.stubFor(get(urlEqualTo("/oauth/access_token")) 68 .willReturn(aResponse().withStatus(200).withBody(String.join("&", 69 "oauth_token=eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX", 70 "oauth_token_secret=nsBUeNor7tPh3SHr5lLaGsGDUD2PYMV1TinL4qUB")))); 71 72 OAuthToken accessToken = client.getAccessToken(null); 73 assertEquals("accessToken.key", "eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX", accessToken.getKey()); 74 assertEquals("accessToken.secret", "nsBUeNor7tPh3SHr5lLaGsGDUD2PYMV1TinL4qUB", accessToken.getSecret()); 50 75 } 51 76 … … 57 82 */ 58 83 @Test 59 public void testCookieHandling() throws Exception { 84 public void testCookieHandlingMock() throws Exception { 85 wireMockRule.stubFor(get(urlEqualTo("/login?cookie_test=true")) 86 .willReturn(aResponse() 87 .withStatus(200) 88 .withHeader("Set-Cookie", "_osm_session=7fe8e2ea36c6b803cb902301b28e0a; path=/; HttpOnly; SameSite=Lax") 89 .withBody("<input type=\"hidden\" " + 90 "name=\"authenticity_token\" " + 91 "value=\"fzp6CWJhp6Vns09re3s2Tw==\" />"))); 92 final OAuthParameters parameters = OAuthParameters.createDefault(wireMockRule.url("/api")); 93 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters); 94 final OsmOAuthAuthorizationClient.SessionId sessionId = client.fetchOsmWebsiteSessionId(); 95 assertNotNull(sessionId); 96 assertEquals("sessionId.id", "7fe8e2ea36c6b803cb902301b28e0a", sessionId.id); 97 assertEquals("sessionId.token", "fzp6CWJhp6Vns09re3s2Tw==", sessionId.token); 98 assertNull("sessionId.userName", sessionId.userName); 99 } 100 101 /** 102 * Unit test for correct cookie handling when logging in to the OSM website. 103 * 104 * https://josm.openstreetmap.de/ticket/12584 105 * @throws Exception if any error occurs 106 */ 107 @Test 108 public void testCookieHandlingCookieManager() throws Exception { 109 // emulate Java Web Start behaviour 110 // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html 60 111 final OAuthParameters parameters = OAuthParameters.createDefault(); 61 112 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters); 62 assertNotNull(client.fetchOsmWebsiteSessionId());63 64 // emulate Java Web Start behaviour65 // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html66 113 final CookieManager cm = new CookieManager(); 67 114 cm.put(new URI(parameters.getOsmLoginUrl()),
Note:
See TracChangeset
for help on using the changeset viewer.