blob: 0119a2134618bab1d1a929d934e4eea93a5b6623 [file] [log] [blame]
margaretha1960ea52023-02-28 11:20:15 +01001package de.ids_mannheim.korap.scenario;
2
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertNotNull;
5import static org.junit.jupiter.api.Assertions.assertTrue;
margaretha1960ea52023-02-28 11:20:15 +01006
Marc Kupietzd43a98d2023-09-22 17:11:46 +02007import org.junit.jupiter.api.Test;
margaretha1960ea52023-02-28 11:20:15 +01008import org.springframework.test.context.ContextConfiguration;
margaretha1960ea52023-02-28 11:20:15 +01009import com.fasterxml.jackson.databind.JsonNode;
margaretha1960ea52023-02-28 11:20:15 +010010import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
11import de.ids_mannheim.korap.config.Attributes;
12import de.ids_mannheim.korap.config.SpringJerseyTest;
13import de.ids_mannheim.korap.exceptions.KustvaktException;
14import de.ids_mannheim.korap.exceptions.StatusCodes;
15import de.ids_mannheim.korap.utils.JsonUtils;
margaretha96c309d2023-08-16 12:24:12 +020016import jakarta.ws.rs.core.Response;
17import jakarta.ws.rs.core.Response.Status;
margaretha1960ea52023-02-28 11:20:15 +010018
19/**
20 * <p>Test scenario for ICC (International Comparable Corpus)
21 * instance</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020022 * <p>
margaretha1960ea52023-02-28 11:20:15 +010023 * The instance requires user authentication and access to data is
24 * restricted to only logged-in users.
Marc Kupietzd43a98d2023-09-22 17:11:46 +020025 * <p>
margaretha1960ea52023-02-28 11:20:15 +010026 * This class uses <em>test-config-icc.xml</em> spring XML config
27 * defining the location of a specific kustvakt configuration file for
Marc Kupietzd43a98d2023-09-22 17:11:46 +020028 * this instance:<em>kustvakt-icc.conf</em>.
29 *
margaretha3af22a42023-04-21 15:31:58 +020030 * <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 Kupietzd43a98d2023-09-22 17:11:46 +020038 * <p>
margaretha3af22a42023-04-21 15:31:58 +020039 * Example:
Marc Kupietzd43a98d2023-09-22 17:11:46 +020040 *
margaretha1960ea52023-02-28 11:20:15 +010041 * <p>
42 * <code>
margaretha3af22a42023-04-21 15:31:58 +020043 * java -jar Kustvakt-full-0.69.3.jar --spring-config
margaretha1960ea52023-02-28 11:20:15 +010044 * test-config-icc.xml
45 * </code>
46 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020047 *
margaretha1960ea52023-02-28 11:20:15 +010048 * <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 Kupietzd43a98d2023-09-22 17:11:46 +020053 *
margaretha1960ea52023-02-28 11:20:15 +010054 * <p>For testing, the ICC configuration uses HTTP Basic
55 * Authentication and doesn't use LDAP.</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020056 *
margaretha1960ea52023-02-28 11:20:15 +010057 * <p>For production, Basic Authentication must be
58 * disabled/commented.</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020059 *
margaretha1960ea52023-02-28 11:20:15 +010060 * <pre><code>
61 * &lt;bean id="basic_auth"
margaretha3af22a42023-04-21 15:31:58 +020062 * class="de.ids_mannheim.korap.authentication.BasicAuthentication"/&gt;
Marc Kupietzd43a98d2023-09-22 17:11:46 +020063 *
margaretha1960ea52023-02-28 11:20:15 +010064 * &lt;util:list id="kustvakt_authproviders"
65 * value-type="de.ids_mannheim.korap.interfaces.AuthenticationIface"&gt;
66 * &lt;!-- &lt;ref bean="basic_auth" /&gt; --&gt;
67 * </code>
68 * </pre>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020069 *
margaretha3af22a42023-04-21 15:31:58 +020070 * <p>For production, the init-method of Initializator should be changed to init.</p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020071 *
margaretha3af22a42023-04-21 15:31:58 +020072 * <pre>
73 * <code>
74 * &lt;bean id="initializator" class="de.ids_mannheim.de.init.Initializator"
Marc Kupietzd43a98d2023-09-22 17:11:46 +020075 * init-method="init"&gt;&lt;/bean&gt;
margaretha3af22a42023-04-21 15:31:58 +020076 * </code>
77 * </pre>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020078 *
margaretha1960ea52023-02-28 11:20:15 +010079 * <h1>Kustvakt configuration file</h1>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020080 *
margaretha3af22a42023-04-21 15:31:58 +020081 * <p>
margaretha1960ea52023-02-28 11:20:15 +010082 * The configuration file: <em>kustvakt-icc.conf</em> includes the
83 * following setup:
margaretha3af22a42023-04-21 15:31:58 +020084 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020085 *
margaretha1960ea52023-02-28 11:20:15 +010086 * <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 Kupietzd43a98d2023-09-22 17:11:46 +020091 *
margaretha1960ea52023-02-28 11:20:15 +010092 * <p>
93 * <code>krill.indexDir=../wiki-index</code>
94 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +020095 *
margaretha1960ea52023-02-28 11:20:15 +010096 * <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 Kupietzd43a98d2023-09-22 17:11:46 +0200102 *
margaretha1960ea52023-02-28 11:20:15 +0100103 * <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 Kupietzd43a98d2023-09-22 17:11:46 +0200107 *
margaretha1960ea52023-02-28 11:20:15 +0100108 * <p>
109 * <code>search.resource.filters=AuthenticationFilter </code>
110 * </p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200111 *
margaretha1960ea52023-02-28 11:20:15 +0100112 * <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 Kupietzd43a98d2023-09-22 17:11:46 +0200117 *
margaretha1960ea52023-02-28 11:20:15 +0100118 * <p><code>oauth2.password.authentication=LDAP</code></p>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200119 *
margaretha1960ea52023-02-28 11:20:15 +0100120 * </ul>
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200121 *
margaretha1960ea52023-02-28 11:20:15 +0100122 * @author elma
123 * @see /src/main/resources/properties/jdbc.properties
124 */
125@ContextConfiguration("classpath:test-config-icc.xml")
126public class ICCTest extends SpringJerseyTest {
127
128 public final static String API_VERSION = "v1.0";
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200129
margaretha1960ea52023-02-28 11:20:15 +0100130 public String basicAuth;
131
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200132 public ICCTest() throws KustvaktException {
133 basicAuth = HttpAuthorizationHandler.createBasicAuthorizationHeaderValue("user", "password");
margaretha1960ea52023-02-28 11:20:15 +0100134 }
135
136 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200137 public void searchWithoutLogin() throws KustvaktException {
138 Response r = target().path(API_VERSION).path("search").queryParam("q", "[orth=das]").queryParam("ql", "poliqarp").request().get();
margaretha1960ea52023-02-28 11:20:15 +0100139 assertEquals(Status.UNAUTHORIZED.getStatusCode(), r.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100140 String entity = r.readEntity(String.class);
141 JsonNode node = JsonUtils.readTree(entity);
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200142 assertEquals(StatusCodes.AUTHORIZATION_FAILED, node.at("/errors/0/0").asInt());
margaretha1960ea52023-02-28 11:20:15 +0100143 }
144
145 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200146 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();
margaretha1960ea52023-02-28 11:20:15 +0100148 assertEquals(Status.OK.getStatusCode(), r.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100149 String entity = r.readEntity(String.class);
150 JsonNode node = JsonUtils.readTree(entity);
151 assertTrue(node.at("/matches").size() > 0);
152 }
153
154 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200155 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();
margaretha1960ea52023-02-28 11:20:15 +0100157 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100158 String entity = response.readEntity(String.class);
159 JsonNode node = JsonUtils.readTree(entity);
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200160 assertEquals(StatusCodes.AUTHORIZATION_FAILED, node.at("/errors/0/0").asInt());
margaretha1960ea52023-02-28 11:20:15 +0100161 }
162
163 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200164 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();
margaretha1960ea52023-02-28 11:20:15 +0100166 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha1960ea52023-02-28 11:20:15 +0100167 String entity = response.readEntity(String.class);
168 JsonNode node = JsonUtils.readTree(entity);
margaretha1960ea52023-02-28 11:20:15 +0100169 assertTrue(node.at("/hasSnippet").asBoolean());
170 assertNotNull(node.at("/matchID").asText());
171 assertNotNull(node.at("/snippet").asText());
172 }
173}