Skip to content

Commit 70b67fe

Browse files
committed
[test] Added a test to the Persistent Login Module for an Elemental login domain
1 parent fb30553 commit 70b67fe

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

extensions/modules/persistentlogin/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
<include>src/test/resources-filtered/conf.xml</include>
117117
<include>src/test/resources/log4j2.xml</include>
118118
<include>src/test/resources/standalone-webapp/WEB-INF/web.xml</include>
119+
<include>src/test/java/org/exist/xquery/modules/persistentlogin/LoginModuleTest.java</include>
119120
</includes>
120121
</licenseSet>
121122

@@ -129,6 +130,7 @@
129130
<exclude>src/test/resources-filtered/conf.xml</exclude>
130131
<exclude>src/test/resources/log4j2.xml</exclude>
131132
<exclude>src/test/resources/standalone-webapp/WEB-INF/web.xml</exclude>
133+
<exclude>src/test/java/org/exist/xquery/modules/persistentlogin/LoginModuleTest.java</exclude>
132134
</excludes>
133135
</licenseSet>
134136

extensions/modules/persistentlogin/src/test/java/org/exist/xquery/modules/persistentlogin/LoginModuleTest.java

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -33,42 +57,53 @@
3357
import org.exist.xmldb.EXistResource;
3458
import org.exist.xmldb.UserManagementService;
3559
import org.exist.xmldb.XmldbURI;
36-
import org.junit.AfterClass;
37-
import org.junit.BeforeClass;
38-
import org.junit.ClassRule;
39-
import org.junit.Test;
60+
import org.junit.*;
61+
import org.junit.runner.RunWith;
62+
import org.junit.runners.Parameterized;
63+
import org.junit.runners.Parameterized.Parameter;
64+
import org.junit.runners.Parameterized.Parameters;
4065
import org.xmldb.api.DatabaseManager;
4166
import org.xmldb.api.base.Collection;
4267
import org.xmldb.api.base.XMLDBException;
4368
import org.xmldb.api.modules.BinaryResource;
4469

4570
import javax.annotation.Nullable;
4671
import java.io.IOException;
72+
import java.util.Arrays;
4773

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

77+
@RunWith(Parameterized.class)
5178
public class LoginModuleTest {
5279

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

5885
@ClassRule
59-
public static final ExistWebServer existWebServer = new ExistWebServer(true, false, true);
86+
public static final ExistWebServer EXIST_WEB_SERVER = new ExistWebServer(true, false, true);
6087

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

63-
private static Collection root;
64-
private static HttpClient client;
90+
private Collection root;
91+
private HttpClient client;
92+
93+
@Parameter
94+
public String loginDomain;
95+
96+
@Before
97+
public void setup() throws XMLDBException {
98+
final String xquery = "import module namespace login=\"http://exist-db.org/xquery/login\" " +
99+
"at \"resource:org/exist/xquery/modules/persistentlogin/login.xql\";" +
100+
"login:set-user('" + loginDomain + "', (), false())," +
101+
"sm:id()/(descendant::sm:effective,descendant::sm:real)[1]/sm:username/string()";
65102

66-
@BeforeClass
67-
public static void beforeClass() throws XMLDBException {
68-
root = DatabaseManager.getCollection("xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc" + XmldbURI.ROOT_COLLECTION, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
103+
root = DatabaseManager.getCollection("xmldb:exist://localhost:" + EXIST_WEB_SERVER.getPort() + "/xmlrpc" + XmldbURI.ROOT_COLLECTION, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
69104
final BinaryResource res = root.createResource(XQUERY_FILENAME, BinaryResource.class);
70105
((EXistResource) res).setMimeType("application/xquery");
71-
res.setContent(XQUERY);
106+
res.setContent(xquery);
72107
root.storeResource(res);
73108
final UserManagementService ums = root.getService(UserManagementService.class);
74109
ums.chmod(res, 0777);
@@ -77,8 +112,8 @@ public static void beforeClass() throws XMLDBException {
77112
client = HttpClientBuilder.create().setDefaultCookieStore(store).build();
78113
}
79114

80-
@AfterClass
81-
public static void afterClass() throws XMLDBException {
115+
@After
116+
public void cleanup() throws XMLDBException {
82117
final BinaryResource res = (BinaryResource)root.getResource(XQUERY_FILENAME);
83118
root.removeResource(res);
84119
}
@@ -98,11 +133,11 @@ public void loginAndLogout() throws IOException {
98133
doGet("logout=true", TestUtils.GUEST_DB_USER);
99134
}
100135

101-
private void doGet(@Nullable String params, String expected) throws IOException {
102-
final HttpGet httpGet = new HttpGet("http://localhost:" + existWebServer.getPort() + "/rest" + XmldbURI.ROOT_COLLECTION + '/' + XQUERY_FILENAME +
136+
private void doGet(@Nullable final String params, final String expected) throws IOException {
137+
final HttpGet httpGet = new HttpGet("http://localhost:" + EXIST_WEB_SERVER.getPort() + "/rest" + XmldbURI.ROOT_COLLECTION + '/' + XQUERY_FILENAME +
103138
(params == null ? "" : "?" + params));
104-
HttpResponse response = client.execute(httpGet);
105-
HttpEntity entity = response.getEntity();
139+
final HttpResponse response = client.execute(httpGet);
140+
final HttpEntity entity = response.getEntity();
106141
final String responseBody = EntityUtils.toString(entity);
107142
assertEquals(responseBody, SC_OK, response.getStatusLine().getStatusCode());
108143
assertEquals(expected, responseBody);

0 commit comments

Comments
 (0)