|
| 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