Changeset 17196 in josm for trunk


Ignore:
Timestamp:
2020-10-13T23:13:00+02:00 (4 years ago)
Author:
simon04
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java

    r16643 r17196  
    4747    private HttpClient connection;
    4848
    49     private static class SessionId {
    50         private String id;
    51         private String token;
    52         private String userName;
     49    protected static class SessionId {
     50        protected String id;
     51        protected String token;
     52        protected String userName;
    5353    }
    5454
  • trunk/test/unit/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClientTest.java

    r13203 r17196  
    22package org.openstreetmap.josm.gui.oauth;
    33
     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;
    49import static org.junit.Assert.assertNotNull;
     10import static org.junit.Assert.assertNull;
    511
    612import java.net.CookieHandler;
    713import java.net.CookieManager;
    8 import java.net.MalformedURLException;
    914import java.net.URI;
    10 import java.net.URL;
    1115import java.util.Collections;
    1216
     
    1923
    2024import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     25
     26import com.github.tomakehurst.wiremock.junit.WireMockRule;
    2127
    2228/**
     
    3339
    3440    /**
     41     * HTTP mock.
     42     */
     43    @Rule
     44    public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());
     45
     46    /**
    3547     * Unit test of {@link OsmOAuthAuthorizationClient}.
    3648     * @throws OsmOAuthAuthorizationException if OAuth authorization error occurs
    3749     * @throws OsmTransferCanceledException  if OSM transfer error occurs
    38      * @throws MalformedURLException in case of invalid URL
    3950     */
    4051    @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
    4360        OAuthToken requestToken = client.getRequestToken(null);
    44         assertNotNull(requestToken);
     61        assertEquals("requestToken.key", "entxUGuwRKV6KyVDF0OWScdGhbqXGMGmosXuiChR", requestToken.getKey());
     62        assertEquals("requestToken.secret", "nsBD2Hr5lLGDUeNoh3SnLaGsUV1TiPYM4qUr7tPB", requestToken.getSecret());
    4563        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());
    5075    }
    5176
     
    5782     */
    5883    @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
    60111        final OAuthParameters parameters = OAuthParameters.createDefault();
    61112        final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters);
    62         assertNotNull(client.fetchOsmWebsiteSessionId());
    63 
    64         // emulate Java Web Start behaviour
    65         // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html
    66113        final CookieManager cm = new CookieManager();
    67114        cm.put(new URI(parameters.getOsmLoginUrl()),
Note: See TracChangeset for help on using the changeset viewer.