xref: /OpenGrok/plugins/src/test/java/opengrok/auth/plugin/UserPluginTest.java (revision 2f7dccc7cd05ce1957006b093948d5359068ae4f)
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) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin;
24 
25 import jakarta.servlet.http.HttpServletRequest;
26 import opengrok.auth.plugin.decoders.OSSOHeaderDecoder;
27 import opengrok.auth.plugin.entity.User;
28 import opengrok.auth.plugin.util.DummyHttpServletRequestUser;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.opengrok.indexer.configuration.Group;
32 import org.opengrok.indexer.configuration.Project;
33 
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertFalse;
36 import static org.junit.jupiter.api.Assertions.assertNull;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38 
39 /**
40  *
41  * @author Krystof Tulinger
42  */
43 public class UserPluginTest {
44 
45     private UserPlugin plugin;
46 
47     @BeforeEach
setUp()48     public void setUp() {
49         plugin = new UserPlugin(new OSSOHeaderDecoder());
50     }
51 
52     @Test
testNoUser()53     public void testNoUser() {
54         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), new Group()));
55         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), new Project()));
56         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), createGroup("some group")));
57         assertFalse(plugin.isAllowed(new DummyHttpServletRequestUser(), createProject("some project")));
58     }
59 
60     @Test
testUser()61     public void testUser() {
62         HttpServletRequest req;
63         assertTrue(plugin.isAllowed(req = createRequest("007"), new Group()));
64         assertEquals("007", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
65         assertTrue(plugin.isAllowed(req = createRequest("008"), new Project()));
66         assertEquals("008", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
67         assertTrue(plugin.isAllowed(req = createRequest("009"), createGroup("some group")));
68         assertEquals("009", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
69         assertTrue(plugin.isAllowed(req = createRequest("00A"), createProject("some project")));
70         assertEquals("00A", ((User) req.getAttribute(UserPlugin.REQUEST_ATTR)).getUsername());
71     }
72 
73     @Test
testTimeoutedUser()74     public void testTimeoutedUser() {
75         HttpServletRequest req;
76         assertFalse(plugin.isAllowed(req = createRequest("007", true), new Group()));
77         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
78         assertFalse(plugin.isAllowed(req = createRequest("008", true), new Project()));
79         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
80         assertFalse(plugin.isAllowed(req = createRequest("009", true), createGroup("some group")));
81         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
82         assertFalse(plugin.isAllowed(req = createRequest("00A", true), createProject("some project")));
83         assertNull(req.getAttribute(UserPlugin.REQUEST_ATTR));
84     }
85 
createRequest(String email)86     protected HttpServletRequest createRequest(String email) {
87         return createRequest(email, false);
88     }
89 
createRequest(String email, Boolean timeout)90     protected HttpServletRequest createRequest(String email, Boolean timeout) {
91         return new DummyHttpServletRequestUser() {
92             {
93                 setHeader("osso-user-dn", email);
94                 setHeader("osso-user-guid", "100");
95                 setHeader("osso-idle-timeout-exceeded", Boolean.toString(timeout));
96             }
97         };
98     }
99 
100     protected Group createGroup(String name) {
101         Group g = new Group();
102         g.setName(name);
103         return g;
104     }
105 
106     protected Project createProject(String name) {
107         Project g = new Project();
108         g.setName(name);
109         return g;
110     }
111 }
112