xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/UserPluginTest.java (revision 2f7dccc7cd05ce1957006b093948d5359068ae4f)
1b28a5538SAdam Hornacek /*
2b28a5538SAdam Hornacek  * CDDL HEADER START
3b28a5538SAdam Hornacek  *
4b28a5538SAdam Hornacek  * The contents of this file are subject to the terms of the
5b28a5538SAdam Hornacek  * Common Development and Distribution License (the "License").
6b28a5538SAdam Hornacek  * You may not use this file except in compliance with the License.
7b28a5538SAdam Hornacek  *
8b28a5538SAdam Hornacek  * See LICENSE.txt included in this distribution for the specific
9b28a5538SAdam Hornacek  * language governing permissions and limitations under the License.
10b28a5538SAdam Hornacek  *
11b28a5538SAdam Hornacek  * When distributing Covered Code, include this CDDL HEADER in each
12b28a5538SAdam Hornacek  * file and include the License file at LICENSE.txt.
13b28a5538SAdam Hornacek  * If applicable, add the following below this CDDL HEADER, with the
14b28a5538SAdam Hornacek  * fields enclosed by brackets "[]" replaced with your own identifying
15b28a5538SAdam Hornacek  * information: Portions Copyright [yyyy] [name of copyright owner]
16b28a5538SAdam Hornacek  *
17b28a5538SAdam Hornacek  * CDDL HEADER END
18b28a5538SAdam Hornacek  */
19b28a5538SAdam Hornacek 
20b28a5538SAdam Hornacek /*
21*2f7dccc7SAdam Hornacek  * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
22b28a5538SAdam Hornacek  */
23b28a5538SAdam Hornacek package opengrok.auth.plugin;
24b28a5538SAdam Hornacek 
25aa6abf42SAdam Hornacek import jakarta.servlet.http.HttpServletRequest;
26b28a5538SAdam Hornacek import opengrok.auth.plugin.decoders.OSSOHeaderDecoder;
27b28a5538SAdam Hornacek import opengrok.auth.plugin.entity.User;
28b28a5538SAdam Hornacek import opengrok.auth.plugin.util.DummyHttpServletRequestUser;
29*2f7dccc7SAdam Hornacek import org.junit.jupiter.api.BeforeEach;
30*2f7dccc7SAdam Hornacek import org.junit.jupiter.api.Test;
31b28a5538SAdam Hornacek import org.opengrok.indexer.configuration.Group;
32b28a5538SAdam Hornacek import org.opengrok.indexer.configuration.Project;
33b28a5538SAdam Hornacek 
34*2f7dccc7SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertEquals;
35*2f7dccc7SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertFalse;
36*2f7dccc7SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertNull;
37*2f7dccc7SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertTrue;
38*2f7dccc7SAdam Hornacek 
39b28a5538SAdam Hornacek /**
40b28a5538SAdam Hornacek  *
41b28a5538SAdam Hornacek  * @author Krystof Tulinger
42b28a5538SAdam Hornacek  */
43b28a5538SAdam Hornacek public class UserPluginTest {
44b28a5538SAdam Hornacek 
45b28a5538SAdam Hornacek     private UserPlugin plugin;
46b28a5538SAdam Hornacek 
47*2f7dccc7SAdam Hornacek     @BeforeEach
setUp()48b28a5538SAdam Hornacek     public void setUp() {
49b28a5538SAdam Hornacek         plugin = new UserPlugin(new OSSOHeaderDecoder());
50b28a5538SAdam Hornacek     }
51b28a5538SAdam Hornacek 
52b28a5538SAdam Hornacek     @Test
testNoUser()53b28a5538SAdam Hornacek     public void testNoUser() {
54*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), new Group()));
55*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), new Project()));
56*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), createGroup("some group")));
57*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), createProject("some project")));
58b28a5538SAdam Hornacek     }
59b28a5538SAdam Hornacek 
60b28a5538SAdam Hornacek     @Test
testUser()61b28a5538SAdam Hornacek     public void testUser() {
62b28a5538SAdam Hornacek         HttpServletRequest req;
63*2f7dccc7SAdam Hornacek         assertTrue(plugin.isAllowed(req = createRequest("007"), new Group()));
64*2f7dccc7SAdam Hornacek         assertEquals("007", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
65*2f7dccc7SAdam Hornacek         assertTrue(plugin.isAllowed(req = createRequest("008"), new Project()));
66*2f7dccc7SAdam Hornacek         assertEquals("008", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
67*2f7dccc7SAdam Hornacek         assertTrue(plugin.isAllowed(req = createRequest("009"), createGroup("some group")));
68*2f7dccc7SAdam Hornacek         assertEquals("009", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
69*2f7dccc7SAdam Hornacek         assertTrue(plugin.isAllowed(req = createRequest("00A"), createProject("some project")));
70*2f7dccc7SAdam Hornacek         assertEquals("00A", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
71b28a5538SAdam Hornacek     }
72b28a5538SAdam Hornacek 
73b28a5538SAdam Hornacek     @Test
testTimeoutedUser()74b28a5538SAdam Hornacek     public void testTimeoutedUser() {
75b28a5538SAdam Hornacek         HttpServletRequest req;
76*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(req = createRequest("007", true), new Group()));
77*2f7dccc7SAdam Hornacek         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
78*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(req = createRequest("008", true), new Project()));
79*2f7dccc7SAdam Hornacek         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
80*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(req = createRequest("009", true), createGroup("some group")));
81*2f7dccc7SAdam Hornacek         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
82*2f7dccc7SAdam Hornacek         assertFalse(plugin.isAllowed(req = createRequest("00A", true), createProject("some project")));
83*2f7dccc7SAdam Hornacek         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
84b28a5538SAdam Hornacek     }
85b28a5538SAdam Hornacek 
createRequest(String email)86b28a5538SAdam Hornacek     protected HttpServletRequest createRequest(String email) {
87b28a5538SAdam Hornacek         return createRequest(email, false);
88b28a5538SAdam Hornacek     }
89b28a5538SAdam Hornacek 
createRequest(String email, Boolean timeout)90b28a5538SAdam Hornacek     protected HttpServletRequest createRequest(String email, Boolean timeout) {
91b28a5538SAdam Hornacek         return new DummyHttpServletRequestUser() {
92b28a5538SAdam Hornacek             {
93b28a5538SAdam Hornacek                 setHeader("osso-user-dn", email);
94b28a5538SAdam Hornacek                 setHeader("osso-user-guid", "100");
95b28a5538SAdam Hornacek                 setHeader("osso-idle-timeout-exceeded", Boolean.toString(timeout));
96b28a5538SAdam Hornacek             }
97b28a5538SAdam Hornacek         };
98b28a5538SAdam Hornacek     }
99b28a5538SAdam Hornacek 
100b28a5538SAdam Hornacek     protected Group createGroup(String name) {
101b28a5538SAdam Hornacek         Group g = new Group();
102b28a5538SAdam Hornacek         g.setName(name);
103b28a5538SAdam Hornacek         return g;
104b28a5538SAdam Hornacek     }
105b28a5538SAdam Hornacek 
106b28a5538SAdam Hornacek     protected Project createProject(String name) {
107b28a5538SAdam Hornacek         Project g = new Project();
108b28a5538SAdam Hornacek         g.setName(name);
109b28a5538SAdam Hornacek         return g;
110b28a5538SAdam Hornacek     }
111b28a5538SAdam Hornacek }
112