Changeset 18991 in josm for trunk/test/unit/org
- Timestamp:
- 2024-02-21T21:26:18+01:00 (9 months ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 4 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/TestUtils.java
r18799 r18991 659 659 /** 660 660 * Determines if OSM DEV_API credential have been provided. Required for functional tests. 661 * @return {@code true} if {@code osm. username} and {@code osm.password} have been defined on the command line661 * @return {@code true} if {@code osm.oauth2} have been defined on the command line 662 662 */ 663 663 public static boolean areCredentialsProvided() { 664 return Utils.getSystemProperty("osm. username") != null && Utils.getSystemProperty("osm.password") != null;664 return Utils.getSystemProperty("osm.oauth2") != null; 665 665 } 666 666 -
trunk/test/unit/org/openstreetmap/josm/data/oauth/OAuth20AuthorizationTest.java
r18786 r18991 186 186 OAuth20Parameters parameters = (OAuth20Parameters) OAuthParameters.createDefault(OsmApi.getOsmApi().getBaseUrl(), OAuthVersion.OAuth20); 187 187 RemoteControl.start(); 188 authorization.authorize(new OAuth20Parameters( parameters.getClientId(), parameters.getClientSecret(),188 authorization.authorize(new OAuth20Parameters(CLIENT_ID_VALUE, parameters.getClientSecret(), 189 189 wireMockRuntimeInfo.getHttpBaseUrl() + "/oauth2", wireMockRuntimeInfo.getHttpBaseUrl() + "/api", 190 190 parameters.getRedirectUri()), consumer::set, OsmScopes.read_gpx); -
trunk/test/unit/org/openstreetmap/josm/data/oauth/OAuthParametersTest.java
r18853 r18991 24 24 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") 25 25 void testCreateDefault() { 26 OAuthParameters def = OAuthParameters.createDefault();26 IOAuthParameters def = OAuthParameters.createDefault(); 27 27 assertNotNull(def); 28 assertEquals(def, OAuthParameters.createDefault(Config.getUrls().getDefaultOsmApiUrl() ));29 OAuthParameters dev = OAuthParameters.createDefault("https://api06.dev.openstreetmap.org/api");28 assertEquals(def, OAuthParameters.createDefault(Config.getUrls().getDefaultOsmApiUrl(), OAuthVersion.OAuth20)); 29 IOAuthParameters dev = OAuthParameters.createDefault("https://api06.dev.openstreetmap.org/api", OAuthVersion.OAuth20); 30 30 assertNotNull(dev); 31 31 assertNotEquals(def, dev); 32 32 Logging.setLogLevel(Logging.LEVEL_TRACE); // enable trace for line coverage 33 assertEquals(def, OAuthParameters.createDefault("wrong_url")); 34 OAuthParameters dev2 = new OAuthParameters(dev); 35 assertEquals(dev, dev2); 33 assertEquals(def, OAuthParameters.createDefault("wrong_url", OAuthVersion.OAuth20)); 36 34 } 37 35 … … 42 40 void testEqualsContract() { 43 41 TestUtils.assumeWorkingEqualsVerifier(); 44 EqualsVerifier.forClass(OAuth Parameters.class).usingGetClass().verify();42 EqualsVerifier.forClass(OAuth20Parameters.class).usingGetClass().verify(); 45 43 } 46 44 } -
trunk/test/unit/org/openstreetmap/josm/gui/io/DownloadOpenChangesetsTaskTest.java
r18870 r18991 9 9 import java.awt.GraphicsEnvironment; 10 10 import java.net.URL; 11 import java.util.Collections; 11 import java.util.HashMap; 12 import java.util.Map; 12 13 13 14 import javax.swing.JOptionPane; 14 15 import javax.swing.JPanel; 15 16 16 import org.junit.jupiter.api.BeforeEach;17 17 import org.junit.jupiter.api.Test; 18 18 import org.openstreetmap.josm.TestUtils; 19 19 import org.openstreetmap.josm.data.UserIdentityManager; 20 20 import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; 21 import org.openstreetmap.josm.spi.preferences.Config;22 21 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 23 22 import org.openstreetmap.josm.testutils.annotations.OsmApi; … … 64 63 65 64 /** 66 * These tests were written with {@link org.openstreetmap.josm.data.oauth.OAuthVersion#OAuth10a} as the default auth method.67 */68 @BeforeEach69 void setup() {70 Config.getPref().put("osm-server.auth-method", "oauth");71 }72 73 /**74 65 * Test of {@link DownloadOpenChangesetsTask} class when anonymous. 75 66 */ … … 80 71 new WindowMocker(); 81 72 } 82 final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker(); 83 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker( 84 Collections.singletonMap( 85 "<html>Could not retrieve the list of your open changesets because<br>JOSM does not know " 73 final Map<String, Object> optionPaneMock = new HashMap<>(2); 74 optionPaneMock.put("<html>Could not retrieve the list of your open changesets because<br>JOSM does not know " 86 75 + "your identity.<br>You have either chosen to work anonymously or you are not " 87 + "entitled<br>to know the identity of the user on whose behalf you are working.</html>", JOptionPane.OK_OPTION 88 )89 );76 + "entitled<br>to know the identity of the user on whose behalf you are working.</html>", JOptionPane.OK_OPTION); 77 optionPaneMock.put("Obtain OAuth 2.0 token for authentication?", JOptionPane.NO_OPTION); 78 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(optionPaneMock); 90 79 91 80 DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel()); … … 96 85 assertNull(task.getChangesets()); 97 86 98 assertEquals( 1, jopsMocker.getInvocationLog().size());99 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get( 0);87 assertEquals(2, jopsMocker.getInvocationLog().size()); 88 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(1); 100 89 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]); 101 90 assertEquals("Missing user identity", invocationLogEntry[2]); 102 91 103 assertTrue(oaWizardMocker.called); 92 invocationLogEntry = jopsMocker.getInvocationLog().get(0); 93 assertEquals(JOptionPane.NO_OPTION, (int) invocationLogEntry[0]); 94 assertEquals("Obtain authentication to OSM servers", invocationLogEntry[2]); 104 95 } 105 96 … … 113 104 new WindowMocker(); 114 105 } 115 final OAuthWizardMocker oaWizardMocker = new OAuthWizardMocker();116 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(117 Collections.singletonMap("There are no open changesets", JOptionPane.OK_OPTION)118 );106 final Map<String, Object> optionPaneMock = new HashMap<>(2); 107 optionPaneMock.put("There are no open changesets", JOptionPane.OK_OPTION); 108 optionPaneMock.put("Obtain OAuth 2.0 token for authentication?", JOptionPane.NO_OPTION); 109 final JOptionPaneSimpleMocker jopsMocker = new JOptionPaneSimpleMocker(optionPaneMock); 119 110 120 111 DownloadOpenChangesetsTask task = new DownloadOpenChangesetsTask(new JPanel()); … … 124 115 assertNotNull(task.getChangesets()); 125 116 126 assertEquals( 1, jopsMocker.getInvocationLog().size());127 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get( 0);117 assertEquals(2, jopsMocker.getInvocationLog().size()); 118 Object[] invocationLogEntry = jopsMocker.getInvocationLog().get(1); 128 119 assertEquals(JOptionPane.OK_OPTION, (int) invocationLogEntry[0]); 129 120 assertEquals("No open changesets", invocationLogEntry[2]); 130 121 131 assertTrue(oaWizardMocker.called); 122 invocationLogEntry = jopsMocker.getInvocationLog().get(0); 123 assertEquals(JOptionPane.NO_OPTION, (int) invocationLogEntry[0]); 124 assertEquals("Obtain authentication to OSM servers", invocationLogEntry[2]); 132 125 } 133 126 } -
trunk/test/unit/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUITest.java
r18037 r18991 2 2 package org.openstreetmap.josm.gui.oauth; 3 3 4 import static org.junit.jupiter.api.Assertions.assert NotNull;4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 5 5 6 import org.junit.jupiter.params.ParameterizedTest; 7 import org.junit.jupiter.params.provider.EnumSource; 8 import org.openstreetmap.josm.data.oauth.OAuthVersion; 6 9 import org.openstreetmap.josm.gui.MainApplication; 7 10 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; 8 9 import org.junit.jupiter.api.Test;10 11 11 12 /** … … 17 18 * Unit test of {@link FullyAutomaticAuthorizationUI#FullyAutomaticAuthorizationUI}. 18 19 */ 19 @Test 20 void testFullyAutomaticAuthorizationUI() { 21 assertNotNull(new FullyAutomaticAuthorizationUI("", MainApplication.worker)); 20 @ParameterizedTest 21 @EnumSource(OAuthVersion.class) 22 void testFullyAutomaticAuthorizationUI(OAuthVersion version) { 23 assertDoesNotThrow(() -> new FullyAutomaticAuthorizationUI("", MainApplication.worker, version)); 22 24 } 23 25 } -
trunk/test/unit/org/openstreetmap/josm/gui/oauth/ManualAuthorizationUITest.java
r18037 r18991 2 2 package org.openstreetmap.josm.gui.oauth; 3 3 4 import static org.junit.jupiter.api.Assertions.assert NotNull;4 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 5 5 6 6 import org.junit.jupiter.api.Test; 7 import org.openstreetmap.josm.data.oauth.OAuthVersion; 7 8 import org.openstreetmap.josm.gui.MainApplication; 8 9 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; … … 18 19 @Test 19 20 void testManualAuthorizationUI() { 20 assert NotNull(new ManualAuthorizationUI("", MainApplication.worker));21 assertDoesNotThrow(() -> new ManualAuthorizationUI("", MainApplication.worker, OAuthVersion.OAuth20)); 21 22 } 22 23 } -
trunk/test/unit/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanelTest.java
r18765 r18991 21 21 import org.junit.jupiter.params.ParameterizedTest; 22 22 import org.junit.jupiter.params.provider.EnumSource; 23 import org.openstreetmap.josm.data.oauth.IOAuthToken;24 23 import org.openstreetmap.josm.data.oauth.OAuth20Exception; 25 24 import org.openstreetmap.josm.data.oauth.OAuth20Parameters; 26 25 import org.openstreetmap.josm.data.oauth.OAuth20Token; 27 26 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 28 import org.openstreetmap.josm.data.oauth.OAuthToken;29 27 import org.openstreetmap.josm.data.oauth.OAuthVersion; 30 28 import org.openstreetmap.josm.io.OsmApi; … … 47 45 List<Exception> exceptionList = new ArrayList<>(); 48 46 try { 49 CredentialsManager.getInstance().storeOAuthAccessToken( null);47 CredentialsManager.getInstance().storeOAuthAccessToken(OsmApi.getOsmApi().getServerUrl(), null); 50 48 } catch (CredentialsAgentException exception) { 51 49 exceptionList.add(exception); … … 61 59 62 60 @ParameterizedTest 63 @EnumSource(value = OAuthVersion.class, names = {"OAuth10a", "OAuth20"})61 @EnumSource(value = OAuthVersion.class, names = "OAuth20") 64 62 void testRemoveToken(OAuthVersion oAuthVersion) throws ReflectiveOperationException, CredentialsAgentException, OAuth20Exception { 65 63 final OAuthAuthenticationPreferencesPanel panel = new OAuthAuthenticationPreferencesPanel(oAuthVersion); … … 76 74 assertSame(pnlAlreadyAuthorised.get(panel), holder.getComponent(0), "Authentication should now be set"); 77 75 assertNotNull(getAuthorization(oAuthVersion)); 78 final JPanel buttons = (JPanel) ((JPanel) pnlAlreadyAuthorised.get(panel)).getComponent( 6);79 final JButton action = (JButton) buttons.getComponent( oAuthVersion == OAuthVersion.OAuth10a ? 2 : 1);76 final JPanel buttons = (JPanel) ((JPanel) pnlAlreadyAuthorised.get(panel)).getComponent(5); 77 final JButton action = (JButton) buttons.getComponent(2); 80 78 assertEquals(tr("Remove token"), action.getText(), "The selected button should be for removing the token"); 81 79 action.getAction().actionPerformed(null); … … 97 95 private static void addAuthorization(OAuthVersion oAuthVersion) throws CredentialsAgentException, OAuth20Exception { 98 96 switch (oAuthVersion) { 99 case OAuth10a:100 CredentialsManager.getInstance().storeOAuthAccessToken(new OAuthToken("fake_key", "fake_secret"));101 break;102 97 case OAuth20: 103 98 case OAuth21: … … 117 112 private static Object getAuthorization(OAuthVersion oAuthVersion) { 118 113 OAuthAccessTokenHolder.getInstance().clear(); 119 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().get Host(), (IOAuthToken)null);114 OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), null); 120 115 OAuthAccessTokenHolder.getInstance().init(CredentialsManager.getInstance()); 121 116 // Ensure that we are not saving authorization data 122 switch (oAuthVersion) { 123 case OAuth10a: 124 return OAuthAccessTokenHolder.getInstance().getAccessToken(); 125 case OAuth20: 126 case OAuth21: 127 return OAuthAccessTokenHolder.getInstance().getAccessToken(OsmApi.getOsmApi().getHost(), oAuthVersion); 128 default: 129 throw new AssertionError("OAuth version not understood"); 130 } 117 return OAuthAccessTokenHolder.getInstance().getAccessToken(OsmApi.getOsmApi().getServerUrl(), oAuthVersion); 131 118 } 132 119 } -
trunk/test/unit/org/openstreetmap/josm/io/auth/CredentialsAgentTest.java
r18650 r18991 19 19 import org.openstreetmap.josm.data.oauth.OAuth20Parameters; 20 20 import org.openstreetmap.josm.data.oauth.OAuth20Token; 21 import org.openstreetmap.josm.data.oauth.OAuthToken;22 21 import org.openstreetmap.josm.io.OsmApi; 23 22 import org.openstreetmap.josm.testutils.annotations.BasicPreferences; … … 76 75 } 77 76 78 @Test79 default void testLookUpAndStoreOAuth10() throws CredentialsAgentException {80 final T agent = createAgent();81 assertNull(agent.lookupOAuthAccessToken());82 final OAuthToken token = new OAuthToken("foo", "bar");83 agent.storeOAuthAccessToken(token);84 final OAuthToken actual = agent.lookupOAuthAccessToken();85 assertEquals(token, actual);86 agent.storeOAuthAccessToken(null);87 assertNull(agent.lookupOAuthAccessToken());88 }89 90 77 @ParameterizedTest 91 78 @MethodSource("getHosts") -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/BasicPreferences.java
r18870 r18991 65 65 if (AnnotationSupport.isAnnotated(context.getElement(), BasicPreferences.class)) { 66 66 this.afterAll(context); 67 if (AnnotationSupport.isAnnotated(context.getTestClass(), BasicPreferences.class)) { 68 this.beforeAll(context); 69 } 67 70 } 68 71 } -
trunk/test/unit/org/openstreetmap/josm/testutils/annotations/TestUser.java
r18974 r18991 10 10 import java.lang.annotation.Target; 11 11 import java.net.Authenticator; 12 import java.net.PasswordAuthentication;13 12 14 13 import org.junit.jupiter.api.extension.AfterEachCallback; … … 19 18 import org.openstreetmap.josm.TestUtils; 20 19 import org.openstreetmap.josm.data.UserIdentityManager; 20 import org.openstreetmap.josm.data.oauth.OAuth20Token; 21 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 22 import org.openstreetmap.josm.data.oauth.OAuthParameters; 21 23 import org.openstreetmap.josm.io.auth.CredentialsManager; 22 24 import org.openstreetmap.josm.spi.preferences.Config; … … 51 53 public void beforeEach(ExtensionContext context) throws Exception { 52 54 assumeTrue(TestUtils.areCredentialsProvided(), 53 "OSM DEV API credentials not provided. Please define them with -Dosm.username and -Dosm.password"); 54 final String username = Utils.getSystemProperty("osm.username"); 55 final String password = Utils.getSystemProperty("osm.password"); 56 assumeTrue(username != null && !username.isEmpty(), "Please add -Dosm.username for the OSM DEV API"); 57 assumeTrue(password != null && !password.isEmpty(), "Please add -Dosm.password for the OSM DEV API"); 58 Config.getPref().put("osm-server.auth-method", "basic"); 55 "OSM DEV API credentials not provided. Please define them with -Dosm.oauth2"); 56 final String oauth2 = Utils.getSystemProperty("osm.oauth2"); 57 Config.getPref().put("osm-server.auth-method", "oauth20"); 59 58 60 59 // don't use atomic upload, the test API server can't cope with large diff uploads 61 60 Config.getPref().putBoolean("osm-server.atomic-upload", false); 62 CredentialsManager.getInstance().store(Authenticator.RequestorType.SERVER, org.openstreetmap.josm.io.OsmApi.getOsmApi().getHost(), 63 new PasswordAuthentication(username, password.toCharArray())); 61 final String serverUrl = org.openstreetmap.josm.io.OsmApi.getOsmApi().getServerUrl(); 62 final OAuth20Token token = new OAuth20Token(OAuthParameters.createDefault(), 63 "{\"token_type\":\"bearer\", \"access_token\": \"" + oauth2 + "\"}"); 64 OAuthAccessTokenHolder.getInstance().setAccessToken(serverUrl, token); 65 CredentialsManager.getInstance().storeOAuthAccessToken(serverUrl, token); 66 if (!UserIdentityManager.getInstance().isFullyIdentified()) { 67 UserIdentityManager.getInstance().initFromOAuth(); 68 } 64 69 } 65 70 }
Note:
See TracChangeset
for help on using the changeset viewer.