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