Skip to content

Commit 09c4287

Browse files
authored
Plugins cleanup (#4490)
remove unused methods and improve test coverage
1 parent ad46a73 commit 09c4287

File tree

7 files changed

+59
-108
lines changed

7 files changed

+59
-108
lines changed

plugins/src/main/java/opengrok/auth/plugin/LdapUserPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

@@ -37,6 +37,7 @@
3737
import opengrok.auth.plugin.ldap.AbstractLdapProvider;
3838
import opengrok.auth.plugin.ldap.LdapException;
3939
import org.jetbrains.annotations.NotNull;
40+
import org.jetbrains.annotations.VisibleForTesting;
4041
import org.opengrok.indexer.authorization.AuthorizationException;
4142
import org.opengrok.indexer.configuration.Group;
4243
import org.opengrok.indexer.configuration.Project;
@@ -78,6 +79,7 @@ public class LdapUserPlugin extends AbstractLdapPlugin {
7879
private Integer instanceNum;
7980

8081
// for testing
82+
@VisibleForTesting
8183
void load(Map<String, Object> parameters, AbstractLdapProvider provider) {
8284
super.load(provider);
8385

plugins/src/main/java/opengrok/auth/plugin/decoders/FakeOSSOHeaderDecoder.java

Lines changed: 0 additions & 44 deletions
This file was deleted.

plugins/src/main/java/opengrok/auth/plugin/decoders/OSSOHeaderDecoder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package opengrok.auth.plugin.decoders;
@@ -43,12 +43,12 @@ public class OSSOHeaderDecoder implements IUserDecoder {
4343

4444
private static final Logger LOGGER = Logger.getLogger(OSSOHeaderDecoder.class.getName());
4545

46-
protected static String OSSO_COOKIE_TIMESTAMP_HEADER = "osso-cookie-timestamp";
47-
protected static String OSSO_TIMEOUT_EXCEEDED_HEADER = "osso-idle-timeout-exceeded";
48-
protected static String OSSO_SUBSCRIBER_DN_HEADER = "osso-subscriber-dn";
49-
protected static String OSSO_SUBSCRIBER_HEADER = "osso-subscriber";
50-
protected static String OSSO_USER_DN_HEADER = "osso-user-dn";
51-
protected static String OSSO_USER_GUID_HEADER = "osso-user-guid";
46+
protected static final String OSSO_COOKIE_TIMESTAMP_HEADER = "osso-cookie-timestamp";
47+
protected static final String OSSO_TIMEOUT_EXCEEDED_HEADER = "osso-idle-timeout-exceeded";
48+
protected static final String OSSO_SUBSCRIBER_DN_HEADER = "osso-subscriber-dn";
49+
protected static final String OSSO_SUBSCRIBER_HEADER = "osso-subscriber";
50+
protected static final String OSSO_USER_DN_HEADER = "osso-user-dn";
51+
protected static final String OSSO_USER_GUID_HEADER = "osso-user-guid";
5252

5353
@Override
5454
public User fromRequest(HttpServletRequest request) {

plugins/src/main/java/opengrok/auth/plugin/entity/User.java

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin.entity;
2424

@@ -65,10 +65,6 @@ public String getId() {
6565
return id;
6666
}
6767

68-
public void setId(String id) {
69-
this.id = id;
70-
}
71-
7268
public String getUsername() {
7369
return username;
7470
}
@@ -81,48 +77,14 @@ public Date getCookieTimestamp() {
8177
return cookieTimestamp;
8278
}
8379

84-
public void setCookieTimestamp(Date cookieTimestamp) {
85-
this.cookieTimestamp = cookieTimestamp;
86-
}
87-
8880
public boolean getTimeouted() {
8981
return isTimeouted();
9082
}
9183

92-
public void setTimeouted(boolean timeouted) {
93-
this.timeouted = timeouted;
94-
}
95-
9684
public boolean isTimeouted() {
9785
return timeouted;
9886
}
9987

100-
/**
101-
* Implemented for the forced authentication as described in
102-
* <a href="https://docs.oracle.com/cd/B28196_01/idmanage.1014/b15997/mod_osso.htm#i1006381">mod_osso documentation</a>.
103-
*
104-
* @param forcedAuthDate the date of the forced authentication trigger
105-
* @param newLoginDate the date of the new login
106-
* @return true if login date was before forced auth date or cookie timestamp
107-
*/
108-
public boolean isForcedTimeouted(Date forcedAuthDate, Date newLoginDate) {
109-
if (cookieTimestamp == null || forcedAuthDate == null || newLoginDate == null) {
110-
return true;
111-
}
112-
113-
return newLoginDate.before(forcedAuthDate) || newLoginDate.before(cookieTimestamp);
114-
}
115-
116-
/**
117-
* Get custom user property.
118-
*
119-
* @param key the key
120-
* @return the the value associated with the key
121-
*/
122-
public Object getAttribute(String key) {
123-
return attrs.get(key);
124-
}
125-
12688
/**
12789
* Set custom user property.
12890
*
@@ -134,16 +96,6 @@ public Object setAttribute(String key, Object value) {
13496
return attrs.put(key, value);
13597
}
13698

137-
/**
138-
* Remote custom user property.
139-
*
140-
* @param key the key
141-
* @return the value previously associated with the key
142-
*/
143-
public Object removeAttribute(String key) {
144-
return attrs.remove(key);
145-
}
146-
14799
@Override
148100
public String toString() {
149101
return "User{" + "id=" + id + ", username=" + username + ", cookieTimestamp=" + cookieTimestamp +

plugins/src/test/java/opengrok/auth/plugin/LdapUserPluginTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

@@ -133,6 +133,24 @@ void testFillSessionWithDnOff() throws LdapException {
133133
assertEquals(dn, ((LdapUser) request.getSession().getAttribute(SESSION_ATTR)).getDn());
134134
}
135135

136+
/**
137+
* Test that supplied LDAP filter is expanded for the LDAP query.
138+
*/
139+
@Test
140+
void testFilterExpansion() throws Exception {
141+
AbstractLdapProvider mockprovider = mock(LdapFacade.class);
142+
HttpServletRequest request = new DummyHttpServletRequestLdap();
143+
User user = new User("[email protected]", "id");
144+
LdapUserPlugin plugin = new LdapUserPlugin();
145+
Map<String, Object> params = getParamsMap();
146+
params.put(LdapUserPlugin.ATTRIBUTES, "mail");
147+
params.put(LdapUserPlugin.LDAP_FILTER, "%guid%");
148+
plugin.load(params, mockprovider);
149+
plugin.fillSession(request, user);
150+
final String expectedFilter = plugin.expandFilter(user);
151+
verify(mockprovider).lookupLdapContent(eq(null), eq(expectedFilter), any(String[].class));
152+
}
153+
136154
@Test
137155
void testNegativeCache() throws LdapException {
138156
AbstractLdapProvider mockprovider = mock(LdapFacade.class);

plugins/src/test/java/opengrok/auth/plugin/decoders/OSSODecoderTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin.decoders;
2424

@@ -61,6 +61,7 @@ void setUp() {
6161
/**
6262
* Test of fromRequest method, of class User.
6363
*/
64+
@Test
6465
void testAll() {
6566
dummyRequest.setHeader(OSSO_COOKIE_TIMESTAMP_HEADER, "5761172f");
6667
dummyRequest.setHeader(OSSO_TIMEOUT_EXCEEDED_HEADER, "false");
@@ -99,10 +100,10 @@ void testGetUserId() {
99100
}
100101

101102
/**
102-
* Test of getUserDn method, of class User.
103+
* Test {@link User#getUsername()}.
103104
*/
104105
@Test
105-
void testGetUserDn() {
106+
void testGetUsername() {
106107
String[] tests = {
107108
"123456",
108109
"sd45gfgf5sd4g5ffd54g",
@@ -118,7 +119,7 @@ void testGetUserDn() {
118119
}
119120

120121
/**
121-
* Test of getCookieTimestamp method, of class User.
122+
* Test {@link User#getCookieTimestamp()}.
122123
*/
123124
@Test
124125
void testGetCookieTimestamp() {
@@ -134,7 +135,7 @@ void testGetCookieTimestamp() {
134135
}
135136

136137
/**
137-
* Test of getCookieTimestamp method, of class User.
138+
* Negative test {@link User#getCookieTimestamp()}.
138139
*/
139140
@Test
140141
void testInvalidGetCookieTimestamp() {

plugins/src/test/java/opengrok/auth/plugin/decoders/UserPrincipalDecoderTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,28 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin.decoders;
2424

25+
import jakarta.servlet.http.HttpServletRequest;
2526
import opengrok.auth.plugin.entity.User;
2627
import opengrok.auth.plugin.util.DummyHttpServletRequestUser;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.MethodSource;
32+
33+
import java.security.Principal;
34+
import java.util.Arrays;
35+
import java.util.Collection;
2936

3037
import static org.junit.jupiter.api.Assertions.assertEquals;
3138
import static org.junit.jupiter.api.Assertions.assertFalse;
3239
import static org.junit.jupiter.api.Assertions.assertNotNull;
3340
import static org.junit.jupiter.api.Assertions.assertNull;
41+
import static org.mockito.Mockito.mock;
42+
import static org.mockito.Mockito.when;
3443

3544
class UserPrincipalDecoderTest {
3645
DummyHttpServletRequestUser dummyRequest;
@@ -53,6 +62,19 @@ void testHttpBasicDecoding() {
5362
assertFalse(result.isTimeouted());
5463
}
5564

65+
private static Collection<Principal> invalidPrincipals() {
66+
return Arrays.asList(() -> null, () -> "");
67+
}
68+
69+
@ParameterizedTest
70+
@MethodSource("invalidPrincipals")
71+
void testInvalidUsername(Principal principal) {
72+
HttpServletRequest mockRequest = mock(DummyHttpServletRequestUser.class);
73+
when(mockRequest.getUserPrincipal()).thenReturn(principal);
74+
User result = decoder.fromRequest(mockRequest);
75+
assertNull(result);
76+
}
77+
5678
@Test
5779
void testMissingHeader() {
5880
assertNull(decoder.fromRequest(new DummyHttpServletRequestUser()));

0 commit comments

Comments
 (0)