source: josm/trunk/test/unit/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClientTest.java@ 17275

Last change on this file since 17275 was 17196, checked in by simon04, 4 years ago

see #15102 - see #16637 - get rid of real HTTP calls to https://www.openstreetmap.org/login in OsmOAuthAuthorizationClientTest, mock them

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5import static com.github.tomakehurst.wiremock.client.WireMock.get;
6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
8import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertNotNull;
10import static org.junit.Assert.assertNull;
11
12import java.net.CookieHandler;
13import java.net.CookieManager;
14import java.net.URI;
15import java.util.Collections;
16
17import org.junit.Rule;
18import org.junit.Test;
19import org.openstreetmap.josm.data.oauth.OAuthParameters;
20import org.openstreetmap.josm.data.oauth.OAuthToken;
21import org.openstreetmap.josm.io.OsmTransferCanceledException;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26import com.github.tomakehurst.wiremock.junit.WireMockRule;
27
28/**
29 * Unit tests of {@link OsmOAuthAuthorizationClient} class.
30 */
31public class OsmOAuthAuthorizationClientTest {
32
33 /**
34 * Setup tests
35 */
36 @Rule
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules().timeout(20000);
39
40 /**
41 * HTTP mock.
42 */
43 @Rule
44 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());
45
46 /**
47 * Unit test of {@link OsmOAuthAuthorizationClient}.
48 * @throws OsmOAuthAuthorizationException if OAuth authorization error occurs
49 * @throws OsmTransferCanceledException if OSM transfer error occurs
50 */
51 @Test
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
60 OAuthToken requestToken = client.getRequestToken(null);
61 assertEquals("requestToken.key", "entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", requestToken.getKey());
62 assertEquals("requestToken.secret", "nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB", requestToken.getSecret());
63 String url = client.getAuthoriseUrl(requestToken);
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());
75 }
76
77 /**
78 * Unit test for correct cookie handling when logging in to the OSM website.
79 *
80 * https://josm.openstreetmap.de/ticket/12584
81 * @throws Exception if any error occurs
82 */
83 @Test
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
111 final OAuthParameters parameters = OAuthParameters.createDefault();
112 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters);
113 final CookieManager cm = new CookieManager();
114 cm.put(new URI(parameters.getOsmLoginUrl()),
115 Collections.singletonMap("Cookie", Collections.singletonList("_osm_session=" + String.valueOf(Math.PI).substring(2))));
116 CookieHandler.setDefault(cm);
117 assertNotNull(client.fetchOsmWebsiteSessionId());
118 }
119}
Note: See TracBrowser for help on using the repository browser.