1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.oauth;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertNotNull;
|
---|
5 |
|
---|
6 | import java.net.CookieHandler;
|
---|
7 | import java.net.CookieManager;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.URI;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.util.Collections;
|
---|
12 |
|
---|
13 | import org.junit.BeforeClass;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.JOSMFixture;
|
---|
16 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
17 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
18 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Unit tests of {@link OsmOAuthAuthorizationClient} class.
|
---|
22 | */
|
---|
23 | public 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 | }
|
---|