xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/TruePluginTest.java (revision 6c62ede99bd45f84e663cda017732f3bcc28db30)
1b5521af9SChris Fraire /*
2b5521af9SChris Fraire  * CDDL HEADER START
3b5521af9SChris Fraire  *
4b5521af9SChris Fraire  * The contents of this file are subject to the terms of the
5b5521af9SChris Fraire  * Common Development and Distribution License (the "License").
6b5521af9SChris Fraire  * You may not use this file except in compliance with the License.
7b5521af9SChris Fraire  *
8b5521af9SChris Fraire  * See LICENSE.txt included in this distribution for the specific
9b5521af9SChris Fraire  * language governing permissions and limitations under the License.
10b5521af9SChris Fraire  *
11b5521af9SChris Fraire  * When distributing Covered Code, include this CDDL HEADER in each
12b5521af9SChris Fraire  * file and include the License file at LICENSE.txt.
13b5521af9SChris Fraire  * If applicable, add the following below this CDDL HEADER, with the
14b5521af9SChris Fraire  * fields enclosed by brackets "[]" replaced with your own identifying
15b5521af9SChris Fraire  * information: Portions Copyright [yyyy] [name of copyright owner]
16b5521af9SChris Fraire  *
17b5521af9SChris Fraire  * CDDL HEADER END
18b5521af9SChris Fraire  */
19b5521af9SChris Fraire 
20b5521af9SChris Fraire /*
21b5521af9SChris Fraire  * Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
22b5521af9SChris Fraire  */
23b5521af9SChris Fraire package opengrok.auth.plugin;
24b5521af9SChris Fraire 
25b5521af9SChris Fraire import opengrok.auth.plugin.entity.User;
26*6c62ede9SAdam Hornacek import org.apache.commons.lang3.RandomStringUtils;
272f7dccc7SAdam Hornacek import org.junit.jupiter.api.BeforeEach;
282f7dccc7SAdam Hornacek import org.junit.jupiter.api.Test;
29b5521af9SChris Fraire import org.opengrok.indexer.configuration.Group;
30b5521af9SChris Fraire import org.opengrok.indexer.configuration.Project;
31b5521af9SChris Fraire import org.opengrok.indexer.web.DummyHttpServletRequest;
32b5521af9SChris Fraire 
332f7dccc7SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertTrue;
34b5521af9SChris Fraire 
35b5521af9SChris Fraire /**
36b5521af9SChris Fraire  * Represents a container for tests of {@link TruePlugin}.
37b5521af9SChris Fraire  */
38b5521af9SChris Fraire public class TruePluginTest {
39b5521af9SChris Fraire 
40b5521af9SChris Fraire     private TruePlugin plugin;
41b5521af9SChris Fraire 
422f7dccc7SAdam Hornacek     @BeforeEach
setUp()43b5521af9SChris Fraire     public void setUp() {
44b5521af9SChris Fraire         plugin = new TruePlugin();
45b5521af9SChris Fraire     }
46b5521af9SChris Fraire 
47b5521af9SChris Fraire     @Test
shouldNotThrowOnLoadIfNullArgument()48b5521af9SChris Fraire     public void shouldNotThrowOnLoadIfNullArgument() {
49b5521af9SChris Fraire         plugin.load(null);
50b5521af9SChris Fraire     }
51b5521af9SChris Fraire 
52b5521af9SChris Fraire     @Test
shouldUnload()53b5521af9SChris Fraire     public void shouldUnload() {
54b5521af9SChris Fraire         plugin.unload();
55b5521af9SChris Fraire     }
56b5521af9SChris Fraire 
57b5521af9SChris Fraire     @Test
shouldAllowRandomUserForAnyProject()58b5521af9SChris Fraire     public void shouldAllowRandomUserForAnyProject() {
59b5521af9SChris Fraire         DummyHttpServletRequest req = new DummyHttpServletRequest();
60*6c62ede9SAdam Hornacek         req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomStringUtils.randomAlphanumeric(8)));
61b5521af9SChris Fraire 
62*6c62ede9SAdam Hornacek         Project randomProject = new Project(RandomStringUtils.randomAlphanumeric(10));
63b5521af9SChris Fraire         boolean projectAllowed = plugin.isAllowed(req, randomProject);
642f7dccc7SAdam Hornacek         assertTrue(projectAllowed, "should allow rando for random project 1");
65b5521af9SChris Fraire 
66*6c62ede9SAdam Hornacek         randomProject = new Project(RandomStringUtils.randomAlphanumeric(10));
67b5521af9SChris Fraire         projectAllowed = plugin.isAllowed(req, randomProject);
682f7dccc7SAdam Hornacek         assertTrue(projectAllowed, "should allow rando for random project 2");
69b5521af9SChris Fraire     }
70b5521af9SChris Fraire 
71b5521af9SChris Fraire     @Test
shouldAllowRandomUserForAnyGroup()72b5521af9SChris Fraire     public void shouldAllowRandomUserForAnyGroup() {
73b5521af9SChris Fraire         DummyHttpServletRequest req = new DummyHttpServletRequest();
74*6c62ede9SAdam Hornacek         req.setAttribute(UserPlugin.REQUEST_ATTR, new User(RandomStringUtils.randomAlphanumeric(8)));
75b5521af9SChris Fraire 
76*6c62ede9SAdam Hornacek         Group randomGroup = new Group(RandomStringUtils.randomAlphanumeric(10));
77b5521af9SChris Fraire         boolean projectAllowed = plugin.isAllowed(req, randomGroup);
782f7dccc7SAdam Hornacek         assertTrue(projectAllowed, "should allow rando for random group 1");
79b5521af9SChris Fraire 
80*6c62ede9SAdam Hornacek         randomGroup = new Group(RandomStringUtils.randomAlphanumeric(10));
81b5521af9SChris Fraire         projectAllowed = plugin.isAllowed(req, randomGroup);
822f7dccc7SAdam Hornacek         assertTrue(projectAllowed, "should allow rando for random group 2");
83b5521af9SChris Fraire     }
84b5521af9SChris Fraire }
85