19
19
20
20
/*
21
21
* Copyright (c) 2020, Chris Fraire <[email protected] >.
22
+ * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
22
23
*/
23
24
package opengrok .auth .plugin ;
24
25
27
28
import org .junit .Before ;
28
29
import org .junit .BeforeClass ;
29
30
import org .junit .Test ;
31
+ import org .junit .runner .RunWith ;
32
+ import org .junit .runners .Parameterized ;
30
33
import org .opengrok .indexer .configuration .Group ;
31
34
import org .opengrok .indexer .configuration .Project ;
32
35
import org .opengrok .indexer .util .RandomString ;
37
40
import java .io .FileOutputStream ;
38
41
import java .io .OutputStreamWriter ;
39
42
import java .nio .charset .StandardCharsets ;
43
+ import java .security .InvalidParameterException ;
44
+ import java .util .Arrays ;
45
+ import java .util .Collection ;
40
46
import java .util .HashMap ;
41
47
42
48
import static org .junit .Assert .assertFalse ;
47
53
/**
48
54
* Represents a container for tests of {@link UserWhiteListPlugin}.
49
55
*/
56
+ @ RunWith (Parameterized .class )
50
57
public class UserWhiteListPluginTest {
51
58
52
59
private static final String OK_USER = "user1321" ;
53
- private static File tempWhitelist ;
60
+ private static final String OK_ID = "id2178" ;
61
+ private static File tempWhitelistUser ;
62
+ private static File tempWhitelistId ;
54
63
private static HashMap <String , Object > validPluginParameters ;
55
64
56
65
private UserWhiteListPlugin plugin ;
66
+ private final String param ;
67
+
68
+ @ Parameterized .Parameters
69
+ public static Collection <String > parameters () {
70
+ return Arrays .asList (UserWhiteListPlugin .ID_FIELD , UserWhiteListPlugin .USERNAME_FIELD );
71
+ }
72
+
73
+ public UserWhiteListPluginTest (String param ) {
74
+ this .param = param ;
75
+ }
57
76
58
77
@ BeforeClass
59
78
public static void beforeClass () throws Exception {
60
- tempWhitelist = File .createTempFile ("UserWhiteListPluginTest " , "txt" );
79
+ tempWhitelistUser = File .createTempFile ("UserWhiteListPluginTestUser " , "txt" );
61
80
try (BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (
62
- new FileOutputStream (tempWhitelist ), StandardCharsets .UTF_8 ))) {
81
+ new FileOutputStream (tempWhitelistUser ), StandardCharsets .UTF_8 ))) {
63
82
writer .write (OK_USER );
64
83
// Don't bother with trailing LF.
65
84
}
66
85
86
+ tempWhitelistId = File .createTempFile ("UserWhiteListPluginTestId" , "txt" );
87
+ try (BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (
88
+ new FileOutputStream (tempWhitelistId ), StandardCharsets .UTF_8 ))) {
89
+ writer .write (OK_ID );
90
+ // Don't bother with trailing LF.
91
+ }
92
+
67
93
validPluginParameters = new HashMap <>();
68
- validPluginParameters .put (UserWhiteListPlugin .FILE_PARAM , tempWhitelist .getPath ());
69
94
}
70
95
71
96
@ AfterClass
72
97
public static void afterClass () {
73
- if (tempWhitelist != null ) {
98
+ if (tempWhitelistUser != null ) {
74
99
//noinspection ResultOfMethodCallIgnored
75
- tempWhitelist .delete ();
100
+ tempWhitelistUser .delete ();
101
+ }
102
+ if (tempWhitelistId != null ) {
103
+ //noinspection ResultOfMethodCallIgnored
104
+ tempWhitelistId .delete ();
76
105
}
77
106
}
78
107
79
108
@ Before
80
109
public void setUp () {
81
110
plugin = new UserWhiteListPlugin ();
111
+ validPluginParameters .put (UserWhiteListPlugin .FIELD_PARAM , this .param );
112
+ if (this .param .equals (UserWhiteListPlugin .USERNAME_FIELD )) {
113
+ validPluginParameters .put (UserWhiteListPlugin .FILE_PARAM , tempWhitelistUser .getPath ());
114
+ } else {
115
+ validPluginParameters .put (UserWhiteListPlugin .FILE_PARAM , tempWhitelistId .getPath ());
116
+ }
82
117
}
83
118
84
119
@ Test
@@ -89,6 +124,16 @@ public void shouldThrowOnLoadIfNullArgument() {
89
124
}, "plugin.load(null)" );
90
125
}
91
126
127
+ @ Test
128
+ public void shouldThrowOnLoadIfInvalidFieldName () {
129
+ assertThrows (IllegalArgumentException .class , () -> {
130
+ HashMap <String , Object > map = new HashMap <>();
131
+ map .put (UserWhiteListPlugin .FILE_PARAM , tempWhitelistUser .getPath ());
132
+ map .put (UserWhiteListPlugin .FIELD_PARAM , "huh" );
133
+ plugin .load (map );
134
+ }, "plugin.load(null)" );
135
+ }
136
+
92
137
@ Test
93
138
public void shouldThrowOnLoadIfUnreadableFileSpecified () {
94
139
HashMap <String , Object > unreadablePluginParameters = new HashMap <>();
@@ -131,15 +176,21 @@ public void shouldAllowWhitelistedUserForAnyProject() {
131
176
plugin .load (validPluginParameters );
132
177
133
178
DummyHttpServletRequest req = new DummyHttpServletRequest ();
134
- req .setAttribute (UserPlugin .REQUEST_ATTR , new User (OK_USER ));
179
+ User user ;
180
+ if (this .param .equals (UserWhiteListPlugin .USERNAME_FIELD )) {
181
+ user = new User (OK_USER );
182
+ } else {
183
+ user = new User ("blurb" , OK_ID );
184
+ }
185
+ req .setAttribute (UserPlugin .REQUEST_ATTR , user );
135
186
136
187
Project randomProject = new Project (RandomString .generateUpper (10 ));
137
188
boolean projectAllowed = plugin .isAllowed (req , randomProject );
138
- assertTrue ("should allow OK_USER for random project 1" , projectAllowed );
189
+ assertTrue ("should allow OK entity for random project 1" , projectAllowed );
139
190
140
191
randomProject = new Project (RandomString .generateUpper (10 ));
141
192
projectAllowed = plugin .isAllowed (req , randomProject );
142
- assertTrue ("should allow OK_USER for random project 2" , projectAllowed );
193
+ assertTrue ("should allow OK entity for random project 2" , projectAllowed );
143
194
}
144
195
145
196
@ Test
@@ -151,19 +202,25 @@ public void shouldNotAllowRandomUserForAnyProject() {
151
202
152
203
Project randomProject = new Project (RandomString .generateUpper (10 ));
153
204
boolean projectAllowed = plugin .isAllowed (req , randomProject );
154
- assertFalse ("should not allow rando for random project 1" , projectAllowed );
205
+ assertFalse ("should not allow random user for random project 1" , projectAllowed );
155
206
156
207
randomProject = new Project (RandomString .generateUpper (10 ));
157
208
projectAllowed = plugin .isAllowed (req , randomProject );
158
- assertFalse ("should not allow rando for random project 2" , projectAllowed );
209
+ assertFalse ("should not allow random user for random project 2" , projectAllowed );
159
210
}
160
211
161
212
@ Test
162
213
public void shouldAllowWhitelistedUserForAnyGroup () {
163
214
plugin .load (validPluginParameters );
164
215
165
216
DummyHttpServletRequest req = new DummyHttpServletRequest ();
166
- req .setAttribute (UserPlugin .REQUEST_ATTR , new User (OK_USER ));
217
+ User user ;
218
+ if (this .param .equals (UserWhiteListPlugin .USERNAME_FIELD )) {
219
+ user = new User (OK_USER );
220
+ } else {
221
+ user = new User ("blurb" , OK_ID );
222
+ }
223
+ req .setAttribute (UserPlugin .REQUEST_ATTR , user );
167
224
168
225
Group randomGroup = new Group (RandomString .generateUpper (10 ));
169
226
boolean groupAllowed = plugin .isAllowed (req , randomGroup );
@@ -183,10 +240,10 @@ public void shouldNotAllowRandomUserForAnyGroup() {
183
240
184
241
Group randomGroup = new Group (RandomString .generateUpper (10 ));
185
242
boolean projectAllowed = plugin .isAllowed (req , randomGroup );
186
- assertFalse ("should not allow rando for random group 1" , projectAllowed );
243
+ assertFalse ("should not allow random random group 1" , projectAllowed );
187
244
188
245
randomGroup = new Group (RandomString .generateUpper (10 ));
189
246
projectAllowed = plugin .isAllowed (req , randomGroup );
190
- assertFalse ("should not allow rando for random group 2" , projectAllowed );
247
+ assertFalse ("should not allow random for random group 2" , projectAllowed );
191
248
}
192
249
}
0 commit comments