View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package waffle.spring;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.springframework.security.core.GrantedAuthority;
35  
36  import waffle.mock.MockWindowsIdentity;
37  import waffle.servlet.WindowsPrincipal;
38  
39  /**
40   * The Class WindowsAuthenticationTokenTest.
41   */
42  class WindowsAuthenticationTokenTest {
43  
44      /** The principal. */
45      private WindowsPrincipal principal;
46  
47      /** The token. */
48      private WindowsAuthenticationToken token;
49  
50      /**
51       * Sets the up.
52       */
53      @BeforeEach
54      void setUp() {
55          final List<String> mockGroups = new ArrayList<>();
56          mockGroups.add("group1");
57          mockGroups.add("group2");
58          final MockWindowsIdentity mockIdentity = new MockWindowsIdentity("localhost\\user1", mockGroups);
59          this.principal = new WindowsPrincipal(mockIdentity);
60          this.token = new WindowsAuthenticationToken(this.principal);
61      }
62  
63      /**
64       * Test windows authentication token.
65       */
66      @Test
67      void testWindowsAuthenticationToken() {
68          Assertions.assertNull(this.token.getCredentials());
69          Assertions.assertNull(this.token.getDetails());
70          Assertions.assertTrue(this.token.isAuthenticated());
71          Assertions.assertEquals("localhost\\user1", this.token.getName());
72          final Collection<GrantedAuthority> authorities = this.token.getAuthorities();
73          Assertions.assertEquals(3, authorities.size());
74  
75          final List<String> list = new ArrayList<>();
76          for (final GrantedAuthority grantedAuthority : authorities) {
77              list.add(grantedAuthority.getAuthority());
78          }
79          Collections.sort(list);
80          Assertions.assertEquals("ROLE_GROUP1", list.get(0));
81          Assertions.assertEquals("ROLE_GROUP2", list.get(1));
82          Assertions.assertEquals("ROLE_USER", list.get(2));
83          Assertions.assertEquals(this.principal, this.token.getPrincipal());
84      }
85  
86      /**
87       * Test custom granted authority factory.
88       */
89      @Test
90      void testCustomGrantedAuthorityFactory() {
91  
92          final WindowsAuthenticationToken myToken = new WindowsAuthenticationToken(this.principal,
93                  new FqnGrantedAuthorityFactory(null, false), null);
94  
95          Assertions.assertNull(myToken.getCredentials());
96          Assertions.assertNull(myToken.getDetails());
97          Assertions.assertTrue(myToken.isAuthenticated());
98          Assertions.assertEquals("localhost\\user1", myToken.getName());
99          final Collection<GrantedAuthority> authorities = myToken.getAuthorities();
100         Assertions.assertEquals(2, authorities.size());
101 
102         final List<String> list = new ArrayList<>();
103         for (final GrantedAuthority grantedAuthority : authorities) {
104             list.add(grantedAuthority.getAuthority());
105         }
106         Collections.sort(list);
107         Assertions.assertEquals("group1", list.get(0));
108         Assertions.assertEquals("group2", list.get(1));
109         Assertions.assertEquals(this.principal, myToken.getPrincipal());
110     }
111 
112     /**
113      * Test authenticated.
114      */
115     @Test
116     void testAuthenticated() {
117         Assertions.assertTrue(this.token.isAuthenticated());
118         Assertions.assertThrows(IllegalArgumentException.class, () -> {
119             this.token.setAuthenticated(true);
120         });
121     }
122 
123 }