1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.testutils.annotations;
|
---|
3 |
|
---|
4 |
|
---|
5 | import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
---|
6 |
|
---|
7 | import java.lang.annotation.ElementType;
|
---|
8 | import java.lang.annotation.Retention;
|
---|
9 | import java.lang.annotation.RetentionPolicy;
|
---|
10 | import java.lang.annotation.Target;
|
---|
11 | import java.net.Authenticator;
|
---|
12 | import java.net.PasswordAuthentication;
|
---|
13 |
|
---|
14 | import org.junit.jupiter.api.extension.AfterEachCallback;
|
---|
15 | import org.junit.jupiter.api.extension.BeforeAllCallback;
|
---|
16 | import org.junit.jupiter.api.extension.BeforeEachCallback;
|
---|
17 | import org.junit.jupiter.api.extension.ExtendWith;
|
---|
18 | import org.junit.jupiter.api.extension.ExtensionContext;
|
---|
19 | import org.openstreetmap.josm.TestUtils;
|
---|
20 | import org.openstreetmap.josm.data.UserIdentityManager;
|
---|
21 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
22 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
23 | import org.openstreetmap.josm.tools.Utils;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Used for tests that require a test user to be defined via -Dosm.username and -Dosm.password.
|
---|
27 | * This uses the {@link OsmApi.APIType#DEV} server.
|
---|
28 | */
|
---|
29 | @BasicPreferences
|
---|
30 | @OsmApi(OsmApi.APIType.DEV)
|
---|
31 | @ExtendWith(TestUser.TestUserExtension.class)
|
---|
32 | @Retention(RetentionPolicy.RUNTIME)
|
---|
33 | @Target({ElementType.TYPE, ElementType.METHOD})
|
---|
34 | public @interface TestUser {
|
---|
35 | /**
|
---|
36 | * Initialize a user for tests
|
---|
37 | */
|
---|
38 | class TestUserExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback {
|
---|
39 | @Override
|
---|
40 | public void afterEach(ExtensionContext context) throws Exception {
|
---|
41 | UserIdentityManager.getInstance().setAnonymous();
|
---|
42 | CredentialsManager.getInstance().purgeCredentialsCache(Authenticator.RequestorType.SERVER);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void beforeAll(ExtensionContext context) throws Exception {
|
---|
47 | this.beforeEach(context);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void beforeEach(ExtensionContext context) throws Exception {
|
---|
52 | 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");
|
---|
59 |
|
---|
60 | // don't use atomic upload, the test API server can't cope with large diff uploads
|
---|
61 | 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()));
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|