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

Last change on this file since 9934 was 9934, checked in by simon04, 8 years ago

see #12584 - Add failing unit test

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.junit.Assert.assertNotNull;
5
6import java.net.CookieHandler;
7import java.net.CookieManager;
8import java.net.MalformedURLException;
9import java.net.URI;
10import java.net.URL;
11import java.util.Collections;
12
13import org.junit.BeforeClass;
14import org.junit.Test;
15import org.openstreetmap.josm.JOSMFixture;
16import org.openstreetmap.josm.data.oauth.OAuthParameters;
17import org.openstreetmap.josm.data.oauth.OAuthToken;
18import org.openstreetmap.josm.io.OsmTransferCanceledException;
19
20/**
21 * Unit tests of {@link OsmOAuthAuthorizationClient} class.
22 */
23public class OsmOAuthAuthorizationClientTest {
24
25 /**
26 * Setup tests
27 */
28 @BeforeClass
29 public static void setUpBeforeClass() {
30 JOSMFixture.createUnitTestFixture().init();
31 }
32
33 /**
34 * Unit test of {@link OsmOAuthAuthorizationClient}.
35 * @throws OsmOAuthAuthorizationException if OAuth authorization error occurs
36 * @throws OsmTransferCanceledException if OSM transfer error occurs
37 * @throws MalformedURLException in case of invalid URL
38 */
39 @Test
40 public void testOsmOAuthAuthorizationClient() throws OsmTransferCanceledException, OsmOAuthAuthorizationException, MalformedURLException {
41 OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(OAuthParameters.createDefault());
42 OAuthToken requestToken = client.getRequestToken(null);
43 assertNotNull(requestToken);
44 String url = client.getAuthoriseUrl(requestToken);
45 assertNotNull(url);
46 System.out.println(new URL(url));
47 //OAuthToken accessToken = client.getAccessToken(null);
48 //assertNotNull(accessToken);
49 }
50
51 /**
52 * Unit test for correct cookie handling when logging in to the OSM website.
53 *
54 * https://josm.openstreetmap.de/ticket/12584
55 * @throws Exception if any error occurs
56 */
57 @Test
58 public void testCookieHandling() throws Exception {
59 final OAuthParameters parameters = OAuthParameters.createDefault();
60 final OsmOAuthAuthorizationClient client = new OsmOAuthAuthorizationClient(parameters);
61 assertNotNull(client.fetchOsmWebsiteSessionId());
62
63 // emulate Java Web Start behaviour
64 // see https://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html
65 final CookieManager cm = new CookieManager();
66 cm.put(new URI(parameters.getOsmLoginUrl()),
67 Collections.singletonMap("Cookie", Collections.singletonList("_osm_session=" + String.valueOf(Math.PI).substring(2))));
68 CookieHandler.setDefault(cm);
69 assertNotNull(client.fetchOsmWebsiteSessionId());
70 }
71}
Note: See TracBrowser for help on using the repository browser.