Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extensions/modules/persistentlogin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<include>src/test/resources-filtered/conf.xml</include>
<include>src/test/resources/log4j2.xml</include>
<include>src/test/resources/standalone-webapp/WEB-INF/web.xml</include>
<include>src/test/java/org/exist/xquery/modules/persistentlogin/LoginModuleTest.java</include>
</includes>
</licenseSet>

Expand All @@ -129,6 +130,7 @@
<exclude>src/test/resources-filtered/conf.xml</exclude>
<exclude>src/test/resources/log4j2.xml</exclude>
<exclude>src/test/resources/standalone-webapp/WEB-INF/web.xml</exclude>
<exclude>src/test/java/org/exist/xquery/modules/persistentlogin/LoginModuleTest.java</exclude>
</excludes>
</licenseSet>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
/*
* Elemental
* Copyright (C) 2024, Evolved Binary Ltd
*
* [email protected]
* https://www.evolvedbinary.com | https://www.elemental.xyz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
* The original license header is included below.
*
* =====================================================================
*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
Expand Down Expand Up @@ -34,42 +58,53 @@
import org.exist.xmldb.EXistResource;
import org.exist.xmldb.UserManagementService;
import org.exist.xmldb.XmldbURI;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.BinaryResource;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Arrays;

import static org.apache.http.HttpStatus.SC_OK;
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class LoginModuleTest {

private static String XQUERY = "import module namespace login=\"http://exist-db.org/xquery/login\" " +
"at \"resource:org/exist/xquery/modules/persistentlogin/login.xql\";" +
"login:set-user('org.exist.login', (), false())," +
"sm:id()/(descendant::sm:effective,descendant::sm:real)[1]/sm:username/string()";
@Parameters
public static Iterable<? extends Object> loginDomains() {
return Arrays.asList("xyz.elemental.login", "org.exist.login");
}

@ClassRule
public static final ExistWebServer existWebServer = new ExistWebServer(true, false, true);
public static final ExistWebServer EXIST_WEB_SERVER = new ExistWebServer(true, false, true);

private final static String XQUERY_FILENAME = "test-login.xql";

private static Collection root;
private static HttpClient client;
private Collection root;
private HttpClient client;

@Parameter
public String loginDomain;

@Before
public void setup() throws XMLDBException {
final String xquery = "import module namespace login=\"http://exist-db.org/xquery/login\" " +
"at \"resource:org/exist/xquery/modules/persistentlogin/login.xql\";" +
"login:set-user('" + loginDomain + "', (), false())," +
"sm:id()/(descendant::sm:effective,descendant::sm:real)[1]/sm:username/string()";

@BeforeClass
public static void beforeClass() throws XMLDBException {
root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc" + XmldbURI.ROOT_COLLECTION, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
root = DatabaseManager.getCollection("xmldb:exist://localhost:" + EXIST_WEB_SERVER.getPort() + "/xmlrpc" + XmldbURI.ROOT_COLLECTION, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
final BinaryResource res = (BinaryResource)root.createResource(XQUERY_FILENAME, "BinaryResource");
((EXistResource) res).setMimeType("application/xquery");
res.setContent(XQUERY);
res.setContent(xquery);
root.storeResource(res);
final UserManagementService ums = (UserManagementService)root.getService("UserManagementService", "1.0");
ums.chmod(res, 0777);
Expand All @@ -78,8 +113,8 @@ public static void beforeClass() throws XMLDBException {
client = HttpClientBuilder.create().setDefaultCookieStore(store).build();
}

@AfterClass
public static void afterClass() throws XMLDBException {
@After
public void cleanup() throws XMLDBException {
final BinaryResource res = (BinaryResource)root.getResource(XQUERY_FILENAME);
root.removeResource(res);
}
Expand All @@ -99,11 +134,11 @@ public void loginAndLogout() throws IOException {
doGet("logout=true", TestUtils.GUEST_DB_USER);
}

private void doGet(@Nullable String params, String expected) throws IOException {
final HttpGet httpGet = new HttpGet("http://localhost:" + existWebServer.getPort() + "/rest" + XmldbURI.ROOT_COLLECTION + '/' + XQUERY_FILENAME +
private void doGet(@Nullable final String params, final String expected) throws IOException {
final HttpGet httpGet = new HttpGet("http://localhost:" + EXIST_WEB_SERVER.getPort() + "/rest" + XmldbURI.ROOT_COLLECTION + '/' + XQUERY_FILENAME +
(params == null ? "" : "?" + params));
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
final HttpResponse response = client.execute(httpGet);
final HttpEntity entity = response.getEntity();
final String responseBody = EntityUtils.toString(entity);
assertEquals(responseBody, SC_OK, response.getStatusLine().getStatusCode());
assertEquals(expected, responseBody);
Expand Down