Skip to content

Commit 895b87d

Browse files
idodeclareVladimir Kotal
authored andcommitted
Add tests of UserWhiteListPlugin
1 parent 8f93647 commit 895b87d

File tree

4 files changed

+212
-10
lines changed

4 files changed

+212
-10
lines changed

opengrok-web/pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1919
CDDL HEADER END
2020
2121
Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22-
Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
22+
Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2323
2424
-->
2525
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
@@ -49,6 +49,13 @@ Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
4949
<artifactId>opengrok</artifactId>
5050
<version>${project.version}</version>
5151
</dependency>
52+
<dependency>
53+
<groupId>${project.groupId}</groupId>
54+
<artifactId>opengrok</artifactId>
55+
<version>${project.version}</version>
56+
<type>test-jar</type>
57+
<scope>test</scope>
58+
</dependency>
5259
<dependency>
5360
<groupId>${project.groupId}</groupId>
5461
<artifactId>suggester</artifactId>
@@ -108,13 +115,6 @@ Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
108115
<artifactId>activation</artifactId>
109116
<version>1.1</version>
110117
</dependency>
111-
<dependency>
112-
<groupId>${project.groupId}</groupId>
113-
<artifactId>opengrok</artifactId>
114-
<version>${project.version}</version>
115-
<type>test-jar</type>
116-
<scope>test</scope>
117-
</dependency>
118118
<dependency>
119119
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
120120
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>

plugins/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1919
CDDL HEADER END
2020
2121
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
22+
Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2223
2324
-->
2425
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
@@ -42,6 +43,13 @@ Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
4243
<artifactId>opengrok</artifactId>
4344
<version>${project.version}</version>
4445
</dependency>
46+
<dependency>
47+
<groupId>${project.groupId}</groupId>
48+
<artifactId>opengrok</artifactId>
49+
<version>${project.version}</version>
50+
<type>test-jar</type>
51+
<scope>test</scope>
52+
</dependency>
4553
<dependency>
4654
<groupId>javax.servlet</groupId>
4755
<artifactId>javax.servlet-api</artifactId>

plugins/src/main/java/opengrok/auth/plugin/UserWhiteListPlugin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2223
*/
2324

2425
package opengrok.auth.plugin;
@@ -43,7 +44,7 @@ public class UserWhiteListPlugin implements IAuthorizationPlugin {
4344
private static final String className = UserWhiteListPlugin.class.getName();
4445
private static final Logger LOGGER = Logger.getLogger(className);
4546

46-
private static final String FILE_PARAM = "file";
47+
static final String FILE_PARAM = "file";
4748

4849
private final Set<String> whitelist = new TreeSet<>();
4950

@@ -52,7 +53,7 @@ public void load(Map<String, Object> parameters) {
5253
String filePath;
5354

5455
if ((filePath = (String) parameters.get(FILE_PARAM)) == null) {
55-
throw new NullPointerException("Missing parameter [" + FILE_PARAM + "] in the configuration");
56+
throw new IllegalArgumentException("Missing parameter [" + FILE_PARAM + "] in the configuration");
5657
}
5758

5859
// Load whitelist from file to memory.
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2020, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package opengrok.auth.plugin;
25+
26+
import opengrok.auth.plugin.entity.User;
27+
import org.junit.AfterClass;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import org.opengrok.indexer.configuration.Group;
32+
import org.opengrok.indexer.configuration.Project;
33+
import org.opengrok.indexer.util.RandomString;
34+
import org.opengrok.indexer.web.DummyHttpServletRequest;
35+
36+
import java.io.BufferedWriter;
37+
import java.io.File;
38+
import java.io.FileOutputStream;
39+
import java.io.OutputStreamWriter;
40+
import java.nio.charset.StandardCharsets;
41+
import java.util.HashMap;
42+
43+
import static org.junit.Assert.assertFalse;
44+
import static org.junit.Assert.assertNotNull;
45+
import static org.junit.Assert.assertTrue;
46+
import static org.junit.jupiter.api.Assertions.assertThrows;
47+
48+
/**
49+
* Represents a container for tests of {@link UserWhiteListPlugin}.
50+
*/
51+
public class UserWhiteListPluginTest {
52+
53+
private static final String OK_USER = "user1321";
54+
private static File tempWhitelist;
55+
private static HashMap<String, Object> validPluginParameters;
56+
57+
private UserWhiteListPlugin plugin;
58+
59+
@BeforeClass
60+
public static void beforeClass() throws Exception {
61+
tempWhitelist = File.createTempFile("UserWhiteListPluginTest", "txt");
62+
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
63+
new FileOutputStream(tempWhitelist), StandardCharsets.UTF_8))) {
64+
writer.write(OK_USER);
65+
// Don't bother with trailing LF.
66+
}
67+
68+
validPluginParameters = new HashMap<>();
69+
validPluginParameters.put(UserWhiteListPlugin.FILE_PARAM, tempWhitelist.getPath());
70+
}
71+
72+
@AfterClass
73+
public static void afterClass() {
74+
if (tempWhitelist != null) {
75+
//noinspection ResultOfMethodCallIgnored
76+
tempWhitelist.delete();
77+
}
78+
}
79+
80+
@Before
81+
public void setUp() {
82+
plugin = new UserWhiteListPlugin();
83+
}
84+
85+
@Test
86+
public void shouldThrowOnLoadIfNullArgument() {
87+
assertThrows(NullPointerException.class, () -> {
88+
//noinspection ConstantConditions
89+
plugin.load(null);
90+
}, "plugin.load(null)");
91+
}
92+
93+
@Test
94+
public void shouldThrowOnLoadIfUnreadableFileSpecified() {
95+
HashMap<String, Object> unreadablePluginParameters = new HashMap<>();
96+
unreadablePluginParameters.put(UserWhiteListPlugin.FILE_PARAM,
97+
RandomString.generateLower(24));
98+
99+
IllegalArgumentException caughtException = null;
100+
try {
101+
plugin.load(unreadablePluginParameters);
102+
} catch (IllegalArgumentException ex) {
103+
caughtException = ex;
104+
}
105+
106+
assertNotNull("caught IllegalArgumentException", caughtException);
107+
assertTrue("caughtException should mention 'Unable to read the file'",
108+
caughtException.getMessage().contains("Unable to read the file"));
109+
}
110+
111+
@Test
112+
public void shouldThrowOnLoadIfNoFileSpecified() {
113+
IllegalArgumentException caughtException = null;
114+
try {
115+
plugin.load(new HashMap<>());
116+
} catch (IllegalArgumentException ex) {
117+
caughtException = ex;
118+
}
119+
120+
assertNotNull("caught IllegalArgumentException", caughtException);
121+
assertTrue("caughtException should mention 'Missing parameter'",
122+
caughtException.getMessage().contains("Missing parameter"));
123+
}
124+
125+
@Test
126+
public void shouldUnload() {
127+
plugin.unload();
128+
}
129+
130+
@Test
131+
public void shouldAllowWhitelistedUserForAnyProject() {
132+
plugin.load(validPluginParameters);
133+
134+
DummyHttpServletRequest req = new DummyHttpServletRequest();
135+
req.setAttribute(UserPlugin.REQUEST_ATTR, new User(OK_USER));
136+
137+
Project randomProject = new Project(RandomString.generateUpper(10));
138+
boolean projectAllowed = plugin.isAllowed(req, randomProject);
139+
assertTrue("should allow OK_USER for random project 1", projectAllowed);
140+
141+
randomProject = new Project(RandomString.generateUpper(10));
142+
projectAllowed = plugin.isAllowed(req, randomProject);
143+
assertTrue("should allow OK_USER for random project 2", projectAllowed);
144+
}
145+
146+
@Test
147+
public void shouldNotAllowRandomUserForAnyProject() {
148+
plugin.load(validPluginParameters);
149+
150+
DummyHttpServletRequest req = new DummyHttpServletRequest();
151+
req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomString.generateUpper(8)));
152+
153+
Project randomProject = new Project(RandomString.generateUpper(10));
154+
boolean projectAllowed = plugin.isAllowed(req, randomProject);
155+
assertFalse("should not allow rando for random project 1", projectAllowed);
156+
157+
randomProject = new Project(RandomString.generateUpper(10));
158+
projectAllowed = plugin.isAllowed(req, randomProject);
159+
assertFalse("should not allow rando for random project 2", projectAllowed);
160+
}
161+
162+
@Test
163+
public void shouldAllowWhitelistedUserForAnyGroup() {
164+
plugin.load(validPluginParameters);
165+
166+
DummyHttpServletRequest req = new DummyHttpServletRequest();
167+
req.setAttribute(UserPlugin.REQUEST_ATTR, new User(OK_USER));
168+
169+
Group randomGroup = new Group(RandomString.generateUpper(10));
170+
boolean groupAllowed = plugin.isAllowed(req, randomGroup);
171+
assertTrue("should allow OK_USER for random group 1", groupAllowed);
172+
173+
randomGroup = new Group(RandomString.generateUpper(10));
174+
groupAllowed = plugin.isAllowed(req, randomGroup);
175+
assertTrue("should allow OK_USER for random group 2", groupAllowed);
176+
}
177+
178+
@Test
179+
public void shouldNotAllowRandomUserForAnyGroup() {
180+
plugin.load(validPluginParameters);
181+
182+
DummyHttpServletRequest req = new DummyHttpServletRequest();
183+
req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomString.generateUpper(8)));
184+
185+
Group randomGroup = new Group(RandomString.generateUpper(10));
186+
boolean projectAllowed = plugin.isAllowed(req, randomGroup);
187+
assertFalse("should not allow rando for random group 1", projectAllowed);
188+
189+
randomGroup = new Group(RandomString.generateUpper(10));
190+
projectAllowed = plugin.isAllowed(req, randomGroup);
191+
assertFalse("should not allow rando for random group 2", projectAllowed);
192+
}
193+
}

0 commit comments

Comments
 (0)