| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | import static org.junit.jupiter.api.Assertions.fail; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 8 | |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 9 | import java.io.IOException; |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 10 | import java.util.ArrayList; |
| 11 | import java.util.List; |
| 12 | import java.util.concurrent.Callable; |
| 13 | import java.util.concurrent.ExecutionException; |
| 14 | import java.util.concurrent.ExecutorService; |
| 15 | import java.util.concurrent.Executors; |
| 16 | import java.util.concurrent.Future; |
| 17 | import java.util.concurrent.TimeUnit; |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 18 | |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 19 | import jakarta.ws.rs.ProcessingException; |
| 20 | import jakarta.ws.rs.client.Entity; |
| 21 | import jakarta.ws.rs.core.Form; |
| 22 | import jakarta.ws.rs.core.Response; |
| 23 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 24 | |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 25 | import org.apache.http.entity.ContentType; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 26 | import org.junit.jupiter.api.Test; |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 27 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 28 | import com.fasterxml.jackson.databind.JsonNode; |
| 29 | import com.google.common.net.HttpHeaders; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 30 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 31 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 32 | import de.ids_mannheim.korap.entity.InstalledPlugin; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 33 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 34 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 35 | import de.ids_mannheim.korap.oauth2.constant.OAuth2ClientType; |
| 36 | import de.ids_mannheim.korap.oauth2.constant.OAuth2Error; |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 37 | import de.ids_mannheim.korap.oauth2.dao.InstalledPluginDao; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 38 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 79f0144 | 2022-05-04 12:03:47 +0200 | [diff] [blame] | 39 | import de.ids_mannheim.korap.utils.TimeUtils; |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 40 | import de.ids_mannheim.korap.web.input.OAuth2ClientJson; |
| 41 | |
| 42 | public class OAuth2PluginTest extends OAuth2TestBase { |
| 43 | |
| 44 | private String username = "plugin-user"; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 45 | |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 46 | @Autowired |
| 47 | private InstalledPluginDao pluginDao; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 48 | |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 49 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 50 | public void testRegisterPlugin () |
| 51 | throws ProcessingException, KustvaktException { |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 52 | JsonNode source = JsonUtils.readTree("{ \"plugin\" : \"source\"}"); |
| margaretha | 79f0144 | 2022-05-04 12:03:47 +0200 | [diff] [blame] | 53 | int refreshTokenExpiry = TimeUtils.convertTimeToSeconds("90D"); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 54 | String clientName = "Plugin"; |
| 55 | OAuth2ClientJson json = new OAuth2ClientJson(); |
| 56 | json.setName(clientName); |
| 57 | json.setType(OAuth2ClientType.CONFIDENTIAL); |
| 58 | json.setDescription("This is a plugin test client."); |
| 59 | json.setSource(source); |
| margaretha | 79f0144 | 2022-05-04 12:03:47 +0200 | [diff] [blame] | 60 | json.setRefreshTokenExpiry(refreshTokenExpiry); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 61 | Response response = registerClient(username, json); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 62 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 63 | JsonNode node = JsonUtils.readTree(response.readEntity(String.class)); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 64 | String clientId = node.at("/client_id").asText(); |
| 65 | String clientSecret = node.at("/client_secret").asText(); |
| 66 | assertNotNull(clientId); |
| 67 | assertNotNull(clientSecret); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 68 | testInstallPluginNotPermitted(clientId); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 69 | testRetrievePluginInfo(clientId, refreshTokenExpiry); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 70 | node = listPlugins(false); |
| 71 | assertEquals(3, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 72 | // permitted only |
| 73 | node = listPlugins(true); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 74 | assertEquals(2, node.size()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 75 | testListUserRegisteredPlugins(username, clientId, clientName, |
| 76 | refreshTokenExpiry); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 77 | deregisterClient(username, clientId); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 80 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 81 | public void testRegisterPublicPlugin () throws KustvaktException { |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 82 | JsonNode source = JsonUtils.readTree("{ \"plugin\" : \"source\"}"); |
| 83 | String clientName = "Public Plugin"; |
| 84 | OAuth2ClientJson json = new OAuth2ClientJson(); |
| 85 | json.setName(clientName); |
| 86 | json.setType(OAuth2ClientType.PUBLIC); |
| 87 | json.setDescription("This is a public plugin."); |
| 88 | json.setSource(source); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 89 | Response response = registerClient(username, json); |
| 90 | JsonNode node = JsonUtils.readTree(response.readEntity(String.class)); |
| margaretha | 4ff862a | 2022-06-03 12:40:21 +0200 | [diff] [blame] | 91 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 92 | assertEquals(OAuth2Error.INVALID_REQUEST, node.at("/error").asText()); |
| 93 | assertFalse(node.at("/error_description").isMissingNode()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 94 | // assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 95 | // String clientId = node.at("/client_id").asText(); |
| 96 | // assertTrue(node.at("/client_secret").isMissingNode()); |
| 97 | // |
| 98 | // deregisterClient(username, clientId); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 101 | private void testRetrievePluginInfo (String clientId, |
| 102 | int refreshTokenExpiry) |
| 103 | throws ProcessingException, KustvaktException { |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 104 | JsonNode clientInfo = retrieveClientInfo(clientId, username); |
| margaretha | 79f0144 | 2022-05-04 12:03:47 +0200 | [diff] [blame] | 105 | assertEquals(clientId, clientInfo.at("/client_id").asText()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 106 | assertEquals(clientInfo.at("/client_name").asText(), "Plugin"); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 107 | assertEquals(OAuth2ClientType.CONFIDENTIAL.name(), |
| 108 | clientInfo.at("/client_type").asText()); |
| margaretha | 79f0144 | 2022-05-04 12:03:47 +0200 | [diff] [blame] | 109 | assertNotNull(clientInfo.at("/client_description").asText()); |
| margaretha | 977fabe | 2022-04-28 09:23:47 +0200 | [diff] [blame] | 110 | assertNotNull(clientInfo.at("/source").asText()); |
| 111 | assertFalse(clientInfo.at("/permitted").asBoolean()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 112 | assertEquals(username, clientInfo.at("/registered_by").asText()); |
| 113 | assertNotNull(clientInfo.at("/registration_date")); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 114 | assertEquals(refreshTokenExpiry, |
| 115 | clientInfo.at("/refresh_token_expiry").asInt()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 118 | private void testListUserRegisteredPlugins (String username, |
| 119 | String clientId, String clientName, int refreshTokenExpiry) |
| 120 | throws ProcessingException, KustvaktException { |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 121 | JsonNode node = listUserRegisteredClients(username); |
| 122 | assertEquals(1, node.size()); |
| 123 | assertEquals(clientId, node.at("/0/client_id").asText()); |
| 124 | assertEquals(clientName, node.at("/0/client_name").asText()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 125 | assertEquals(OAuth2ClientType.CONFIDENTIAL.name(), |
| 126 | node.at("/0/client_type").asText()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 127 | assertFalse(node.at("/0/permitted").asBoolean()); |
| 128 | assertFalse(node.at("/0/registration_date").isMissingNode()); |
| 129 | assertFalse(node.at("/0/source").isMissingNode()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 130 | assertEquals(refreshTokenExpiry, |
| 131 | node.at("/0/refresh_token_expiry").asInt()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 135 | public void testListPluginsUnauthorizedPublic () |
| 136 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 137 | Form form = new Form(); |
| 138 | form.param("super_client_id", publicClientId); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 139 | testListPluginsClientUnauthorized(form); |
| 140 | } |
| 141 | |
| 142 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 143 | public void testListPluginsUnauthorizedConfidential () |
| 144 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 145 | Form form = new Form(); |
| 146 | form.param("super_client_id", confidentialClientId2); |
| 147 | form.param("super_client_secret", clientSecret); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 148 | testListPluginsClientUnauthorized(form); |
| 149 | } |
| 150 | |
| 151 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 152 | public void testListPluginsMissingClientSecret () |
| 153 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 154 | Form form = new Form(); |
| 155 | form.param("super_client_id", confidentialClientId); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 156 | Response response = target().path(API_VERSION).path("plugins").request() |
| 157 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 158 | .createBasicAuthorizationHeaderValue(username, "pass")) |
| 159 | .header(HttpHeaders.CONTENT_TYPE, |
| 160 | ContentType.APPLICATION_FORM_URLENCODED) |
| 161 | .post(Entity.form(form)); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 162 | String entity = response.readEntity(String.class); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 163 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 164 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 165 | assertEquals(OAuth2Error.INVALID_REQUEST, node.at("/error").asText()); |
| 166 | assertFalse(node.at("/error_description").isMissingNode()); |
| 167 | } |
| 168 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 169 | private void testListPluginsClientUnauthorized (Form form) |
| 170 | throws ProcessingException, KustvaktException { |
| 171 | Response response = target().path(API_VERSION).path("plugins").request() |
| 172 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 173 | .createBasicAuthorizationHeaderValue(username, "pass")) |
| 174 | .header(HttpHeaders.CONTENT_TYPE, |
| 175 | ContentType.APPLICATION_FORM_URLENCODED) |
| 176 | .post(Entity.form(form)); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 177 | String entity = response.readEntity(String.class); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 178 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 179 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 180 | assertEquals(OAuth2Error.UNAUTHORIZED_CLIENT, |
| 181 | node.at("/error").asText()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 182 | assertFalse(node.at("/error_description").isMissingNode()); |
| 183 | } |
| 184 | |
| 185 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 186 | public void testListPluginsUserUnauthorized () |
| 187 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 188 | Form form = getSuperClientForm(); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 189 | Response response = target().path(API_VERSION).path("plugins").request() |
| 190 | .header(Attributes.AUTHORIZATION, "Bearer blahblah") |
| 191 | .header(HttpHeaders.CONTENT_TYPE, |
| 192 | ContentType.APPLICATION_FORM_URLENCODED) |
| 193 | .post(Entity.form(form)); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 194 | String entity = response.readEntity(String.class); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 195 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 196 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 197 | assertEquals(StatusCodes.INVALID_ACCESS_TOKEN, |
| 198 | node.at("/errors/0/0").asInt()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 201 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 202 | public void testListPluginsConcurrent () throws InterruptedException { |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 203 | ExecutorService executorService = Executors.newFixedThreadPool(3); |
| 204 | List<Future<Void>> futures = new ArrayList<>(); |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 205 | for (int i = 0; i < 3; i++) { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 206 | futures.add(executorService |
| 207 | .submit(new PluginListCallable("Thread " + (i + 1)))); |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 208 | } |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 209 | executorService.shutdown(); |
| 210 | executorService.awaitTermination(2, TimeUnit.SECONDS); |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 211 | for (Future<Void> future : futures) { |
| 212 | try { |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 213 | // This will re-throw any exceptions |
| 214 | future.get(); |
| 215 | // that occurred in threads |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 216 | } |
| 217 | catch (ExecutionException e) { |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 218 | fail("Test failed: " + e.getCause().getMessage()); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | class PluginListCallable implements Callable<Void> { |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 224 | |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 225 | private final String name; |
| 226 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 227 | public PluginListCallable (String name) { |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 228 | this.name = name; |
| 229 | } |
| 230 | |
| 231 | @Override |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 232 | public Void call () { |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 233 | Form form = getSuperClientForm(); |
| 234 | try { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 235 | Response response = target().path(API_VERSION).path("plugins") |
| 236 | .request() |
| 237 | .header(Attributes.AUTHORIZATION, |
| 238 | HttpAuthorizationHandler |
| 239 | .createBasicAuthorizationHeaderValue( |
| 240 | username, "pass")) |
| 241 | .header(HttpHeaders.CONTENT_TYPE, |
| 242 | ContentType.APPLICATION_FORM_URLENCODED) |
| 243 | .post(Entity.form(form)); |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 244 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 245 | String entity = response.readEntity(String.class); |
| 246 | JsonNode node = JsonUtils.readTree(entity); |
| 247 | assertEquals(2, node.size()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 248 | } |
| 249 | catch (KustvaktException e) { |
| margaretha | 8fe51e9 | 2023-09-20 11:22:20 +0200 | [diff] [blame] | 250 | e.printStackTrace(); |
| 251 | throw new RuntimeException(name, e); |
| 252 | } |
| 253 | return null; |
| 254 | } |
| 255 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 256 | |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 257 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 258 | public void testListAllPlugins () |
| 259 | throws ProcessingException, KustvaktException { |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 260 | JsonNode node = listPlugins(false); |
| 261 | assertEquals(2, node.size()); |
| 262 | assertFalse(node.at("/0/client_id").isMissingNode()); |
| 263 | assertFalse(node.at("/0/client_name").isMissingNode()); |
| 264 | assertFalse(node.at("/0/client_description").isMissingNode()); |
| 265 | assertFalse(node.at("/0/client_type").isMissingNode()); |
| 266 | assertFalse(node.at("/0/permitted").isMissingNode()); |
| 267 | assertFalse(node.at("/0/registration_date").isMissingNode()); |
| 268 | assertFalse(node.at("/0/source").isMissingNode()); |
| margaretha | 977fabe | 2022-04-28 09:23:47 +0200 | [diff] [blame] | 269 | assertFalse(node.at("/0/refresh_token_expiry").isMissingNode()); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 270 | // assertTrue(node.at("/1/refresh_token_expiry").isMissingNode()); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 273 | private JsonNode listPlugins (boolean permitted_only) |
| 274 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 275 | Form form = getSuperClientForm(); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 276 | if (permitted_only) { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 277 | form.param("permitted_only", Boolean.toString(permitted_only)); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 278 | } |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 279 | Response response = target().path(API_VERSION).path("plugins").request() |
| 280 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 281 | .createBasicAuthorizationHeaderValue(username, "pass")) |
| 282 | .header(HttpHeaders.CONTENT_TYPE, |
| 283 | ContentType.APPLICATION_FORM_URLENCODED) |
| 284 | .post(Entity.form(form)); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 285 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 286 | String entity = response.readEntity(String.class); |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 287 | return JsonUtils.readTree(entity); |
| 288 | } |
| 289 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 290 | private void testInstallConfidentialPlugin (String superClientId, |
| 291 | String clientId, String username) |
| 292 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 293 | Form form = getSuperClientForm(); |
| 294 | form.param("client_id", clientId); |
| 295 | Response response = installPlugin(form); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 296 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 297 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 298 | assertEquals(clientId, node.at("/client_id").asText()); |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 299 | assertEquals(superClientId, node.at("/super_client_id").asText()); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 300 | assertFalse(node.at("/name").isMissingNode()); |
| 301 | assertFalse(node.at("/description").isMissingNode()); |
| 302 | assertFalse(node.at("/url").isMissingNode()); |
| 303 | assertFalse(node.at("/installed_date").isMissingNode()); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 304 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 305 | testRetrieveInstalledPlugin(superClientId, clientId, username); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 306 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 307 | |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 308 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 309 | public void testInstallPublicPlugin () |
| 310 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 311 | Form form = getSuperClientForm(); |
| 312 | form.param("client_id", publicClientId2); |
| 313 | Response response = installPlugin(form); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 314 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 315 | JsonNode node = JsonUtils.readTree(entity); |
| 316 | assertEquals(publicClientId2, node.at("/client_id").asText()); |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 317 | assertEquals(superClientId, node.at("/super_client_id").asText()); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 318 | assertFalse(node.at("/name").isMissingNode()); |
| 319 | assertFalse(node.at("/description").isMissingNode()); |
| 320 | assertFalse(node.at("/url").isMissingNode()); |
| 321 | assertFalse(node.at("/installed_date").isMissingNode()); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 322 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 323 | testInstallPluginRedundant(form); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 324 | testRetrieveInstalledPlugin(superClientId, publicClientId2, username); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 325 | response = uninstallPlugin(publicClientId2, username); |
| 326 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 327 | node = retrieveUserInstalledPlugin(getSuperClientForm()); |
| 328 | assertTrue(node.isEmpty()); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 329 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 330 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 331 | private void testInstallPluginRedundant (Form form) |
| 332 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 333 | Response response = installPlugin(form); |
| 334 | String entity = response.readEntity(String.class); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 335 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 336 | assertEquals(StatusCodes.PLUGIN_HAS_BEEN_INSTALLED, |
| 337 | node.at("/errors/0/0").asInt()); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 338 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 341 | private void testInstallPluginNotPermitted (String clientId) |
| 342 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 343 | Form form = getSuperClientForm(); |
| 344 | form.param("client_id", clientId); |
| 345 | Response response = installPlugin(form); |
| 346 | String entity = response.readEntity(String.class); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 347 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 348 | assertEquals(StatusCodes.PLUGIN_NOT_PERMITTED, |
| 349 | node.at("/errors/0/0").asInt()); |
| margaretha | d552732 | 2022-05-03 07:52:54 +0200 | [diff] [blame] | 350 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 351 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 352 | |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 353 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 354 | public void testInstallPluginMissingClientId () |
| 355 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 356 | Form form = getSuperClientForm(); |
| 357 | Response response = installPlugin(form); |
| 358 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 359 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 360 | assertEquals(StatusCodes.INVALID_ARGUMENT, |
| 361 | node.at("/errors/0/0").asInt()); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 362 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 363 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 364 | |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 365 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 366 | public void testInstallPluginInvalidClientId () |
| 367 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 368 | Form form = getSuperClientForm(); |
| 369 | form.param("client_id", "unknown"); |
| 370 | Response response = installPlugin(form); |
| 371 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 372 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 373 | assertEquals(node.at("/error_description").asText(), |
| 374 | "Unknown client: unknown"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 375 | assertEquals(node.at("/error").asText(), "invalid_client"); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 376 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 377 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 378 | |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 379 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 380 | public void testInstallPluginMissingSuperClientSecret () |
| 381 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 382 | Form form = new Form(); |
| 383 | form.param("super_client_id", superClientId); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 384 | Response response = installPlugin(form); |
| 385 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 386 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 387 | assertEquals(node.at("/error_description").asText(), |
| 388 | "Missing parameter: super_client_secret"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 389 | assertEquals(node.at("/error").asText(), "invalid_request"); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 390 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 391 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 392 | |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 393 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 394 | public void testInstallPluginMissingSuperClientId () |
| 395 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 396 | Form form = new Form(); |
| 397 | Response response = installPlugin(form); |
| 398 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 399 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 400 | assertEquals(node.at("/error_description").asText(), |
| 401 | "Missing parameter: super_client_id"); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 402 | assertEquals(node.at("/error").asText(), "invalid_request"); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 403 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 404 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 405 | |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 406 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 407 | public void testInstallPluginUnauthorizedClient () |
| 408 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 409 | Form form = new Form(); |
| 410 | form.param("super_client_id", confidentialClientId); |
| 411 | form.param("super_client_secret", clientSecret); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 412 | Response response = installPlugin(form); |
| 413 | String entity = response.readEntity(String.class); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 414 | JsonNode node = JsonUtils.readTree(entity); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 415 | assertEquals(node.at("/error").asText(), "unauthorized_client"); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 416 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 417 | } |
| 418 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 419 | private Response installPlugin (Form form) |
| 420 | throws ProcessingException, KustvaktException { |
| 421 | return target().path(API_VERSION).path("plugins").path("install") |
| 422 | .request() |
| 423 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 424 | .createBasicAuthorizationHeaderValue(username, "pass")) |
| 425 | .header(HttpHeaders.CONTENT_TYPE, |
| 426 | ContentType.APPLICATION_FORM_URLENCODED) |
| 427 | .post(Entity.form(form)); |
| margaretha | 7da2390 | 2022-05-02 08:38:45 +0200 | [diff] [blame] | 428 | } |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 429 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 430 | private Response uninstallPlugin (String clientId, String username) |
| 431 | throws ProcessingException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 432 | Form form = getSuperClientForm(); |
| 433 | form.param("client_id", clientId); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 434 | return target().path(API_VERSION).path("plugins").path("uninstall") |
| 435 | .request() |
| 436 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 437 | .createBasicAuthorizationHeaderValue(username, "pass")) |
| 438 | .header(HttpHeaders.CONTENT_TYPE, |
| 439 | ContentType.APPLICATION_FORM_URLENCODED) |
| 440 | .post(Entity.form(form)); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 443 | private void testRetrieveInstalledPlugin (String superClientId, |
| 444 | String clientId, String installedBy) throws KustvaktException { |
| 445 | InstalledPlugin plugin = pluginDao |
| 446 | .retrieveInstalledPlugin(superClientId, clientId, installedBy); |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 447 | assertEquals(clientId, plugin.getClient().getId()); |
| 448 | assertEquals(superClientId, plugin.getSuperClient().getId()); |
| 449 | assertEquals(installedBy, plugin.getInstalledBy()); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 450 | assertTrue(plugin.getId() > 0); |
| 451 | assertTrue(plugin.getInstalledDate() != null); |
| 452 | } |
| 453 | |
| 454 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 455 | public void testListUserInstalledPlugins () |
| 456 | throws ProcessingException, KustvaktException, IOException { |
| 457 | testInstallConfidentialPlugin(superClientId, confidentialClientId, |
| 458 | username); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 459 | JsonNode node = testRequestAccessToken(confidentialClientId); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 460 | String accessToken = node.at("/access_token").asText(); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 461 | String refreshToken = node.at("/refresh_token").asText(); |
| 462 | testSearchWithOAuth2Token(accessToken); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 463 | testInstallConfidentialPlugin(superClientId, confidentialClientId2, |
| 464 | username); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 465 | node = retrieveUserInstalledPlugin(getSuperClientForm()); |
| 466 | assertEquals(2, node.size()); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 467 | Response response = uninstallPlugin(confidentialClientId, username); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 468 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 469 | node = retrieveUserInstalledPlugin(getSuperClientForm()); |
| 470 | assertEquals(1, node.size()); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 471 | testRequestTokenWithRevokedRefreshToken(confidentialClientId, |
| 472 | clientSecret, refreshToken); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 473 | testSearchWithRevokedAccessToken(accessToken); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 474 | response = uninstallPlugin(confidentialClientId2, username); |
| 475 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 476 | node = retrieveUserInstalledPlugin(getSuperClientForm()); |
| 477 | assertEquals(0, node.size()); |
| margaretha | d7cac1d | 2023-02-10 10:51:45 +0100 | [diff] [blame] | 478 | testReinstallUninstalledPlugin(); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 479 | testUninstallNotInstalledPlugin(); |
| 480 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 481 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 482 | private void testReinstallUninstalledPlugin () |
| 483 | throws ProcessingException, KustvaktException { |
| 484 | testInstallConfidentialPlugin(superClientId, confidentialClientId2, |
| 485 | username); |
| margaretha | d7cac1d | 2023-02-10 10:51:45 +0100 | [diff] [blame] | 486 | JsonNode node = retrieveUserInstalledPlugin(getSuperClientForm()); |
| 487 | assertEquals(1, node.size()); |
| margaretha | d7cac1d | 2023-02-10 10:51:45 +0100 | [diff] [blame] | 488 | Response response = uninstallPlugin(confidentialClientId2, username); |
| 489 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | d7cac1d | 2023-02-10 10:51:45 +0100 | [diff] [blame] | 490 | node = retrieveUserInstalledPlugin(getSuperClientForm()); |
| 491 | assertEquals(0, node.size()); |
| 492 | } |
| 493 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 494 | private JsonNode testRequestAccessToken (String clientId) |
| 495 | throws KustvaktException { |
| 496 | String userAuthHeader = HttpAuthorizationHandler |
| 497 | .createBasicAuthorizationHeaderValue(username, "password"); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 498 | String code = requestAuthorizationCode(clientId, userAuthHeader); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 499 | Response response = requestTokenWithAuthorizationCodeAndForm(clientId, |
| 500 | clientSecret, code); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 501 | JsonNode node = JsonUtils.readTree(response.readEntity(String.class)); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 502 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 503 | return node; |
| 504 | } |
| 505 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 506 | private void testUninstallNotInstalledPlugin () |
| 507 | throws ProcessingException, KustvaktException { |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 508 | Response response = uninstallPlugin(confidentialClientId2, username); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 509 | assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 510 | JsonNode node = JsonUtils.readTree(response.readEntity(String.class)); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 511 | assertEquals(StatusCodes.NO_RESOURCE_FOUND, |
| 512 | node.at("/errors/0/0").asInt()); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 515 | private JsonNode retrieveUserInstalledPlugin (Form form) |
| 516 | throws ProcessingException, KustvaktException { |
| 517 | Response response = target().path(API_VERSION).path("plugins") |
| 518 | .path("installed").request() |
| 519 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 520 | .createBasicAuthorizationHeaderValue(username, "pass")) |
| 521 | .header(HttpHeaders.CONTENT_TYPE, |
| 522 | ContentType.APPLICATION_FORM_URLENCODED) |
| 523 | .post(Entity.form(form)); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 524 | String entity = response.readEntity(String.class); |
| margaretha | d484156 | 2022-06-01 12:24:47 +0200 | [diff] [blame] | 525 | return JsonUtils.readTree(entity); |
| margaretha | 68ccf1e | 2022-05-04 09:59:54 +0200 | [diff] [blame] | 526 | } |
| margaretha | e20a280 | 2022-04-21 12:37:38 +0200 | [diff] [blame] | 527 | } |