1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.remotecontrol.handler;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertAll;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
9 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
10 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
11 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
12 |
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | import org.junit.jupiter.api.Test;
|
---|
16 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Test class for {@link AuthorizationHandler}
|
---|
20 | */
|
---|
21 | class AuthorizationHandlerTest {
|
---|
22 | private static class TestAuthorizationConsumer implements AuthorizationHandler.AuthorizationConsumer {
|
---|
23 | boolean validated;
|
---|
24 | boolean handled;
|
---|
25 | @Override
|
---|
26 | public void validateRequest(String sender, String request, Map<String, String> args)
|
---|
27 | throws RequestHandler.RequestHandlerBadRequestException {
|
---|
28 | this.validated = true;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public AuthorizationHandler.ResponseRecord handleRequest(String sender, String request, Map<String, String> args)
|
---|
33 | throws RequestHandler.RequestHandlerErrorException, RequestHandler.RequestHandlerBadRequestException {
|
---|
34 | this.handled = true;
|
---|
35 | return null;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Test
|
---|
40 | void testValidateAndHandleRequest() {
|
---|
41 | final AuthorizationHandler handler = new AuthorizationHandler();
|
---|
42 | TestAuthorizationConsumer consumer = new TestAuthorizationConsumer();
|
---|
43 | AuthorizationHandler.addAuthorizationConsumer("test_state", consumer);
|
---|
44 | assertDoesNotThrow(() -> handler.setUrl("http://localhost:8111/oauth_authorization?code=code&state=test_state"));
|
---|
45 | assertAll(() -> assertDoesNotThrow(handler::validateRequest),
|
---|
46 | () -> assertDoesNotThrow(handler::handleRequest),
|
---|
47 | () -> assertTrue(consumer.validated),
|
---|
48 | () -> assertTrue(consumer.handled));
|
---|
49 | // The consumer should only ever be called once
|
---|
50 | consumer.validated = false;
|
---|
51 | consumer.handled = false;
|
---|
52 | assertAll(() -> assertThrows(RequestHandler.RequestHandlerBadRequestException.class, handler::validateRequest),
|
---|
53 | () -> assertThrows(NullPointerException.class, handler::handleRequest),
|
---|
54 | () -> assertFalse(consumer.validated),
|
---|
55 | () -> assertFalse(consumer.handled));
|
---|
56 | // Check to make certain that a bad state doesn't work
|
---|
57 | AuthorizationHandler.addAuthorizationConsumer("testState", consumer);
|
---|
58 | AuthorizationHandler.addAuthorizationConsumer("test_state", consumer);
|
---|
59 | assertThrows(IllegalArgumentException.class, () -> AuthorizationHandler.addAuthorizationConsumer("test_state", consumer));
|
---|
60 | assertDoesNotThrow(() -> handler.setUrl("http://localhost:8111/oauth_authorization?code=code&testState=test_state"));
|
---|
61 | assertAll(() -> assertThrows(RequestHandler.RequestHandlerBadRequestException.class, handler::validateRequest),
|
---|
62 | () -> assertThrows(NullPointerException.class, handler::handleRequest),
|
---|
63 | () -> assertFalse(consumer.validated),
|
---|
64 | () -> assertFalse(consumer.handled));
|
---|
65 | assertDoesNotThrow(() -> handler.setUrl("http://localhost:8111/oauth_authorization?code=code&state=no_state_handler"));
|
---|
66 | assertAll(() -> assertThrows(RequestHandler.RequestHandlerBadRequestException.class, handler::validateRequest),
|
---|
67 | () -> assertThrows(NullPointerException.class, handler::handleRequest),
|
---|
68 | () -> assertFalse(consumer.validated),
|
---|
69 | () -> assertFalse(consumer.handled));
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Test
|
---|
73 | void testGetPermissionMessage() {
|
---|
74 | assertEquals("Allow OAuth remote control to set credentials", new AuthorizationHandler().getPermissionMessage());
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test
|
---|
78 | void testGetPermissionPref() {
|
---|
79 | assertNull(new AuthorizationHandler().getPermissionPref());
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Test
|
---|
83 | void testGetPermissionPreference() {
|
---|
84 | final BooleanProperty property = new AuthorizationHandler().getPermissionPreference();
|
---|
85 | assertEquals("remotecontrol.permission.authorization", property.getKey());
|
---|
86 | assertFalse(property.getDefaultValue());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | void testGetMandatoryParams() {
|
---|
91 | assertArrayEquals(new String[] {"code", "state"}, new AuthorizationHandler().getMandatoryParams());
|
---|
92 | }
|
---|
93 | }
|
---|