View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import static org.junit.jupiter.api.Assertions.assertFalse;
14  import static org.junit.jupiter.api.Assertions.assertTrue;
15  import static org.mockito.Mockito.mock;
16  import static org.mockito.Mockito.when;
17  
18  import com.codebox.bean.JavaBeanTester;
19  
20  import jakarta.servlet.ServletContext;
21  
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.Test;
27  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
28  import org.springframework.security.core.GrantedAuthority;
29  import org.springframework.security.core.authority.SimpleGrantedAuthority;
30  import org.springframework.security.core.context.SecurityContextHolder;
31  
32  /**
33   * Tests for {@link SecurityUtils}.
34   */
35  class SecurityUtilsTest {
36  
37    @AfterEach
38    void clearContext() {
39      SecurityContextHolder.clearContext();
40    }
41  
42    @Test
43    void testPrivateConstructor() {
44      JavaBeanTester.builder(SecurityUtils.class).testPrivateConstructor();
45    }
46  
47    @Test
48    void testHasAttributeValueRoleTrue() {
49      Collection<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_ADMIN"));
50      SecurityContextHolder.getContext()
51          .setAuthentication(new UsernamePasswordAuthenticationToken("user", "pass", authorities));
52  
53      ServletContext context = mock(ServletContext.class);
54      when(context.getInitParameter("attribute.value.roles")).thenReturn("ROLE_ADMIN");
55  
56      assertTrue(SecurityUtils.hasAttributeValueRole(context));
57    }
58  
59    @Test
60    void testHasAttributeValueRoleFalse() {
61      Collection<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_USER"));
62      SecurityContextHolder.getContext()
63          .setAuthentication(new UsernamePasswordAuthenticationToken("user", "pass", authorities));
64  
65      ServletContext context = mock(ServletContext.class);
66      when(context.getInitParameter("attribute.value.roles")).thenReturn("ROLE_ADMIN");
67  
68      assertFalse(SecurityUtils.hasAttributeValueRole(context));
69    }
70  
71    @Test
72    void testHasAttributeValueRoleMultipleRoles() {
73      Collection<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_MANAGER"));
74      SecurityContextHolder.getContext()
75          .setAuthentication(new UsernamePasswordAuthenticationToken("user", "pass", authorities));
76  
77      ServletContext context = mock(ServletContext.class);
78      when(context.getInitParameter("attribute.value.roles"))
79          .thenReturn("ROLE_ADMIN,ROLE_MANAGER,ROLE_USER");
80  
81      assertTrue(SecurityUtils.hasAttributeValueRole(context));
82    }
83  
84    @Test
85    void testHasAttributeValueRoleNoMatch() {
86      Collection<GrantedAuthority> authorities =
87          List.of(new SimpleGrantedAuthority("ROLE_A"), new SimpleGrantedAuthority("ROLE_B"));
88      SecurityContextHolder.getContext()
89          .setAuthentication(new UsernamePasswordAuthenticationToken("user", "pass", authorities));
90  
91      ServletContext context = mock(ServletContext.class);
92      when(context.getInitParameter("attribute.value.roles")).thenReturn("ROLE_X,ROLE_Y");
93  
94      assertFalse(SecurityUtils.hasAttributeValueRole(context));
95    }
96  
97    @Test
98    void testHasAttributeValueRoleEmptyAuthorities() {
99      SecurityContextHolder.getContext()
100         .setAuthentication(new UsernamePasswordAuthenticationToken("user", "pass", List.of()));
101 
102     ServletContext context = mock(ServletContext.class);
103     when(context.getInitParameter("attribute.value.roles")).thenReturn("ROLE_ADMIN");
104 
105     assertFalse(SecurityUtils.hasAttributeValueRole(context));
106   }
107 }