| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.scenario; |
| 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.assertNotNull; |
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 6 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 7 | import org.junit.jupiter.api.Test; |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 8 | import org.springframework.test.context.ContextConfiguration; |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 10 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 11 | import de.ids_mannheim.korap.config.Attributes; |
| 12 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| 13 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 14 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 15 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 16 | import jakarta.ws.rs.core.Response; |
| 17 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * <p>Test scenario for ICC (International Comparable Corpus) |
| 21 | * instance</p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 22 | * <p> |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 23 | * The instance requires user authentication and access to data is |
| 24 | * restricted to only logged-in users. |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 25 | * <p> |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 26 | * This class uses <em>test-config-icc.xml</em> spring XML config |
| 27 | * defining the location of a specific kustvakt configuration file for |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 28 | * this instance:<em>kustvakt-icc.conf</em>. |
| 29 | * |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 30 | * <p> |
| 31 | * To run a Kustvakt jar with ICC setup, the following files are needed: |
| 32 | * </p> |
| 33 | * <ul> |
| 34 | * <li>a Spring configuration file</li> |
| 35 | * <li>a Kustvakt configuration file that must be placed at the jar folder</li> |
| 36 | * <li>a JDBC properties file that must be placed at the jar folder</li> |
| 37 | * </ul> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 38 | * <p> |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 39 | * Example: |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 40 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 41 | * <p> |
| 42 | * <code> |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 43 | * java -jar Kustvakt-full-0.69.3.jar --spring-config |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 44 | * test-config-icc.xml |
| 45 | * </code> |
| 46 | * </p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 47 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 48 | * <h1>Spring configuration file</h1> |
| 49 | * <p> |
| 50 | * For ICC, collectionRewrite in the Spring XML configuration must |
| 51 | * be disabled. This has been done in <em>test-config-icc.xml</em>. |
| 52 | * </p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 53 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 54 | * <p>For testing, the ICC configuration uses HTTP Basic |
| 55 | * Authentication and doesn't use LDAP.</p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 56 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 57 | * <p>For production, Basic Authentication must be |
| 58 | * disabled/commented.</p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 59 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 60 | * <pre><code> |
| 61 | * <bean id="basic_auth" |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 62 | * class="de.ids_mannheim.korap.authentication.BasicAuthentication"/> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 63 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 64 | * <util:list id="kustvakt_authproviders" |
| 65 | * value-type="de.ids_mannheim.korap.interfaces.AuthenticationIface"> |
| 66 | * <!-- <ref bean="basic_auth" /> --> |
| 67 | * </code> |
| 68 | * </pre> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 69 | * |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 70 | * <p>For production, the init-method of Initializator should be changed to init.</p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 71 | * |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 72 | * <pre> |
| 73 | * <code> |
| 74 | * <bean id="initializator" class="de.ids_mannheim.de.init.Initializator" |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 75 | * init-method="init"></bean> |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 76 | * </code> |
| 77 | * </pre> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 78 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 79 | * <h1>Kustvakt configuration file</h1> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 80 | * |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 81 | * <p> |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 82 | * The configuration file: <em>kustvakt-icc.conf</em> includes the |
| 83 | * following setup: |
| margaretha | 3af22a4 | 2023-04-21 15:31:58 +0200 | [diff] [blame] | 84 | * </p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 85 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 86 | * <ul> |
| 87 | * <li> |
| 88 | * <em>krill.indexDir</em> should indicate the location of the index. |
| 89 | * It is set to the wiki-index for the test. |
| 90 | * </li> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 91 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 92 | * <p> |
| 93 | * <code>krill.indexDir=../wiki-index</code> |
| 94 | * </p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 95 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 96 | * <li> |
| 97 | * <em>availability.regex</em> |
| 98 | * properties should be removed or commented since the data doesn't |
| 99 | * contain availability and access to data is not determined by this |
| 100 | * field. |
| 101 | * </li> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 102 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 103 | * <li> |
| 104 | * Resource filter class names for the search and match info services |
| 105 | * should be defined by <em>search.resource.filters property</em>. For |
| 106 | * example, to restricts access with only authentication filter:</li> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 107 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 108 | * <p> |
| 109 | * <code>search.resource.filters=AuthenticationFilter </code> |
| 110 | * </p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 111 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 112 | * <li><em>oauth2.password.authentication</em> indicating the |
| 113 | * authentication method to match usernames and password. |
| 114 | * <code>TEST</code> is a dummy authentication that doesn't do any |
| 115 | * matching. For production, it must be changed to |
| 116 | * <code>LDAP</code>.</li> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 117 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 118 | * <p><code>oauth2.password.authentication=LDAP</code></p> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 119 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 120 | * </ul> |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 121 | * |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 122 | * @author elma |
| 123 | * @see /src/main/resources/properties/jdbc.properties |
| 124 | */ |
| 125 | @ContextConfiguration("classpath:test-config-icc.xml") |
| 126 | public class ICCTest extends SpringJerseyTest { |
| 127 | |
| 128 | public final static String API_VERSION = "v1.0"; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 129 | |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 130 | public String basicAuth; |
| 131 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 132 | public ICCTest() throws KustvaktException { |
| 133 | basicAuth = HttpAuthorizationHandler.createBasicAuthorizationHeaderValue("user", "password"); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 137 | public void searchWithoutLogin() throws KustvaktException { |
| 138 | Response r = target().path(API_VERSION).path("search").queryParam("q", "[orth=das]").queryParam("ql", "poliqarp").request().get(); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 139 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), r.getStatus()); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 140 | String entity = r.readEntity(String.class); |
| 141 | JsonNode node = JsonUtils.readTree(entity); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 142 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, node.at("/errors/0/0").asInt()); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 146 | public void searchWithLogin() throws KustvaktException { |
| 147 | Response r = target().path(API_VERSION).path("search").queryParam("q", "[orth=das]").queryParam("ql", "poliqarp").request().header(Attributes.AUTHORIZATION, basicAuth).get(); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 148 | assertEquals(Status.OK.getStatusCode(), r.getStatus()); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 149 | String entity = r.readEntity(String.class); |
| 150 | JsonNode node = JsonUtils.readTree(entity); |
| 151 | assertTrue(node.at("/matches").size() > 0); |
| 152 | } |
| 153 | |
| 154 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 155 | public void matchInfoWithoutLogin() throws KustvaktException { |
| 156 | Response response = target().path(API_VERSION).path("corpus").path("WDD17").path("982").path("72848").path("p2815-2816").queryParam("foundry", "*").request().get(); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 157 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 158 | String entity = response.readEntity(String.class); |
| 159 | JsonNode node = JsonUtils.readTree(entity); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 160 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, node.at("/errors/0/0").asInt()); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 164 | public void matchInfoWithLogin() throws KustvaktException { |
| 165 | Response response = target().path(API_VERSION).path("corpus").path("WDD17").path("982").path("72848").path("p2815-2816").queryParam("foundry", "*").request().header(Attributes.AUTHORIZATION, basicAuth).get(); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 166 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 167 | String entity = response.readEntity(String.class); |
| 168 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 1960ea5 | 2023-02-28 11:20:15 +0100 | [diff] [blame] | 169 | assertTrue(node.at("/hasSnippet").asBoolean()); |
| 170 | assertNotNull(node.at("/matchID").asText()); |
| 171 | assertNotNull(node.at("/snippet").asText()); |
| 172 | } |
| 173 | } |