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 <cfraire@me.com>. 22 */ 23 package opengrok.auth.plugin; 24 25 import opengrok.auth.plugin.entity.User; 26 import org.apache.commons.lang3.RandomStringUtils; 27 import org.junit.jupiter.api.BeforeEach; 28 import org.junit.jupiter.api.Test; 29 import org.opengrok.indexer.configuration.Group; 30 import org.opengrok.indexer.configuration.Project; 31 import org.opengrok.indexer.web.DummyHttpServletRequest; 32 33 import static org.junit.jupiter.api.Assertions.assertTrue; 34 35 /** 36 * Represents a container for tests of {@link TruePlugin}. 37 */ 38 public class TruePluginTest { 39 40 private TruePlugin plugin; 41 42 @BeforeEach setUp()43 public void setUp() { 44 plugin = new TruePlugin(); 45 } 46 47 @Test shouldNotThrowOnLoadIfNullArgument()48 public void shouldNotThrowOnLoadIfNullArgument() { 49 plugin.load(null); 50 } 51 52 @Test shouldUnload()53 public void shouldUnload() { 54 plugin.unload(); 55 } 56 57 @Test shouldAllowRandomUserForAnyProject()58 public void shouldAllowRandomUserForAnyProject() { 59 DummyHttpServletRequest req = new DummyHttpServletRequest(); 60 req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomStringUtils.randomAlphanumeric(8))); 61 62 Project randomProject = new Project(RandomStringUtils.randomAlphanumeric(10)); 63 boolean projectAllowed = plugin.isAllowed(req, randomProject); 64 assertTrue(projectAllowed, "should allow rando for random project 1"); 65 66 randomProject = new Project(RandomStringUtils.randomAlphanumeric(10)); 67 projectAllowed = plugin.isAllowed(req, randomProject); 68 assertTrue(projectAllowed, "should allow rando for random project 2"); 69 } 70 71 @Test shouldAllowRandomUserForAnyGroup()72 public void shouldAllowRandomUserForAnyGroup() { 73 DummyHttpServletRequest req = new DummyHttpServletRequest(); 74 req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomStringUtils.randomAlphanumeric(8))); 75 76 Group randomGroup = new Group(RandomStringUtils.randomAlphanumeric(10)); 77 boolean projectAllowed = plugin.isAllowed(req, randomGroup); 78 assertTrue(projectAllowed, "should allow rando for random group 1"); 79 80 randomGroup = new Group(RandomStringUtils.randomAlphanumeric(10)); 81 projectAllowed = plugin.isAllowed(req, randomGroup); 82 assertTrue(projectAllowed, "should allow rando for random group 2"); 83 } 84 } 85