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

Last change on this file since 18106 was 18106, checked in by Don-vip, 3 years ago

fix #21150 - Add JUnit5 annotation for WireMockServer (patch by taylor.smock)

  • 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 org.junit.jupiter.api.Assertions.assertEquals;
8import static org.junit.jupiter.api.Assertions.assertNotNull;
9import static org.junit.jupiter.api.Assertions.assertNull;
10
11import java.net.CookieHandler;
12import java.net.CookieManager;
13import java.net.URI;
14import java.util.Collections;
15
16import org.junit.jupiter.api.Test;
17import org.junit.jupiter.api.Timeout;
18import org.openstreetmap.josm.data.oauth.OAuthParameters;
19import org.openstreetmap.josm.data.oauth.OAuthToken;
20import org.openstreetmap.josm.io.OsmTransferCanceledException;
21import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
22import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
23import org.openstreetmap.josm.testutils.annotations.HTTP;
24
25import com.github.tomakehurst.wiremock.WireMockServer;
26
27/**
28 * Unit tests of {@link OsmOAuthAuthorizationClient} class.
29 */
30@Timeout(20)
31@BasicWiremock
32// Needed for OAuthParameters
33@BasicPreferences
34@HTTP
35class OsmOAuthAuthorizationClientTest {
36 /**
37 * HTTP mock.
38 */
39 @BasicWiremock
40 WireMockServer wireMockServer;
41
42 /**
43 * Unit test of {@link OsmOAuthAuthorizationClient}.
44 * @throws OsmOAuthAuthorizationException if OAuth authorization error occurs
45 * @throws OsmTransferCanceledException if OSM transfer error occurs
46 */
47 @Test
48 void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException {
49 // request token
50 wireMockServer.stubFor(get(urlEqualTo("/oauth/request_token"))
51 .willReturn(aResponse().withStatus(200).withBody(String.join("&",
52 "oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR",
53 "oauth_token_secret=nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB"))));
54 OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(OAuthParameters.createDefault(
55 wireMockServer.url("/api")));
56
57 OAuthToken requestToken = client.getRequestToken(null);
58 assertEquals("entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", requestToken.getKey(), "requestToken.key");
59 assertEquals("nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB", requestToken.getSecret(), "requestToken.secret");
60 String url = client.getAuthoriseUrl(requestToken);
61 assertEquals(wireMockServer.url("/oauth/authorize?oauth_token=entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR"), url, "url");
62
63 // access token
64 wireMockServer.stubFor(get(urlEqualTo("/oauth/access_token"))
65 .willReturn(aResponse().withStatus(200).withBody(String.join("&",
66 "oauth_token=eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX",
67 "oauth_token_secret=nsBUeNor7tPh3SHr5lLaGsGDUD2PYMV1TinL4qUB"))));
68
69 OAuthToken accessToken = client.getAccessToken(null);
70 assertEquals("eGMGmosXuiChRntxUGuwRKV6KyVDF0OWScdGhbqX", accessToken.getKey(), "accessToken.key");
71 assertEquals("nsBUeNor7tPh3SHr5lLaGsGDUD2PYMV1TinL4qUB", accessToken.getSecret(), "accessToken.secret");
72 }
73
74 /**
75 * Unit test for correct cookie handling when logging in to the OSM website.
76 *
77 * https://josm.openstreetmap.de/ticket/12584
78 * @throws Exception if any error occurs
79 */
80 @Test
81 void testCookieHandlingMock() throws Exception {
82 wireMockServer.stubFor(get(urlEqualTo("/login?cookie_test=true"))
83 .willReturn(aResponse()
84 .withStatus(200)
85 .withHeader("Set-Cookie", "_osm_session=7fe8e2ea36c6b803cb902301b28e0a; path=/; HttpOnly; SameSite=Lax")
86 .withBody("<input type=\"hidden\" " +
87 "name=\"authenticity_token\" " +
88 "value=\"fzp6CWJhp6Vns09re3s2Tw==\" />")));
89 final OAuthParameters parameters = OAuthParameters.createDefault(wireMockServer.url("/api"));
90 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters);
91 final OsmOAuthAuthorizationClient.SessionId sessionId = client.fetchOsmWebsiteSessionId();
92 assertNotNull(sessionId);
93 assertEquals("7fe8e2ea36c6b803cb902301b28e0a", sessionId.id, "sessionId.id");
94 assertEquals("fzp6CWJhp6Vns09re3s2Tw==", sessionId.token, "sessionId.token");
95 assertNull(sessionId.userName, "sessionId.userName");
96 }
97
98 /**
99 * Unit test for correct cookie handling when logging in to the OSM website.
100 *
101 * https://josm.openstreetmap.de/ticket/12584
102 * @throws Exception if any error occurs
103 */
104 @Test
105 void testCookieHandlingCookieManager() throws Exception {
106 // emulate Java Web Start behaviour
107 // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html
108 final OAuthParameters parameters = OAuthParameters.createDefault();
109 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters);
110 final CookieManager cm = new CookieManager();
111 cm.put(new URI(parameters.getOsmLoginUrl()),
112 Collections.singletonMap("Cookie", Collections.singletonList("_osm_session=" + String.valueOf(Math.PI).substring(2))));
113 CookieHandler.setDefault(cm);
114 assertNotNull(client.fetchOsmWebsiteSessionId());
115 }
116}
Note: See TracBrowser for help on using the repository browser.