|
| 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.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.opengrok.indexer.configuration.Group; |
| 30 | +import org.opengrok.indexer.configuration.Project; |
| 31 | +import org.opengrok.indexer.util.RandomString; |
| 32 | +import org.opengrok.indexer.web.DummyHttpServletRequest; |
| 33 | + |
| 34 | +import static org.junit.Assert.assertFalse; |
| 35 | + |
| 36 | +/** |
| 37 | + * Represents a container for tests of {@link FalsePlugin}. |
| 38 | + */ |
| 39 | +public class FalsePluginTest { |
| 40 | + |
| 41 | + private FalsePlugin plugin; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setUp() { |
| 45 | + plugin = new FalsePlugin(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void shouldNotThrowOnLoadIfNullArgument() { |
| 50 | + plugin.load(null); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldUnload() { |
| 55 | + plugin.unload(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void shouldNotAllowRandomUserForAnyProject() { |
| 60 | + DummyHttpServletRequest req = new DummyHttpServletRequest(); |
| 61 | + req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomString.generateUpper(8))); |
| 62 | + |
| 63 | + Project randomProject = new Project(RandomString.generateUpper(10)); |
| 64 | + boolean projectAllowed = plugin.isAllowed(req, randomProject); |
| 65 | + assertFalse("should not allow rando for random project 1", projectAllowed); |
| 66 | + |
| 67 | + randomProject = new Project(RandomString.generateUpper(10)); |
| 68 | + projectAllowed = plugin.isAllowed(req, randomProject); |
| 69 | + assertFalse("should not allow rando for random project 2", projectAllowed); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void shouldNotAllowRandomUserForAnyGroup() { |
| 74 | + DummyHttpServletRequest req = new DummyHttpServletRequest(); |
| 75 | + req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomString.generateUpper(8))); |
| 76 | + |
| 77 | + Group randomGroup = new Group(RandomString.generateUpper(10)); |
| 78 | + boolean projectAllowed = plugin.isAllowed(req, randomGroup); |
| 79 | + assertFalse("should not allow rando for random group 1", projectAllowed); |
| 80 | + |
| 81 | + randomGroup = new Group(RandomString.generateUpper(10)); |
| 82 | + projectAllowed = plugin.isAllowed(req, randomGroup); |
| 83 | + assertFalse("should not allow rando for random group 2", projectAllowed); |
| 84 | + } |
| 85 | +} |
0 commit comments