Skip to content

Commit 0500c22

Browse files
committed
Fix unit tests
1 parent 46bd059 commit 0500c22

File tree

4 files changed

+95
-55
lines changed

4 files changed

+95
-55
lines changed

core/security/src/test/java/org/phoebus/security/store/SecureStoreTest.java

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,11 @@ public class SecureStoreTest {
4040

4141
private static SecureStore memorySecureStore;
4242

43-
private static AuthenticationScope scope1 = new AuthenticationScope() {
44-
@Override
45-
public String getScope() {
46-
return "service1";
47-
}
48-
49-
@Override
50-
public String getDisplayName() {
51-
return "";
52-
}
53-
};
54-
55-
private static AuthenticationScope scope2 = new AuthenticationScope() {
56-
@Override
57-
public String getScope() {
58-
return "service2";
59-
}
60-
61-
@Override
62-
public String getDisplayName() {
63-
return "";
64-
}
65-
};
43+
private static List<ServiceAuthenticationProvider> authenticationProviders;
6644

6745
@BeforeAll
6846
public static void setup() throws Exception {
69-
List<ServiceAuthenticationProvider> authenticationProviders =
47+
authenticationProviders =
7048
ServiceLoader.load(ServiceAuthenticationProvider.class).stream().map(ServiceLoader.Provider::get)
7149
.collect(Collectors.toList());
7250
File secureStoreFile;
@@ -184,19 +162,9 @@ public void testGetScopedToken() throws Exception {
184162
assertNotNull(token);
185163
assertNull(token.getAuthenticationScope());
186164

187-
token = secureStore.getScopedAuthenticationToken(new AuthenticationScope() {
188-
@Override
189-
public String getScope() {
190-
return "service1";
191-
}
192-
193-
@Override
194-
public String getDisplayName() {
195-
return "";
196-
}
197-
});
165+
token = secureStore.getScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope());
198166
assertNotNull(token);
199-
assertEquals("service1", token.getAuthenticationScope());
167+
assertEquals("service1", token.getAuthenticationScope().getScope());
200168
assertEquals("username1", token.getUsername());
201169

202170
token = secureStore.getScopedAuthenticationToken(null);
@@ -215,9 +183,9 @@ public String getDisplayName() {
215183
assertNotNull(token);
216184
assertNull(token.getAuthenticationScope());
217185

218-
token = memorySecureStore.getScopedAuthenticationToken(scope1);
186+
token = memorySecureStore.getScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope());
219187
assertNotNull(token);
220-
assertEquals("service1", token.getAuthenticationScope());
188+
assertEquals("service1", token.getAuthenticationScope().getScope());
221189
assertEquals("username1", token.getUsername());
222190

223191
token = memorySecureStore.getScopedAuthenticationToken(null);
@@ -248,18 +216,18 @@ public void testGetAllScopedTokensLegacyOnly() throws Exception {
248216
@Test
249217
public void testSetScopedAuthenticationToken() throws Exception {
250218
secureStore.setScopedAuthentication(new ScopedAuthenticationToken("username", "password"));
251-
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username1", "password1"));
252-
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope2, "username2", "password2"));
219+
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username1", "password1"));
220+
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(1).getAuthenticationScope(), "username2", "password2"));
253221

254222
List<ScopedAuthenticationToken> tokens = secureStore.getAuthenticationTokens();
255223
assertEquals(3, tokens.size());
256224

257225
secureStore.deleteAllScopedAuthenticationTokens();
258226

259227
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken("username", "password"));
260-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username1", "password1"));
261-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope2, "username2", "password2"));
262-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope2, "username3", "password3"));
228+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username1", "password1"));
229+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(1).getAuthenticationScope(), "username2", "password2"));
230+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(1).getAuthenticationScope(), "username3", "password3"));
263231

264232
tokens = memorySecureStore.getAuthenticationTokens();
265233
assertEquals(3, tokens.size());
@@ -333,8 +301,8 @@ public void testSetInvalidAuthenticationToken() throws Exception {
333301
public void testDeleteAllScopedAuthenticationTokens() throws Exception {
334302

335303
secureStore.setScopedAuthentication(new ScopedAuthenticationToken("username", "password"));
336-
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username1", "password1"));
337-
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope2, "username2", "password2"));
304+
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username1", "password1"));
305+
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(1).getAuthenticationScope(), "username2", "password2"));
338306

339307
List<ScopedAuthenticationToken> tokens = secureStore.getAuthenticationTokens();
340308
assertEquals(3, tokens.size());
@@ -344,8 +312,8 @@ public void testDeleteAllScopedAuthenticationTokens() throws Exception {
344312
assertEquals(0, tokens.size());
345313

346314
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken("username", "password"));
347-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username1", "password1"));
348-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope2, "username2", "password2"));
315+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username1", "password1"));
316+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(1).getAuthenticationScope(), "username2", "password2"));
349317

350318
tokens = memorySecureStore.getAuthenticationTokens();
351319
assertEquals(3, tokens.size());
@@ -357,22 +325,22 @@ public void testDeleteAllScopedAuthenticationTokens() throws Exception {
357325

358326
@Test
359327
public void testOverwriteScopedAuthentication() throws Exception {
360-
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username1", "password1"));
328+
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username1", "password1"));
361329

362-
ScopedAuthenticationToken token = secureStore.getScopedAuthenticationToken(scope1);
330+
ScopedAuthenticationToken token = secureStore.getScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope());
363331
assertEquals("username1", token.getUsername());
364332

365-
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username2", "password1"));
366-
token = secureStore.getScopedAuthenticationToken(scope1);
333+
secureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username2", "password1"));
334+
token = secureStore.getScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope());
367335
assertEquals("username2", token.getUsername());
368336

369-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username1", "password1"));
337+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username1", "password1"));
370338

371-
token = memorySecureStore.getScopedAuthenticationToken(scope1);
339+
token = memorySecureStore.getScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope());
372340
assertEquals("username1", token.getUsername());
373341

374-
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(scope1, "username2", "password1"));
375-
token = memorySecureStore.getScopedAuthenticationToken(scope1);
342+
memorySecureStore.setScopedAuthentication(new ScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope(), "username2", "password1"));
343+
token = memorySecureStore.getScopedAuthenticationToken(authenticationProviders.get(0).getAuthenticationScope());
376344
assertEquals("username2", token.getUsername());
377345
}
378346
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2025 European Spallation Source ERIC.
3+
*/
4+
5+
package org.phoebus.security.store;
6+
7+
import org.phoebus.security.authorization.ServiceAuthenticationProvider;
8+
import org.phoebus.security.tokens.AuthenticationScope;
9+
10+
import java.net.ConnectException;
11+
12+
public class ServiceAuthenticationProviderOne implements ServiceAuthenticationProvider {
13+
14+
@Override
15+
public void authenticate(String username, String password) throws ConnectException {
16+
17+
}
18+
19+
@Override
20+
public AuthenticationScope getAuthenticationScope() {
21+
return new AuthenticationScope() {
22+
@Override
23+
public String getScope() {
24+
return "service1";
25+
}
26+
27+
@Override
28+
public String getDisplayName() {
29+
return "";
30+
}
31+
};
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2025 European Spallation Source ERIC.
3+
*/
4+
5+
package org.phoebus.security.store;
6+
7+
import org.phoebus.security.authorization.ServiceAuthenticationProvider;
8+
import org.phoebus.security.tokens.AuthenticationScope;
9+
10+
import java.net.ConnectException;
11+
12+
public class ServiceAuthenticationProviderTwo implements ServiceAuthenticationProvider {
13+
14+
@Override
15+
public void authenticate(String username, String password) throws ConnectException {
16+
17+
}
18+
19+
@Override
20+
public AuthenticationScope getAuthenticationScope() {
21+
return new AuthenticationScope() {
22+
@Override
23+
public String getScope() {
24+
return "service2";
25+
}
26+
27+
@Override
28+
public String getDisplayName() {
29+
return "";
30+
}
31+
};
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright (C) 2025 European Spallation Source ERIC.
3+
#
4+
5+
org.phoebus.security.store.ServiceAuthenticationProviderOne
6+
org.phoebus.security.store.ServiceAuthenticationProviderTwo

0 commit comments

Comments
 (0)